Switch to loadInternalLib

7220-lua-add-a-general-purpose-lexical-parser
uramer 2 years ago
parent 539ee77888
commit bbbef96087

@ -141,7 +141,7 @@ namespace MWLua
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) {
auto element = LuaUi::Element::make(layout);
context.mLuaManager->addAction(std::make_unique<UiAction>(UiAction::CREATE, element, context.mLua));

@ -11,13 +11,11 @@ namespace
struct LuaUiContentTest : Test
{
LuaUtil::LuaState mLuaState{ nullptr, nullptr };
sol::state_view mSol;
sol::protected_function mNew;
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)
@ -28,7 +26,7 @@ namespace
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)
{

@ -272,7 +272,7 @@ add_component_dir (lua_ui
properties widget element util layers content alignment resources
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)

@ -2,23 +2,11 @@
namespace LuaUi::Content
{
namespace
sol::protected_function loadConstructor(LuaUtil::LuaState* state)
{
sol::table loadMetatable(sol::state_view sol)
{
std::string scriptBody =
#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);
sol::function loader = state->loadInternalLib("content");
sol::set_environment(state->newInternalLibEnvironment(), loader);
sol::table metatable = loader().get<sol::table>();
if (metatable["new"].get_type() != sol::type::function)
throw std::logic_error("Expected function");
return metatable["new"].get<sol::protected_function>();

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

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