From b69494aea3094bdfdef044c2152a4aae3b834a18 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 29 Nov 2024 17:57:18 +0300 Subject: [PATCH 1/3] rename --- res/content/base/modules/util.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/res/content/base/modules/util.lua b/res/content/base/modules/util.lua index 11e3717b..2112c210 100644 --- a/res/content/base/modules/util.lua +++ b/res/content/base/modules/util.lua @@ -1,6 +1,6 @@ -local base_entities = {} +local util = {} -function base_entities.drop(ppos, itemid, count, pickup_delay) +function util.drop(ppos, itemid, count, pickup_delay) if itemid == 0 or not itemid then return nil end @@ -11,4 +11,4 @@ function base_entities.drop(ppos, itemid, count, pickup_delay) }}) end -return base_entities +return util From 05003a408299c9f74ab21c9f20e9ba239b68ae27 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 29 Nov 2024 20:32:18 +0300 Subject: [PATCH 2/3] fix fatal error on uncaught lua exceptions in network functions calls --- src/logic/scripting/lua/libs/libnetwork.cpp | 8 ++++---- src/logic/scripting/lua/lua_util.cpp | 17 +++++++++++++++++ src/logic/scripting/lua/lua_util.hpp | 1 + 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/logic/scripting/lua/libs/libnetwork.cpp b/src/logic/scripting/lua/libs/libnetwork.cpp index a2d48b5b..7f503c23 100644 --- a/src/logic/scripting/lua/libs/libnetwork.cpp +++ b/src/logic/scripting/lua/libs/libnetwork.cpp @@ -9,7 +9,7 @@ static int l_get(lua::State* L) { std::string url(lua::require_lstring(L, 1)); lua::pushvalue(L, 2); - auto onResponse = lua::create_lambda(L); + auto onResponse = lua::create_lambda_nothrow(L); engine->getNetwork().get(url, [onResponse](std::vector bytes) { engine->postRunnable([=]() { @@ -23,7 +23,7 @@ static int l_get_binary(lua::State* L) { std::string url(lua::require_lstring(L, 1)); lua::pushvalue(L, 2); - auto onResponse = lua::create_lambda(L); + auto onResponse = lua::create_lambda_nothrow(L); engine->getNetwork().get(url, [onResponse](std::vector bytes) { auto buffer = std::make_shared>( @@ -40,7 +40,7 @@ static int l_connect(lua::State* L) { std::string address = lua::require_string(L, 1); int port = lua::tointeger(L, 2); lua::pushvalue(L, 3); - auto callback = lua::create_lambda(L); + auto callback = lua::create_lambda_nothrow(L); u64id_t id = engine->getNetwork().connect(address, port, [callback](u64id_t id) { engine->postRunnable([=]() { callback({id}); @@ -122,7 +122,7 @@ static int l_recv(lua::State* L) { static int l_open(lua::State* L) { int port = lua::tointeger(L, 1); lua::pushvalue(L, 2); - auto callback = lua::create_lambda(L); + auto callback = lua::create_lambda_nothrow(L); u64id_t id = engine->getNetwork().openServer(port, [callback](u64id_t id) { engine->postRunnable([=]() { callback({id}); diff --git a/src/logic/scripting/lua/lua_util.cpp b/src/logic/scripting/lua/lua_util.cpp index 86419906..8c83dc50 100644 --- a/src/logic/scripting/lua/lua_util.cpp +++ b/src/logic/scripting/lua/lua_util.cpp @@ -263,6 +263,23 @@ scripting::common_func lua::create_lambda(State* L) { }; } +scripting::common_func lua::create_lambda_nothrow(State* L) { + auto funcptr = create_lambda_handler(L); + return [=](const std::vector& args) -> dv::value { + getglobal(L, LAMBDAS_TABLE); + getfield(L, *funcptr); + for (const auto& arg : args) { + pushvalue(L, arg); + } + if (call_nothrow(L, args.size(), 1)) { + auto result = tovalue(L, -1); + pop(L); + return result; + } + return nullptr; + }; +} + int lua::create_environment(State* L, int parent) { int id = nextEnvironment++; diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index 70dd7705..983abb39 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -574,6 +574,7 @@ namespace lua { runnable create_runnable(lua::State*); scripting::common_func create_lambda(lua::State*); + scripting::common_func create_lambda_nothrow(lua::State*); inline int pushenv(lua::State* L, int env) { if (getglobal(L, env_name(env))) { From c9040df0903342a4a964637bf7ae45e6be83c170 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 29 Nov 2024 20:44:16 +0300 Subject: [PATCH 3/3] fix it temporary and think deeper after 0.25 release --- src/logic/scripting/lua/lua_util.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/logic/scripting/lua/lua_util.cpp b/src/logic/scripting/lua/lua_util.cpp index 8c83dc50..091418cd 100644 --- a/src/logic/scripting/lua/lua_util.cpp +++ b/src/logic/scripting/lua/lua_util.cpp @@ -175,7 +175,7 @@ int lua::call_nothrow(State* L, int argc, int nresults) { int handler_pos = gettop(L) - argc; pushcfunction(L, l_error_handler); insert(L, handler_pos); - if (lua_pcall(L, argc, LUA_MULTRET, handler_pos)) { + if (lua_pcall(L, argc, -1, handler_pos)) { auto errorstr = tostring(L, -1); if (errorstr) { log_error(errorstr); @@ -187,7 +187,7 @@ int lua::call_nothrow(State* L, int argc, int nresults) { return 0; } remove(L, handler_pos); - return nresults == -1 ? 1 : nresults; + return 1; } void lua::dump_stack(State* L) {