From 17e0c29d9b435392016b707309ed51409b0aea12 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 15 Jan 2024 11:31:49 -0300 Subject: Clear interface between references and predefines The reference system has a defined way to add initial values to the table where it operates. --- ltests.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'ltests.c') diff --git a/ltests.c b/ltests.c index 6eebc732..6de62e52 100644 --- a/ltests.c +++ b/ltests.c @@ -1084,27 +1084,39 @@ static int string_query (lua_State *L) { } +static int getreftable (lua_State *L) { + if (lua_istable(L, 2)) /* is there a table as second argument? */ + return 2; /* use it as the table */ + else + return LUA_REGISTRYINDEX; /* default is to use the register */ +} + + static int tref (lua_State *L) { + int t = getreftable(L); int level = lua_gettop(L); luaL_checkany(L, 1); lua_pushvalue(L, 1); - lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX)); + lua_pushinteger(L, luaL_ref(L, t)); cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level+1); /* +1 for result */ return 1; } + static int getref (lua_State *L) { + int t = getreftable(L); int level = lua_gettop(L); - lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1)); + lua_rawgeti(L, t, luaL_checkinteger(L, 1)); cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level+1); return 1; } static int unref (lua_State *L) { + int t = getreftable(L); int level = lua_gettop(L); - luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1))); + luaL_unref(L, t, cast_int(luaL_checkinteger(L, 1))); cast_void(level); /* to avoid warnings */ lua_assert(lua_gettop(L) == level); return 0; @@ -1373,6 +1385,16 @@ static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) { (*pc)++; return res; } + else if (**pc == '!') { + (*pc)++; + if (**pc == 'G') + res = LUA_RIDX_GLOBALS; + else if (**pc == 'M') + res = LUA_RIDX_MAINTHREAD; + else lua_assert(0); + (*pc)++; + return res; + } else if (**pc == '-') { sig = -1; (*pc)++; -- cgit v1.2.3-55-g6feb