Merge branch 'main' into release-0.25

This commit is contained in:
MihailRis 2024-11-29 20:45:17 +03:00
commit 52a8880c4b
4 changed files with 27 additions and 9 deletions

View File

@ -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

View File

@ -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<char> 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<char> bytes) {
auto buffer = std::make_shared<util::Buffer<ubyte>>(
@ -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});

View File

@ -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) {
@ -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<dv::value>& 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++;

View File

@ -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))) {