Skip to content

Commit

Permalink
Fix DataDescriptor Value to possibly be empty (#1419)
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed Jul 22, 2021
1 parent 970a611 commit 65473b5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
6 changes: 4 additions & 2 deletions boa/src/object/gcobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
///
Expand Down
22 changes: 19 additions & 3 deletions boa/src/object/internal_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ impl GcObject {
}

self.insert(key, desc);

return true;
};

Expand Down Expand Up @@ -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)) => {
Expand Down Expand Up @@ -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
}

Expand Down
18 changes: 18 additions & 0 deletions boa/src/property/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub use attribute::Attribute;
pub struct DataDescriptor {
pub(crate) value: Value,
attributes: Attribute,
has_value: bool,
}

impl DataDescriptor {
Expand All @@ -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,
}
}

Expand All @@ -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 {
Expand Down

0 comments on commit 65473b5

Please sign in to comment.