Add C++ API to introspect exported global singletons in the interpreter

This adds the necessary shims to expose the same API as Rust.
This commit is contained in:
Simon Hausmann 2021-09-13 13:38:25 +02:00 committed by Simon Hausmann
parent a855d868fc
commit c3d0fd04af
3 changed files with 116 additions and 4 deletions

View file

@ -888,6 +888,42 @@ public:
cbindgen_private::sixtyfps_interpreter_component_definition_name(&inner, &name);
return name;
}
/// Returns a vector of strings with the names of all exported global singletons.
sixtyfps::SharedVector<sixtyfps::SharedString> globals() const
{
sixtyfps::SharedVector<sixtyfps::SharedString> names;
cbindgen_private::sixtyfps_interpreter_component_definition_globals(&inner, &names);
return names;
}
/// Returns a vector of the property descriptors of the properties of the specified
/// publicly exported global singleton. An empty optional is returned if there exists no
/// exported global singleton under the specified name.
std::optional<sixtyfps::SharedVector<PropertyDescriptor>>
global_properties(std::string_view global_name) const
{
sixtyfps::SharedVector<PropertyDescriptor> properties;
if (cbindgen_private::sixtyfps_interpreter_component_definition_global_properties(
&inner, sixtyfps::private_api::string_to_slice(global_name), &properties)) {
return properties;
}
return {};
}
/// Returns a vector of the names of the callbacks of the specified publicly exported global
/// singleton. An empty optional is returned if there exists no exported global singleton
/// under the specified name.
std::optional<sixtyfps::SharedVector<sixtyfps::SharedString>>
global_callbacks(std::string_view global_name) const
{
sixtyfps::SharedVector<sixtyfps::SharedString> names;
if (cbindgen_private::sixtyfps_interpreter_component_definition_global_callbacks(
&inner, sixtyfps::private_api::string_to_slice(global_name), &names)) {
return names;
}
return {};
}
};
#if !defined(DOXYGEN)

View file

@ -101,7 +101,8 @@ SCENARIO("Value API")
sixtyfps::private_api::WindowRc wnd;
REQUIRE(!value.to_image().has_value());
sixtyfps::Image image = sixtyfps::Image::load_from_path(SOURCE_DIR "/../../vscode_extension/extension-logo.png");
sixtyfps::Image image = sixtyfps::Image::load_from_path(
SOURCE_DIR "/../../vscode_extension/extension-logo.png");
REQUIRE(image.size().width == 128);
value = Value(image);
REQUIRE(value.type() == Value::Type::Image);
@ -433,8 +434,10 @@ SCENARIO("Angle between .60 and C++")
ComponentCompiler compiler;
auto result = compiler.build_from_source(
"export Dummy := Rectangle { property <angle> the_angle: 0.25turn; property <bool> test: the_angle == 0.5turn; }", "");
auto result =
compiler.build_from_source("export Dummy := Rectangle { property <angle> the_angle: "
"0.25turn; property <bool> test: the_angle == 0.5turn; }",
"");
REQUIRE(result.has_value());
auto instance = result->create();
@ -513,7 +516,30 @@ SCENARIO("Global properties")
for (auto &&x : compiler.diagnostics())
std::cerr << x.message << std::endl;
REQUIRE(result.has_value());
auto instance = result->create();
auto component_definition = *result;
SECTION("Globals introspection")
{
auto globals = component_definition.globals();
REQUIRE(globals.size() == 1);
REQUIRE(globals[0] == "The-Global");
REQUIRE(!component_definition.global_properties("not there").has_value());
REQUIRE(component_definition.global_properties("The_Global").has_value());
REQUIRE(component_definition.global_properties("The-Global").has_value());
auto properties = *component_definition.global_properties("The-Global");
REQUIRE(properties.size() == 1);
REQUIRE(properties[0].property_name == "the-property");
REQUIRE(properties[0].property_type == Value::Type::String);
auto callbacks = *component_definition.global_callbacks("The-Global");
REQUIRE(callbacks.size() == 1);
REQUIRE(callbacks[0] == "to_uppercase");
}
auto instance = component_definition.create();
SECTION("Invalid read")
{