Skip to content

Commit

Permalink
Call Array::create_array_from_list where necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Aug 20, 2021
1 parent a44658e commit 8a9a576
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 58 deletions.
9 changes: 3 additions & 6 deletions boa/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,9 @@ impl ArrayIterator {
}
ArrayIterationKind::KeyAndValue => {
let element_value = array_iterator.array.get_field(index, context)?;
let result = Array::constructor(
&JsValue::new_object(context),
&[index.into(), element_value],
context,
)?;
Ok(create_iter_result_object(context, result, false))
let result =
Array::create_array_from_list([index.into(), element_value], context);
Ok(create_iter_result_object(context, result.into(), false))
}
}
} else {
Expand Down
43 changes: 1 addition & 42 deletions boa/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl Array {
array.ordinary_define_own_property(
"length".into(),
PropertyDescriptor::builder()
.value(length as f64)
.value(length)
.writable(true)
.enumerable(false)
.configurable(false)
Expand Down Expand Up @@ -290,47 +290,6 @@ impl Array {
array
}

/// Utility function for creating array objects.
///
/// `array_obj` can be any array with prototype already set (it will be wiped and
/// recreated from `array_contents`)
pub(crate) fn construct_array(
array_obj: &JsValue,
array_contents: &[JsValue],
context: &mut Context,
) -> Result<JsValue> {
let array_obj_ptr = array_obj.clone();

// Wipe existing contents of the array object
let orig_length = array_obj.get_field("length", context)?.to_length(context)?;
for n in 0..orig_length {
array_obj_ptr.remove_property(n);
}

// Create length
array_obj_ptr.set_property(
"length".to_string(),
PropertyDescriptor::builder()
.value(array_contents.len())
.writable(true)
.enumerable(false)
.configurable(false)
.build(),
);

for (n, value) in array_contents.iter().enumerate() {
array_obj_ptr.set_property(
n,
PropertyDescriptor::builder()
.value(value)
.configurable(true)
.enumerable(true)
.writable(true),
);
}
Ok(array_obj_ptr)
}

/// Utility function for concatenating array objects.
///
/// Returns a Boolean valued property that if `true` indicates that
Expand Down
11 changes: 6 additions & 5 deletions boa/src/builtins/map/map_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,14 @@ impl MapIterator {
));
}
MapIterationKind::KeyAndValue => {
let result = Array::construct_array(
&Array::new_array(context),
&[key.clone(), value.clone()],
let result = Array::create_array_from_list(
[key.clone(), value.clone()],
context,
)?;
);
return Ok(create_iter_result_object(
context, result, false,
context,
result.into(),
false,
));
}
}
Expand Down
11 changes: 6 additions & 5 deletions boa/src/builtins/set/set_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,14 @@ impl SetIterator {
));
}
SetIterationKind::KeyAndValue => {
let result = Array::construct_array(
&Array::new_array(context),
&[value.clone(), value.clone()],
let result = Array::create_array_from_list(
[value.clone(), value.clone()],
context,
)?;
);
return Ok(create_iter_result_object(
context, result, false,
context,
result.into(),
false,
));
}
}
Expand Down

0 comments on commit 8a9a576

Please sign in to comment.