fix gui.load_document environment extension

This commit is contained in:
MihailRis 2026-05-31 22:09:14 +03:00
parent cfacc3dd48
commit b1974405c6
7 changed files with 44 additions and 40 deletions

View file

@ -1116,28 +1116,34 @@ static int l_gui_load_document(lua::State* L) {
auto args = lua::tovalue(L, 3);
auto prefix = filename.entryPoint();
auto env = scripting::get_root_environment();
auto parentEnv = scripting::get_root_environment();
if (content) {
if (auto runtime = content->getPackRuntime(prefix)) {
env = runtime->getEnvironment();
parentEnv = runtime->getEnvironment();
}
}
auto documentPtr = UiDocument::read(
engine->getGUI(), std::move(env), alias, filename, filename.string()
);
auto document = documentPtr.get();
engine->requireAssets().store(std::move(documentPtr), alias);
auto env = scripting::create_doc_environment(parentEnv, alias);
// namespace extension
if (lua::istable(L, 4)) {
if (lua::get_from(L, "table", "merge")) {
lua::pushenv(L, *document->getEnvironment());
if (lua::get_from(L, "table", "merge_replace")) {
lua::pushenv(L, *env);
lua::pushvalue(L, 4);
lua::call(L, 2, 0);
lua::pop(L);
}
}
auto documentPtr = UiDocument::read(
engine->getGUI(),
nullptr,
alias,
filename,
filename.string(),
std::move(env)
);
auto document = documentPtr.get();
engine->requireAssets().store(std::move(documentPtr), alias);
scripting::on_ui_open(document, {args});
return 0;
}

View file

@ -414,3 +414,14 @@ std::string_view lua::bytearray_as_string(lua::State* L, int idx) {
return bytearray_as_string_indirect(L, idx);
}
}
void lua::loadbuffer(
lua::State* L, int env, const std::string& src, const std::string& file
) {
if (luaL_loadbuffer(L, src.c_str(), src.length(), file.c_str())) {
throw luaerror(tostring(L, -1));
}
if (env && getregistry(L, ENVS_TABLE, env_name(env))) {
lua_setfenv(L, -2);
}
}

View file

@ -555,16 +555,9 @@ namespace lua {
return lua_setfenv(L, idx);
}
inline void loadbuffer(
void loadbuffer(
lua::State* L, int env, const std::string& src, const std::string& file
) {
if (luaL_loadbuffer(L, src.c_str(), src.length(), file.c_str())) {
throw luaerror(tostring(L, -1));
}
if (env && getregistry(L, ENVS_TABLE, env_name(env))) {
lua_setfenv(L, -2);
}
}
);
inline void store_in(
lua::State* L, const std::string& tableName, const std::string& name