Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DataDescriptor Value to possibly be empty #1419

Merged
merged 2 commits into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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