From 50aa5b5029165be03d0cfb34e7d664795dd80898 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 17 Feb 2017 09:27:51 +0100 Subject: More accurate state and closure init --- src/lapi.js | 1 + src/ldo.js | 22 ++++++++++++++++++++++ src/lfunc.js | 20 +++++++++++++++----- src/lstate.js | 21 +++++---------------- src/lundump.js | 6 +++++- 5 files changed, 48 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/lapi.js b/src/lapi.js index 0e3c3bd..976841d 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -10,6 +10,7 @@ const lfunc = require('./lfunc.js'); const lua = require('./lua.js'); const lstate = require('./lstate.js'); const lvm = require('./lvm.js'); +const lundump = require('./lundump.js'); const MAXUPVAL = lfunc.MAXUPVAL; const CT = lua.constant_types; const TS = lua.thread_status; diff --git a/src/ldo.js b/src/ldo.js index f965a47..3686f7d 100644 --- a/src/ldo.js +++ b/src/ldo.js @@ -261,6 +261,28 @@ const luaD_callnoyield = function(L, off, nResults) { L.nny--; }; +// 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 lundump.BytecodeParser(data); + let cl = p.luaU_undump(); + + assert(cl.nupvalues == cl.p.sizeupvalues); + + lfunc.luaF_initupvals(L, cl); +}; + +const luaD_protectedparser = function(L, data, name) { + L.nny++; + + let status = luaD_pcall(L, f_parser, data, L.top, L.errfunc); + + L.nny--; + + return status; +}; + module.exports.nil = nil; module.exports.luaD_precall = luaD_precall; module.exports.luaD_poscall = luaD_poscall; diff --git a/src/lfunc.js b/src/lfunc.js index f3e11d3..39ad049 100644 --- a/src/lfunc.js +++ b/src/lfunc.js @@ -88,8 +88,18 @@ const luaF_close = function(L, level) { } }; -module.exports.Proto = Proto; -module.exports.UpVal = UpVal; -module.exports.findupval = findupval; -module.exports.luaF_close = luaF_close; -module.exports.MAXUPVAL = 255; \ No newline at end of file +const luaF_initupvals = function(L, cl) { + for (let i = 0; i < cl.nupvalues; i++) { + let uv = new UpVal(); + uv.refcount = 1; + uv.u.value = null; + uv.v = uv.u.value; + } +}; + +module.exports.Proto = Proto; +module.exports.UpVal = UpVal; +module.exports.findupval = findupval; +module.exports.luaF_close = luaF_close; +module.exports.MAXUPVAL = 255; +module.exports.luaF_initupvals = luaF_initupvals; \ No newline at end of file diff --git a/src/lstate.js b/src/lstate.js index 4d71be7..54e7ac7 100644 --- a/src/lstate.js +++ b/src/lstate.js @@ -44,22 +44,11 @@ class CallInfo { class lua_State { constructor(cl) { - if (cl) { // TODO: remove - this.top = 1; - this.ci = new CallInfo(0, cl, 1, 1, null, null); - this.ci.u.l.savedpc = cl.p.code; - this.ci.nresults = LUA_MULTRET; - this.ciOff = 0; - this.stack = [ - cl - ]; - } else { - this.base_ci = new CallInfo(); // Will be populated later - this.top = 0; - this.ci = null; - this.ciOff = null; - this.stack = []; - } + this.base_ci = new CallInfo(); // Will be populated later + this.top = 0; + this.ci = null; + this.ciOff = null; + this.stack = []; this.openupval = []; this.status = TS.LUA_OK; this.next = null; diff --git a/src/lundump.js b/src/lundump.js index 0ff3143..ab1dd6f 100644 --- a/src/lundump.js +++ b/src/lundump.js @@ -290,10 +290,14 @@ class BytecodeParser { } - luaU_undump() { + luaU_undump(L) { this.checkHeader(); let cl = new LClosure(this.readByte()); + + L.stack[L.top] = cl; + L.top++; + cl.p = new Proto(); this.readFunction(cl.p); -- cgit v1.2.3-70-g09d2