aboutsummaryrefslogtreecommitdiff
path: root/src/lapi.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/lapi.js')
-rw-r--r--src/lapi.js29
1 files changed, 28 insertions, 1 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;