_G.core = {}
+_G.vector = {metatable = {}}
dofile("builtin/common/vector.lua")
dofile("builtin/common/misc_helpers.lua")
_G.core = {}
+_G.vector = {metatable = {}}
_G.setfenv = require 'busted.compatibility'.setfenv
-_G.vector = {}
+_G.vector = {metatable = {}}
dofile("builtin/common/vector.lua")
describe("vector", function()
-- 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"}
_G.core = {}
+_G.vector = {metatable = {}}
_G.unpack = table.unpack
_G.serverlistmgr = {}
-- 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})
/**
- * 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)
#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
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
"type",
"unpack",
"_VERSION",
+ "vector",
"xpcall",
};
static const char *whitelist_tables[] = {
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
"type",
"unpack",
"_VERSION",
+ "vector",
"xpcall",
// Completely safe libraries
"coroutine",