1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-12-13 08:13:06 +00:00

Switch to loadInternalLib

This commit is contained in:
uramer 2023-01-31 19:50:33 +01:00
parent 539ee77888
commit bbbef96087
6 changed files with 20 additions and 28 deletions

View file

@ -141,7 +141,7 @@ namespace MWLua
luaManager->addAction([wm, obj = obj.as<LObject>()] { wm->setConsoleSelectedObject(obj.ptr()); }); luaManager->addAction([wm, obj = obj.as<LObject>()] { wm->setConsoleSelectedObject(obj.ptr()); });
} }
}; };
api["content"] = LuaUi::Content::makeFactory(context.mLua->sol()); api["content"] = LuaUi::Content::loadConstructor(context.mLua);
api["create"] = [context](const sol::table& layout) { api["create"] = [context](const sol::table& layout) {
auto element = LuaUi::Element::make(layout); auto element = LuaUi::Element::make(layout);
context.mLuaManager->addAction(std::make_unique<UiAction>(UiAction::CREATE, element, context.mLua)); context.mLuaManager->addAction(std::make_unique<UiAction>(UiAction::CREATE, element, context.mLua));

View file

@ -11,13 +11,11 @@ namespace
struct LuaUiContentTest : Test struct LuaUiContentTest : Test
{ {
LuaUtil::LuaState mLuaState{ nullptr, nullptr }; LuaUtil::LuaState mLuaState{ nullptr, nullptr };
sol::state_view mSol;
sol::protected_function mNew; sol::protected_function mNew;
LuaUiContentTest() LuaUiContentTest()
: mSol(mLuaState.sol())
, mNew(LuaUi::Content::makeFactory(mSol))
{ {
mSol.open_libraries(sol::lib::base, sol::lib::table); mLuaState.addInternalLibSearchPath("resources/lua_libs");
mNew = LuaUi::Content::loadConstructor(&mLuaState);
} }
LuaUi::Content::View makeContent(sol::table source) LuaUi::Content::View makeContent(sol::table source)
@ -28,7 +26,7 @@ namespace
return LuaUi::Content::View(result.get<sol::table>()); return LuaUi::Content::View(result.get<sol::table>());
} }
sol::table makeTable() { return sol::table(mSol, sol::create); } sol::table makeTable() { return sol::table(mLuaState.sol(), sol::create); }
sol::table makeTable(std::string name) sol::table makeTable(std::string name)
{ {

View file

@ -272,7 +272,7 @@ add_component_dir (lua_ui
properties widget element util layers content alignment resources properties widget element util layers content alignment resources
adapter text textedit window image container flex adapter text textedit window image container flex
) )
list (APPEND OPENMW_FILES "lua_ui/content.lua") copy_resource_file("lua_ui/content.lua" "${OPENMW_RESOURCES_ROOT}" "resources/lua_libs/content.lua")
if(WIN32) if(WIN32)

View file

@ -2,23 +2,11 @@
namespace LuaUi::Content namespace LuaUi::Content
{ {
namespace sol::protected_function loadConstructor(LuaUtil::LuaState* state)
{ {
sol::table loadMetatable(sol::state_view sol) sol::function loader = state->loadInternalLib("content");
{ sol::set_environment(state->newInternalLibEnvironment(), loader);
std::string scriptBody = sol::table metatable = loader().get<sol::table>();
#include "content.lua"
;
auto result = sol.safe_script(scriptBody);
if (result.get_type() != sol::type::table)
throw std::logic_error("Expected a meta table");
return result.get<sol::table>();
}
}
sol::protected_function makeFactory(sol::state_view sol)
{
sol::table metatable = loadMetatable(sol);
if (metatable["new"].get_type() != sol::type::function) if (metatable["new"].get_type() != sol::type::function)
throw std::logic_error("Expected function"); throw std::logic_error("Expected function");
return metatable["new"].get<sol::protected_function>(); return metatable["new"].get<sol::protected_function>();

View file

@ -6,9 +6,11 @@
#include <sol/sol.hpp> #include <sol/sol.hpp>
#include <components/lua/luastate.hpp>
namespace LuaUi::Content namespace LuaUi::Content
{ {
sol::protected_function makeFactory(sol::state_view); sol::protected_function loadConstructor(LuaUtil::LuaState* state);
class View class View
{ {

View file

@ -1,4 +1,3 @@
R"(
local M = {} local M = {}
M.__Content = true M.__Content = true
M.new = function(source) M.new = function(source)
@ -23,6 +22,7 @@ local function validateIndex(self, index)
error('Invalid Content index: ' .. tostring(index)) error('Invalid Content index: ' .. tostring(index))
end end
end end
local function getIndexFromKey(self, key) local function getIndexFromKey(self, key)
local index = key local index = key
if type(key) == 'string' then if type(key) == 'string' then
@ -34,6 +34,7 @@ local function getIndexFromKey(self, key)
validateIndex(self, index) validateIndex(self, index)
return index return index
end end
local methods = { local methods = {
insert = function(self, index, value) insert = function(self, index, value)
validateIndex(self, index) validateIndex(self, index)
@ -78,6 +79,7 @@ local function nameAt(self, index)
local v = rawget(self, index) local v = rawget(self, index)
return v and type(v.name) == 'string' and v.name return v and type(v.name) == 'string' and v.name
end end
local function remove(self, index) local function remove(self, index)
local oldName = nameAt(self, index) local oldName = nameAt(self, index)
if oldName then if oldName then
@ -95,6 +97,7 @@ local function remove(self, index)
end end
rawset(self, #self, nil) rawset(self, #self, nil)
end end
local function assign(self, index, value) local function assign(self, index, value)
local oldName = nameAt(self, index) local oldName = nameAt(self, index)
if oldName then if oldName then
@ -105,6 +108,7 @@ local function assign(self, index, value)
self.__nameIndex[value.name] = index self.__nameIndex[value.name] = index
end end
end end
M.__newindex = function(self, key, value) M.__newindex = function(self, key, value)
local index = getIndexFromKey(self, key) local index = getIndexFromKey(self, key)
if value == nil then if value == nil then
@ -126,14 +130,14 @@ local function next(self, index)
return nil, nil return nil, nil
end end
end end
M.__pairs = function(self) M.__pairs = function(self)
return next, self, 1 return next, self, 1
end end
M.__ipairs = M.__pairs M.__ipairs = M.__pairs
M.__metatable = {} M.__metatable = {}
assert(not pcall(function() setmetatable(M.new({}), {}) end), 'Metatable is not protected') assert(not pcall(function() setmetatable(M.new {}, {}) end), 'Metatable is not protected')
assert(getmetatable(M.new) ~= M, 'Metatable is not protected') assert(getmetatable(M.new {}) ~= M, 'Metatable is not protected')
return M return M
)"