aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2017-04-26 19:32:40 +1000
committerdaurnimator <quae@daurnimator.com>2017-04-26 19:35:40 +1000
commit77878e487ba645463886378b646809157c98ec6c (patch)
tree1e06e005cdeab8cbd142a581c6fd29356b6b58eb /src
parente9472c358593e359cba1ae7a5731a054683c564e (diff)
downloadfengari-77878e487ba645463886378b646809157c98ec6c.tar.gz
fengari-77878e487ba645463886378b646809157c98ec6c.tar.bz2
fengari-77878e487ba645463886378b646809157c98ec6c.zip
src/lauxlib: Refactor luaL_tolstring to use lapi internals less
Diffstat (limited to 'src')
-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));