From 9bec9a5d038cfa488019e9370ff0b9ba785b313d Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 24 May 2017 16:48:20 +1000 Subject: src/lvm.js: Factor out OP_CLOSURE to pushclosure --- src/lvm.js | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/lvm.js b/src/lvm.js index 1f78dd3..e796e4e 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -607,20 +607,7 @@ const luaV_execute = function(L) { } case OCi.OP_CLOSURE: { let p = cl.p.p[i.Bx]; - let nup = p.upvalues.length; - let uv = p.upvalues; - let ncl = new lobject.LClosure(L, nup); - ncl.p = p; - - L.stack[ra] = new lobject.TValue(CT.LUA_TLCL, ncl); - - for (let i = 0; i < nup; i++) { - if (uv[i].instack) - ncl.upvals[i] = lfunc.luaF_findupval(L, base + uv[i].idx); - else - ncl.upvals[i] = cl.upvals[uv[i].idx]; - ncl.upvals[i].refcount++; - } + pushclosure(L, p, cl.upvals, base, ra); /* create a new one */ break; } case OCi.OP_VARARG: { @@ -948,6 +935,25 @@ const luaV_shiftl = function(x, y) { } }; +/* +** create a new Lua closure, push it in the stack, and initialize +** its upvalues. +*/ +const pushclosure = function(L, p, encup, base, ra) { + let nup = p.upvalues.length; + let uv = p.upvalues; + let ncl = new lobject.LClosure(L, nup); + ncl.p = p; + L.stack[ra] = new lobject.TValue(CT.LUA_TLCL, ncl); + for (let i = 0; i < nup; i++) { + if (uv[i].instack) + ncl.upvals[i] = lfunc.luaF_findupval(L, base + uv[i].idx); + else + ncl.upvals[i] = encup[uv[i].idx]; + ncl.upvals[i].refcount++; + } +}; + const cvt2str = function(o) { return o.ttisnumber(); }; -- cgit v1.2.3-70-g09d2