From 48b5ebfac37a9d0e4c3622530121e28a50a38a38 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 3 May 2017 16:11:25 +1000 Subject: Move .id field from TValue to values themselves --- src/lauxlib.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'src/lauxlib.js') diff --git a/src/lauxlib.js b/src/lauxlib.js index 1921237..9940c77 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -444,7 +444,8 @@ const luaL_tolstring = function(L, idx) { if (!lua.lua_isstring(L, -1)) luaL_error(L, lua.to_luastring("'__tostring' must return a string", true)); } else { - switch(lua.lua_type(L, idx)) { + let t = lua.lua_type(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())); @@ -479,7 +480,29 @@ const luaL_tolstring = function(L, idx) { default: let tt = luaL_getmetafield(L, idx, lua.to_luastring("__name", true)); let kind = tt === lua.LUA_TSTRING ? lua.lua_tostring(L, -1) : luaL_typename(L, idx); - lua.lua_pushstring(L, lua.to_luastring(`${lua.to_jsstring(kind)}: 0x${lapi.index2addr(L, -1).id.toString(16)}`)); + let p = lua.lua_topointer(L, idx); + let id; + switch (t) { + case lua.LUA_TLIGHTUSERDATA: + /* user provided object. no id available */ + id = ""; + break; + case lua.LUA_TFUNCTION: + /* light C functions are returned from lua_topointer directly */ + if (typeof p == "function") { + id = ""; + break; + } + /* fall through */ + case lua.LUA_TTABLE: + case lua.LUA_TTHREAD: + case lua.LUA_TUSERDATA: + id = `0x${p.id.toString(16)}`; + break; + default: + throw Error("unhandled type: "+t); + } + lua.lua_pushstring(L, lua.to_luastring(`${lua.to_jsstring(kind)}: ${id}`)); if (tt !== lua.LUA_TNIL) lua.lua_remove(L, -2); break; -- cgit v1.2.3-54-g00ecf