diff options
author | Benoit Giannangeli <giann008@gmail.com> | 2017-04-11 13:58:15 +0200 |
---|---|---|
committer | Benoit Giannangeli <giann008@gmail.com> | 2017-04-11 13:58:15 +0200 |
commit | 58fcb1d7f55424de904fd6ae53954b46d82aae8c (patch) | |
tree | b6969917ab16a139b918b33d29aa1c93ae66336c /src | |
parent | ea8b3d63af92085d1563c670968152c7dbbb7642 (diff) | |
download | fengari-58fcb1d7f55424de904fd6ae53954b46d82aae8c.tar.gz fengari-58fcb1d7f55424de904fd6ae53954b46d82aae8c.tar.bz2 fengari-58fcb1d7f55424de904fd6ae53954b46d82aae8c.zip |
debug.upvalueid
Diffstat (limited to 'src')
-rw-r--r-- | src/lapi.js | 46 | ||||
-rw-r--r-- | src/ldblib.js | 21 |
2 files changed, 63 insertions, 4 deletions
diff --git a/src/lapi.js b/src/lapi.js index 00db461..7f8cda8 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -463,7 +463,7 @@ const lua_newuserdata = function(L, size) { return L.stack[L.top - 1].value; }; -const aux_upvalue = function(fi, n) { +const aux_upvalue = function(L, fi, n) { switch(fi.ttype()) { case CT.LUAT_TCCL: { /* C closure */ let f = fi.value; @@ -480,17 +480,26 @@ const aux_upvalue = function(fi, n) { let name = p.upvalues[n-1].name; return { name: name ? name : "(*no name)", - val: f.upvals[n-1].val() + val: f.upvals[n-1].val(L) }; } default: return null; /* not a closure */ } }; +const lua_getupvalue = function(L, funcindex, n) { + let up = aux_upvalue(L, index2addr(L, funcindex), n); + let name = up.name; + let val = up.val; + if (name) + L.stack[L.top++] = new TValue(name.type, name.value); + return name; +}; + 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 aux = aux_upvalue(L, fi, n); let name = aux.name; let val = aux.val; if (name) { @@ -899,6 +908,35 @@ const lua_len = function(L, idx) { assert(L.top <= L.ci.top, "stack overflow"); }; +const getupvalref = function(L, fidx, n, pf) { + let fi = index2addr(L, fidx); + assert(fi.ttisLclosure(), "Lua function expected"); + let f = fi.value; + assert(1 <= n && n <= f.p.upvalues.length, "invalid upvalue index"); + return { + closure: f, + upval: f.upvals[n - 1] + }; +}; + +const lua_upvalueid = function(L, fidx, n) { + let fi = index2addr(L, fidx); + switch (fi.ttype()) { + case CT.LUA_TLCL: { /* lua closure */ + return getupvalref(L, fidx, n, null).upval; + } + case CT.LUA_TCCL: { /* C closure */ + let f = fi.value; + assert(1 <= n && n <= f.nupvalues, "invalid upvalue index"); + return f.upvalue[n - 1]; + } + default: { + assert(false, "closure expected"); + return null; + } + } +}; + // This functions are only there for compatibility purposes const lua_gc = function () {}; @@ -934,6 +972,7 @@ module.exports.lua_geti = lua_geti; module.exports.lua_getmetatable = lua_getmetatable; module.exports.lua_gettable = lua_gettable; module.exports.lua_gettop = lua_gettop; +module.exports.lua_getupvalue = lua_getupvalue; module.exports.lua_insert = lua_insert; module.exports.lua_isfunction = lua_isfunction; module.exports.lua_isinteger = lua_isinteger; @@ -1001,5 +1040,6 @@ module.exports.lua_tothread = lua_tothread; module.exports.lua_touserdata = lua_touserdata; module.exports.lua_type = lua_type; module.exports.lua_typename = lua_typename; +module.exports.lua_upvalueid = lua_upvalueid; module.exports.lua_version = lua_version; module.exports.lua_xmove = lua_xmove; diff --git a/src/ldblib.js b/src/ldblib.js index efab872..426cef2 100644 --- a/src/ldblib.js +++ b/src/ldblib.js @@ -66,8 +66,27 @@ const db_getlocal = function(L) { } }; +/* +** Check whether a given upvalue from a given closure exists and +** returns its index +*/ +const checkupval = function(L, argf, argnup) { + let nup = lauxlib.luaL_checkinteger(L, argnup); /* upvalue index */ + lauxlib.luaL_checktype(L, argf, lua.CT.LUA_TFUNCTION); /* closure */ + lauxlib.luaL_argcheck(L, (lapi.lua_getupvalue(L, argf, nup) !== null), argnup, lua.to_luastring("invalid upvalue index")); + return nup; +}; + + +const db_upvalueid = function(L) { + let n = checkupval(L, 1, 2); + lapi.lua_pushlightuserdata(L, lapi.lua_upvalueid(L, 1, n)); + return 1; +}; + const dblib = { - "getlocal": db_getlocal + "getlocal": db_getlocal, + "upvalueid": db_upvalueid }; // Only with Node |