Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add deep reactivity to rmlui data models #1955

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 84 additions & 24 deletions rts/Rml/SolLua/bind/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,57 @@ void bindTable(SolLuaDataModel* data, sol::table& table)
}
}

sol::object getFromTable(sol::table t, std::vector<std::string> keychain, int depth) {
sol::table item = t;
for(int i = 0; i <= depth; i++) {
sol::object o = item.raw_get<sol::object>(keychain[i]);
if (o.get_type() == sol::type::table) {
item = o.as<sol::table>();
} else {
return o;
}
}
return item;
}

std::function<void(sol::object, const std::string&, sol::object, sol::this_state)>
createNewIndexFunction(std::shared_ptr<Rml::SolLua::SolLuaDataModel> data, std::vector<std::string> keychain, int depth)
{
return ([data, keychain, depth] (sol::table t, const std::string& key, sol::object value, sol::this_state s) {
auto prop = getFromTable(data->Table, keychain, depth-1);
auto type = prop.get_type();
if (type == sol::type::table) {
prop.as<sol::table>().raw_set(key,value);
std::string pointer_str = std::to_string(reinterpret_cast<intptr_t>(prop.pointer()));
data->ObjectMap.insert_or_assign(pointer_str + "_" + key, value);
data->Handle.DirtyVariable(keychain[0]);
}
});
}


std::function<sol::object(sol::object, const std::string&, sol::this_state)>
createIndexFunction(std::shared_ptr<Rml::SolLua::SolLuaDataModel> data, std::vector<std::string> keychain, int depth)
{
return ([data, keychain, depth](sol::table t, const std::string& key, sol::this_state s) {
std::vector<std::string> kc{keychain};
kc.push_back(key);
auto prop = getFromTable(data->Table, kc, depth);
auto type = prop.get_type();
if (type == sol::type::table && depth < 20) {
sol::state_view bindings{s};
sol::table obj_metatable = bindings.create_table();
obj_metatable[sol::meta_function::index] = createIndexFunction(data, kc, depth+1);
obj_metatable[sol::meta_function::new_index] = createNewIndexFunction(data, kc, depth+1);

sol::table obj_table = bindings.create_table();
obj_table[sol::metatable_key] = obj_metatable;
return sol::make_object(s, obj_table);
}
return prop;
});
};

/// <summary>
/// Opens a Lua data model.
/// </summary>
Expand All @@ -127,6 +178,11 @@ sol::table openDataModel(Rml::Context& self, const Rml::String& name, sol::objec
{
sol::state_view bindings{s};

// Only bind table.
if (model.get_type() != sol::type::table) {
return sol::lua_nil;
}

// Create data model.
auto constructor = self.CreateDataModel(name);

Expand All @@ -141,48 +197,52 @@ sol::table openDataModel(Rml::Context& self, const Rml::String& name, sol::objec
data->Constructor = constructor;
data->Handle = constructor.GetModelHandle();
data->ObjectDef = std::make_unique<SolLuaObjectDef>(data.get());
data->Table = model.as<sol::table>();
datamodel::bindTable(data.get(), data->Table);

// Only bind table.
if (model.get_type() == sol::type::table) {
data->Table = model.as<sol::table>();
datamodel::bindTable(data.get(), data->Table);
}

auto obj_table = bindings.create_table();
auto new_index_func = //
auto new_index_func =
([data](sol::object t, const std::string& key, sol::object value, sol::this_state s) {
auto iter = data->BindingMap.find(key);
if (iter == data->BindingMap.end())
luaL_error(s, "Assigning a new key ('%s') in a DataModel is not allowed.",
key.c_str());
luaL_error(s, "Assigning a new key ('%s') to the DataModel root is not allowed.", key.c_str());

if (iter->second == SolLuaDataModel::BindingType::Function)
luaL_error(
s,
"Changing the value of a key ('%s') bound to a Function in a DataModel is not "
"allowed.",
key.c_str());
luaL_error(s, "Changing the value of a key ('%s') bound to a Function in a DataModel is not allowed.", key.c_str());

data->Table.raw_set(key, value);
data->ObjectMap.insert_or_assign(key, value);
data->Handle.DirtyVariable(key);
});

auto index_function =
([data](sol::object t, const std::string& key, sol::this_state s) {
auto item = data->Table.raw_get<sol::object>(key);
auto type = item.get_type();
if (type == sol::type::table) {
sol::state_view bindings{s};
std::vector keychain{key};

sol::table obj_metatable = bindings.create_table();
obj_metatable[sol::meta_function::new_index] = createNewIndexFunction(data, keychain, 1);
obj_metatable[sol::meta_function::index] = createIndexFunction(data, keychain, 1);

sol::table obj_table = bindings.create_table();
obj_table[sol::metatable_key] = obj_metatable;
return sol::make_object(s, obj_table);
}
return item;
});

sol::table obj_metatable = bindings.create_table();
obj_metatable[sol::meta_function::new_index] = new_index_func;
obj_metatable[sol::meta_function::index] = index_function;

obj_metatable[sol::meta_function::index] =
([data](sol::object t, const std::string& key, sol::this_state s) {
return data->Table.get<sol::object>(key);
sol::table obj_table = bindings.create_table();
obj_table["__SetDirty"] = ([data](sol::object t, const std::string& key) {
data->Handle.DirtyVariable(key);
});

obj_table[sol::metatable_key] = obj_metatable;

// absolutely no assigning of keys to the top level table allowed
sol::table internal_data_metatable = bindings.create_table();
internal_data_metatable[sol::meta_function::new_index] = new_index_func;
data->Table[sol::metatable_key] = internal_data_metatable;

return obj_table;
}
} // namespace datamodel
Expand Down
Loading