Browse Source

Fix DataDescriptor Value to possibly be empty (#1419)

pull/1390/head
raskad 3 years ago committed by GitHub
parent
commit
65473b5903
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      boa/src/object/gcobject.rs
  2. 22
      boa/src/object/internal_methods.rs
  3. 18
      boa/src/property/mod.rs

6
boa/src/object/gcobject.rs

@ -534,12 +534,14 @@ impl GcObject {
}
Ok(AccessorDescriptor::new(get, set, attribute).into())
} else if let Some(v) = value {
Ok(DataDescriptor::new(v, attribute).into())
} else {
Ok(DataDescriptor::new(value.unwrap_or_else(Value::undefined), attribute).into())
Ok(DataDescriptor::new_without_value(attribute).into())
}
}
/// Reeturn `true` if it is a native object and the native type is `T`.
/// Return `true` if it is a native object and the native type is `T`.
///
/// # Panics
///

22
boa/src/object/internal_methods.rs

@ -196,6 +196,7 @@ impl GcObject {
}
self.insert(key, desc);
return true;
};
@ -234,8 +235,8 @@ impl GcObject {
return false;
}
let current = DataDescriptor::new(value, current.attributes());
self.insert(key, current);
self.insert(key, DataDescriptor::new(value, current.attributes()));
return true;
}
(PropertyDescriptor::Data(current), PropertyDescriptor::Data(desc)) => {
@ -268,7 +269,22 @@ impl GcObject {
}
}
self.insert(key, desc);
match (&current, &desc) {
(PropertyDescriptor::Data(current_data), PropertyDescriptor::Data(desc_data)) => {
if desc_data.has_value() {
self.insert(key, desc);
} else {
self.insert(
key,
DataDescriptor::new(current_data.value.clone(), desc_data.attributes()),
);
}
}
_ => {
self.insert(key, desc);
}
}
true
}

18
boa/src/property/mod.rs

@ -36,6 +36,7 @@ pub use attribute::Attribute;
pub struct DataDescriptor {
pub(crate) value: Value,
attributes: Attribute,
has_value: bool,
}
impl DataDescriptor {
@ -48,6 +49,17 @@ impl DataDescriptor {
Self {
value: value.into(),
attributes,
has_value: true,
}
}
/// Create a new `DataDescriptor` without a value.
#[inline]
pub fn new_without_value(attributes: Attribute) -> Self {
Self {
value: Value::undefined(),
attributes,
has_value: false,
}
}
@ -57,6 +69,12 @@ impl DataDescriptor {
self.value.clone()
}
/// Check whether the data descriptor has a value.
#[inline]
pub fn has_value(&self) -> bool {
self.has_value
}
/// Return the attributes of the descriptor.
#[inline]
pub fn attributes(&self) -> Attribute {

Loading…
Cancel
Save