Interpreter API: preserve the dashes and underscore when listing properties

This commit is contained in:
Olivier Goffart 2021-08-17 17:01:59 +02:00 committed by Olivier Goffart
parent 77f441b395
commit a39dd6ac4f
4 changed files with 24 additions and 8 deletions

View file

@ -116,8 +116,10 @@ fn create<'cx>(
let persistent_context = persistent_context::PersistentContext::new(cx); let persistent_context = persistent_context::PersistentContext::new(cx);
if let Some(args) = cx.argument_opt(0).and_then(|arg| arg.downcast::<JsObject>().ok()) { if let Some(args) = cx.argument_opt(0).and_then(|arg| arg.downcast::<JsObject>().ok()) {
let properties = let properties = component_type
component_type.properties_and_callbacks().collect::<std::collections::HashMap<_, _>>(); .properties_and_callbacks()
.map(|(k, v)| (k.replace('_', "-"), v))
.collect::<std::collections::HashMap<_, _>>();
for x in args.get_own_property_names(cx)?.to_vec(cx)? { for x in args.get_own_property_names(cx)?.to_vec(cx)? {
let prop_name = x.to_string(cx)?.value().replace('_', "-"); let prop_name = x.to_string(cx)?.value().replace('_', "-");
let value = args.get(cx, x)?; let value = args.get(cx, x)?;

View file

@ -943,6 +943,7 @@ fn component_definition_properties() {
r#" r#"
export Dummy := Rectangle { export Dummy := Rectangle {
property <string> test; property <string> test;
property <int> underscores-and-dashes_preserved;
callback hello; callback hello;
}"# }"#
.into(), .into(),
@ -953,9 +954,11 @@ fn component_definition_properties() {
let props = comp_def.properties().collect::<Vec<(_, _)>>(); let props = comp_def.properties().collect::<Vec<(_, _)>>();
assert_eq!(props.len(), 1); assert_eq!(props.len(), 2);
assert_eq!(props[0].0, "test"); assert_eq!(props[0].0, "test");
assert_eq!(props[0].1, ValueType::String); assert_eq!(props[0].1, ValueType::String);
assert_eq!(props[1].0, "underscores-and-dashes_preserved");
assert_eq!(props[1].1, ValueType::Number);
} }
#[test] #[test]

View file

@ -309,13 +309,24 @@ impl<'id> ComponentDescription<'id> {
} }
/// List of publicly declared properties or callbacks /// List of publicly declared properties or callbacks
///
/// We try to preserve the dashes and underscore as written in the property declaration
pub fn properties( pub fn properties(
&self, &self,
) -> impl Iterator<Item = (String, sixtyfps_compilerlib::langtype::Type)> + '_ { ) -> impl Iterator<Item = (String, sixtyfps_compilerlib::langtype::Type)> + '_ {
self.public_properties self.public_properties.iter().filter(|(_, v)| v.expose_in_public_api).map(|(s, v)| {
.iter() let name = v
.filter(|(_, v)| v.expose_in_public_api) .node
.map(|(s, v)| (s.clone(), v.property_type.clone())) .as_ref()
.and_then(|n| {
n.as_ref()
.either(|n| n.DeclaredIdentifier(), |n| n.DeclaredIdentifier())
.child_token(parser::SyntaxKind::Identifier)
})
.map(|n| n.to_string())
.unwrap_or_else(|| s.clone());
(name, v.property_type.clone())
})
} }
/// Instantiate a runtime component from this ComponentDescription /// Instantiate a runtime component from this ComponentDescription

View file

@ -85,7 +85,7 @@ try {
instance.test_callback(); instance.test_callback();
assert(false); assert(false);
} catch(e) { } catch(e) {
assert.equal(e.toString(), "Error: test-callback expect 1 arguments, but 0 where provided"); assert.equal(e.toString(), "Error: test_callback expect 1 arguments, but 0 where provided");
} }
assert.equal(instance.callback_emission_count, 0); assert.equal(instance.callback_emission_count, 0);