]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Store vector metatable in registry
authorJude Melton-Houghton <jwmhjwmh@gmail.com>
Tue, 29 Mar 2022 16:07:00 +0000 (12:07 -0400)
committerGitHub <noreply@github.com>
Tue, 29 Mar 2022 16:07:00 +0000 (18:07 +0200)
builtin/common/tests/misc_helpers_spec.lua
builtin/common/tests/serialize_spec.lua
builtin/common/tests/vector_spec.lua
builtin/common/vector.lua
builtin/mainmenu/tests/serverlistmgr_spec.lua
games/devtest/mods/unittests/misc.lua
src/script/common/c_converter.cpp
src/script/common/c_internal.h
src/script/cpp_api/s_base.cpp
src/script/cpp_api/s_security.cpp

index b16987f0b6a011d4fdd0ec2f0fa8d5f3c995446a..b112368605886aed85a7de325eecff02f909d534 100644 (file)
@@ -1,4 +1,5 @@
 _G.core = {}
+_G.vector = {metatable = {}}
 dofile("builtin/common/vector.lua")
 dofile("builtin/common/misc_helpers.lua")
 
index e46b7dcc529ce07482f357dfd953bf98b05c3338..69b2b567c68c9d34859a5d0b75ece67d350d7e82 100644 (file)
@@ -1,4 +1,5 @@
 _G.core = {}
+_G.vector = {metatable = {}}
 
 _G.setfenv = require 'busted.compatibility'.setfenv
 
index 2f72f33830981b96e48f2ef233d6d4fac4b782e9..25880236bb62ffeccdcda313b870ce2b688898cb 100644 (file)
@@ -1,4 +1,4 @@
-_G.vector = {}
+_G.vector = {metatable = {}}
 dofile("builtin/common/vector.lua")
 
 describe("vector", function()
index 581d014e06b688dd5734a6855766824d4a7f5008..90010f6de300f6abcbaf75032d8cb66b4cdbb415 100644 (file)
@@ -6,10 +6,8 @@ Note: The vector.*-functions must be able to accept old vectors that had no meta
 -- localize functions
 local setmetatable = setmetatable
 
-vector = {}
-
-local metatable = {}
-vector.metatable = metatable
+-- vector.metatable is set by C++.
+local metatable = vector.metatable
 
 local xyz = {"x", "y", "z"}
 
index a091959fbbf45bca91fa74349e030c6f3e397a6f..ab7a6c60c85e5a27b1890ca1aa9f4cf9dcb83ef1 100644 (file)
@@ -1,4 +1,5 @@
 _G.core = {}
+_G.vector = {metatable = {}}
 _G.unpack = table.unpack
 _G.serverlistmgr = {}
 
index cf4f92cfa66984948a37143c81798cd595c4cb86..ba980866a12976281320346b7ec599015dbd629b 100644 (file)
@@ -36,3 +36,15 @@ local function test_dynamic_media(cb, player)
        -- if the callback isn't called this test will just hang :shrug:
 end
 unittests.register("test_dynamic_media", test_dynamic_media, {async=true, player=true})
+
+local function test_v3f_metatable(player)
+       assert(vector.check(player:get_pos()))
+end
+unittests.register("test_v3f_metatable", test_v3f_metatable, {player=true})
+
+local function test_v3s16_metatable(player, pos)
+       local node = minetest.get_node(pos)
+       local found_pos = minetest.find_node_near(pos, 0, node.name, true)
+       assert(vector.check(found_pos))
+end
+unittests.register("test_v3s16_metatable", test_v3s16_metatable, {map=true})
index 08fb9ad307e5dcd88b4343078a21b019b8c246f9..b5ff52f73bcaf6572466b65cd3b20cd7d1f003f5 100644 (file)
@@ -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)
index 94cfd61fb1912edb4ea76524ada16361c136646c..c43db34aa2091a5572ef1e7e2ac4853277fe5010 100644 (file)
@@ -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
index f7b8a5102864400a2e3434de9ea4041ea3332ed4..595c9e540b3eec64f566adcf9d95d89a5068135d 100644 (file)
@@ -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
index a6c5114b2f5c8ebdb8a8b600da519656482df0f9..f68cd17771690ff7459c5dbb937ae915e5992dfc 100644 (file)
@@ -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",