From 1b48f9b50a2e4edefdffd5d33efab929296a395c Mon Sep 17 00:00:00 2001 From: daurnimator Date: Wed, 24 May 2017 14:46:19 +1000 Subject: Implement %p specifier in luaO_pushvfstring Move hacks out of luaL_tolstring --- src/lauxlib.js | 24 +----------------------- src/lobject.js | 16 +++++++++++++++- src/ltable.js | 1 + 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/lauxlib.js b/src/lauxlib.js index 3959817..b14379c 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -490,29 +490,7 @@ 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); - 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}`)); + lua.lua_pushfstring(L, lua.to_luastring("%s: %p"), kind, lua.lua_topointer(L, idx)); if (tt !== lua.LUA_TNIL) lua.lua_remove(L, -2); break; diff --git a/src/lobject.js b/src/lobject.js index 119e20e..5cbf2c3 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -7,7 +7,9 @@ const defs = require('./defs.js'); const ljstype = require('./ljstype.js'); const ldebug = require('./ldebug.js'); const ldo = require('./ldo.js'); +const lstate = require('./lstate.js'); const lstring = require('./lstring.js'); +const ltable = require('./ltable.js'); const luaconf = require('./luaconf.js'); const lvm = require('./lvm.js'); const llimit = require('./llimit.js'); @@ -514,7 +516,19 @@ const luaO_pushvfstring = function(L, fmt, argp) { ldo.luaD_inctop(L); L.stack[L.top-1] = luaO_tostring(L, new TValue(CT.LUA_TNUMFLT, argp[a++])); break; - // case char['p']: + case char['p']: + let v = argp[a++]; + if (v instanceof lstate.lua_State || + v instanceof ltable.Table || + v instanceof Udata || + v instanceof LClosure || + v instanceof CClosure) { + pushstr(L, defs.to_luastring("0x"+v.id.toString(16))); + } else { + /* user provided object. no id available */ + pushstr(L, defs.to_luastring("")); + } + break; case char['U']: pushstr(L, defs.to_luastring(String.fromCodePoint(argp[a++]))); break; diff --git a/src/ltable.js b/src/ltable.js index cd45fcf..5a31b48 100644 --- a/src/ltable.js +++ b/src/ltable.js @@ -276,3 +276,4 @@ module.exports.luaH_set = luaH_set; module.exports.luaH_setint = luaH_setint; module.exports.luaH_new = luaH_new; module.exports.luaH_next = luaH_next; +module.exports.Table = Table; -- cgit v1.2.3-70-g09d2