aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lapi.js8
-rw-r--r--src/lobject.js17
-rw-r--r--src/lvm.js4
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;
}