Add public Rust interpreter API to introspect the public properties

This commit is contained in:
Simon Hausmann 2021-03-30 14:20:21 +02:00
parent 9a6e9c3a53
commit 342f77139b
2 changed files with 68 additions and 9 deletions

View file

@ -115,7 +115,8 @@ fn create<'cx>(
let persistent_context = persistent_context::PersistentContext::new(cx);
if let Some(args) = cx.argument_opt(0).and_then(|arg| arg.downcast::<JsObject>().ok()) {
let properties = component_type.properties().collect::<std::collections::HashMap<_, _>>();
let properties =
component_type.properties_and_callbacks().collect::<std::collections::HashMap<_, _>>();
for x in args.get_own_property_names(cx)?.to_vec(cx)? {
let prop_name = x.to_string(cx)?.value();
let value = args.get(cx, x)?;
@ -295,10 +296,10 @@ declare_types! {
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"))?;
let properties = ct.properties();
let properties = ct.properties_and_callbacks().filter(|(_, prop_type)| prop_type.is_property_type());
let array = JsArray::new(&mut cx, 0);
let mut len: u32 = 0;
for (p, _) in properties.filter(|(_, prop_type)| prop_type.is_property_type()) {
for (p, _) in properties {
let prop_name = JsString::new(&mut cx, p);
array.set(&mut cx, len, prop_name)?;
len = len + 1;
@ -309,10 +310,10 @@ declare_types! {
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"))?;
let properties = ct.properties();
let callbacks = ct.properties_and_callbacks().filter(|(_, prop_type)| matches!(prop_type, Type::Callback{..}));
let array = JsArray::new(&mut cx, 0);
let mut len: u32 = 0;
for (p, _) in properties.filter(|(_, prop_type)| matches!(prop_type, Type::Callback{..})) {
for (p, _) in callbacks {
let prop_name = JsString::new(&mut cx, p);
array.set(&mut cx, len, prop_name)?;
len = len + 1;
@ -371,7 +372,7 @@ declare_types! {
let this = cx.this();
let component = cx.borrow(&this, |x| x.0.as_ref().map(|c| c.clone_strong()));
let component = component.ok_or(()).or_else(|()| cx.throw_error("Invalid type"))?;
let ty = component.definition().properties()
let ty = component.definition().properties_and_callbacks()
.find_map(|(name, proptype)| if name == prop_name { Some(proptype) } else { None })
.ok_or(())
.or_else(|()| {
@ -394,7 +395,7 @@ declare_types! {
let this = cx.this();
let component = cx.borrow(&this, |x| x.0.as_ref().map(|c| c.clone_strong()));
let component = component.ok_or(()).or_else(|()| cx.throw_error("Invalid type"))?;
let ty = component.definition().properties()
let ty = component.definition().properties_and_callbacks()
.find_map(|(name, proptype)| if name == callback_name { Some(proptype) } else { None })
.ok_or(())
.or_else(|()| {
@ -435,7 +436,7 @@ declare_types! {
let component = cx.borrow(&this, |x| x.0.as_ref().map(|c| c.clone_strong()));
let component = component.ok_or(()).or_else(|()| cx.throw_error("Invalid type"))?;
let ty = component.definition().properties()
let ty = component.definition().properties_and_callbacks()
.find_map(|(name, proptype)| if name == callback_name { Some(proptype) } else { None })
.ok_or(())
.or_else(|()| {