From e6639768e4a059aeaf80e232c729dffa3b5e93ba Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 22 May 2017 23:20:02 +1000 Subject: src/lauxlib.js: Use lua_pushfstring for formatting numbers --- src/lauxlib.js | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/lauxlib.js b/src/lauxlib.js index 559e1bc..3959817 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -473,24 +473,9 @@ const luaL_tolstring = function(L, idx) { switch(t) { case lua.LUA_TNUMBER: { 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 (Object.is(n, Infinity)) - s = 'inf'; - else if (Object.is(n, -Infinity)) - s = '-inf'; - else if (Number.isNaN(n)) - s = 'nan'; - else if (a >= 100000000000000 || (a > 0 && 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)); - } + lua.lua_pushfstring(L, lua.to_luastring("%I"), lua.lua_tointeger(L, idx)); + else + lua.lua_pushfstring(L, lua.to_luastring("%f"), lua.lua_tonumber(L, idx)); break; } case lua.LUA_TSTRING: -- cgit v1.2.3-70-g09d2