From 313f55905253697fe4966f979865296aeaa7a801 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 29 Jan 2018 19:01:22 +1100 Subject: src/: Upvalues are now just TValues (possibly referencing on-stack) - Removes `Upval` class - closing over upvalues is now done by creating new on-stack TValue objects - No more `openupval` linked list With this fix, upvalues from collected coroutines will no longer keep other values alive Closes #44 --- src/lapi.js | 13 +++--------- src/ldebug.js | 2 +- src/lfunc.js | 64 ++++++++-------------------------------------------------- src/lobject.js | 6 ++---- src/lstate.js | 4 ---- src/lvm.js | 13 ++++++------ 6 files changed, 20 insertions(+), 82 deletions(-) diff --git a/src/lapi.js b/src/lapi.js index ec643b3..8ed67f5 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -32,10 +32,7 @@ const { from_userstring, to_luastring, } = require('./defs.js'); -const { - api_check, - lua_assert -} = require('./llimits.js'); +const { api_check } = require('./llimits.js'); const ldebug = require('./ldebug.js'); const ldo = require('./ldo.js'); const { luaU_dump } = require('./ldump.js'); @@ -555,7 +552,7 @@ const aux_upvalue = function(L, fi, n) { let name = p.upvalues[n-1].name; return { name: name ? name.getstr() : to_luastring("(*no name)", true), - val: f.upvals[n-1].v + val: f.upvals[n-1] }; } default: return null; /* not a closure */ @@ -930,7 +927,7 @@ const lua_load = function(L, reader, data, chunkname, mode) { /* get global table from registry */ let gt = ltable.luaH_getint(L.l_G.l_registry.value, LUA_RIDX_GLOBALS); /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */ - f.upvals[0].v.setfrom(gt); + f.upvals[0].setfrom(gt); } } return status; @@ -1111,13 +1108,9 @@ const lua_upvalueid = function(L, fidx, n) { const lua_upvaluejoin = function(L, fidx1, n1, fidx2, n2) { let ref1 = getupvalref(L, fidx1, n1); let ref2 = getupvalref(L, fidx2, n2); - let up1 = ref1.upval; let up2 = ref2.upval; let f1 = ref1.closure; - lua_assert(up1.refcount > 0); - up1.refcount--; f1.upvals[ref1.upvalOff] = up2; - up2.refcount++; }; // This functions are only there for compatibility purposes diff --git a/src/ldebug.js b/src/ldebug.js index e403ba6..a471c75 100644 --- a/src/ldebug.js +++ b/src/ldebug.js @@ -537,7 +537,7 @@ const isinstack = function(L, ci, o) { const getupvalname = function(L, ci, o) { let c = ci.func.value; for (let i = 0; i < c.nupvalues; i++) { - if (c.upvals[i].v === o) { + if (c.upvals[i] === o) { return { name: upvalname(c.p, i), funcname: to_luastring('upvalue', true) diff --git a/src/lfunc.js b/src/lfunc.js index 2f28d0e..198c4b9 100644 --- a/src/lfunc.js +++ b/src/lfunc.js @@ -1,7 +1,6 @@ "use strict"; const { constant_types: { LUA_TNIL } } = require('./defs.js'); -const { lua_assert } = require('./llimits.js'); const lobject = require('./lobject.js'); class Proto { @@ -25,22 +24,6 @@ class Proto { } -class UpVal { - - constructor(L) { - this.id = L.l_G.id_counter++; - this.v = void 0; /* if open: reference to TValue on stack. if closed: TValue */ - this.vOff = void 0; /* if open: index on stack. if closed: undefined */ - this.refcount = 0; - this.open_next = null; /* linked list (when open) */ - } - - isopen() { - return this.vOff !== void 0; - } - -} - const luaF_newLclosure = function(L, n) { let c = new lobject.LClosure(L, n); return c; @@ -48,54 +31,24 @@ const luaF_newLclosure = function(L, n) { const luaF_findupval = function(L, level) { - let prevp; - let p = L.openupval; - while (p !== null && p.vOff >= level) { - lua_assert(p.isopen()); - if (p.vOff === level) /* found a corresponding upvalue? */ - return p; /* return it */ - prevp = p; - p = p.open_next; - } - /* not found: create a new upvalue */ - let uv = new UpVal(L); - /* link it to list of open upvalues */ - uv.open_next = p; - if (prevp) - prevp.open_next = uv; - else - L.openupval = uv; - uv.v = L.stack[level]; /* current value lives in the stack */ - uv.vOff = level; - return uv; + return L.stack[level]; }; const luaF_close = function(L, level) { - while (L.openupval !== null && L.openupval.vOff >= level) { - let uv = L.openupval; - lua_assert(uv.isopen()); - L.openupval = uv.open_next; /* remove from 'open' list */ - if (uv.refcount === 0) { /* no references? */ - /* free upvalue */ - uv.v = void 0; - uv.open_next = null; - } else { - let from = uv.v; - uv.v = new lobject.TValue(from.type, from.value); - } - uv.vOff = void 0; + /* Create new TValues on stack; + * any closures will keep referencing old TValues */ + for (let i=level; i