From e4c9580d20924a0db1ff7ed0d30da9b71dbb5066 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 22 May 2017 21:21:50 +1000 Subject: Introduce lvm.cvt2str In future this could be configurable --- src/lvm.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/lvm.js') diff --git a/src/lvm.js b/src/lvm.js index 4804cde..bd370a6 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -958,12 +958,16 @@ const luaV_shiftl = function(x, y) { } }; +const cvt2str = function(o) { + return o.ttisnumber(); +}; + const tostring = function(L, i) { let o = L.stack[i]; if (o.ttisstring()) return true; - if (o.ttisnumber() && !isNaN(o.value)) { + if (cvt2str(o) && !isNaN(o.value)) { L.stack[i] = new lobject.TValue(CT.LUA_TLNGSTR, lstring.luaS_bless(L, defs.to_luastring(`${o.value}`))); return true; } @@ -985,7 +989,7 @@ 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() || cvt2str(L.stack[top-2])) || !tostring(L, top - 1)) { ltm.luaT_trybinTM(L, L.stack[top-2], L.stack[top-1], top-2, ltm.TMS.TM_CONCAT); delete L.stack[top - 1]; } else if (isemptystr(L.stack[top-1])) { @@ -1090,6 +1094,7 @@ module.exports.RB = RB; module.exports.RC = RC; module.exports.RKB = RKB; module.exports.RKC = RKC; +module.exports.cvt2str = cvt2str; module.exports.dojump = dojump; module.exports.donextjump = donextjump; module.exports.forlimit = forlimit; -- cgit v1.2.3-70-g09d2 From 4f4e76d8de5fea7e636bdf5b5e271a2216531345 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 22 May 2017 21:32:41 +1000 Subject: src/lvm.js: Introduce cvt2num at mirror cvt2str --- src/lvm.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'src/lvm.js') diff --git a/src/lvm.js b/src/lvm.js index bd370a6..83feb9b 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -804,9 +804,10 @@ const luaV_tointeger = function(obj, mode) { return luaconf.lua_numbertointeger(f); } else if (obj.ttisinteger()) { return obj.value; - } else if (obj.ttisstring()) { - let n = lobject.luaO_str2num(obj.svalue()); - return n !== false ? luaV_tointeger(n, mode) : false; + } else if (cvt2num(obj)) { + let v = lobject.luaO_str2num(obj.svalue()); + if (v !== false) + return luaV_tointeger(v, mode); } return false; @@ -816,13 +817,14 @@ const tointeger = function(o) { return o.ttisinteger() ? o.value : luaV_tointeger(o, 0); }; -const tonumber = function(v) { - if (v.ttnov() === CT.LUA_TNUMBER) - return v.value; +const tonumber = function(o) { + if (o.ttnov() === CT.LUA_TNUMBER) + return o.value; - if (v.ttnov() === CT.LUA_TSTRING) { - let number = lobject.luaO_str2num(v.svalue()); - return number ? number.value : false; + if (cvt2num(o)) { /* string convertible to number? */ + let v = lobject.luaO_str2num(o.svalue()); + if (v !== false) + return v.value; } return false; @@ -962,6 +964,10 @@ const cvt2str = function(o) { return o.ttisnumber(); }; +const cvt2num = function(o) { + return o.ttisstring(); +}; + const tostring = function(L, i) { let o = L.stack[i]; @@ -1095,6 +1101,7 @@ module.exports.RC = RC; module.exports.RKB = RKB; module.exports.RKC = RKC; module.exports.cvt2str = cvt2str; +module.exports.cvt2num = cvt2num; module.exports.dojump = dojump; module.exports.donextjump = donextjump; module.exports.forlimit = forlimit; -- cgit v1.2.3-70-g09d2 From 128ae8cc451126ee7201de7efb6cc0c39fb1a2b4 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 22 May 2017 23:19:22 +1000 Subject: Introduce luaO_tostring --- src/lapi.js | 8 ++++---- src/lobject.js | 17 ++++++++++++++++- src/lvm.js | 4 ++-- 3 files changed, 22 insertions(+), 7 deletions(-) (limited to 'src/lvm.js') diff --git a/src/lapi.js b/src/lapi.js index f7ef82e..efc59aa 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -671,9 +671,9 @@ const lua_tolstring = function(L, idx) { if (!lvm.cvt2str(o)) { /* not convertible? */ return null; } + o = lobject.luaO_tostring(L, o); } - - return o.ttisstring() ? o.svalue() : defs.to_luastring(`${o.value}`); + return o.svalue(); }; const lua_tostring = lua_tolstring; @@ -685,9 +685,9 @@ const lua_toljsstring = function(L, idx) { if (!lvm.cvt2str(o)) { /* not convertible? */ return null; } + o = lobject.luaO_tostring(L, o); } - - return o.ttisstring() ? o.jsstring() : `${o.value}`; + return o.jsstring(); }; const lua_tojsstring = lua_toljsstring; diff --git a/src/lobject.js b/src/lobject.js index 41c3a84..e7b716e 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -448,6 +448,18 @@ const luaO_utf8esc = function(x) { }; }; +/* this currently returns new TValue instead of modifying */ +const luaO_tostring = function(L, obj) { + let buff; + if (obj.ttisinteger()) + buff = defs.to_luastring(luaconf.lua_integer2str(obj.value)); + else { + let str = luaconf.lua_number2str(obj.value); + buff = defs.to_luastring(str); + } + return new TValue(CT.LUA_TLNGSTR, lstring.luaS_bless(L, buff)); +}; + const pushstr = function(L, str) { L.stack[L.top++] = new TValue(CT.LUA_TLNGSTR, lstring.luaS_new(L, str)); }; @@ -476,8 +488,10 @@ const luaO_pushvfstring = function(L, fmt, argp) { break; case char['d']: case char['I']: + L.stack[L.top++] = luaO_tostring(L, new TValue(CT.LUA_TNUMINT, argp[a++])); + break; case char['f']: - pushstr(L, defs.to_luastring(''+argp[a++])); + L.stack[L.top++] = luaO_tostring(L, new TValue(CT.LUA_TNUMFLT, argp[a++])); break; // case char['p']: case char['U']: @@ -616,6 +630,7 @@ module.exports.luaO_int2fb = luaO_int2fb; module.exports.luaO_pushfstring = luaO_pushfstring; module.exports.luaO_pushvfstring = luaO_pushvfstring; module.exports.luaO_str2num = luaO_str2num; +module.exports.luaO_tostring = luaO_tostring; module.exports.luaO_utf8desc = luaO_utf8desc; module.exports.luaO_utf8esc = luaO_utf8esc; module.exports.numarith = numarith; diff --git a/src/lvm.js b/src/lvm.js index 83feb9b..0081376 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -973,8 +973,8 @@ const tostring = function(L, i) { if (o.ttisstring()) return true; - if (cvt2str(o) && !isNaN(o.value)) { - L.stack[i] = new lobject.TValue(CT.LUA_TLNGSTR, lstring.luaS_bless(L, defs.to_luastring(`${o.value}`))); + if (cvt2str(o)) { + L.stack[i] = lobject.luaO_tostring(L, o); return true; } -- cgit v1.2.3-70-g09d2