From 0133e556602950f71747ef518cdaa861fe422b32 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Sun, 23 Apr 2017 14:52:30 +0200 Subject: Handle when no upvalue found --- src/lapi.js | 23 +++++++++++++++-------- src/lfunc.js | 5 +++-- 2 files changed, 18 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/lapi.js b/src/lapi.js index 99b0859..0231597 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -507,26 +507,33 @@ const aux_upvalue = function(L, fi, n) { const lua_getupvalue = function(L, funcindex, n) { let up = aux_upvalue(L, index2addr(L, funcindex), n); - let name = up.name; - let val = up.val; - if (name) + if (up) { + let name = up.name; + let val = up.val; + L.stack[L.top++] = new TValue(val.type, val.value); - return name.value; + + return name.value; + } + return null; }; const lua_setupvalue = function(L, funcindex, n) { let fi = index2addr(L, funcindex); assert(1 < L.top - L.ci.funcOff, "not enough elements in the stack"); let aux = aux_upvalue(L, fi, n); - let name = aux.name; - let val = aux.val; - if (name) { + if (aux) { + let name = aux.name; + let val = aux.val; + L.top--; // TODO: what if it's not a pure TValue (closure, table) val.type = L.stack[L.top].type; val.value = L.stack[L.top].value; + + return name.value; } - return name.value; + return null; }; const lua_newtable = function(L) { diff --git a/src/lfunc.js b/src/lfunc.js index bc88702..bde96d7 100644 --- a/src/lfunc.js +++ b/src/lfunc.js @@ -43,9 +43,10 @@ class UpVal { } setval(L, ra) { + let o = L.stack[ra]; if (this.v !== null) { - this.L.stack[this.v] = L.stack[ra]; - } else this.u.value = L.stack[ra]; + this.L.stack[this.v] = new lobject.TValue(o.type, o.value); + } else this.u.value = new lobject.TValue(o.type, o.value); } isopen() { -- cgit v1.2.3-70-g09d2