Node: Allow to init properties to value

This commit is contained in:
Olivier Goffart 2020-06-03 16:42:16 +02:00
parent 30d61452ba
commit 5ee09398e8
5 changed files with 58 additions and 8 deletions

View file

@ -3,10 +3,9 @@ native = require('../native/index.node');
require.extensions['.60'] = function (module, filename) {
var c = native.load(filename);
module.exports["show"] = function () {
c.create().show();
module.exports[c.name()] = function (init_properties) {
return c.create(init_properties);
}
}
module.exports = native;

View file

@ -14,6 +14,7 @@ name = "sixtyfps_node_native"
[dependencies]
once_cell = "1.4"
sixtyfps_compiler = { path="../../../sixtyfps_compiler" }
interpreter = { path="../../../sixtyfps_runtime/interpreter" }
corelib = { path="../../../sixtyfps_runtime/corelib" }
gl = { path="../../../sixtyfps_runtime/rendering_backends/gl" }

View file

@ -3,6 +3,11 @@ use neon::prelude::*;
struct WrappedComponentType(Option<std::rc::Rc<interpreter::ComponentDescription>>);
struct WrappedComponentBox(Option<corelib::abi::datastructures::ComponentBox>);
/// Load a .60 files.
///
/// The first argument of this finction is a string to the .60 file
///
/// The return value is a SixtyFpsComponentType
fn load(mut cx: FunctionContext) -> JsResult<JsValue> {
let path = cx.argument::<JsString>(0)?.value();
let path = std::path::Path::new(path.as_str());
@ -24,15 +29,53 @@ fn create<'cx>(
cx: &mut CallContext<'cx, impl neon::object::This>,
component_type: std::rc::Rc<interpreter::ComponentDescription>,
) -> JsResult<'cx, JsValue> {
let component = component_type.create();
let component = component_type.clone().create();
//component_type.set_property(component, name, value);
if let Some(args) = cx.argument_opt(0).and_then(|arg| arg.downcast::<JsObject>().ok()) {
let properties = component_type.properties();
for x in args.get_own_property_names(cx)?.to_vec(cx)? {
let prop_name = x.to_string(cx)?.value();
let ty = properties
.get(&prop_name)
.ok_or(())
.or_else(|()| {
cx.throw_error(format!("Property {} not found in the component", prop_name))
})?
.clone();
let value = args.get(cx, x)?;
let value = to_eval_value(value, ty, cx)?;
component_type
.set_property(component.borrow(), prop_name.as_str(), value)
.or_else(|_| cx.throw_error(format!("Cannot assign property")))?;
}
}
let mut obj = SixtyFpsComponent::new::<_, JsValue, _>(cx, std::iter::empty())?;
cx.borrow_mut(&mut obj, |mut obj| obj.0 = Some(component));
Ok(obj.as_value(cx))
}
fn to_eval_value<'a>(
val: Handle<JsValue>,
ty: sixtyfps_compiler::typeregister::Type,
cx: &mut impl Context<'a>,
) -> NeonResult<interpreter::Value> {
use interpreter::Value;
use sixtyfps_compiler::typeregister::Type;
match ty {
Type::Invalid | Type::Component(_) | Type::Builtin(_) | Type::Signal => {
cx.throw_error("Cannot convert to a Sixtyfps property value")
}
Type::Float32 | Type::Int32 => {
Ok(Value::Number(val.downcast_or_throw::<JsNumber, _>(cx)?.value()))
}
Type::String => Ok(Value::String(val.to_string(cx)?.value().as_str().into())),
Type::Color => todo!(),
Type::Image => todo!(),
Type::Bool => Ok(Value::Bool(val.downcast_or_throw::<JsBoolean, _>(cx)?.value())),
}
}
declare_types! {
class SixtyFpsComponentType for WrappedComponentType {
init(_) {
@ -44,6 +87,12 @@ declare_types! {
let ct = ct.ok_or(()).or_else(|()| cx.throw_error("Invalid type"))?;
create(&mut cx, ct)
}
method name(mut cx) {
let this = cx.this();
let ct = cx.borrow(&this, |x| x.0.clone());
let ct = ct.ok_or(()).or_else(|()| cx.throw_error("Invalid type"))?;
Ok(cx.string(ct.id()).as_value(&mut cx))
}
}
class SixtyFpsComponent for WrappedComponentBox {

View file

@ -7,7 +7,8 @@ require("sixtyfps");
//import * as myModule from "../cpptest/hello.60";
let hello = require("../cpptest/hello.60");
hello.show();
let x = new hello.Hello({ counter: 55 });
x.show();

View file

@ -8,7 +8,7 @@ pub use eval::Value;
use corelib::abi::datastructures::{ComponentBox, ComponentRef, ComponentRefMut};
pub(crate) use dynamic_component::ComponentImpl;
use std::rc::Rc;
use std::{collections::HashMap, rc::Rc};
impl ComponentDescription {
/// The name of this Component as written in the .60 file
@ -17,7 +17,7 @@ impl ComponentDescription {
}
/// List of publicly declared properties or signal
pub fn properties(&self) -> Vec<(String, sixtyfps_compiler::typeregister::Type)> {
pub fn properties(&self) -> HashMap<String, sixtyfps_compiler::typeregister::Type> {
self.original
.root_component
.root_element