From a7b98a2e62c49a6c0ced2b57ddcea9bb6bab108e Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Sat, 11 Feb 2017 08:53:10 +0100 Subject: Fixed bad tailcall, _ENV is a closed upvalue --- src/lobject.js | 3 ++- src/lstate.js | 3 +-- src/lvm.js | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/lobject.js b/src/lobject.js index a1ed6ca..4c3290d 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -115,9 +115,10 @@ class LClosure extends TValue { let _ENV = new UpVal(); _ENV.refcount = 0; - _ENV.v = 0; // _ENV is on the stack at index 0 + _ENV.v = null; _ENV.u.open.next = null; _ENV.u.open.touched = true; + _ENV.u.value = new Table(); this.upvals = [ _ENV diff --git a/src/lstate.js b/src/lstate.js index 2c322c3..fcc3eb6 100644 --- a/src/lstate.js +++ b/src/lstate.js @@ -28,13 +28,12 @@ class CallInfo { class lua_State { constructor(cl) { - this.top = 2; + 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 = [ - new Table(), // _ENV cl ]; this.openupval = []; diff --git a/src/lvm.js b/src/lvm.js index feb0a53..7b70c01 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -431,7 +431,7 @@ class LuaVM { } case "OP_TAILCALL": { if (i.B !== 0) L.top = ra + i.B; - if (this.precall(ra, L.stack[ra], LUA_MULTRET)) { + if (this.precall(ra, L.stack[ra], LUA_MULTRET)) { // JS function base = ci.u.l.base; } else { /* tail call: put called frame (n) in place of caller one (o) */ @@ -445,7 +445,7 @@ class LuaVM { if (cl.p.p.length > 0) this.closeupvals(oci.u.l.base); for (let aux = 0; nfuncOff + aux < lim; aux++) L.stack[ofuncOff + aux] = L.stack[nfuncOff + aux]; - + oci.func = nci.func; oci.u.l.base = ofuncOff + (nci.u.l.base - nfuncOff); L.top = ofuncOff + (L.top - nfuncOff); oci.top = L.top; @@ -718,6 +718,7 @@ class LuaVM { } closeupvals(level) { + let L = this.L; while (L.openupval !== null && L.openupval.v >= level) { let uv = L.openupval; assert(uv.isopen()); -- cgit v1.2.3-70-g09d2