1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Add metatables to lua vectors (#11039)

Add backwards-compatible metatable functions for vectors.
This commit is contained in:
DS 2021-06-04 21:22:33 +02:00 committed by GitHub
parent e15cae9fa0
commit 8f085e02a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 570 additions and 179 deletions

View file

@ -51,6 +51,29 @@ if (value < F1000_MIN || value > F1000_MAX) { \
#define CHECK_POS_TAB(index) CHECK_TYPE(index, "position", LUA_TTABLE)
/**
* A helper which sets (if available) the vector metatable from builtin as 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);
}
void push_float_string(lua_State *L, float value)
{
std::stringstream ss;
@ -69,6 +92,7 @@ void push_v3f(lua_State *L, v3f p)
lua_setfield(L, -2, "y");
lua_pushnumber(L, p.Z);
lua_setfield(L, -2, "z");
set_vector_metatable(L);
}
void push_v2f(lua_State *L, v2f p)
@ -281,6 +305,7 @@ void push_v3s16(lua_State *L, v3s16 p)
lua_setfield(L, -2, "y");
lua_pushinteger(L, p.Z);
lua_setfield(L, -2, "z");
set_vector_metatable(L);
}
v3s16 read_v3s16(lua_State *L, int index)