Add the skeleton for CompilerCompiler in C++

This commit is contained in:
Simon Hausmann 2021-03-19 09:26:42 +01:00
parent 7303374927
commit 7af77839ad
3 changed files with 77 additions and 28 deletions

View file

@ -349,48 +349,57 @@ inline void Struct::set_field(std::string_view name, const Value &value)
cbindgen_private::sixtyfps_interpreter_struct_set_field(&inner, name_view, &value.inner); cbindgen_private::sixtyfps_interpreter_struct_set_field(&inner, name_view, &value.inner);
} }
class ComponentInstance { class ComponentInstance
{
cbindgen_private::ComponentInstance inner; cbindgen_private::ComponentInstance inner;
ComponentInstance() = delete; ComponentInstance() = delete;
ComponentInstance(ComponentInstance &) = delete; ComponentInstance(ComponentInstance &) = delete;
ComponentInstance &operator=(ComponentInstance &) = delete; ComponentInstance &operator=(ComponentInstance &) = delete;
public: public:
void show() const { void show() const
{
cbindgen_private::sixtyfps_interpreter_component_instance_show(&inner, true); cbindgen_private::sixtyfps_interpreter_component_instance_show(&inner, true);
} }
void hide() const { void hide() const
{
cbindgen_private::sixtyfps_interpreter_component_instance_show(&inner, false); cbindgen_private::sixtyfps_interpreter_component_instance_show(&inner, false);
} }
void run() const { void run() const
{
show(); show();
cbindgen_private::sixtyfps_run_event_loop(); cbindgen_private::sixtyfps_run_event_loop();
hide(); hide();
} }
bool set_property(std::string_view name, const Value &value) const { bool set_property(std::string_view name, const Value &value) const
{
cbindgen_private::Slice<uint8_t> name_view { cbindgen_private::Slice<uint8_t> name_view {
const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())), const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())),
name.size() name.size()
}; };
return cbindgen_private::sixtyfps_interpreter_component_instance_set_property(&inner, name_view, &value.inner); return cbindgen_private::sixtyfps_interpreter_component_instance_set_property(
&inner, name_view, &value.inner);
} }
std::optional<Value> get_property(std::string_view name) const { std::optional<Value> get_property(std::string_view name) const
{
cbindgen_private::Slice<uint8_t> name_view { cbindgen_private::Slice<uint8_t> name_view {
const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())), const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())),
name.size() name.size()
}; };
cbindgen_private::ValueOpaque out; cbindgen_private::ValueOpaque out;
if (cbindgen_private::sixtyfps_interpreter_component_instance_get_property(&inner, name_view, &out)) { if (cbindgen_private::sixtyfps_interpreter_component_instance_get_property(
&inner, name_view, &out)) {
return Value(out); return Value(out);
} else { } else {
return {}; return {};
} }
} }
// FIXME! Slice in public API? Should be std::span (c++20) or we need to improve the Slice API // FIXME! Slice in public API? Should be std::span (c++20) or we need to improve the Slice API
std::optional<Value> invoke_callback(std::string_view name, Slice<Value> args) const { std::optional<Value> invoke_callback(std::string_view name, Slice<Value> args) const
{
cbindgen_private::Slice<cbindgen_private::ValueOpaque> args_view { cbindgen_private::Slice<cbindgen_private::ValueOpaque> args_view {
reinterpret_cast<cbindgen_private::ValueOpaque *>(args.ptr), reinterpret_cast<cbindgen_private::ValueOpaque *>(args.ptr), args.len
args.len
}; };
cbindgen_private::Slice<uint8_t> name_view { cbindgen_private::Slice<uint8_t> name_view {
const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())), const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())),
@ -406,26 +415,39 @@ public:
} }
template<typename F> template<typename F>
bool set_callback(std::string_view name, F callback) const { bool set_callback(std::string_view name, F callback) const
{
using cbindgen_private::ValueOpaque; using cbindgen_private::ValueOpaque;
cbindgen_private::Slice<uint8_t> name_view { cbindgen_private::Slice<uint8_t> name_view {
const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())), const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(name.data())),
name.size() name.size()
}; };
auto actual_cb = [](void *data, Slice<ValueOpaque> arg, ValueOpaque *ret) { auto actual_cb = [](void *data, Slice<ValueOpaque> arg, ValueOpaque *ret) {
Slice<Value> args_view { Slice<Value> args_view { reinterpret_cast<Value *>(arg.ptr), arg.len };
reinterpret_cast<Value*>(arg.ptr),
arg.len
};
Value r = (*reinterpret_cast<F *>(data))(arg); Value r = (*reinterpret_cast<F *>(data))(arg);
new (ret) Value(std::move(r)); new (ret) Value(std::move(r));
}; };
return cbindgen_private::sixtyfps_interpreter_component_instance_set_callback( return cbindgen_private::sixtyfps_interpreter_component_instance_set_callback(
&inner, name_view, actual_cb, &inner, name_view, actual_cb, new F(std::move(callback)),
new F(std::move(callback)),
[](void *data) { delete reinterpret_cast<F *>(data); }); [](void *data) { delete reinterpret_cast<F *>(data); });
} }
}; };
class ComponentCompiler
{
cbindgen_private::ComponentCompilerOpaque inner;
ComponentCompiler() = delete;
ComponentCompiler(ComponentCompiler &) = delete;
ComponentCompiler &operator=(ComponentCompiler &) = delete;
public:
ComponentCompiler() { cbindgen_private::sixtyfps_interpreter_component_compiler_new(&inner); }
~ComponentCompiler()
{
cbindgen_private::sixtyfps_interpreter_component_compiler_destructor(&inner);
}
};
} }

