From dd087be6f10daf6dae87be07458dfaddb967edd1 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 9 May 2017 14:40:07 +1000 Subject: src/lvm.js: In luaV_concat use correct stack indices + clean up --- src/lvm.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/lvm.js b/src/lvm.js index 760f686..eba7d38 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -995,21 +995,26 @@ const luaV_concat = function(L, total) { let top = L.top; let n = 2; /* number of elements handled in this pass (at least 2) */ - if (!(L.stack[top-2].ttisstring() || L.stack[top-2].ttisnumber()) || !tostring(L, top - 1)) + if (!(L.stack[top-2].ttisstring() || L.stack[top-2].ttisnumber()) || !tostring(L, top - 1)) { ltm.luaT_trybinTM(L, L.stack[top-2], L.stack[top-1], top-2, ltm.TMS.TM_CONCAT); - else if (isemptystr(L.stack[top-1])) { + delete L.stack[top - 1]; + } else if (isemptystr(L.stack[top-1])) { tostring(L, top - 2); + delete L.stack[top - 1]; } else if (isemptystr(L.stack[top-2])) { L.stack[top - 2] = L.stack[top - 1]; + delete L.stack[top - 1]; } else { /* at least two non-empty string values; get as many as possible */ let toconcat = new Array(total); toconcat[total-1] = L.stack[top-1].svalue(); + delete L.stack[top - 1]; for (n = 1; n < total && tostring(L, top - n - 1); n++) { toconcat[total-n-1] = L.stack[top - n - 1].svalue(); + delete L.stack[top - n - 1]; } let ts = lstring.luaS_bless(L, Array.prototype.concat.apply([], toconcat)); - L.stack[top - total] = new lobject.TValue(CT.LUA_TLNGSTR, ts); + L.stack[top - n] = new lobject.TValue(CT.LUA_TLNGSTR, ts); } total -= n - 1; /* got 'n' strings to create 1 new */ L.top -= n - 1; /* popped 'n' strings and pushed one */ -- cgit v1.2.3-70-g09d2