Rename struct priperties to fields

This commit is contained in:
Olivier Goffart 2021-03-16 12:50:33 +01:00
parent 3db3400951
commit ad75d41e20
3 changed files with 14 additions and 16 deletions

View file

@ -263,7 +263,7 @@ fn to_js_value<'cx>(
Value::Struct(o) => {
let js_object = JsObject::new(cx);
for (k, e) in o.iter() {
let v = to_js_value(e, cx)?;
let v = to_js_value(e.clone(), cx)?;
js_object.set(cx, k, v)?;
}
js_object.as_value(cx)

View file

@ -152,7 +152,7 @@ macro_rules! declare_value_struct_conversion {
impl From<$name> for Value {
fn from($name { $($field),* }: $name) -> Self {
let mut struct_ = Struct::default();
$(struct_.set_property(stringify!($field).into(), $field.into());)*
$(struct_.set_field(stringify!($field).into(), $field.into());)*
Value::Struct(struct_)
}
}
@ -163,7 +163,7 @@ macro_rules! declare_value_struct_conversion {
Self::Struct(x) => {
type Ty = $name;
Ok(Ty {
$($field: x.get_property(stringify!($field)).ok_or(())?.clone().try_into().map_err(|_|())?),*
$($field: x.get_field(stringify!($field)).ok_or(())?.clone().try_into().map_err(|_|())?),*
})
}
_ => Err(()),
@ -289,25 +289,23 @@ impl TryInto<sixtyfps_corelib::Color> for Value {
///
/// // get the properties of a `{ foo: 45, bar: true }`
/// let s : Struct = value.try_into().unwrap();
/// assert_eq!(s.get_property("foo").unwrap().try_into(), Ok(45u32));
/// assert_eq!(s.get_field("foo").unwrap().try_into(), Ok(45u32));
/// ```
/// FIXME: the documentation of langref.md uses "Object" and we probably should make that uniform.
/// also, is "property" the right term here?
#[derive(Clone, PartialEq, Debug, Default)]
pub struct Struct(HashMap<String, Value>);
impl Struct {
/// Get the value for a given struct property
pub fn get_property(&self, name: &str) -> Option<Value> {
/// Get the value for a given struct field
pub fn get_field(&self, name: &str) -> Option<Value> {
self.0.get(name).cloned()
}
/// Set the value of a given struct property
pub fn set_property(&mut self, name: String, value: Value) {
/// Set the value of a given struct field
pub fn set_field(&mut self, name: String, value: Value) {
self.0.insert(name, value);
}
/// Iterate over all the property in this struct
pub fn iter(&self) -> impl Iterator<Item = (&str, Value)> {
self.0.iter().map(|(a, b)| (a.as_str(), b.clone()))
/// Iterate over all the fields in this struct
pub fn iter(&self) -> impl Iterator<Item = (&str, &Value)> {
self.0.iter().map(|(a, b)| (a.as_str(), b))
}
}

View file

@ -173,7 +173,7 @@ pub fn eval_expression(e: &Expression, local_context: &mut EvalLocalContext) ->
}
Expression::StructFieldAccess { base, name } => {
if let Value::Struct(o) = eval_expression(base, local_context) {
o.get_property(name).unwrap_or(Value::Void)
o.get_field(name).unwrap_or(Value::Void)
} else {
Value::Void
}
@ -580,9 +580,9 @@ fn eval_assignement(lhs: &Expression, op: char, rhs: Value, local_context: &mut
}
Expression::StructFieldAccess { base, name } => {
if let Value::Struct(mut o) = eval_expression(base, local_context) {
let mut r = o.get_property(name).unwrap();
let mut r = o.get_field(name).unwrap();
r = if op == '=' { rhs } else { eval(std::mem::take(&mut r)) };
o.set_property(name.to_owned(), r);
o.set_field(name.to_owned(), r);
eval_assignement(base, '=', Value::Struct(o), local_context)
}
}