move loadstring out of LuaRoot

This commit is contained in:
Kae 2024-11-30 09:17:44 +11:00
parent e437282d5c
commit cbde26aebe
2 changed files with 9 additions and 8 deletions

View File

@ -537,8 +537,16 @@ LuaContext LuaEngine::createContext() {
LuaDetail::shallowCopy(m_state, -1, -2);
lua_pop(m_state, 1);
auto context = LuaContext(LuaDetail::LuaHandle(RefPtr<LuaEngine>(this), popHandle(m_state)));
// Add loadstring
auto handleIndex = context.handleIndex();
context.set("loadstring", createFunction([this, handleIndex](String const& source, Maybe<String> const& name, Maybe<LuaTable> 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<LuaEngine>(this), popHandle(m_state)));
return context;
}
void LuaEngine::collectGarbage(Maybe<unsigned> steps) {

View File

@ -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<String> const& name, Maybe<LuaTable> 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) {