diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-01-15 11:31:49 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-01-15 11:31:49 -0300 |
| commit | 17e0c29d9b435392016b707309ed51409b0aea12 (patch) | |
| tree | c7178cf68fac2f1f9c3b22ce50be29b67f416cc0 /ltests.c | |
| parent | 8eb0abc9db4d47db5192bed18565e3d1aa53566d (diff) | |
| download | lua-17e0c29d9b435392016b707309ed51409b0aea12.tar.gz lua-17e0c29d9b435392016b707309ed51409b0aea12.tar.bz2 lua-17e0c29d9b435392016b707309ed51409b0aea12.zip | |
Clear interface between references and predefines
The reference system has a defined way to add initial values to the
table where it operates.
Diffstat (limited to 'ltests.c')
| -rw-r--r-- | ltests.c | 28 |
1 files changed, 25 insertions, 3 deletions
| @@ -1084,27 +1084,39 @@ static int string_query (lua_State *L) { | |||
| 1084 | } | 1084 | } |
| 1085 | 1085 | ||
| 1086 | 1086 | ||
| 1087 | static int getreftable (lua_State *L) { | ||
| 1088 | if (lua_istable(L, 2)) /* is there a table as second argument? */ | ||
| 1089 | return 2; /* use it as the table */ | ||
| 1090 | else | ||
| 1091 | return LUA_REGISTRYINDEX; /* default is to use the register */ | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | |||
| 1087 | static int tref (lua_State *L) { | 1095 | static int tref (lua_State *L) { |
| 1096 | int t = getreftable(L); | ||
| 1088 | int level = lua_gettop(L); | 1097 | int level = lua_gettop(L); |
| 1089 | luaL_checkany(L, 1); | 1098 | luaL_checkany(L, 1); |
| 1090 | lua_pushvalue(L, 1); | 1099 | lua_pushvalue(L, 1); |
| 1091 | lua_pushinteger(L, luaL_ref(L, LUA_REGISTRYINDEX)); | 1100 | lua_pushinteger(L, luaL_ref(L, t)); |
| 1092 | cast_void(level); /* to avoid warnings */ | 1101 | cast_void(level); /* to avoid warnings */ |
| 1093 | lua_assert(lua_gettop(L) == level+1); /* +1 for result */ | 1102 | lua_assert(lua_gettop(L) == level+1); /* +1 for result */ |
| 1094 | return 1; | 1103 | return 1; |
| 1095 | } | 1104 | } |
| 1096 | 1105 | ||
| 1106 | |||
| 1097 | static int getref (lua_State *L) { | 1107 | static int getref (lua_State *L) { |
| 1108 | int t = getreftable(L); | ||
| 1098 | int level = lua_gettop(L); | 1109 | int level = lua_gettop(L); |
| 1099 | lua_rawgeti(L, LUA_REGISTRYINDEX, luaL_checkinteger(L, 1)); | 1110 | lua_rawgeti(L, t, luaL_checkinteger(L, 1)); |
| 1100 | cast_void(level); /* to avoid warnings */ | 1111 | cast_void(level); /* to avoid warnings */ |
| 1101 | lua_assert(lua_gettop(L) == level+1); | 1112 | lua_assert(lua_gettop(L) == level+1); |
| 1102 | return 1; | 1113 | return 1; |
| 1103 | } | 1114 | } |
| 1104 | 1115 | ||
| 1105 | static int unref (lua_State *L) { | 1116 | static int unref (lua_State *L) { |
| 1117 | int t = getreftable(L); | ||
| 1106 | int level = lua_gettop(L); | 1118 | int level = lua_gettop(L); |
| 1107 | luaL_unref(L, LUA_REGISTRYINDEX, cast_int(luaL_checkinteger(L, 1))); | 1119 | luaL_unref(L, t, cast_int(luaL_checkinteger(L, 1))); |
| 1108 | cast_void(level); /* to avoid warnings */ | 1120 | cast_void(level); /* to avoid warnings */ |
| 1109 | lua_assert(lua_gettop(L) == level); | 1121 | lua_assert(lua_gettop(L) == level); |
| 1110 | return 0; | 1122 | return 0; |
| @@ -1373,6 +1385,16 @@ static int getnum_aux (lua_State *L, lua_State *L1, const char **pc) { | |||
| 1373 | (*pc)++; | 1385 | (*pc)++; |
| 1374 | return res; | 1386 | return res; |
| 1375 | } | 1387 | } |
| 1388 | else if (**pc == '!') { | ||
| 1389 | (*pc)++; | ||
| 1390 | if (**pc == 'G') | ||
| 1391 | res = LUA_RIDX_GLOBALS; | ||
| 1392 | else if (**pc == 'M') | ||
| 1393 | res = LUA_RIDX_MAINTHREAD; | ||
| 1394 | else lua_assert(0); | ||
| 1395 | (*pc)++; | ||
| 1396 | return res; | ||
| 1397 | } | ||
| 1376 | else if (**pc == '-') { | 1398 | else if (**pc == '-') { |
| 1377 | sig = -1; | 1399 | sig = -1; |
| 1378 | (*pc)++; | 1400 | (*pc)++; |
