C++ Interpreter: add API to get/set global value

This commit is contained in:
Olivier Goffart 2021-08-26 15:23:14 +02:00 committed by Olivier Goffart
parent c5d2ac2dd3
commit 81688906f7
4 changed files with 140 additions and 1 deletions

View file

@ -709,6 +709,38 @@ public:
inner(), sixtyfps::private_api::string_to_slice(name), actual_cb,
new F(std::move(callback)), [](void *data) { delete reinterpret_cast<F *>(data); });
}
/// Set the value for a property within an exported global
///
/// For example, if the main file has a global `TheGlobal` with a `property <int> hello`,
/// we can set this property
/// ```
/// instance->set_global_property("TheGlobal", "hello", 42);
/// ```
///
/// Returns true if the property was correctly set. Returns false if the property
/// could not be set because it either do not exist (was not declared in .60) or if
/// the value is not of the proper type for the property's type.
bool set_global_property(std::string_view global, std::string_view prop_name, const Value &value) const
{
using namespace cbindgen_private;
return sixtyfps_interpreter_component_instance_set_global_property(
inner(), sixtyfps::private_api::string_to_slice(global),
sixtyfps::private_api::string_to_slice(prop_name), &value.inner);
}
/// Returns the value behind a property in a exported global.
std::optional<Value> get_global_property(std::string_view global, std::string_view prop_name) const
{
using namespace cbindgen_private;
ValueOpaque out;
if (sixtyfps_interpreter_component_instance_get_global_property(
inner(), sixtyfps::private_api::string_to_slice(global),
sixtyfps::private_api::string_to_slice(prop_name), &out)) {
return Value(out);
} else {
return {};
}
}
};
#if !defined(DOXYGEN)

View file

@ -491,3 +491,49 @@ SCENARIO("Send key events")
sixtyfps::testing::send_keyboard_string_sequence(&*instance, "Hello keys!", {});
REQUIRE(*instance->get_property("result")->to_string() == "Hello keys!");
}
SCENARIO("Global properties")
{
using namespace sixtyfps::interpreter;
using namespace sixtyfps;
ComponentCompiler compiler;
auto result = compiler.build_from_source(
R"(
export global The-Global := {
property <string> the-property: "€€€";
}
export Dummy := Rectangle { }
)", "");
REQUIRE(result.has_value());
auto instance = result->create();
SECTION("Invalid read")
{
REQUIRE(!instance->get_global_property("the - global", "the-property").has_value());
REQUIRE(!instance->get_global_property("The-Global", "the property").has_value());
}
SECTION("Invalid set")
{
REQUIRE(!instance->set_global_property("the - global", "the-property", 5.));
REQUIRE(!instance->set_global_property("The-Global", "the property", 5.));
REQUIRE(!instance->set_global_property("The-Global", "the-property", 5.));
}
SECTION("get property")
{
auto value = instance->get_global_property("The_Global", "the-property");
REQUIRE(value.has_value());
REQUIRE(value->to_string().has_value());
REQUIRE(value->to_string().value() == "€€€");
}
SECTION("set property")
{
REQUIRE(instance->set_global_property("The-Global", "the-property", SharedString("§§§")));
auto value = instance->get_global_property("The-Global", "the_property");
REQUIRE(value.has_value());
REQUIRE(value->to_string().has_value());
REQUIRE(value->to_string().value() == "§§§");
}
}