View file

@ -156,7 +156,6 @@ SCENARIO("Value API")
REQUIRE(destroyed); REQUIRE(destroyed);
} }
SECTION("Compare Values") SECTION("Compare Values")
{ {
Value str1 { sixtyfps::SharedString("Hello1") }; Value str1 { sixtyfps::SharedString("Hello1") };
@ -177,7 +176,6 @@ SCENARIO("Value API")
REQUIRE(fl1 != sixtyfps::SharedString("Hello2")); REQUIRE(fl1 != sixtyfps::SharedString("Hello2"));
REQUIRE(fl2 == 12.); REQUIRE(fl2 == 12.);
} }
} }
SCENARIO("Struct API") SCENARIO("Struct API")
@ -240,3 +238,10 @@ SCENARIO("Struct Initializer List Constructor")
REQUIRE(struc.get_field("field_a").value().to_bool().value()); REQUIRE(struc.get_field("field_a").value().to_bool().value());
REQUIRE(struc.get_field("field_b").value().to_number().value() == 42.0); REQUIRE(struc.get_field("field_b").value().to_number().value() == 42.0);
} }
SCENARIO("Component Compiler")
{
using namespace sixtyfps::interpreter;
ComponentCompiler compiler;
}

View file

@ -1290,4 +1290,26 @@ pub(crate) mod ffi {
) { ) {
notify.as_model_notify().row_removed(row, count); notify.as_model_notify().row_removed(row, count);
} }
#[repr(C)]
pub struct ComponentCompilerOpaque([usize; 12]);
/// Asserts that ComponentCompilerOpaque is as large as ComponentCompiler and has the same alignment, to make transmute safe.
const _: [(); std::mem::size_of::<ComponentCompilerOpaque>()] =
[(); std::mem::size_of::<ComponentCompiler>()];
const _: [(); std::mem::align_of::<ComponentCompilerOpaque>()] =
[(); std::mem::align_of::<ComponentCompiler>()];
#[no_mangle]
pub unsafe extern "C" fn sixtyfps_interpreter_component_compiler_new(
compiler: *mut ComponentCompilerOpaque,
) {
std::ptr::write(compiler as *mut ComponentCompiler, ComponentCompiler::new())
}
#[no_mangle]
pub unsafe extern "C" fn sixtyfps_interpreter_component_compiler_destructor(
compiler: *mut ComponentCompilerOpaque,
) {
drop(std::ptr::read(compiler as *mut ComponentCompiler))
}
} }