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
@@ -40,6 +40,7 @@ static int l_add_callback(lua::State* L) {
throw std::runtime_error("on_hud_open is not called yet"); throw std::runtime_error("on_hud_open is not called yet");
} }
auto key = input_util::keycode_from(bindname.substr(pos + 1)); auto key = input_util::keycode_from(bindname.substr(pos + 1));
lua::pushvalue(L, 2);
auto callback = lua::create_simple_handler(L); auto callback = lua::create_simple_handler(L);
hud->keepAlive(Events::keyCallbacks[key].add(callback)); hud->keepAlive(Events::keyCallbacks[key].add(callback));
return 0; return 0;
+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) { static std::shared_ptr<std::string> create_lambda_handler(State* L) {
auto ptr = reinterpret_cast<ptrdiff_t>(topointer(L, -1)); auto ptr = reinterpret_cast<ptrdiff_t>(topointer(L, -1));
auto name = util::mangleid(ptr); auto name = util::mangleid(ptr);
getglobal(L, LAMBDAS_TABLE); requireglobal(L, LAMBDAS_TABLE);
pushvalue(L, -2); pushvalue(L, -2);
setfield(L, name); setfield(L, name);
pop(L, 2); pop(L, 2);
@@ -227,7 +227,7 @@ static std::shared_ptr<std::string> create_lambda_handler(State* L) {
return std::shared_ptr<std::string>( return std::shared_ptr<std::string>(
new std::string(name), new std::string(name),
[=](std::string* name) { [=](std::string* name) {
getglobal(L, LAMBDAS_TABLE); requireglobal(L, LAMBDAS_TABLE);
pushnil(L); pushnil(L);
setfield(L, *name); setfield(L, *name);
pop(L); pop(L);
@@ -239,41 +239,48 @@ static std::shared_ptr<std::string> create_lambda_handler(State* L) {
runnable lua::create_runnable(State* L) { runnable lua::create_runnable(State* L) {
auto funcptr = create_lambda_handler(L); auto funcptr = create_lambda_handler(L);
return [=]() { return [=]() {
getglobal(L, LAMBDAS_TABLE); if (!get_from(L, LAMBDAS_TABLE, *funcptr, false))
getfield(L, *funcptr); return;
if (call_nothrow(L, 0)) { call_nothrow(L, 0, 0);
pop(L); pop(L);
}
}; };
} }
KeyCallback lua::create_simple_handler(State* L) { KeyCallback lua::create_simple_handler(State* L) {
auto funcptr = create_lambda_handler(L); auto funcptr = create_lambda_handler(L);
return [=]() -> bool { return [=]() -> bool {
getglobal(L, LAMBDAS_TABLE); if (!get_from(L, LAMBDAS_TABLE, *funcptr, false))
getfield(L, *funcptr);
if (call_nothrow(L, 0)) {
bool result = toboolean(L, -1);
pop(L);
return result;
} else {
return 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) { scripting::common_func lua::create_lambda(State* L) {
auto funcptr = create_lambda_handler(L); auto funcptr = create_lambda_handler(L);
return [=](const std::vector<dv::value>& args) -> dv::value { return [=](const std::vector<dv::value>& args) -> dv::value {
getglobal(L, LAMBDAS_TABLE); int top = gettop(L) + 1;
getfield(L, *funcptr); if (!get_from(L, LAMBDAS_TABLE, *funcptr, false))
return nullptr;
for (const auto& arg : args) { for (const auto& arg : args) {
pushvalue(L, arg); pushvalue(L, arg);
} }
if (call(L, args.size(), 1)) { if (call(L, args.size(), 1)) {
auto result = tovalue(L, -1); int nres = gettop(L) - top;
pop(L, 2); if (nres) {
return result; auto result = tovalue(L, -1);
pop(L, 1 + nres);
return result;
}
} }
pop(L); pop(L);
return nullptr; return nullptr;
@@ -283,16 +290,21 @@ scripting::common_func lua::create_lambda(State* L) {
scripting::common_func lua::create_lambda_nothrow(State* L) { scripting::common_func lua::create_lambda_nothrow(State* L) {
auto funcptr = create_lambda_handler(L); auto funcptr = create_lambda_handler(L);
return [=](const std::vector<dv::value>& args) -> dv::value { return [=](const std::vector<dv::value>& args) -> dv::value {
getglobal(L, LAMBDAS_TABLE); int top = gettop(L) - 1;
getfield(L, *funcptr); if (!get_from(L, LAMBDAS_TABLE, *funcptr, false))
return nullptr;
for (const auto& arg : args) { for (const auto& arg : args) {
pushvalue(L, arg); pushvalue(L, arg);
} }
if (call_nothrow(L, args.size(), 1)) { if (call_nothrow(L, args.size(), 1)) {
auto result = tovalue(L, -1); int nres = gettop(L) - top;
pop(L); if (nres) {
return result; auto result = tovalue(L, -1);
pop(L, 1 + nres);
return result;
}
} }
pop(L);
return nullptr; return nullptr;
}; };
} }
+2
View File
@@ -539,10 +539,12 @@ namespace lua {
if (getfield(L, name)) { if (getfield(L, name)) {
return 1; return 1;
} else if (required) { } else if (required) {
pop(L);
throw std::runtime_error( throw std::runtime_error(
"table " + tableName + " has no member " + name "table " + tableName + " has no member " + name
); );
} }
pop(L);
return 0; return 0;
} else { } else {
throw std::runtime_error("table " + tableName + " not found"); throw std::runtime_error("table " + tableName + " not found");