diff options
author | daurnimator <quae@daurnimator.com> | 2017-05-22 23:20:02 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-05-23 00:52:04 +1000 |
commit | e6639768e4a059aeaf80e232c729dffa3b5e93ba (patch) | |
tree | 6e0e67b5281de24d92b588e56f4a715001f5e4fa | |
parent | 128ae8cc451126ee7201de7efb6cc0c39fb1a2b4 (diff) | |
download | fengari-e6639768e4a059aeaf80e232c729dffa3b5e93ba.tar.gz fengari-e6639768e4a059aeaf80e232c729dffa3b5e93ba.tar.bz2 fengari-e6639768e4a059aeaf80e232c729dffa3b5e93ba.zip |
src/lauxlib.js: Use lua_pushfstring for formatting numbers
-rw-r--r-- | src/lauxlib.js | 21 | ||||
-rw-r--r-- | tests/test-suite/strings.js | 2 |
2 files changed, 4 insertions, 19 deletions
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: diff --git a/tests/test-suite/strings.js b/tests/test-suite/strings.js index 3584a80..d7b30a8 100644 --- a/tests/test-suite/strings.js +++ b/tests/test-suite/strings.js @@ -288,7 +288,7 @@ test('[test-suite] strings: tostring', function (t) { end if tostring(0.0) == "0.0" then -- "standard" coercion float->string - -- assert('' .. 12 == '12' and 12.0 .. '' == '12.0') -- TODO: How to do this in JS ? + assert('' .. 12 == '12' and 12.0 .. '' == '12.0') assert(tostring(-1203 + 0.0) == "-1203.0") else -- compatible coercion assert(tostring(0.0) == "0") |