mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-02 06:41:14 +00:00
JS: implement conversion to Type::Object
This commit is contained in:
parent
04661913b1
commit
67dddd69e7
2 changed files with 35 additions and 2 deletions
|
@ -150,9 +150,27 @@ fn to_eval_value<'cx>(
|
|||
Ok(Value::Number(val.downcast_or_throw::<JsNumber, _>(cx)?.value()))
|
||||
}
|
||||
Type::String => Ok(Value::String(val.to_string(cx)?.value().into())),
|
||||
Type::Color | Type::Array(_) | Type::Object(_) => todo!(),
|
||||
Type::Color | Type::Array(_) => todo!("conversion to this type is not yet implemented"),
|
||||
Type::Resource => Ok(Value::String(val.to_string(cx)?.value().into())),
|
||||
Type::Bool => Ok(Value::Bool(val.downcast_or_throw::<JsBoolean, _>(cx)?.value())),
|
||||
Type::Object(o) => {
|
||||
let obj = val.downcast_or_throw::<JsObject, _>(cx)?;
|
||||
Ok(Value::Object(
|
||||
o
|
||||
.iter()
|
||||
.map(|(pro_name, pro_ty)| {
|
||||
Ok((
|
||||
pro_name.clone(),
|
||||
to_eval_value(
|
||||
obj.get(cx, pro_name.as_str())?,
|
||||
pro_ty.clone(),
|
||||
cx,
|
||||
)?,
|
||||
))
|
||||
})
|
||||
.collect::<Result<_, _>>()?,
|
||||
))
|
||||
}
|
||||
Type::Component(c) if c.root_element.borrow().base_type == Type::Void => {
|
||||
let obj = val.downcast_or_throw::<JsObject, _>(cx)?;
|
||||
Ok(Value::Object(
|
||||
|
|
|
@ -33,6 +33,12 @@ assert_eq!(instance.get_foo_b(), 12);
|
|||
instance.emit_change_foo();
|
||||
assert_eq!(instance.get_foo_a(), sixtyfps::SharedString::from("hello"));
|
||||
assert_eq!(instance.get_foo_b(), 20);
|
||||
|
||||
// This API to set with a tuple should maybe not be accessible?
|
||||
instance.set_foo(("yo".into(), 33));
|
||||
assert_eq!(instance.get_foo_a(), sixtyfps::SharedString::from("yo"));
|
||||
assert_eq!(instance.get_foo_b(), 33);
|
||||
|
||||
```
|
||||
|
||||
```cpp
|
||||
|
@ -42,6 +48,11 @@ assert_eq(instance.get_foo_b(), 12);
|
|||
instance.emit_change_foo();
|
||||
assert_eq(instance.get_foo_a(), sixtyfps::SharedString("hello"));
|
||||
assert_eq(instance.get_foo_b(), 20);
|
||||
|
||||
// This API to set with a tuple should maybe not be accessible?
|
||||
instance.set_foo(std::make_tuple(sixtyfps::SharedString("yo"), 33));
|
||||
assert_eq(instance.get_foo_a(), sixtyfps::SharedString("yo"));
|
||||
assert_eq(instance.get_foo_b(), 33);
|
||||
```
|
||||
|
||||
|
||||
|
@ -50,7 +61,11 @@ var instance = new sixtyfps.TestCase({});
|
|||
assert.equal(instance.foo_a, ("444"));
|
||||
assert.equal(instance.foo_b, 12);
|
||||
instance.change_foo();
|
||||
assert.equal(instance.foo_a, ("hello"));
|
||||
assert.equal(instance.foo_a, "hello");
|
||||
assert.equal(instance.foo_b, 20);
|
||||
|
||||
instance.foo = { a: "yo", b: 33 };
|
||||
assert.equal(instance.foo_a, "yo");
|
||||
assert.equal(instance.foo_b, 33);
|
||||
```
|
||||
*/
|
Loading…
Add table
Add a link
Reference in a new issue