Remove some remains of code that allowed to use Component as Object

This commit is contained in:
Olivier Goffart 2020-10-27 16:23:25 +01:00
parent 4281893389
commit 70d6648880
6 changed files with 46 additions and 121 deletions

View file

@ -156,33 +156,46 @@ fn to_eval_value<'cx>(
) -> NeonResult<sixtyfps_interpreter::Value> {
use sixtyfps_interpreter::Value;
match ty {
Type::Float32 | Type::Int32 | Type::Duration | Type::Length | Type::LogicalLength | Type::Percent => {
Ok(Value::Number(val.downcast_or_throw::<JsNumber, _>(cx)?.value()))
}
Type::Float32
| Type::Int32
| Type::Duration
| Type::Length
| Type::LogicalLength
| Type::Percent => Ok(Value::Number(val.downcast_or_throw::<JsNumber, _>(cx)?.value())),
Type::String => Ok(Value::String(val.to_string(cx)?.value().into())),
Type::Color => {
let c = val.to_string(cx)?.value().parse::<css_color_parser2::Color>().or_else(|e| cx.throw_error(&e.to_string()))?;
Ok(Value::Color(sixtyfps_corelib::Color::from_argb_u8((c.a * 255.) as u8, c.r, c.g, c.b)))
let c = val
.to_string(cx)?
.value()
.parse::<css_color_parser2::Color>()
.or_else(|e| cx.throw_error(&e.to_string()))?;
Ok(Value::Color(sixtyfps_corelib::Color::from_argb_u8(
(c.a * 255.) as u8,
c.r,
c.g,
c.b,
)))
}
Type::Array(a) => {
match val.downcast::<JsArray>() {
Ok(arr) => {
let vec = arr.to_vec(cx)?;
Ok(Value::Array(vec.into_iter().map(|i| to_eval_value(i, (*a).clone(), cx, persistent_context)).collect::<Result<Vec<_>, _>>()?))
}
Err(_) => {
let obj = val.downcast_or_throw::<JsObject, _>(cx)?;
obj.get(cx, "rowCount")?.downcast_or_throw::<JsFunction, _>(cx)?;
obj.get(cx, "rowData")?.downcast_or_throw::<JsFunction, _>(cx)?;
let m = js_model::JsModel::new(obj, *a, cx, persistent_context)?;
Ok(Value::Model(sixtyfps_interpreter::ModelPtr(m)))
}
Type::Array(a) => match val.downcast::<JsArray>() {
Ok(arr) => {
let vec = arr.to_vec(cx)?;
Ok(Value::Array(
vec.into_iter()
.map(|i| to_eval_value(i, (*a).clone(), cx, persistent_context))
.collect::<Result<Vec<_>, _>>()?,
))
}
Err(_) => {
let obj = val.downcast_or_throw::<JsObject, _>(cx)?;
obj.get(cx, "rowCount")?.downcast_or_throw::<JsFunction, _>(cx)?;
obj.get(cx, "rowData")?.downcast_or_throw::<JsFunction, _>(cx)?;
let m = js_model::JsModel::new(obj, *a, cx, persistent_context)?;
Ok(Value::Model(sixtyfps_interpreter::ModelPtr(m)))
}
},
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{fields, ..} => {
Type::Object { fields, .. } => {
let obj = val.downcast_or_throw::<JsObject, _>(cx)?;
Ok(Value::Object(
fields
@ -194,28 +207,7 @@ fn to_eval_value<'cx>(
obj.get(cx, pro_name.as_str())?,
pro_ty.clone(),
cx,
persistent_context
)?,
))
})
.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(
c.root_element
.borrow()
.property_declarations
.iter()
.map(|(pro_name, pro_decl)| {
Ok((
pro_name.clone(),
to_eval_value(
obj.get(cx, pro_name.as_str())?,
pro_decl.property_type.clone(),
cx,
persistent_context
persistent_context,
)?,
))
})
@ -232,7 +224,7 @@ fn to_eval_value<'cx>(
| Type::Model
| Type::Signal { .. }
| Type::Easing
| Type::Component(_) // The struct case is handled before
| Type::Component(_)
| Type::PathElements
| Type::ElementReference => cx.throw_error("Cannot convert to a Sixtyfps property value"),
}

View file

@ -220,9 +220,6 @@ impl CppType for Type {
Type::Resource => Some("sixtyfps::Resource".to_owned()),
Type::Builtin(elem) => elem.native_class.cpp_type.clone(),
Type::Enumeration(enumeration) => Some(format!("sixtyfps::{}", enumeration.name)),
Type::Component(c) if c.root_element.borrow().base_type == Type::Void => {
Some(c.id.clone())
}
_ => None,
}
}
@ -1202,9 +1199,6 @@ fn compile_expression(e: &crate::expression_tree::Expression, component: &Rc<Com
Type::Object{..} => {
format!("{}.{}", compile_expression(base, component), name)
}
Type::Component(c) if c.root_element.borrow().base_type == Type::Void => {
format!("{}.{}", compile_expression(base, component), name)
}
_ => panic!("Expression::ObjectAccess's base expression is not an Object type"),
},
Expression::Cast { from, to } => {
@ -1220,19 +1214,6 @@ fn compile_expression(e: &crate::expression_tree::Expression, component: &Rc<Com
(Type::Float32, Type::Color) => {
format!("sixtyfps::Color::from_argb_encoded({})", f)
}
(Type::Object { .. }, Type::Component(c))
if c.root_element.borrow().base_type == Type::Void =>
{
format!(
"[&](const auto &o){{ {struct_name} s; auto& [{field_members}] = s; {fields}; return s; }}({obj})",
struct_name = component_id(c),
field_members = (0..c.root_element.borrow().property_declarations.len()).map(|idx| format!("f_{}", idx)).join(", "),
obj = f,
fields = (0..c.root_element.borrow().property_declarations.len())
.map(|idx| format!("f_{} = std::get<{}>(o)", idx, idx))
.join("; ")
)
}
(Type::Object { .. }, Type::Object{ fields, name: Some(n)}) => {
format!(
"[&](const auto &o){{ {struct_name} s; auto& [{field_members}] = s; {fields}; return s; }}({obj})",
@ -1404,9 +1385,6 @@ fn compile_assignment(
format!("std::get<{}>({})", index, tmpobj)
}
Type::Object { .. } => format!("{}.{}", tmpobj, name),
Type::Component(c) if c.root_element.borrow().base_type == Type::Void => {
format!("{}.{}", tmpobj, name)
}
_ => panic!("Expression::ObjectAccess's base expression is not an Object type"),
};
let op = if op == '=' { ' ' } else { op };

View file

@ -47,9 +47,6 @@ fn rust_type(
let inner = rust_type(&o, span)?;
Ok(quote!(sixtyfps::re_exports::ModelHandle<#inner>))
}
Type::Component(c) if c.root_element.borrow().base_type == Type::Void => {
Ok(c.id.parse().unwrap())
}
_ => Err(CompilerDiagnostic {
message: format!("Cannot map property type {} to Rust", ty),
span: span.clone(),
@ -1049,11 +1046,6 @@ fn compile_expression(e: &Expression, component: &Rc<Component>) -> TokenStream
let base_e = compile_expression(base, component);
quote!((#base_e).#name)
}
Type::Component(c) if c.root_element.borrow().base_type == Type::Void => {
let base_e = compile_expression(base, component);
let name = format_ident!("{}", name);
quote!((#base_e).#name)
}
_ => panic!("Expression::ObjectAccess's base expression is not an Object type"),
},
Expression::CodeBlock(sub) => {
@ -1262,15 +1254,10 @@ fn compile_assignment(
let index = proc_macro2::Literal::usize_unsuffixed(index);
(quote!(#index), fields[name].clone())
}
Type::Object { fields, name: Some(_) } => {
let n = format_ident!("{}", name);
(quote!(#n), fields[name].clone())
}
Type::Component(c) if c.root_element.borrow().base_type == Type::Void => {
let n = format_ident!("{}", name);
(quote!(#n), c.root_element.borrow().lookup_property(name))
}
_ => panic!("Expression::ObjectAccess's base expression is not an Object type"),
};

View file

@ -20,7 +20,7 @@ pub enum Type {
Invalid,
/// The type of an expression that return nothing
Void,
Component(Rc<crate::object_tree::Component>),
Component(Rc<Component>),
Builtin(Rc<BuiltinElement>),
Native(Rc<NativeClass>),
@ -185,7 +185,6 @@ impl Type {
| Self::ElementReference
| Self::Object { .. }
| Self::Array(_) => true,
Self::Component(c) => c.root_element.borrow().base_type == Type::Void,
_ => false,
}
}
@ -273,7 +272,7 @@ impl Type {
}
/// Assime it is a Component, panic if it isn't
pub fn as_component(&self) -> &Rc<crate::object_tree::Component> {
pub fn as_component(&self) -> &Rc<Component> {
match self {
Type::Component(c) => c,
_ => panic!("should be a component because of the repeater_component pass"),
@ -300,19 +299,6 @@ impl Type {
}
true
};
let can_convert_object_to_component = |a: &BTreeMap<String, Type>, c: &Component| {
let root_element = c.root_element.borrow();
if root_element.base_type != Type::Void {
//component is not a struct
return false;
}
for (k, v) in &root_element.property_declarations {
if !a.get(k).map_or(false, |t| t.can_convert(&v.property_type)) {
return false;
}
}
true
};
match (self, other) {
(a, b) if a == b => true,
@ -328,15 +314,8 @@ impl Type {
| (Type::Length, Type::LogicalLength)
| (Type::LogicalLength, Type::Length)
| (Type::Percent, Type::Float32) => true,
(Type::Object { fields: a, .. }, Type::Object { fields: b, .. })
if can_convert_object(a, b) =>
{
true
}
(Type::Object { fields, .. }, Type::Component(c))
if can_convert_object_to_component(fields, c) =>
{
true
(Type::Object { fields: a, .. }, Type::Object { fields: b, .. }) => {
can_convert_object(a, b)
}
_ => false,
}

View file

@ -12,7 +12,7 @@ use std::{cell::RefCell, rc::Rc};
use crate::expression_tree::{BuiltinFunction, Expression, Unit};
use crate::langtype::{BuiltinElement, Enumeration, NativeClass, Type};
use crate::object_tree::{Component, Element};
use crate::object_tree::Component;
/// reserved property injected in every item
pub fn reserved_property(name: &str) -> Type {
@ -390,18 +390,10 @@ impl TypeRegister {
.for_each(|ty| ty.collect_contextual_types(&mut context_restricted_types));
register.context_restricted_types = context_restricted_types;
let standard_listview_item = Type::Component(Rc::new(Component {
id: "sixtyfps::StandardListViewItem".into(),
root_element: Rc::new(RefCell::new(Element {
base_type: Type::Void,
property_declarations: [("text".to_owned(), Type::String.into())]
.iter()
.cloned()
.collect(),
..Element::default()
})),
..Component::default()
}));
let standard_listview_item = Type::Object {
name: Some("sixtyfps::StandardListViewItem".into()),
fields: [("text".to_owned(), Type::String.into())].iter().cloned().collect(),
};
register.types.insert("StandardListViewItem".into(), standard_listview_item.clone());
// FIXME: should this be auto generated or placed somewhere else
@ -588,11 +580,11 @@ impl TypeRegister {
self.lookup(qualified[0].as_ref())
}
pub fn add(&mut self, comp: Rc<crate::object_tree::Component>) {
pub fn add(&mut self, comp: Rc<Component>) {
self.add_with_name(comp.id.clone(), comp);
}
pub fn add_with_name(&mut self, name: String, comp: Rc<crate::object_tree::Component>) {
pub fn add_with_name(&mut self, name: String, comp: Rc<Component>) {
self.types.insert(name, Type::Component(comp));
}

View file

@ -601,9 +601,6 @@ fn generate_component<'id>(
}
Type::Object { .. } => property_info::<eval::Value>(),
Type::Array(_) => property_info::<eval::Value>(),
Type::Component(ref c) if c.root_element.borrow().base_type == Type::Void => {
property_info::<eval::Value>()
}
Type::Percent => property_info::<f32>(),
_ => panic!("bad type"),
};