diff options
Diffstat (limited to 'src/lobject.js')
-rw-r--r-- | src/lobject.js | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/lobject.js b/src/lobject.js index 41c3a84..8c70985 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -448,8 +448,26 @@ 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); + // Assume no LUA_COMPAT_FLOATSTRING + if (/^[-0123456789]+$/.test(str)) { /* looks like an int? */ + buff.push(char[luaconf.lua_getlocaledecpoint()]); + buff.push(char['0']); /* adds '.0' to result */ + } + } + 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)); + 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) { @@ -476,8 +494,12 @@ const luaO_pushvfstring = function(L, fmt, argp) { break; 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++])); + break; case char['f']: - pushstr(L, defs.to_luastring(''+argp[a++])); + ldo.luaD_inctop(L); + L.stack[L.top-1] = luaO_tostring(L, new TValue(CT.LUA_TNUMFLT, argp[a++])); break; // case char['p']: case char['U']: @@ -616,6 +638,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; |