mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-02 14:51:15 +00:00
Test and fix C++ invokation of callback
This commit is contained in:
parent
d79131f18f
commit
bd6cace54c
3 changed files with 43 additions and 1 deletions
|
@ -422,7 +422,7 @@ public:
|
|||
using cbindgen_private::ValueOpaque;
|
||||
auto actual_cb = [](void *data, Slice<ValueOpaque> arg, ValueOpaque *ret) {
|
||||
Slice<Value> args_view { reinterpret_cast<Value *>(arg.ptr), arg.len };
|
||||
Value r = (*reinterpret_cast<F *>(data))(arg);
|
||||
Value r = (*reinterpret_cast<F *>(data))(args_view);
|
||||
new (ret) Value(std::move(r));
|
||||
};
|
||||
return cbindgen_private::sixtyfps_interpreter_component_instance_set_callback(
|
||||
|
|
|
@ -264,3 +264,41 @@ SCENARIO("Component Compiler")
|
|||
REQUIRE(result.has_value());
|
||||
}
|
||||
}
|
||||
|
||||
SCENARIO("Invoke callback")
|
||||
{
|
||||
using namespace sixtyfps::interpreter;
|
||||
using namespace sixtyfps;
|
||||
|
||||
ComponentCompiler compiler;
|
||||
|
||||
SECTION("valid")
|
||||
{
|
||||
auto result = compiler.build_from_source("export Dummy := Rectangle { callback foo(string, int) -> string; }", "");
|
||||
REQUIRE(result.has_value());
|
||||
auto instance = result->create();
|
||||
REQUIRE(instance->set_callback("foo", [](auto args) {
|
||||
SharedString arg1 = *args[0].to_string();
|
||||
int arg2 = *args[1].to_number();
|
||||
std::string res = std::string(arg1) + ":" + std::to_string(arg2);
|
||||
return Value(SharedString(res));
|
||||
}));
|
||||
Value args[] = { SharedString("Hello"), 42. };
|
||||
auto res = instance->invoke_callback("foo", Slice<Value>{args, 2});
|
||||
REQUIRE(res.has_value());
|
||||
REQUIRE(*res->to_string() == SharedString("Hello:42"));
|
||||
}
|
||||
|
||||
SECTION("invalid")
|
||||
{
|
||||
auto result = compiler.build_from_source("export Dummy := Rectangle { callback foo(string, int) -> string; }", "");
|
||||
REQUIRE(result.has_value());
|
||||
auto instance = result->create();
|
||||
REQUIRE(!instance->set_callback("bar", [](auto) {
|
||||
return Value();
|
||||
}));
|
||||
Value args[] = { SharedString("Hello"), 42. };
|
||||
auto res = instance->invoke_callback("bar", Slice<Value>{args, 2});
|
||||
REQUIRE(!res.has_value());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue