diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2024-03-17 01:52:02 +1100 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2024-03-17 01:52:02 +1100 |
commit | d577a98980b81d843b8600b79bb833db6ab8e08f (patch) | |
tree | 71e10cf6639a940acc8bf83a770a731bbbba2cb6 /source/core/StarLua.cpp | |
parent | 8f8741bcb2f164f5d947cc4557806a6b72e50835 (diff) |
Lua: allow jarray and jobject to convert existing tables
[skip ci]
Diffstat (limited to 'source/core/StarLua.cpp')
-rw-r--r-- | source/core/StarLua.cpp | 41 |
1 files changed, 30 insertions, 11 deletions
diff --git a/source/core/StarLua.cpp b/source/core/StarLua.cpp index b5e6b8a..b5ac71e 100644 --- a/source/core/StarLua.cpp +++ b/source/core/StarLua.cpp @@ -370,8 +370,8 @@ LuaEnginePtr LuaEngine::create(bool safe) { self->m_scriptDefaultEnvRegistryId = luaL_ref(self->m_state, LUA_REGISTRYINDEX); lua_pop(self->m_state, 1); - self->setGlobal("jarray", self->createFunction(&LuaDetail::jarrayCreate)); - self->setGlobal("jobject", self->createFunction(&LuaDetail::jobjectCreate)); + self->setGlobal("jarray", self->createFunction(&LuaDetail::jarray)); + self->setGlobal("jobject", self->createFunction(&LuaDetail::jobject)); self->setGlobal("jremove", self->createFunction(&LuaDetail::jcontRemove)); self->setGlobal("jsize", self->createFunction(&LuaDetail::jcontSize)); self->setGlobal("jresize", self->createFunction(&LuaDetail::jcontResize)); @@ -1308,10 +1308,7 @@ void LuaDetail::shallowCopy(lua_State* state, int sourceIndex, int targetIndex) } } -LuaTable LuaDetail::jsonContainerToTable(LuaEngine& engine, Json const& container) { - if (!container.isType(Json::Type::Array) && !container.isType(Json::Type::Object)) - throw LuaException("jsonContainerToTable called on improper json type"); - +LuaTable LuaDetail::insertJsonMetatable(LuaEngine& engine, LuaTable const& table, Json::Type type) { auto newIndexMetaMethod = [](LuaTable const& table, LuaValue const& key, LuaValue const& value) { auto mt = table.getMetatable(); auto nils = mt->rawGet<LuaTable>("__nils"); @@ -1330,13 +1327,16 @@ LuaTable LuaDetail::jsonContainerToTable(LuaEngine& engine, Json const& containe auto nils = engine.createTable(); mt.rawSet("__nils", nils); mt.rawSet("__newindex", engine.createFunction(newIndexMetaMethod)); - if (container.isType(Json::Type::Array)) - mt.rawSet("__typehint", 1); - else - mt.rawSet("__typehint", 2); + mt.rawSet("__typehint", type == Json::Type::Array ? 1 : 2); + return nils; +} + +LuaTable LuaDetail::jsonContainerToTable(LuaEngine& engine, Json const& container) { + if (!container.isType(Json::Type::Array) && !container.isType(Json::Type::Object)) + throw LuaException("jsonContainerToTable called on improper json type"); auto table = engine.createTable(); - table.setMetatable(mt); + auto nils = insertJsonMetatable(engine, table, container.type()); if (container.isType(Json::Type::Array)) { auto vlist = container.arrayPtr(); @@ -1437,6 +1437,25 @@ Json LuaDetail::jobjectCreate() { return JsonObject(); } +LuaTable LuaDetail::jarray(LuaEngine& engine, Maybe<LuaTable> table) { + if (auto t = table.ptr()) { + insertJsonMetatable(engine, *t, Json::Type::Array); + return *t; + } else { + return jsonContainerToTable(engine, JsonArray()); + } +} + +LuaTable LuaDetail::jobject(LuaEngine& engine, Maybe<LuaTable> table) { + if (auto t = table.ptr()) { + insertJsonMetatable(engine, *t, Json::Type::Object); + return *t; + } else { + return jsonContainerToTable(engine, JsonObject()); + } +} + + void LuaDetail::jcontRemove(LuaTable const& table, LuaValue const& key) { if (auto mt = table.getMetatable()) { if (auto nils = mt->rawGet<Maybe<LuaTable>>("__nils")) |