Skip to content

Commit

Permalink
Merge eadc11e into e286d9f
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat authored Mar 30, 2023
2 parents e286d9f + eadc11e commit 621c245
Show file tree
Hide file tree
Showing 80 changed files with 2,647 additions and 621 deletions.
3 changes: 2 additions & 1 deletion boa_engine/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ impl ArrayIterator {
kind: PropertyNameKind,
context: &Context<'_>,
) -> JsValue {
let array_iterator = JsObject::from_proto_and_data(
let array_iterator = JsObject::from_proto_and_data_with_shared_shape(
context.realm.root_shape.clone(),
context.intrinsics().objects().iterator_prototypes().array(),
ObjectData::array_iterator(Self::new(array, kind)),
);
Expand Down
53 changes: 30 additions & 23 deletions boa_engine/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,18 @@ impl BuiltInConstructor for Array {

// b. Let array be ? ArrayCreate(numberOfArgs, proto).
let array = Self::array_create(number_of_args as u64, Some(prototype), context)?;

// c. Let k be 0.
// d. Repeat, while k < numberOfArgs,
for (i, item) in args.iter().cloned().enumerate() {
// i. Let Pk be ! ToString(𝔽(k)).
// ii. Let itemK be values[k].
// iii. Perform ! CreateDataPropertyOrThrow(array, Pk, itemK).
array
.create_data_property_or_throw(i, item, context)
.expect("this CreateDataPropertyOrThrow must not fail");
// iv. Set k to k + 1.
}
// i. Let Pk be ! ToString(𝔽(k)).
// ii. Let itemK be values[k].
// iii. Perform ! CreateDataPropertyOrThrow(array, Pk, itemK).
// iv. Set k to k + 1.
array
.borrow_mut()
.properties_mut()
.override_indexed_properties(args.iter().cloned().collect());

// e. Assert: The mathematical value of array's "length" property is numberOfArgs.
// f. Return array.
Ok(array.into())
Expand All @@ -239,14 +240,28 @@ impl Array {
.with_message("array exceeded max size")
.into());
}

// Fast path:
if prototype.is_none() || prototype == context.realm.array_object_template.prototype() {
return Ok(context
.realm
.array_object_template
.create(ObjectData::array(), vec![JsValue::new(length)]));
}

// 7. Return A.
// 2. If proto is not present, set proto to %Array.prototype%.
// 3. Let A be ! MakeBasicObject(« [[Prototype]], [[Extensible]] »).
// 4. Set A.[[Prototype]] to proto.
// 5. Set A.[[DefineOwnProperty]] as specified in 10.4.2.1.
let prototype =
prototype.unwrap_or_else(|| context.intrinsics().constructors().array().prototype());
let array = JsObject::from_proto_and_data(prototype, ObjectData::array());

let array = JsObject::from_proto_and_data_with_shared_shape(
context.realm.root_shape.clone(),
prototype,
ObjectData::array(),
);

// 6. Perform ! OrdinaryDefineOwnProperty(A, "length", PropertyDescriptor { [[Value]]: 𝔽(length), [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }).
crate::object::internal_methods::ordinary_define_own_property(
Expand Down Expand Up @@ -276,27 +291,19 @@ impl Array {
{
// 1. Assert: elements is a List whose elements are all ECMAScript language values.
// 2. Let array be ! ArrayCreate(0).
let array = Self::array_create(0, None, context)
.expect("creating an empty array with the default prototype must not fail");

// 3. Let n be 0.
// 4. For each element e of elements, do
// a. Perform ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(n)), e).
// b. Set n to n + 1.
//
// 5. Return array.
// NOTE: This deviates from the spec, but it should have the same behaviour.
let elements: ThinVec<_> = elements.into_iter().collect();
let length = elements.len();
array
.borrow_mut()
.properties_mut()
.override_indexed_properties(elements);
array
.set(utf16!("length"), length, true, context)
.expect("Should not fail");

// 5. Return array.
array
context
.realm
.array_object_template
.create_with_index_properties(ObjectData::array(), vec![JsValue::new(length)], elements)
}

/// Utility function for concatenating array objects.
Expand Down
3 changes: 2 additions & 1 deletion boa_engine/src/builtins/array_buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ impl ArrayBuffer {

// 3. Set obj.[[ArrayBufferData]] to block.
// 4. Set obj.[[ArrayBufferByteLength]] to byteLength.
let obj = JsObject::from_proto_and_data(
let obj = JsObject::from_proto_and_data_with_shared_shape(
context.realm.root_shape.clone(),
prototype,
ObjectData::array_buffer(Self {
array_buffer_data: Some(block),
Expand Down
6 changes: 5 additions & 1 deletion boa_engine/src/builtins/boolean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ impl BuiltInConstructor for Boolean {
}
let prototype =
get_prototype_from_constructor(new_target, StandardConstructors::boolean, context)?;
let boolean = JsObject::from_proto_and_data(prototype, ObjectData::boolean(data));
let boolean = JsObject::from_proto_and_data_with_shared_shape(
context.realm.root_shape.clone(),
prototype,
ObjectData::boolean(data),
);

Ok(boolean.into())
}
Expand Down
3 changes: 2 additions & 1 deletion boa_engine/src/builtins/dataview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ impl BuiltInConstructor for DataView {
.into());
}

let obj = JsObject::from_proto_and_data(
let obj = JsObject::from_proto_and_data_with_shared_shape(
context.realm.root_shape.clone(),
prototype,
ObjectData::data_view(Self {
// 11. Set O.[[ViewedArrayBuffer]] to buffer.
Expand Down
6 changes: 5 additions & 1 deletion boa_engine/src/builtins/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ impl BuiltInConstructor for Date {
get_prototype_from_constructor(new_target, StandardConstructors::date, context)?;

// 7. Set O.[[DateValue]] to dv.
let obj = JsObject::from_proto_and_data(prototype, ObjectData::date(dv));
let obj = JsObject::from_proto_and_data_with_shared_shape(
context.realm.root_shape.clone(),
prototype,
ObjectData::date(dv),
);

// 8. Return O.
Ok(obj.into())
Expand Down
6 changes: 5 additions & 1 deletion boa_engine/src/builtins/error/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ impl BuiltInConstructor for AggregateError {
StandardConstructors::aggregate_error,
context,
)?;
let o = JsObject::from_proto_and_data(prototype, ObjectData::error(ErrorKind::Aggregate));
let o = JsObject::from_proto_and_data_with_shared_shape(
context.realm.root_shape.clone(),
prototype,
ObjectData::error(ErrorKind::Aggregate),
);

// 3. If message is not undefined, then
let message = args.get_or_undefined(1);
Expand Down
6 changes: 5 additions & 1 deletion boa_engine/src/builtins/error/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ impl BuiltInConstructor for EvalError {
// 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%NativeError.prototype%", « [[ErrorData]] »).
let prototype =
get_prototype_from_constructor(new_target, StandardConstructors::eval_error, context)?;
let o = JsObject::from_proto_and_data(prototype, ObjectData::error(ErrorKind::Eval));
let o = JsObject::from_proto_and_data_with_shared_shape(
context.realm.root_shape.clone(),
prototype,
ObjectData::error(ErrorKind::Eval),
);

// 3. If message is not undefined, then
let message = args.get_or_undefined(0);
Expand Down
6 changes: 5 additions & 1 deletion boa_engine/src/builtins/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ impl BuiltInConstructor for Error {
// 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%Error.prototype%", « [[ErrorData]] »).
let prototype =
get_prototype_from_constructor(new_target, StandardConstructors::error, context)?;
let o = JsObject::from_proto_and_data(prototype, ObjectData::error(ErrorKind::Error));
let o = JsObject::from_proto_and_data_with_shared_shape(
context.realm.root_shape.clone(),
prototype,
ObjectData::error(ErrorKind::Error),
);

// 3. If message is not undefined, then
let message = args.get_or_undefined(0);
Expand Down
6 changes: 5 additions & 1 deletion boa_engine/src/builtins/error/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ impl BuiltInConstructor for RangeError {
// 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%NativeError.prototype%", « [[ErrorData]] »).
let prototype =
get_prototype_from_constructor(new_target, StandardConstructors::range_error, context)?;
let o = JsObject::from_proto_and_data(prototype, ObjectData::error(ErrorKind::Range));
let o = JsObject::from_proto_and_data_with_shared_shape(
context.realm.root_shape.clone(),
prototype,
ObjectData::error(ErrorKind::Range),
);

// 3. If message is not undefined, then
let message = args.get_or_undefined(0);
Expand Down
6 changes: 5 additions & 1 deletion boa_engine/src/builtins/error/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ impl BuiltInConstructor for ReferenceError {
StandardConstructors::reference_error,
context,
)?;
let o = JsObject::from_proto_and_data(prototype, ObjectData::error(ErrorKind::Reference));
let o = JsObject::from_proto_and_data_with_shared_shape(
context.realm.root_shape.clone(),
prototype,
ObjectData::error(ErrorKind::Reference),
);

// 3. If message is not undefined, then
let message = args.get_or_undefined(0);
Expand Down
6 changes: 5 additions & 1 deletion boa_engine/src/builtins/error/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ impl BuiltInConstructor for SyntaxError {
StandardConstructors::syntax_error,
context,
)?;
let o = JsObject::from_proto_and_data(prototype, ObjectData::error(ErrorKind::Syntax));
let o = JsObject::from_proto_and_data_with_shared_shape(
context.realm.root_shape.clone(),
prototype,
ObjectData::error(ErrorKind::Syntax),
);

// 3. If message is not undefined, then
let message = args.get_or_undefined(0);
Expand Down
6 changes: 5 additions & 1 deletion boa_engine/src/builtins/error/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ impl BuiltInConstructor for TypeError {
// 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%NativeError.prototype%", « [[ErrorData]] »).
let prototype =
get_prototype_from_constructor(new_target, StandardConstructors::type_error, context)?;
let o = JsObject::from_proto_and_data(prototype, ObjectData::error(ErrorKind::Type));
let o = JsObject::from_proto_and_data_with_shared_shape(
context.realm.root_shape.clone(),
prototype,
ObjectData::error(ErrorKind::Type),
);

// 3. If message is not undefined, then
let message = args.get_or_undefined(0);
Expand Down
6 changes: 5 additions & 1 deletion boa_engine/src/builtins/error/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ impl BuiltInConstructor for UriError {
// 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%NativeError.prototype%", « [[ErrorData]] »).
let prototype =
get_prototype_from_constructor(new_target, StandardConstructors::uri_error, context)?;
let o = JsObject::from_proto_and_data(prototype, ObjectData::error(ErrorKind::Uri));
let o = JsObject::from_proto_and_data_with_shared_shape(
context.realm.root_shape.clone(),
prototype,
ObjectData::error(ErrorKind::Uri),
);

// 3. If message is not undefined, then
let message = args.get_or_undefined(0);
Expand Down
Loading

0 comments on commit 621c245

Please sign in to comment.