aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lauxlib.js21
1 files changed, 17 insertions, 4 deletions
diff --git a/src/lauxlib.js b/src/lauxlib.js
index acdecbf..3921519 100644
--- a/src/lauxlib.js
+++ b/src/lauxlib.js
@@ -363,14 +363,27 @@ const luaL_tolstring = function(L, idx) {
} else {
switch(lua.lua_type(L, idx)) {
case lua.LUA_TNUMBER:
- case lua.LUA_TBOOLEAN:
- lua.lua_pushstring(L, lua.to_luastring(`${lapi.index2addr(L, idx).value}`));
+ if (lua.lua_isinteger(L, idx))
+ lua.lua_pushstring(L, lua.to_luastring(lua.lua_tointeger(L, idx).toString()));
+ else {
+ let n = lua.lua_tonumber(L, idx);
+ let a = Math.abs(n);
+ let s;
+ if (a > 100000000000000 || a < 0.0001)
+ s = n.toExponential();
+ else
+ s = n.toPrecision(16).replace(/(\.[0-9][1-9]*)0+$/, "$1");
+ lua.lua_pushstring(L, lua.to_luastring(s));
+ }
break;
case lua.LUA_TSTRING:
- lua.lua_pushstring(L, lapi.index2addr(L, idx).value);
+ lua.lua_pushvalue(L, idx);
+ break;
+ case lua.LUA_TBOOLEAN:
+ lua.lua_pushliteral(L, (lua.lua_toboolean(L, idx) ? "true" : "false"));
break;
case lua.LUA_TNIL:
- lua.lua_pushstring(L, lua.to_luastring(`nil`));
+ lua.lua_pushliteral(L, "nil");
break;
default:
let tt = luaL_getmetafield(L, idx, lua.to_luastring("__name", true));