From a8e82fc01f2558550289f76f55c917039296ec11 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 17 Feb 2017 11:33:23 +0100 Subject: lua_load (bytecode only), lua_call(k) --- src/lapi.js | 19 +++++++++++++++++++ src/ldo.js | 35 ++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/lapi.js b/src/lapi.js index 976841d..6cc95b2 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -240,6 +240,24 @@ const lua_typename = function(L, t) { ** 'load' and 'call' functions (run Lua code) */ +const lua_load = function(L, data, chunckname) { + if (!chunckname) chunckname = "?"; + + let status = ldo.luaD_protectedparser(L, data, chunckname); + if (status === TS.LUA_OK) { + let f = L.stack[L.top - 1]; /* get newly created function */ + if (f.nupvalues >= 1) { /* does it have an upvalue? */ + /* get global table from registry */ + let reg = L.l_G.l_registry; + let gt = reg.value.array[lua.LUA_RIDX_GLOBALS]; + /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ + f.upvals[0].v = gt; // TODO: is gt on the stack ? is that upvalue opened or closed ? + } + } + + return status; +}; + const lua_callk = function(L, nargs, nresults, ctx, k) { assert(k === null || !(L.ci.callstatus & CIST_LUA), "cannot use continuations inside hooks"); assert(nargs + 1 < L.top - L.ci.funcOff, "not enough elements in the stack"); @@ -333,6 +351,7 @@ module.exports.lua_tointeger = lua_tointeger; module.exports.lua_toboolean = lua_toboolean; module.exports.lua_tolstring = lua_tolstring; module.exports.lua_tostring = lua_tostring; +module.exports.lua_load = lua_load; module.exports.lua_callk = lua_callk; module.exports.lua_call = lua_call; module.exports.lua_pop = lua_pop; \ No newline at end of file diff --git a/src/ldo.js b/src/ldo.js index 3686f7d..632bc22 100644 --- a/src/ldo.js +++ b/src/ldo.js @@ -9,6 +9,8 @@ const lstate = require('./lstate.js'); const llimit = require('./llimit.js'); const ltm = require('./ltm.js'); const lvm = require('./lvm.js'); +const lfunc = require('./lfunc.js'); +const BytecodeParser = require('./lundump.js'); const CT = lua.constant_types; const TS = lua.thread_status; const LUA_MULTRET = lua.LUA_MULTRET; @@ -237,7 +239,7 @@ const luaD_pcall = function(L, func, u, old_top, ef) { let old_errfunc = L.errfunc; L.errfunc = ef; - status = luaD_rawrunprotected(L, func, u); + let status = luaD_rawrunprotected(L, func, u); if (status !== TS.LUA_OK) { lfunc.luaF_close(L, old_top); @@ -263,12 +265,10 @@ const luaD_callnoyield = function(L, off, nResults) { // TODO: since we only handle binary, no need for a reader or mode const f_parser = function(L, data) { - assert(data instanceof DataView, "data must be a DataView"); + let p = new BytecodeParser(data); + let cl = p.luaU_undump(L); - let p = new lundump.BytecodeParser(data); - let cl = p.luaU_undump(); - - assert(cl.nupvalues == cl.p.sizeupvalues); + assert(cl.nupvalues == cl.p.upvalues.length); lfunc.luaF_initupvals(L, cl); }; @@ -283,14 +283,15 @@ const luaD_protectedparser = function(L, data, name) { return status; }; -module.exports.nil = nil; -module.exports.luaD_precall = luaD_precall; -module.exports.luaD_poscall = luaD_poscall; -module.exports.moveresults = moveresults; -module.exports.adjust_varargs = adjust_varargs; -module.exports.tryfuncTM = tryfuncTM; -module.exports.stackerror = stackerror; -module.exports.luaD_call = luaD_call; -module.exports.luaD_callnoyield = luaD_callnoyield; -module.exports.luaD_pcall = luaD_pcall; -module.exports.luaD_rawrunprotected = luaD_rawrunprotected; \ No newline at end of file +module.exports.nil = nil; +module.exports.luaD_precall = luaD_precall; +module.exports.luaD_poscall = luaD_poscall; +module.exports.moveresults = moveresults; +module.exports.adjust_varargs = adjust_varargs; +module.exports.tryfuncTM = tryfuncTM; +module.exports.stackerror = stackerror; +module.exports.luaD_call = luaD_call; +module.exports.luaD_callnoyield = luaD_callnoyield; +module.exports.luaD_pcall = luaD_pcall; +module.exports.luaD_rawrunprotected = luaD_rawrunprotected; +module.exports.luaD_protectedparser = luaD_protectedparser; \ No newline at end of file -- cgit v1.2.3-70-g09d2