Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require(), some parser things #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 61 additions & 31 deletions js/lua-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ case 19:
if ($$[$0-2].length == 1) {
// avoid tmp entirely for certain situations
if ($$[$0].exps.length == 1) {
tmp = "var " + $$[$0-2][0] + " = " + $$[$0].exps[0] + ";";
tmp = "var " + $$[$0-2][0] + " = " + $$[$0].exps[0] + ";";
} else {
tmp = "var " + $$[$0-2][0] + " = " + getTempDecl($$[$0]) + "[0];";
}
Expand Down Expand Up @@ -1495,6 +1495,18 @@ function lua_mod(op1, op2) {
}
}
}
var kFUNCS = 0;
function rawget_table(table, key)
{
if (key == null) {
throw new Error("Table index is nil");
}
for (var i in table) {
if (table[i][0] == key) {
return table[i][1];
}
}
}
function lua_rawget(table, key) {
switch (typeof key) {
case "string":
Expand All @@ -1515,19 +1527,38 @@ function lua_rawget(table, key) {
case "boolean":
return table.bool[key];
case "object":
if (key == null) {
throw new Error("Table index is nil");
}
for (var i in table.objs) {
if (table.objs[i][0] == key) {
return table.objs[i][1];
}
}
return rawget_table(table.objs, key);
break;
case "function":
if (table.bool[kFUNCS] != null) {
return rawget_table(table.bool[kFUNCS], key);
}
break;
default:
throw new Error("Unsupported key for table: " + (typeof key));
}
}
function rawset_table(table, key, value)
{
if (key == null) {
throw new Error("Table index is nil");
}
var bFound = false;
for (var i in table) {
if (table[i][0] == key) {
if (value == null) {
table.splice(i,1); // remove element [i]
} else {
bFound = true;
table[i][1] = value; // modifiy/overwrite existing entry
}
break;
}
}
if (!bFound) {
table.push([key,value]); // add new entry
}
}
function lua_rawset(table, key, value) {
delete table.length;
switch (typeof key) {
Expand Down Expand Up @@ -1565,25 +1596,12 @@ function lua_rawset(table, key, value) {
}
break;
case "object":
if (key == null) {
throw new Error("Table index is nil");
}
var bFound = false;
for (var i in table.objs) {
if (table.objs[i][0] == key) {
if (value == null) {
table.objs.splice(i,1); // remove element [i]
} else {
bFound = true;
table.objs[i][1] = value; // modifiy/overwrite existing entry
}
break;
}
}
if (!bFound) {
table.objs.push([key,value]); // add new entry
}
rawset_table(table.objs, key, value);
break;
case "function":
table.bool[kFUNCS] = table.bool[kFUNCS] || [];
rawset_table(table.bool[kFUNCS], key, value);
break;
default:
throw new Error("Unsupported key for table: " + (typeof key));
}
Expand Down Expand Up @@ -1669,6 +1687,12 @@ function _ipairs_next(table, index) {
return [index + 1, entry];
}
var lua_libs = {};
var tostring_id = 0;
function get_id (e) {
e.id = e.id || tostring_id++;

return e.id;
}
var lua_core = {
"assert": function (value, message) {
if (arguments.length < 1) {
Expand Down Expand Up @@ -1741,12 +1765,17 @@ var lua_core = {
for (i in table.floats) {
props.push(parseFloat(i));
}
for (i in table.bools) {
for (i in table.bool) {
if (i == kFUNCS) continue;
props.push(i === "true" ? true : false);
}
for (i in table.objs) {
props.push(table.objs[i][0]);
}
var funcs = table.bool[kFUNCS]
for (i in funcs) {
props.push(funcs[i][0]);
}

// okay, so I'm faking it here
// regardless of what key is given, this function will return the next value
Expand Down Expand Up @@ -1832,9 +1861,9 @@ var lua_core = {
case "string":
return [e];
case "object":
return ["table"];
return ["table" + get_id(e)];
case "function":
return ["function"];
return ["function" + get_id(e)];
default:
return ["nil"];
}
Expand Down Expand Up @@ -1887,7 +1916,8 @@ var _lua_coroutine = lua_libs["coroutine"] = {};
_lua_coroutine["resume"] = _lua_coroutine["running"] = _lua_coroutine["status"] = _lua_coroutine["wrap"] = _lua_coroutine["yield"] = _lua_coroutine["create"] = function () {
not_supported();
};

function XX () {}
function YY () {}
// debug
var _lua_debug = lua_libs["debug"] = {
"getmetatable": function (obj) {
Expand Down
17 changes: 14 additions & 3 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,22 @@ function LuaBootStrap (G) {
// path transformations
if (path.substr(-4) == ".lua") path = path.slice(0, -4); // require automatically appends .lua to the filepath later, remove it here
path = path.replace(/\./g, "/");


var t = lua_tableget(lua_packages, path);

if (t != null) return t;

var initpath = path+"init.lua"; // require("shaders") -> shaders/init.lua

if (LoveFS_exists(initpath))
return RunLuaFromPath(initpath);
else return RunLuaFromPath(path + ".lua"); // require("bla") -> bla.lua
t = RunLuaFromPath(initpath);
else t = RunLuaFromPath(path + ".lua"); // require("bla") -> bla.lua

t = t || true;

lua_tableset(lua_packages, path, t);

return t;

//~ NOTE: replaces parser lib lua_require(G, path);
};
Expand Down