From 77878e487ba645463886378b646809157c98ec6c Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 26 Apr 2017 19:32:40 +1000 Subject: src/lauxlib: Refactor luaL_tolstring to use lapi internals less --- src/lauxlib.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src') 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)); -- cgit v1.2.3-70-g09d2