diff options
Diffstat (limited to 'src/lfunc.js')
-rw-r--r-- | src/lfunc.js | 77 |
1 files changed, 11 insertions, 66 deletions
diff --git a/src/lfunc.js b/src/lfunc.js index b07b803..9290f4b 100644 --- a/src/lfunc.js +++ b/src/lfunc.js @@ -1,13 +1,9 @@ "use strict"; -const assert = require('assert'); - -const defs = require('./defs.js'); +const { constant_types: { LUA_TNIL } } = require('./defs.js'); const lobject = require('./lobject.js'); -const CT = defs.constant_types; class Proto { - constructor(L) { this.id = L.l_G.id_counter++; this.k = []; // constants used by the function @@ -24,81 +20,32 @@ class Proto { this.lastlinedefined = 0; // debug information this.source = null; // used for debug information } - -} - -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; + return new lobject.LClosure(L, n); }; const luaF_findupval = function(L, level) { - let prevp; - let p = L.openupval; - while (p !== null && p.vOff >= level) { - 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; - 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<L.top; i++) { + let old = L.stack[i]; + L.stack[i] = new lobject.TValue(old.type, old.value); } }; /* -** fill a closure with new closed upvalues +** fill a closure with new upvalues */ const luaF_initupvals = function(L, cl) { - for (let i = 0; i < cl.nupvalues; i++) { - let uv = new UpVal(L); - uv.refcount = 1; - uv.v = new lobject.TValue(CT.LUA_TNIL, null); - cl.upvals[i] = uv; - } + for (let i = 0; i < cl.nupvalues; i++) + cl.upvals[i] = new lobject.TValue(LUA_TNIL, null); }; /* @@ -116,10 +63,8 @@ const luaF_getlocalname = function(f, local_number, pc) { return null; /* not found */ }; - module.exports.MAXUPVAL = 255; module.exports.Proto = Proto; -module.exports.UpVal = UpVal; module.exports.luaF_findupval = luaF_findupval; module.exports.luaF_close = luaF_close; module.exports.luaF_getlocalname = luaF_getlocalname; |