1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-21 07:53:53 +00:00

Merge branch 'lua_require' into 'master'

Allow `require` to return not a table in Lua

See merge request OpenMW/openmw!1650
This commit is contained in:
psi29a 2022-02-13 15:30:37 +00:00
commit a2a741c71f

View file

@ -162,26 +162,27 @@ namespace LuaUtil
std::string envName = namePrefix + "[" + path + "]:";
env["print"] = mLua["printGen"](envName);
auto maybeRunLoader = [&hiddenData](const sol::object& package) -> sol::object
{
if (package.is<sol::function>())
return call(package.as<sol::function>(), hiddenData);
else
return package;
};
sol::table loaded(mLua, sol::create);
for (const auto& [key, value] : mCommonPackages)
loaded[key] = value;
loaded[key] = maybeRunLoader(value);
for (const auto& [key, value] : packages)
loaded[key] = value;
env["require"] = [this, env, loaded, hiddenData](std::string_view packageName)
{
sol::table packages = loaded;
sol::object package = packages[packageName];
if (package == sol::nil)
loaded[key] = maybeRunLoader(value);
env["require"] = [this, env, loaded, hiddenData](std::string_view packageName) mutable
{
sol::object package = loaded[packageName];
if (package != sol::nil)
return package;
sol::protected_function packageLoader = loadScriptAndCache(packageNameToVfsPath(packageName, mVFS));
sol::set_environment(env, packageLoader);
package = call(packageLoader, packageName);
if (!package.is<sol::table>())
throw std::runtime_error("Lua package must return a table.");
packages[packageName] = package;
}
else if (package.is<sol::function>())
package = packages[packageName] = call(package.as<sol::protected_function>(), hiddenData);
loaded[packageName] = package;
return package;
};