aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lapi.js5
-rw-r--r--src/lauxlib.js27
-rw-r--r--src/lobject.js8
-rw-r--r--src/lstate.js4
-rw-r--r--src/ltable.js1
5 files changed, 38 insertions, 7 deletions
diff --git a/src/lapi.js b/src/lapi.js
index 826dee2..7123628 100644
--- a/src/lapi.js
+++ b/src/lapi.js
@@ -270,7 +270,7 @@ const lua_pushcclosure = function(L, fn, n) {
assert(n < L.top - L.ci.funcOff, "not enough elements in the stack");
assert(n <= MAXUPVAL, "upvalue index too large");
- let cl = new CClosure(fn, n);
+ let cl = new CClosure(L, fn, n);
L.top -= n;
while (n--) {
@@ -473,6 +473,7 @@ const lua_createtable = function(L, narray, nrec) {
const luaS_newudata = function(L, size) {
return {
+ id: L.l_G.id_counter++,
metatable: null,
uservalue: null,
len: size,
@@ -693,7 +694,7 @@ const lua_topointer = function(L, idx) {
case CT.LUA_TCCL:
case CT.LUA_TLCF:
case CT.LUA_TTHREAD:
- case CT.LUA_TUSERDATA:
+ case CT.LUA_TUSERDATA: /* note: this differs in behaviour to reference lua implementation */
case CT.LUA_TLIGHTUSERDATA:
return o.value;
default:
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 = "<id NYI>";
+ break;
+ case lua.LUA_TFUNCTION:
+ /* light C functions are returned from lua_topointer directly */
+ if (typeof p == "function") {
+ id = "<id NYI>";
+ 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;
diff --git a/src/lobject.js b/src/lobject.js
index d38e276..fe04f23 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -10,12 +10,10 @@ const llimit = require('./llimit.js');
const CT = defs.constant_types;
const char = defs.char;
-let tvalueCount = 0;
class TValue {
constructor(type, value) {
- this.id = tvalueCount++;
this.type = type;
this.value = value;
}
@@ -166,6 +164,8 @@ module.exports.luaO_nilobject = luaO_nilobject;
class LClosure {
constructor(L, n) {
+ this.id = L.l_G.id_counter++;
+
this.p = null;
this.nupvalues = n;
this.upvals = Array(n);
@@ -175,7 +175,9 @@ class LClosure {
class CClosure {
- constructor(f, n) {
+ constructor(L, f, n) {
+ this.id = L.l_G.id_counter++;
+
this.f = f;
this.nupvalues = n;
this.upvalue = new Array(n);
diff --git a/src/lstate.js b/src/lstate.js
index 1b077d2..d389419 100644
--- a/src/lstate.js
+++ b/src/lstate.js
@@ -45,6 +45,7 @@ class CallInfo {
class lua_State {
constructor() {
+ this.id = null;
this.base_ci = new CallInfo(); // Will be populated later
this.top = 0;
this.ci = null;
@@ -64,6 +65,8 @@ class lua_State {
class global_State {
constructor(L) {
+ this.id_counter = 0; /* used to give objects unique ids */
+
this.mainthread = L;
this.l_registry = new lobject.TValue(CT.LUA_TNIL, null);
this.panic = null;
@@ -123,6 +126,7 @@ const f_luaopen = function(L) {
};
const preinit_thread = function(L, g) {
+ L.id = g.id_counter++;
L.l_G = g;
L.stack = [];
L.ci = null;
diff --git a/src/ltable.js b/src/ltable.js
index 0d711b4..874a9f0 100644
--- a/src/ltable.js
+++ b/src/ltable.js
@@ -36,6 +36,7 @@ const table_hash = function(key) {
const luaH_new = function(L) {
let t = {
+ id: L.l_G.id_counter++,
strong: new Map(),
metatable: null
};