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

@ -61,16 +61,19 @@ std::unique_ptr<UiDocument> UiDocument::read(
const scriptenv& penv, const scriptenv& penv,
const std::string& name, const std::string& name,
const io::path& file, const io::path& file,
const std::string& fileName const std::string& fileName,
scriptenv&& env
) { ) {
const std::string text = io::read_string(file); const std::string text = io::read_string(file);
auto xmldoc = xml::parse(file.string(), text); auto xmldoc = xml::parse(file.string(), text);
auto env = penv == nullptr if (env == nullptr) {
? scripting::create_doc_environment(scripting::get_root_environment(), name) env = penv == nullptr
: scripting::create_doc_environment(penv, name); ? scripting::create_doc_environment(scripting::get_root_environment(), name)
: scripting::create_doc_environment(penv, name);
}
gui::UiXmlReader reader(gui, scriptenv(env)); gui::UiXmlReader reader(gui, env);
auto view = reader.readXML(file.string(), *xmldoc->getRoot()); auto view = reader.readXML(file.string(), *xmldoc->getRoot());
view->setId("root"); view->setId("root");
UiDocScript script {}; UiDocScript script {};
@ -82,9 +85,3 @@ std::unique_ptr<UiDocument> UiDocument::read(
} }
return std::make_unique<UiDocument>(name, script, view, env); return std::make_unique<UiDocument>(name, script, view, env);
} }
std::shared_ptr<gui::UINode> UiDocument::readElement(
gui::GUI& gui, const io::path& file, const std::string& fileName
) {
return read(gui, nullptr, file.name(), file, fileName)->getRoot();
}

View file

@ -50,9 +50,7 @@ public:
const scriptenv& parent_env, const scriptenv& parent_env,
const std::string& name, const std::string& name,
const io::path& file, const io::path& file,
const std::string& fileName const std::string& fileName,
); scriptenv&& env = nullptr
static std::shared_ptr<gui::UINode> readElement(
gui::GUI&, const io::path& file, const std::string& fileName
); );
}; };

View file

@ -889,8 +889,8 @@ static std::shared_ptr<UINode> read_iframe(
return iframe; return iframe;
} }
UiXmlReader::UiXmlReader(gui::GUI& gui, scriptenv&& env) UiXmlReader::UiXmlReader(gui::GUI& gui, const scriptenv& env)
: gui(gui), env(std::move(env)) { : gui(gui), env(env) {
contextStack.emplace(""); contextStack.emplace("");
add("image", read_image); add("image", read_image);
add("canvas", read_canvas); add("canvas", read_canvas);
@ -922,8 +922,7 @@ void UiXmlReader::addIgnore(const std::string& tag) {
ignored.insert(tag); ignored.insert(tag);
} }
std::shared_ptr<UINode> UiXmlReader::readUINode(const xml::xmlelement& element std::shared_ptr<UINode> UiXmlReader::readUINode(const xml::xmlelement& element) {
) {
if (element.has("if")) { if (element.has("if")) {
const auto& cond = element.attr("if").getText(); const auto& cond = element.attr("if").getText();
if (cond.empty() || cond == "false" || cond == "nil") return nullptr; if (cond.empty() || cond == "false" || cond == "nil") return nullptr;

View file

@ -20,9 +20,9 @@ namespace gui {
std::unordered_set<std::string> ignored; std::unordered_set<std::string> ignored;
std::stack<std::string> contextStack; std::stack<std::string> contextStack;
std::string filename; std::string filename;
scriptenv env; const scriptenv& env;
public: public:
UiXmlReader(gui::GUI& gui, scriptenv&& env); UiXmlReader(gui::GUI& gui, const scriptenv& env);
void add(const std::string& tag, uinode_reader reader); void add(const std::string& tag, uinode_reader reader);
bool hasReader(const std::string& tag) const; bool hasReader(const std::string& tag) const;

View file

@ -1116,28 +1116,34 @@ static int l_gui_load_document(lua::State* L) {
auto args = lua::tovalue(L, 3); auto args = lua::tovalue(L, 3);
auto prefix = filename.entryPoint(); auto prefix = filename.entryPoint();
auto env = scripting::get_root_environment(); auto parentEnv = scripting::get_root_environment();
if (content) { if (content) {
if (auto runtime = content->getPackRuntime(prefix)) { if (auto runtime = content->getPackRuntime(prefix)) {
env = runtime->getEnvironment(); parentEnv = runtime->getEnvironment();
} }
} }
auto documentPtr = UiDocument::read( auto env = scripting::create_doc_environment(parentEnv, alias);
engine->getGUI(), std::move(env), alias, filename, filename.string()
);
auto document = documentPtr.get();
engine->requireAssets().store(std::move(documentPtr), alias);
// namespace extension // namespace extension
if (lua::istable(L, 4)) { if (lua::istable(L, 4)) {
if (lua::get_from(L, "table", "merge")) { if (lua::get_from(L, "table", "merge_replace")) {
lua::pushenv(L, *document->getEnvironment()); lua::pushenv(L, *env);
lua::pushvalue(L, 4); lua::pushvalue(L, 4);
lua::call(L, 2, 0); lua::call(L, 2, 0);
lua::pop(L); 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}); scripting::on_ui_open(document, {args});
return 0; 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); 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); return lua_setfenv(L, idx);
} }
inline void loadbuffer( void loadbuffer(
lua::State* L, int env, const std::string& src, const std::string& file 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( inline void store_in(
lua::State* L, const std::string& tableName, const std::string& name lua::State* L, const std::string& tableName, const std::string& name