From cbde26aebeec3a3cb72cfd6b55c7867d27b8c789 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Sat, 30 Nov 2024 09:17:44 +1100 Subject: [PATCH] move loadstring out of LuaRoot --- source/core/StarLua.cpp | 10 +++++++++- source/game/scripting/StarLuaRoot.cpp | 7 ------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/source/core/StarLua.cpp b/source/core/StarLua.cpp index 5a2effa..91aebab 100644 --- a/source/core/StarLua.cpp +++ b/source/core/StarLua.cpp @@ -537,8 +537,16 @@ LuaContext LuaEngine::createContext() { LuaDetail::shallowCopy(m_state, -1, -2); lua_pop(m_state, 1); + auto context = LuaContext(LuaDetail::LuaHandle(RefPtr(this), popHandle(m_state))); + // Add loadstring + auto handleIndex = context.handleIndex(); + context.set("loadstring", createFunction([this, handleIndex](String const& source, Maybe const& name, Maybe const& env) -> LuaFunction { + String functionName = name ? strf("loadstring: {}", *name) : "loadstring"; + return createFunctionFromSource(env ? env->handleIndex() : handleIndex, source.utf8Ptr(), source.utf8Size(), functionName.utf8Ptr()); + })); + // Then set that environment as the new context environment in the registry. - return LuaContext(LuaDetail::LuaHandle(RefPtr(this), popHandle(m_state))); + return context; } void LuaEngine::collectGarbage(Maybe steps) { diff --git a/source/game/scripting/StarLuaRoot.cpp b/source/game/scripting/StarLuaRoot.cpp index a14d10a..592c24b 100644 --- a/source/game/scripting/StarLuaRoot.cpp +++ b/source/game/scripting/StarLuaRoot.cpp @@ -106,13 +106,6 @@ LuaContext LuaRoot::createContext(StringList const& scriptPaths) { } }); - auto handleIndex = newContext.handleIndex(); - auto engine = m_luaEngine.get(); - newContext.set("loadstring", m_luaEngine->createFunction([engine, handleIndex](String const& source, Maybe const& name, Maybe const& env) -> LuaFunction { - String functionName = name ? strf("loadstring: {}", *name) : "loadstring"; - return engine->createFunctionFromSource(env ? env->handleIndex() : handleIndex, source.utf8Ptr(), source.utf8Size(), functionName.utf8Ptr()); - })); - auto assets = Root::singleton().assets(); for (auto const& scriptPath : scriptPaths) {