Skip to content

Commit 8658c8d

Browse files
kahrlest31
authored andcommittedAug 26, 2015
Use numeric indices and raw table access with LUA_REGISTRYINDEX
1 parent 34b7a14 commit 8658c8d

File tree

7 files changed

+36
-16
lines changed

7 files changed

+36
-16
lines changed
 

Diff for: ‎src/script/common/c_internal.h

+21
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ extern "C" {
3434

3535
#include "common/c_types.h"
3636

37+
38+
/*
39+
Define our custom indices into the Lua registry table.
40+
41+
Lua 5.2 and above define the LUA_RIDX_LAST macro. Only numbers above that
42+
may be used for custom indices, anything else is reserved.
43+
44+
Lua 5.1 / LuaJIT do not use any numeric indices (only string indices),
45+
so we can use numeric indices freely.
46+
*/
47+
#ifdef LUA_RIDX_LAST
48+
#define CUSTOM_RIDX_BASE ((LUA_RIDX_LAST)+1)
49+
#else
50+
#define CUSTOM_RIDX_BASE 1
51+
#endif
52+
53+
#define CUSTOM_RIDX_SCRIPTAPI (CUSTOM_RIDX_BASE)
54+
#define CUSTOM_RIDX_GLOBALS_BACKUP (CUSTOM_RIDX_BASE + 1)
55+
#define CUSTOM_RIDX_CURRENT_MOD_NAME (CUSTOM_RIDX_BASE + 2)
56+
57+
3758
#define PCALL_RESL(L, RES) do { \
3859
int result_ = (RES); \
3960
if (result_ != 0) { \

Diff for: ‎src/script/cpp_api/s_base.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ class ModNameStorer
5252
{
5353
// Store current mod name in registry
5454
lua_pushstring(L, mod_name.c_str());
55-
lua_setfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
55+
lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
5656
}
5757
~ModNameStorer()
5858
{
5959
// Clear current mod name from registry
6060
lua_pushnil(L);
61-
lua_setfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
61+
lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
6262
}
6363
};
6464

@@ -84,7 +84,7 @@ ScriptApiBase::ScriptApiBase()
8484

8585
// Make the ScriptApiBase* accessible to ModApiBase
8686
lua_pushlightuserdata(m_luastack, this);
87-
lua_setfield(m_luastack, LUA_REGISTRYINDEX, "scriptapi");
87+
lua_rawseti(m_luastack, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI);
8888

8989
// If we are using LuaJIT add a C++ wrapper function to catch
9090
// exceptions thrown in Lua -> C++ calls

Diff for: ‎src/script/cpp_api/s_base.h

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ extern "C" {
3636
#define SCRIPTAPI_LOCK_DEBUG
3737
#define SCRIPTAPI_DEBUG
3838

39-
#define SCRIPT_MOD_NAME_FIELD "current_mod_name"
4039
// MUST be an invalid mod name so that mods can't
4140
// use that name to bypass security!
4241
#define BUILTIN_MOD_NAME "*builtin*"

Diff for: ‎src/script/cpp_api/s_security.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static inline void copy_safe(lua_State *L, const char *list[], unsigned len, int
4747
// Pushes the original version of a library function on the stack, from the old version
4848
static inline void push_original(lua_State *L, const char *lib, const char *func)
4949
{
50-
lua_getfield(L, LUA_REGISTRYINDEX, "globals_backup");
50+
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
5151
lua_getfield(L, -1, lib);
5252
lua_remove(L, -2); // Remove globals_backup
5353
lua_getfield(L, -1, func);
@@ -143,7 +143,7 @@ void ScriptApiSecurity::initializeSecurity()
143143

144144
// Backup globals to the registry
145145
lua_getglobal(L, "_G");
146-
lua_setfield(L, LUA_REGISTRYINDEX, "globals_backup");
146+
lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
147147

148148
// Replace the global environment with an empty one
149149
#if LUA_VERSION_NUM <= 501
@@ -165,7 +165,7 @@ void ScriptApiSecurity::initializeSecurity()
165165
#endif
166166

167167
// Get old globals
168-
lua_getfield(L, LUA_REGISTRYINDEX, "globals_backup");
168+
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
169169
int old_globals = lua_gettop(L);
170170

171171

@@ -241,7 +241,7 @@ void ScriptApiSecurity::initializeSecurity()
241241

242242
bool ScriptApiSecurity::isSecure(lua_State *L)
243243
{
244-
lua_getfield(L, LUA_REGISTRYINDEX, "globals_backup");
244+
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
245245
bool secure = !lua_isnil(L, -1);
246246
lua_pop(L, 1);
247247
return secure;
@@ -356,15 +356,15 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path)
356356
if (!removed.empty()) abs_path += DIR_DELIM + removed;
357357

358358
// Get server from registry
359-
lua_getfield(L, LUA_REGISTRYINDEX, "scriptapi");
359+
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI);
360360
ScriptApiBase *script = (ScriptApiBase *) lua_touserdata(L, -1);
361361
lua_pop(L, 1);
362362
const Server *server = script->getServer();
363363

364364
if (!server) return false;
365365

366366
// Get mod name
367-
lua_getfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
367+
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
368368
if (lua_isstring(L, -1)) {
369369
std::string mod_name = lua_tostring(L, -1);
370370

Diff for: ‎src/script/lua_api/l_base.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2626
ScriptApiBase *ModApiBase::getScriptApiBase(lua_State *L)
2727
{
2828
// Get server from registry
29-
lua_getfield(L, LUA_REGISTRYINDEX, "scriptapi");
29+
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_SCRIPTAPI);
3030
ScriptApiBase *sapi_ptr = (ScriptApiBase*) lua_touserdata(L, -1);
3131
lua_pop(L, 1);
3232
return sapi_ptr;
@@ -49,7 +49,7 @@ GUIEngine *ModApiBase::getGuiEngine(lua_State *L)
4949

5050
std::string ModApiBase::getCurrentModPath(lua_State *L)
5151
{
52-
lua_getfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
52+
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
5353
const char *current_mod_name = lua_tostring(L, -1);
5454
if (!current_mod_name)
5555
return ".";

Diff for: ‎src/script/lua_api/l_server.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ int ModApiServer::l_show_formspec(lua_State *L)
345345
int ModApiServer::l_get_current_modname(lua_State *L)
346346
{
347347
NO_MAP_LOCK_REQUIRED;
348-
lua_getfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
348+
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
349349
return 1;
350350
}
351351

@@ -442,7 +442,7 @@ int ModApiServer::l_notify_authentication_modified(lua_State *L)
442442
int ModApiServer::l_get_last_run_mod(lua_State *L)
443443
{
444444
NO_MAP_LOCK_REQUIRED;
445-
lua_getfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
445+
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
446446
const char *current_mod = lua_tostring(L, -1);
447447
if (current_mod == NULL || current_mod[0] == '\0') {
448448
lua_pop(L, 1);

Diff for: ‎src/script/lua_api/l_util.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ int ModApiUtil::l_request_insecure_environment(lua_State *L)
371371
lua_getglobal(L, "_G");
372372
return 1;
373373
}
374-
lua_getfield(L, LUA_REGISTRYINDEX, SCRIPT_MOD_NAME_FIELD);
374+
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
375375
if (!lua_isstring(L, -1)) {
376376
lua_pushnil(L);
377377
return 1;
@@ -383,7 +383,7 @@ int ModApiUtil::l_request_insecure_environment(lua_State *L)
383383
lua_pushnil(L);
384384
return 1;
385385
}
386-
lua_getfield(L, LUA_REGISTRYINDEX, "globals_backup");
386+
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
387387
return 1;
388388
}
389389

0 commit comments

Comments
 (0)
Please sign in to comment.