diff options
author | daurnimator <quae@daurnimator.com> | 2017-05-22 23:19:22 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-05-23 00:52:02 +1000 |
commit | 128ae8cc451126ee7201de7efb6cc0c39fb1a2b4 (patch) | |
tree | bcda9fa49eca0aeb2859d0daf252ed01a121d9ce /src | |
parent | b525c305b524f5b3ffaba3187ccc828d332aa835 (diff) | |
download | fengari-128ae8cc451126ee7201de7efb6cc0c39fb1a2b4.tar.gz fengari-128ae8cc451126ee7201de7efb6cc0c39fb1a2b4.tar.bz2 fengari-128ae8cc451126ee7201de7efb6cc0c39fb1a2b4.zip |
Introduce luaO_tostring
Diffstat (limited to 'src')
-rw-r--r-- | src/lapi.js | 8 | ||||
-rw-r--r-- | src/lobject.js | 17 | ||||
-rw-r--r-- | 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; @@ -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; } |