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(-) 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