diff options
author | daurnimator <quae@daurnimator.com> | 2017-04-26 19:32:40 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-04-26 19:35:40 +1000 |
commit | 77878e487ba645463886378b646809157c98ec6c (patch) | |
tree | 1e06e005cdeab8cbd142a581c6fd29356b6b58eb /src/lauxlib.js | |
parent | e9472c358593e359cba1ae7a5731a054683c564e (diff) | |
download | fengari-77878e487ba645463886378b646809157c98ec6c.tar.gz fengari-77878e487ba645463886378b646809157c98ec6c.tar.bz2 fengari-77878e487ba645463886378b646809157c98ec6c.zip |
src/lauxlib: Refactor luaL_tolstring to use lapi internals less
Diffstat (limited to 'src/lauxlib.js')
-rw-r--r-- | src/lauxlib.js | 21 |
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)); |