diff options
author | Jude Melton-Houghton <jwmhjwmh@gmail.com> | 2022-03-29 12:07:00 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-29 18:07:00 +0200 |
commit | 06d197cdd042392e1551e5e7244c61300a6bb4e3 (patch) | |
tree | c47a0c58917f5f89af1c5128e42611d0d8a3d320 /src | |
parent | 11aab4198be1a0f1104a433896e908e4c86de0c9 (diff) | |
download | minetest-06d197cdd042392e1551e5e7244c61300a6bb4e3.tar.gz minetest-06d197cdd042392e1551e5e7244c61300a6bb4e3.tar.bz2 minetest-06d197cdd042392e1551e5e7244c61300a6bb4e3.zip |
Store vector metatable in registry
Diffstat (limited to 'src')
-rw-r--r-- | src/script/common/c_converter.cpp | 19 | ||||
-rw-r--r-- | src/script/common/c_internal.h | 1 | ||||
-rw-r--r-- | src/script/cpp_api/s_base.cpp | 8 | ||||
-rw-r--r-- | src/script/cpp_api/s_security.cpp | 6 |
4 files changed, 18 insertions, 16 deletions
diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp index 08fb9ad30..b5ff52f73 100644 --- a/src/script/common/c_converter.cpp +++ b/src/script/common/c_converter.cpp @@ -52,25 +52,12 @@ if (value < F1000_MIN || value > F1000_MAX) { \ /** - * A helper which sets (if available) the vector metatable from builtin as metatable - * for the table on top of the stack + * A helper which sets the vector metatable for the table on top of the stack */ static void set_vector_metatable(lua_State *L) { - // get vector.metatable - lua_getglobal(L, "vector"); - if (!lua_istable(L, -1)) { - // there is no global vector table - lua_pop(L, 1); - errorstream << "set_vector_metatable in c_converter.cpp: " << - "missing global vector table" << std::endl; - return; - } - lua_getfield(L, -1, "metatable"); - // set the metatable - lua_setmetatable(L, -3); - // pop vector global - lua_pop(L, 1); + lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_VECTOR_METATABLE); + lua_setmetatable(L, -2); } void push_v3f(lua_State *L, v3f p) diff --git a/src/script/common/c_internal.h b/src/script/common/c_internal.h index 94cfd61fb..c43db34aa 100644 --- a/src/script/common/c_internal.h +++ b/src/script/common/c_internal.h @@ -55,6 +55,7 @@ extern "C" { #define CUSTOM_RIDX_CURRENT_MOD_NAME (CUSTOM_RIDX_BASE + 2) #define CUSTOM_RIDX_BACKTRACE (CUSTOM_RIDX_BASE + 3) #define CUSTOM_RIDX_HTTP_API_LUA (CUSTOM_RIDX_BASE + 4) +#define CUSTOM_RIDX_VECTOR_METATABLE (CUSTOM_RIDX_BASE + 5) // Determine if CUSTOM_RIDX_SCRIPTAPI will hold a light or full userdata diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp index f7b8a5102..595c9e540 100644 --- a/src/script/cpp_api/s_base.cpp +++ b/src/script/cpp_api/s_base.cpp @@ -121,6 +121,14 @@ ScriptApiBase::ScriptApiBase(ScriptingType type): lua_newtable(m_luastack); lua_setglobal(m_luastack, "core"); + // vector.metatable is stored in the registry for quick access from C++. + lua_newtable(m_luastack); + lua_rawseti(m_luastack, LUA_REGISTRYINDEX, CUSTOM_RIDX_VECTOR_METATABLE); + lua_newtable(m_luastack); + lua_rawgeti(m_luastack, LUA_REGISTRYINDEX, CUSTOM_RIDX_VECTOR_METATABLE); + lua_setfield(m_luastack, -2, "metatable"); + lua_setglobal(m_luastack, "vector"); + if (m_type == ScriptingType::Client) lua_pushstring(m_luastack, "/"); else diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp index a6c5114b2..f68cd1777 100644 --- a/src/script/cpp_api/s_security.cpp +++ b/src/script/cpp_api/s_security.cpp @@ -98,6 +98,7 @@ void ScriptApiSecurity::initializeSecurity() "type", "unpack", "_VERSION", + "vector", "xpcall", }; static const char *whitelist_tables[] = { @@ -254,6 +255,10 @@ void ScriptApiSecurity::initializeSecurity() lua_pushnil(L); lua_setfield(L, old_globals, "core"); + // 'vector' as well. + lua_pushnil(L); + lua_setfield(L, old_globals, "vector"); + lua_pop(L, 1); // Pop globals_backup @@ -296,6 +301,7 @@ void ScriptApiSecurity::initializeSecurityClient() "type", "unpack", "_VERSION", + "vector", "xpcall", // Completely safe libraries "coroutine", |