From 78f764030414e5c45ea059a5f582885da0282e39 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 23 May 2017 18:02:28 +1000 Subject: Introduce setsvalue2s --- src/lapi.js | 35 ++++++++++++++++++----------------- src/ldo.js | 7 ++++--- src/lobject.js | 12 ++++++++---- src/lvm.js | 4 ++-- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/lapi.js b/src/lapi.js index 2cf3a1c..8b2e164 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -235,9 +235,9 @@ const lua_pushlstring = function(L, s, len) { assert(Array.isArray(s), "lua_pushlstring expects array of byte"); assert(typeof len === "number"); - let ts = new TValue(CT.LUA_TLNGSTR, lstring.luaS_bless(L, s.slice(0, len))); - L.stack[L.top++] = ts; - + let ts = lstring.luaS_bless(L, s.slice(0, len)); + lobject.setsvalue2s(L, L.top, ts); + L.top++; assert(L.top <= L.ci.top, "stack overflow"); return ts.value; @@ -249,9 +249,9 @@ const lua_pushstring = function (L, s) { if (s === undefined || s === null) L.stack[L.top] = new TValue(CT.LUA_TNIL, null); else { - L.stack[L.top] = new TValue(CT.LUA_TLNGSTR, lstring.luaS_new(L, s)); + let ts = lstring.luaS_new(L, s); + lobject.setsvalue2s(L, L.top, ts); } - L.top++; assert(L.top <= L.ci.top, "stack overflow"); @@ -350,11 +350,11 @@ const lua_pushglobaltable = function(L) { const auxsetstr = function(L, t, k) { assert(Array.isArray(k), "key must be an array of bytes"); - let str = new TValue(CT.LUA_TLNGSTR, lstring.luaS_new(L, k)); - + let str = lstring.luaS_new(L, k); assert(1 < L.top - L.ci.funcOff, "not enough elements in the stack"); - - L.stack[L.top++] = str; + lobject.setsvalue2s(L, L.top, str); /* push 'str' (to make it a TValue) */ + L.top++; + assert(L.top <= L.ci.top, "stack overflow"); lvm.settable(L, t, L.stack[L.top - 1], L.stack[L.top - 2]); /* pop value and key */ delete L.stack[--L.top]; @@ -461,13 +461,11 @@ const lua_rawsetp = function(L, idx, p) { const auxgetstr = function(L, t, k) { assert(Array.isArray(k), "key must be an array of bytes"); - - let str = new TValue(CT.LUA_TLNGSTR, lstring.luaS_new(L, k)); - - L.stack[L.top++] = str; + let str = lstring.luaS_new(L, k); + lobject.setsvalue2s(L, L.top, str); + L.top++; assert(L.top <= L.ci.top, "stack overflow"); lvm.gettable(L, t, L.stack[L.top - 1], L.top - 1); - return L.stack[L.top - 1].ttnov(); }; @@ -649,7 +647,8 @@ const lua_tolstring = function(L, idx) { if (!lvm.cvt2str(o)) { /* not convertible? */ return null; } - o = lobject.luaO_tostring(L, o); + /* TODO: this should modify number on the stack */ + return lobject.luaO_tostring(L, o).getstr(); } return o.svalue(); }; @@ -663,7 +662,8 @@ const lua_toljsstring = function(L, idx) { if (!lvm.cvt2str(o)) { /* not convertible? */ return null; } - o = lobject.luaO_tostring(L, o); + /* TODO: this should modify number on the stack */ + return defs.to_jsstring(lobject.luaO_tostring(L, o).getstr()); } return o.jsstring(); }; @@ -1041,7 +1041,8 @@ const lua_concat = function(L, n) { if (n >= 2) lvm.luaV_concat(L, n); else if (n === 0) { - L.stack[L.top++] = new TValue(CT.LUA_TLNGSTR, lstring.luaS_newliteral(L, [])); + lobject.setsvalue2s(L, L.top, lstring.luaS_newliteral(L, [])); + L.top++; assert(L.top <= L.ci.top, "stack overflow"); } }; diff --git a/src/ldo.js b/src/ldo.js index 5fa51db..b32e404 100644 --- a/src/ldo.js +++ b/src/ldo.js @@ -24,11 +24,11 @@ const TS = defs.thread_status; const seterrorobj = function(L, errcode, oldtop) { switch (errcode) { case TS.LUA_ERRMEM: { - L.stack[oldtop] = new lobject.TValue(CT.LUA_TLNGSTR, lstring.luaS_newliteral(L, "not enough memory")); + lobject.setsvalue2s(L, oldtop, lstring.luaS_newliteral(L, "not enough memory")); break; } case TS.LUA_ERRERR: { - L.stack[oldtop] = new lobject.TValue(CT.LUA_TLNGSTR, lstring.luaS_newliteral(L, "error in error handling")); + lobject.setsvalue2s(L, oldtop, lstring.luaS_newliteral(L, "error in error handling")); break; } default: { @@ -465,7 +465,8 @@ const recover = function(L, status) { */ const resume_error = function(L, msg, narg) { L.top -= narg; /* remove args from the stack */ - L.stack[L.top++] = new lobject.TValue(CT.LUA_TLNGSTR, lstring.luaS_newliteral(L, msg)); /* push error message */ + lobject.setsvalue2s(L, L.top, lstring.luaS_newliteral(L, msg)); /* push error message */ + L.top++; assert(L.top <= L.ci.top, "stack overflow"); return TS.LUA_ERRRUN; }; diff --git a/src/lobject.js b/src/lobject.js index 3a442ee..ee7a51d 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -193,6 +193,9 @@ const setobjs2s = function(L, newidx, oldidx) { const setobj2s = function(L, newidx, oldtv) { L.stack[newidx] = new TValue(oldtv.type, oldtv.value); }; +const setsvalue2s = function(L, newidx, ts) { + L.stack[newidx] = new TValue(CT.LUA_TLNGSTR, ts); +}; const luaO_nilobject = new TValue(CT.LUA_TNIL, null); Object.freeze(luaO_nilobject); @@ -486,12 +489,12 @@ const luaO_tostring = function(L, obj) { buff.push(char['0']); /* adds '.0' to result */ } } - return new TValue(CT.LUA_TLNGSTR, lstring.luaS_bless(L, buff)); + return lstring.luaS_bless(L, buff); }; const pushstr = function(L, str) { + setsvalue2s(L, L.top, lstring.luaS_new(L, str)); ldo.luaD_inctop(L); - L.stack[L.top-1] = new TValue(CT.LUA_TLNGSTR, lstring.luaS_new(L, str)); }; const luaO_pushvfstring = function(L, fmt, argp) { @@ -519,11 +522,11 @@ const luaO_pushvfstring = function(L, fmt, argp) { case char['d']: case char['I']: ldo.luaD_inctop(L); - L.stack[L.top-1] = luaO_tostring(L, new TValue(CT.LUA_TNUMINT, argp[a++])); + setsvalue2s(L, L.top-1, luaO_tostring(L, new TValue(CT.LUA_TNUMINT, argp[a++]))); break; case char['f']: ldo.luaD_inctop(L); - L.stack[L.top-1] = luaO_tostring(L, new TValue(CT.LUA_TNUMFLT, argp[a++])); + setsvalue2s(L, L.top-1, luaO_tostring(L, new TValue(CT.LUA_TNUMFLT, argp[a++]))); break; case char['p']: let v = argp[a++]; @@ -678,3 +681,4 @@ module.exports.luaO_utf8esc = luaO_utf8esc; module.exports.numarith = numarith; module.exports.setobjs2s = setobjs2s; module.exports.setobj2s = setobj2s; +module.exports.setsvalue2s = setsvalue2s; diff --git a/src/lvm.js b/src/lvm.js index ecacd92..b9c2972 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -967,7 +967,7 @@ const tostring = function(L, i) { if (o.ttisstring()) return true; if (cvt2str(o)) { - L.stack[i] = lobject.luaO_tostring(L, o); + lobject.setsvalue2s(L, i, lobject.luaO_tostring(L, o)); return true; } @@ -1011,7 +1011,7 @@ const luaV_concat = function(L, total) { delete L.stack[top - n - 1]; } let ts = lstring.luaS_bless(L, Array.prototype.concat.apply([], toconcat)); - L.stack[top - n] = new lobject.TValue(CT.LUA_TLNGSTR, ts); + lobject.setsvalue2s(L, top - n, 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-54-g00ecf