From 1ee37148a8072fe6350124cd51c812c3d3fb069a Mon Sep 17 00:00:00 2001 From: Lars Müller <34514239+appgurueu@users.noreply.github.com> Date: Fri, 4 Feb 2022 20:28:43 +0100 Subject: Fix types of get_mapgen_setting_noiseparams (#12025) --- src/script/common/c_converter.cpp | 27 --------------------------- 1 file changed, 27 deletions(-) (limited to 'src/script/common/c_converter.cpp') diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp index 19734b913..716405593 100644 --- a/src/script/common/c_converter.cpp +++ b/src/script/common/c_converter.cpp @@ -73,13 +73,6 @@ static void set_vector_metatable(lua_State *L) lua_pop(L, 1); } - -void push_float_string(lua_State *L, float value) -{ - auto str = ftos(value); - lua_pushstring(L, str.c_str()); -} - void push_v3f(lua_State *L, v3f p) { lua_createtable(L, 0, 3); @@ -101,26 +94,6 @@ void push_v2f(lua_State *L, v2f p) lua_setfield(L, -2, "y"); } -void push_v3_float_string(lua_State *L, v3f p) -{ - lua_createtable(L, 0, 3); - push_float_string(L, p.X); - lua_setfield(L, -2, "x"); - push_float_string(L, p.Y); - lua_setfield(L, -2, "y"); - push_float_string(L, p.Z); - lua_setfield(L, -2, "z"); -} - -void push_v2_float_string(lua_State *L, v2f p) -{ - lua_createtable(L, 0, 2); - push_float_string(L, p.X); - lua_setfield(L, -2, "x"); - push_float_string(L, p.Y); - lua_setfield(L, -2, "y"); -} - v2s16 read_v2s16(lua_State *L, int index) { v2s16 p; -- cgit v1.2.3 From ba6fbc417ecb812345c1747f42b6606dfc8e1d5b Mon Sep 17 00:00:00 2001 From: sfan5 Date: Thu, 3 Feb 2022 21:35:08 +0100 Subject: Remove awful Mingw32 workarounds Instead a warning is triggered if an affected compiler is detected. closes #12022 --- src/main.cpp | 8 ++++++++ src/script/common/c_converter.cpp | 8 -------- src/serialization.cpp | 28 ++-------------------------- 3 files changed, 10 insertions(+), 34 deletions(-) (limited to 'src/script/common/c_converter.cpp') diff --git a/src/main.cpp b/src/main.cpp index ca95ef874..5ea212d8a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -65,6 +65,14 @@ extern "C" { #error Minetest cannot be built without exceptions or RTTI #endif +#if defined(__MINGW32__) && !defined(__MINGW64__) && !defined(__clang__) && \ + (__GNUC__ < 11 || (__GNUC__ == 11 && __GNUC_MINOR__ < 1)) +// see e.g. https://github.com/minetest/minetest/issues/10137 +#warning ================================== +#warning 32-bit MinGW gcc before 11.1 has known issues with crashes on thread exit, you should upgrade. +#warning ================================== +#endif + #define DEBUGFILE "debug.txt" #define DEFAULT_SERVER_PORT 30000 diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp index 716405593..08fb9ad30 100644 --- a/src/script/common/c_converter.cpp +++ b/src/script/common/c_converter.cpp @@ -452,17 +452,9 @@ size_t read_stringlist(lua_State *L, int index, std::vector *result Table field getters */ -#if defined(__MINGW32__) && !defined(__MINGW64__) -/* MinGW 32-bit somehow crashes in the std::set destructor when this - * variable is thread-local, so just don't do that. */ -static std::set warned_msgs; -#endif - bool check_field_or_nil(lua_State *L, int index, int type, const char *fieldname) { -#if !defined(__MINGW32__) || defined(__MINGW64__) thread_local std::set warned_msgs; -#endif int t = lua_type(L, index); if (t == LUA_TNIL) diff --git a/src/serialization.cpp b/src/serialization.cpp index d4d7b5f6e..d3009bc83 100644 --- a/src/serialization.cpp +++ b/src/serialization.cpp @@ -208,30 +208,11 @@ struct ZSTD_Deleter { } }; -#if defined(__MINGW32__) && !defined(__MINGW64__) -/* - * This is exactly as dumb as it looks. - * Yes, this is a memory leak. No, we don't have better solution right now. - */ -template class leaky_ptr -{ - T *value; -public: - leaky_ptr(T *value) : value(value) {}; - T *get() { return value; } -}; -#endif - void compressZstd(const u8 *data, size_t data_size, std::ostream &os, int level) { -#if defined(__MINGW32__) && !defined(__MINGW64__) - // leaks one context per thread but doesn't crash :shrug: - thread_local leaky_ptr stream(ZSTD_createCStream()); -#else // reusing the context is recommended for performance - // it will destroyed when the thread ends + // it will be destroyed when the thread ends thread_local std::unique_ptr stream(ZSTD_createCStream()); -#endif ZSTD_initCStream(stream.get(), level); @@ -276,14 +257,9 @@ void compressZstd(const std::string &data, std::ostream &os, int level) void decompressZstd(std::istream &is, std::ostream &os) { -#if defined(__MINGW32__) && !defined(__MINGW64__) - // leaks one context per thread but doesn't crash :shrug: - thread_local leaky_ptr stream(ZSTD_createDStream()); -#else // reusing the context is recommended for performance - // it will destroyed when the thread ends + // it will be destroyed when the thread ends thread_local std::unique_ptr stream(ZSTD_createDStream()); -#endif ZSTD_initDStream(stream.get()); -- cgit v1.2.3 From 06d197cdd042392e1551e5e7244c61300a6bb4e3 Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Tue, 29 Mar 2022 12:07:00 -0400 Subject: Store vector metatable in registry --- builtin/common/tests/misc_helpers_spec.lua | 1 + builtin/common/tests/serialize_spec.lua | 1 + builtin/common/tests/vector_spec.lua | 2 +- builtin/common/vector.lua | 6 ++---- builtin/mainmenu/tests/serverlistmgr_spec.lua | 1 + games/devtest/mods/unittests/misc.lua | 12 ++++++++++++ src/script/common/c_converter.cpp | 19 +++---------------- src/script/common/c_internal.h | 1 + src/script/cpp_api/s_base.cpp | 8 ++++++++ src/script/cpp_api/s_security.cpp | 6 ++++++ 10 files changed, 36 insertions(+), 21 deletions(-) (limited to 'src/script/common/c_converter.cpp') diff --git a/builtin/common/tests/misc_helpers_spec.lua b/builtin/common/tests/misc_helpers_spec.lua index b16987f0b..b11236860 100644 --- a/builtin/common/tests/misc_helpers_spec.lua +++ b/builtin/common/tests/misc_helpers_spec.lua @@ -1,4 +1,5 @@ _G.core = {} +_G.vector = {metatable = {}} dofile("builtin/common/vector.lua") dofile("builtin/common/misc_helpers.lua") diff --git a/builtin/common/tests/serialize_spec.lua b/builtin/common/tests/serialize_spec.lua index e46b7dcc5..69b2b567c 100644 --- a/builtin/common/tests/serialize_spec.lua +++ b/builtin/common/tests/serialize_spec.lua @@ -1,4 +1,5 @@ _G.core = {} +_G.vector = {metatable = {}} _G.setfenv = require 'busted.compatibility'.setfenv diff --git a/builtin/common/tests/vector_spec.lua b/builtin/common/tests/vector_spec.lua index 2f72f3383..25880236b 100644 --- a/builtin/common/tests/vector_spec.lua +++ b/builtin/common/tests/vector_spec.lua @@ -1,4 +1,4 @@ -_G.vector = {} +_G.vector = {metatable = {}} dofile("builtin/common/vector.lua") describe("vector", function() diff --git a/builtin/common/vector.lua b/builtin/common/vector.lua index 581d014e0..90010f6de 100644 --- a/builtin/common/vector.lua +++ b/builtin/common/vector.lua @@ -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"} diff --git a/builtin/mainmenu/tests/serverlistmgr_spec.lua b/builtin/mainmenu/tests/serverlistmgr_spec.lua index a091959fb..ab7a6c60c 100644 --- a/builtin/mainmenu/tests/serverlistmgr_spec.lua +++ b/builtin/mainmenu/tests/serverlistmgr_spec.lua @@ -1,4 +1,5 @@ _G.core = {} +_G.vector = {metatable = {}} _G.unpack = table.unpack _G.serverlistmgr = {} diff --git a/games/devtest/mods/unittests/misc.lua b/games/devtest/mods/unittests/misc.lua index cf4f92cfa..ba980866a 100644 --- a/games/devtest/mods/unittests/misc.lua +++ b/games/devtest/mods/unittests/misc.lua @@ -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}) 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", -- cgit v1.2.3