aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lapi.js29
-rw-r--r--src/lauxlib.js21
-rw-r--r--src/lbaselib.js21
-rw-r--r--src/lcode.js2
-rw-r--r--src/ldo.js2
-rw-r--r--src/lvm.js2
6 files changed, 63 insertions, 14 deletions
diff --git a/src/lapi.js b/src/lapi.js
index 32a9873..a11dbca 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -429,6 +429,30 @@ 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");
@@ -437,7 +461,9 @@ const lua_setupvalue = function(L, funcindex, n) {
let val = aux.val;
if (name) {
L.top--;
- setobj(L, val, 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;
};
@@ -866,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 a7666b5..0b5c2f7 100644
--- a/src/lbaselib.js
+++ b/src/lbaselib.js
@@ -312,23 +312,24 @@ const luaB_load = function(L) {
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)];