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: move loadstring out of LuaRoot --- source/core/StarLua.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'source/core/StarLua.cpp') 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) { -- cgit v1.2.3