Skip to content

Commit

Permalink
Implement abstract operation DeletePropertyOrThrow
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Jun 19, 2021
1 parent 9497636 commit 323bd9b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
14 changes: 7 additions & 7 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ impl Array {
}
let pop_index = curr_length.wrapping_sub(1);
let pop_value: Value = this.get_field(pop_index.to_string(), context)?;
this.remove_property(pop_index);
this.delete_property_or_throw(pop_index, context)?;
this.set_field("length", Value::from(pop_index), true, context)?;
Ok(pop_value)
}
Expand Down Expand Up @@ -621,10 +621,10 @@ impl Array {
this.set_property(lower, DataDescriptor::new(upper_value, Attribute::all()));
} else if upper_exists {
this.set_property(lower, DataDescriptor::new(upper_value, Attribute::all()));
this.remove_property(upper);
this.delete_property_or_throw(upper, context)?;
} else if lower_exists {
this.set_property(upper, DataDescriptor::new(lower_value, Attribute::all()));
this.remove_property(lower);
this.delete_property_or_throw(lower, context)?;
}
}

Expand Down Expand Up @@ -657,14 +657,14 @@ impl Array {

let from_value = this.get_field(from, context)?;
if from_value.is_undefined() {
this.remove_property(to);
this.delete_property_or_throw(to, context)?;
} else {
this.set_property(to, DataDescriptor::new(from_value, Attribute::all()));
}
}

let final_index = len.wrapping_sub(1);
this.remove_property(final_index);
this.delete_property_or_throw(final_index, context)?;
this.set_field("length", Value::from(final_index), true, context)?;

Ok(first)
Expand Down Expand Up @@ -694,7 +694,7 @@ impl Array {

let from_value = this.get_field(from, context)?;
if from_value.is_undefined() {
this.remove_property(to);
this.delete_property_or_throw(to, context)?;
} else {
this.set_property(to, DataDescriptor::new(from_value, Attribute::all()));
}
Expand Down Expand Up @@ -1624,7 +1624,7 @@ impl Array {
let val = this.get_field(from, context)?;
this.set_field(to, val, true, context)?;
} else {
this.remove_property(to);
this.delete_property_or_throw(to, context)?;
}
match direction {
Direction::Forward => {
Expand Down
18 changes: 18 additions & 0 deletions boa/src/object/internal_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ impl GcObject {
}
}

/// Abstract operation `DeletePropertyOrThrow ( O, P )`
///
/// More information:
/// - [ECMAScript][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-deletepropertyorthrow
pub fn delete_property_or_throw(
&mut self,
key: &PropertyKey,
context: &mut Context,
) -> Result<()> {
if self.delete(key) {
Ok(())
} else {
Err(context.construct_type_error("Failed deleting property of Object"))
}
}

/// `[[Get]]`
/// <https://tc39.es/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots-get-p-receiver>
pub fn get(&self, key: &PropertyKey, receiver: Value, context: &mut Context) -> Result<Value> {
Expand Down
16 changes: 16 additions & 0 deletions boa/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,22 @@ impl Value {
.is_some()
}

/// Abstract operation `DeletePropertyOrThrow ( O, P )`
///
/// More information:
/// - [ECMAScript][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-deletepropertyorthrow
// todo: remove function after separation of Value and Object
pub fn delete_property_or_throw<Key>(&self, key: Key, context: &mut Context) -> Result<()>
where
Key: Into<PropertyKey>,
{
self.as_object()
.expect("expected object type")
.delete_property_or_throw(&key.into(), context)
}

/// Resolve the property in the object.
///
/// A copy of the Property is returned.
Expand Down

0 comments on commit 323bd9b

Please sign in to comment.