summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-03-02 21:15:42 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-03 14:39:45 +0100
commit0ba3c5d516b1a0b67cff6d5f4b4583284086dd4b (patch)
tree72db338fe02e0e4b84238a54e422bbc21b40a5d0 /src
parent319c40c4439a9eda7bd4a68769057cb12b04755a (diff)
downloadfengari-0ba3c5d516b1a0b67cff6d5f4b4583284086dd4b.tar.gz
fengari-0ba3c5d516b1a0b67cff6d5f4b4583284086dd4b.tar.bz2
fengari-0ba3c5d516b1a0b67cff6d5f4b4583284086dd4b.zip
load, luaL_loadbuffer(x), luaL_loadstring, lua_replace, lua_isnone, lua_isnoneornil, lua_setupvalue
Diffstat (limited to 'src')
-rw-r--r--src/lapi.js56
-rw-r--r--src/lauxlib.js21
-rw-r--r--src/lbaselib.js79
-rw-r--r--src/lcode.js2
-rw-r--r--src/ldo.js2
-rw-r--r--src/lvm.js2
6 files changed, 149 insertions, 13 deletions
diff --git a/src/lapi.js b/src/lapi.js
index 72ff554..a11dbca 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -182,6 +182,11 @@ const lua_insert = function(L, idx) {
lua_rotate(L, idx, 1);
};
+const lua_replace = function(L, idx) {
+ lua_copy(L, -1, idx);
+ lua_pop(L, 1);
+};
+
/*
** push functions (JS -> stack)
*/
@@ -424,6 +429,45 @@ const lua_createtable = function(L, narray, nrec) {
assert(L.top <= L.ci.top, "stack overflow");
};
+const aux_upvalue = function(fi, n) {
+ switch(fi.ttype()) {
+ case CT.LUAT_TCCL: { /* C closure */
+ let f = fi.value;
+ if (!(1 <= n && n <= f.nupvalues)) return null;
+ return {
+ name: "",
+ val: f.upvalue[n-1]
+ };
+ }
+ case CT.LUA_TLCL: { /* Lua closure */
+ let f = fi.value;
+ let p = f.p;
+ if (!(1 <= n && n <= p.upvalues.length)) return null;
+ let name = p.upvalues[n-1].name;
+ return {
+ name: name ? name : "(*no name)",
+ val: f.upvals[n-1].val()
+ };
+ }
+ default: return null; /* not a closure */
+ }
+};
+
+const lua_setupvalue = function(L, funcindex, n) {
+ let fi = index2addr(L, funcindex);
+ assert(1 < L.top - L.ci.funcOff, "not enough elements in the stack");
+ let aux = aux_upvalue(fi, n);
+ let name = aux.name;
+ let val = aux.val;
+ if (name) {
+ L.top--;
+ // TODO: what if it's not a pure TValue (closure, table)
+ val.type = L.stack[L.top].type;
+ val.value = L.stack[L.top].value;
+ }
+ return name;
+};
+
const lua_newtable = function(L) {
lua_createtable(L, 0, 0);
};
@@ -595,6 +639,14 @@ const lua_typename = function(L, t) {
return ltm.ttypename(t);
};
+const lua_isnil = function(L, n) {
+ return lua_type(L, n) === CT.LUA_TNIL;
+};
+
+const lua_isnone = function(L, n) {
+ return lua_type(L, n) === CT.LUA_TNONE;
+};
+
const lua_isnoneornil = function(L, n) {
return lua_type(L, n) <= 0;
};
@@ -798,6 +850,8 @@ module.exports.lua_gettable = lua_gettable;
module.exports.lua_gettop = lua_gettop;
module.exports.lua_insert = lua_insert;
module.exports.lua_isinteger = lua_isinteger;
+module.exports.lua_isnil = lua_isnil;
+module.exports.lua_isnone = lua_isnone;
module.exports.lua_isnoneornil = lua_isnoneornil;
module.exports.lua_isnumber = lua_isnumber;
module.exports.lua_isstring = lua_isstring;
@@ -830,6 +884,7 @@ module.exports.lua_rawgeti = lua_rawgeti;
module.exports.lua_rawlen = lua_rawlen;
module.exports.lua_rawset = lua_rawset;
module.exports.lua_remove = lua_remove;
+module.exports.lua_replace = lua_replace;
module.exports.lua_rotate = lua_rotate;
module.exports.lua_setfield = lua_setfield;
module.exports.lua_setglobal = lua_setglobal;
@@ -837,6 +892,7 @@ module.exports.lua_seti = lua_seti;
module.exports.lua_setmetatable = lua_setmetatable;
module.exports.lua_settable = lua_settable;
module.exports.lua_settop = lua_settop;
+module.exports.lua_setupvalue = lua_setupvalue;
module.exports.lua_status = lua_status;
module.exports.lua_stringtonumber = lua_stringtonumber;
module.exports.lua_toboolean = lua_toboolean;
diff --git a/src/lauxlib.js b/src/lauxlib.js
index 728469a..d58a171 100644
--- a/src/lauxlib.js
+++ b/src/lauxlib.js
@@ -225,6 +225,24 @@ const luaL_opt = function(L, f, n, d) {
return lapi.lua_type(L, n) <= 0 ? d : f(L, n);
};
+const getS = function(L, ud) {
+ let s = ud.string;
+ ud.string = null;
+ return s;
+};
+
+const luaL_loadbufferx = function(L, buff, size, name, mode) {
+ return lapi.lua_load(L, getS, {string: buff}, name, mode);
+};
+
+const luaL_loadbuffer = function(L, s, sz, n) {
+ return luaL_loadbufferx(L, s, sz, n, null);
+};
+
+const luaL_loadstring = function(L, s) {
+ return luaL_loadbuffer(L, s, s.length, s);
+};
+
const luaL_getmetafield = function(L, obj, event) {
if (!lapi.lua_getmetatable(L, obj))
return CT.LUA_TNIL;
@@ -381,6 +399,9 @@ module.exports.luaL_error = luaL_error;
module.exports.luaL_getmetafield = luaL_getmetafield;
module.exports.luaL_getsubtable = luaL_getsubtable;
module.exports.luaL_len = luaL_len;
+module.exports.luaL_loadbuffer = luaL_loadbuffer;
+module.exports.luaL_loadbufferx = luaL_loadbufferx;
+module.exports.luaL_loadstring = luaL_loadstring;
module.exports.luaL_newlib = luaL_newlib;
module.exports.luaL_newstate = luaL_newstate;
module.exports.luaL_opt = luaL_opt;
diff --git a/src/lbaselib.js b/src/lbaselib.js
index 77638d0..0b5c2f7 100644
--- a/src/lbaselib.js
+++ b/src/lbaselib.js
@@ -251,26 +251,85 @@ const luaB_xpcall = function(L) {
return finishpcall(L, status, 2);
};
+const load_aux = function(L, status, envidx) {
+ if (status === TS.LUA_OK) {
+ if (envidx !== 0) { /* 'env' parameter? */
+ lapi.lua_pushvalue(L, envidx); /* environment for loaded function */
+ if (!lapi.lua_setupvalue(L, -2, 1)) /* set it as 1st upvalue */
+ lapi.lua_pop(L, 1); /* remove 'env' if not used by previous call */
+ }
+ return 1;
+ } else { /* error (message is on top of the stack) */
+ lapi.lua_pushnil(L);
+ lapi.lua_insert(L, -2); /* put before error message */
+ return 2; /* return nil plus error message */
+ }
+};
+
+/*
+** reserved slot, above all arguments, to hold a copy of the returned
+** string to avoid it being collected while parsed. 'load' has four
+** optional arguments (chunk, source name, mode, and environment).
+*/
+const RESERVEDSLOT = 5;
+
+/*
+** Reader for generic 'load' function: 'lua_load' uses the
+** stack for internal stuff, so the reader cannot change the
+** stack top. Instead, it keeps its resulting string in a
+** reserved slot inside the stack.
+*/
+const generic_reader = function(L, ud) {
+ lauxlib.luaL_checkstack(L, 2, "too many nested functions");
+ lapi.lua_pushvalue(L, 1); /* get function */
+ lapi.lua_call(L, 0, 1); /* call it */
+ if (lapi.lua_isnil(L, -1)) {
+ lapi.lua_pop(L, 1); /* pop result */
+ return null;
+ } else if (!lapi.lua_isstring(L, -1))
+ lauxlib.luaL_error(L, "reader function must return a string");
+ lapi.lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
+ return lapi.lua_tostring(L, RESERVEDSLOT);
+};
+
+const luaB_load = function(L) {
+ let s = lapi.lua_tostring(L, 1);
+ let mode = lauxlib.luaL_optstring(L, 3, "bt");
+ let env = !lapi.lua_isnone(L, 4) ? 4 : 0; /* 'env' index or 0 if no 'env' */
+ let status;
+ if (s !== null) { /* loading a string? */
+ let chunkname = lauxlib.luaL_optstring(L, 2, s);
+ status = lauxlib.luaL_loadbufferx(L, s, chunkname, mode);
+ } else { /* loading from a reader function */
+ let chunkname = lauxlib.luaL_optstring(L, 2, "=(load)");
+ lauxlib.luaL_checktype(L, 1, CT.LUA_TFUNCTION);
+ lapi.lua_settop(L, RESERVEDSLOT); /* create reserved slot */
+ status = lapi.lua_load(L, generic_reader, null, chunkname, mode);
+ }
+ return load_aux(L, status, env);
+};
+
const base_funcs = {
"collectgarbage": function () {},
"assert": luaB_assert,
- "print": luaB_print,
- "tostring": luaB_tostring,
- "tonumber": luaB_tonumber,
+ "error": luaB_error,
"getmetatable": luaB_getmetatable,
+ "ipairs": luaB_ipairs,
+ "load": luaB_load,
"next": luaB_next,
"pairs": luaB_pairs,
- "ipairs": luaB_ipairs,
- "select": luaB_select,
- "setmetatable": luaB_setmetatable,
+ "pcall": luaB_pcall,
+ "print": luaB_print,
"rawequal": luaB_rawequal,
+ "rawget": luaB_rawget,
"rawlen": luaB_rawlen,
"rawset": luaB_rawset,
- "rawget": luaB_rawget,
+ "select": luaB_select,
+ "setmetatable": luaB_setmetatable,
+ "tonumber": luaB_tonumber,
+ "tostring": luaB_tostring,
"type": luaB_type,
- "error": luaB_error,
- "pcall": luaB_pcall,
- "xpcall": luaB_xpcall,
+ "xpcall": luaB_xpcall
};
const luaopen_base = function(L) {
diff --git a/src/lcode.js b/src/lcode.js
index 9fb32e2..fedf1e2 100644
--- a/src/lcode.js
+++ b/src/lcode.js
@@ -359,7 +359,7 @@ const luaK_patchclose = function(fs, list, level) {
** line information. Return 'i' position.
*/
const luaK_code = function(fs, i) {
- console.log(`${i.opcode}\t${i.A}\t${i.B}\t${i.C}\t${i.Ax}\t${i.Bx}\t${i.sBx}`);
+ // console.log(`${i.opcode}\t${i.A}\t${i.B}\t${i.C}\t${i.Ax}\t${i.Bx}\t${i.sBx}`);
let f = fs.f;
dischargejpc(fs); /* 'pc' will change */
/* put new instruction in code array */
diff --git a/src/ldo.js b/src/ldo.js
index 668ea9b..f4d8336 100644
--- a/src/ldo.js
+++ b/src/ldo.js
@@ -521,7 +521,7 @@ class SParser {
}
const checkmode = function(L, mode, x) {
- if (mode && mode !== x) {
+ if (mode && mode.charAt(0) !== x.charAt(0)) {
lapi.lua_pushstring(L, `attempt to load a ${x} chunk (mode is '${mode}')`);
luaD_throw(L, TS.LUA_ERRSYNTAX);
}
diff --git a/src/lvm.js b/src/lvm.js
index 75fe44c..534ba7b 100644
--- a/src/lvm.js
+++ b/src/lvm.js
@@ -128,7 +128,7 @@ const luaV_execute = function(L) {
if (i.breakpoint) // TODO: remove, used until lapi
return;
- console.log(`> ${opcode}`);
+ // console.log(`> ${opcode}`);
switch (opcode) {
case "OP_MOVE": {
L.stack[ra] = L.stack[RB(L, base, i)];