fix lua stack manipulations

This commit is contained in:
MihailRis
2025-01-07 20:57:38 +03:00
parent 13de581b4b
commit e7555448cf
3 changed files with 39 additions and 24 deletions
+36 -24
View File
@@ -219,7 +219,7 @@ void lua::dump_stack(State* L) {
static std::shared_ptr<std::string> create_lambda_handler(State* L) {
auto ptr = reinterpret_cast<ptrdiff_t>(topointer(L, -1));
auto name = util::mangleid(ptr);
getglobal(L, LAMBDAS_TABLE);
requireglobal(L, LAMBDAS_TABLE);
pushvalue(L, -2);
setfield(L, name);
pop(L, 2);
@@ -227,7 +227,7 @@ static std::shared_ptr<std::string> create_lambda_handler(State* L) {
return std::shared_ptr<std::string>(
new std::string(name),
[=](std::string* name) {
getglobal(L, LAMBDAS_TABLE);
requireglobal(L, LAMBDAS_TABLE);
pushnil(L);
setfield(L, *name);
pop(L);
@@ -239,41 +239,48 @@ static std::shared_ptr<std::string> create_lambda_handler(State* L) {
runnable lua::create_runnable(State* L) {
auto funcptr = create_lambda_handler(L);
return [=]() {
getglobal(L, LAMBDAS_TABLE);
getfield(L, *funcptr);
if (call_nothrow(L, 0)) {
pop(L);
}
if (!get_from(L, LAMBDAS_TABLE, *funcptr, false))
return;
call_nothrow(L, 0, 0);
pop(L);
};
}
KeyCallback lua::create_simple_handler(State* L) {
auto funcptr = create_lambda_handler(L);
return [=]() -> bool {
getglobal(L, LAMBDAS_TABLE);
getfield(L, *funcptr);
if (call_nothrow(L, 0)) {
bool result = toboolean(L, -1);
pop(L);
return result;
} else {
if (!get_from(L, LAMBDAS_TABLE, *funcptr, false))
return false;
int top = gettop(L) - 1;
if (call_nothrow(L, 0)) {
int nres = gettop(L) - top;
if (nres) {
bool result = toboolean(L, -1);
pop(L, 1 + nres);
return result;
}
}
pop(L);
return false;
};
}
scripting::common_func lua::create_lambda(State* L) {
auto funcptr = create_lambda_handler(L);
return [=](const std::vector<dv::value>& args) -> dv::value {
getglobal(L, LAMBDAS_TABLE);
getfield(L, *funcptr);
int top = gettop(L) + 1;
if (!get_from(L, LAMBDAS_TABLE, *funcptr, false))
return nullptr;
for (const auto& arg : args) {
pushvalue(L, arg);
}
if (call(L, args.size(), 1)) {
auto result = tovalue(L, -1);
pop(L, 2);
return result;
int nres = gettop(L) - top;
if (nres) {
auto result = tovalue(L, -1);
pop(L, 1 + nres);
return result;
}
}
pop(L);
return nullptr;
@@ -283,16 +290,21 @@ 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);
int top = gettop(L) - 1;
if (!get_from(L, LAMBDAS_TABLE, *funcptr, false))
return nullptr;
for (const auto& arg : args) {
pushvalue(L, arg);
}
if (call_nothrow(L, args.size(), 1)) {
auto result = tovalue(L, -1);
pop(L);
return result;
int nres = gettop(L) - top;
if (nres) {
auto result = tovalue(L, -1);
pop(L, 1 + nres);
return result;
}
}
pop(L);
return nullptr;
};
}