Skip to content

Commit

Permalink
Implement unscopables for Array.prototype (#1963)
Browse files Browse the repository at this point in the history
This Pull Request fixes/closes #1916.

It implements `Array.prototype [ @@unscopables ]` as described in [ECMAScript](https://tc39.es/ecma262/#sec-array.prototype-@@unscopables)
  • Loading branch information
NorbertGarfield authored and Razican committed Jun 8, 2022
1 parent 93979a3 commit dc54837
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions boa_engine/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ impl BuiltIn for Array {
let _timer = Profiler::global().start_event(Self::NAME, "init");

let symbol_iterator = WellKnownSymbols::iterator();
let symbol_unscopables = WellKnownSymbols::unscopables();

let get_species = FunctionBuilder::native(context, Self::get_species)
.name("get [Symbol.species]")
.constructor(false)
.build();

let values_function = Self::values_intrinsic(context);
let unscopables_object = Self::unscopables_intrinsic(context);

ConstructorBuilder::with_standard_constructor(
context,
Expand Down Expand Up @@ -81,6 +83,11 @@ impl BuiltIn for Array {
values_function,
Attribute::WRITABLE | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.property(
symbol_unscopables,
unscopables_object,
Attribute::READONLY | Attribute::NON_ENUMERABLE | Attribute::CONFIGURABLE,
)
.method(Self::at, "at", 1)
.method(Self::concat, "concat", 1)
.method(Self::push, "push", 1)
Expand Down Expand Up @@ -2855,4 +2862,69 @@ impl Array {
.constructor(false)
.build()
}

/// `Array.prototype [ @@unscopables ]`
///
/// The initial value of the 'unscopables' data property is an ordinary object
/// with the following boolean properties set to true:
/// 'at', 'copyWithin', 'entries', 'fill', 'find', 'findIndex', 'flat',
/// 'flatMap', 'includes', 'keys', 'values'
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-array.prototype-@@unscopables
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/@@unscopables
pub(crate) fn unscopables_intrinsic(context: &mut Context) -> JsObject {
// 1. Let unscopableList be OrdinaryObjectCreate(null).
let unscopable_list = JsObject::empty();
// 2. Perform ! CreateDataPropertyOrThrow(unscopableList, "at", true).
unscopable_list
.create_data_property_or_throw("at", true, context)
.expect("CreateDataPropertyOrThrow for 'at' must not fail");
// 3. Perform ! CreateDataPropertyOrThrow(unscopableList, "copyWithin", true).
unscopable_list
.create_data_property_or_throw("copyWithin", true, context)
.expect("CreateDataPropertyOrThrow for 'copyWithin' must not fail");
// 4. Perform ! CreateDataPropertyOrThrow(unscopableList, "entries", true).
unscopable_list
.create_data_property_or_throw("entries", true, context)
.expect("CreateDataPropertyOrThrow for 'entries' must not fail");
// 5. Perform ! CreateDataPropertyOrThrow(unscopableList, "fill", true).
unscopable_list
.create_data_property_or_throw("fill", true, context)
.expect("CreateDataPropertyOrThrow for 'fill' must not fail");
// 6. Perform ! CreateDataPropertyOrThrow(unscopableList, "find", true).
unscopable_list
.create_data_property_or_throw("find", true, context)
.expect("CreateDataPropertyOrThrow for 'find' must not fail");
// 7. Perform ! CreateDataPropertyOrThrow(unscopableList, "findIndex", true).
unscopable_list
.create_data_property_or_throw("findIndex", true, context)
.expect("CreateDataPropertyOrThrow for 'findIndex' must not fail");
// 8. Perform ! CreateDataPropertyOrThrow(unscopableList, "flat", true).
unscopable_list
.create_data_property_or_throw("flat", true, context)
.expect("CreateDataPropertyOrThrow for 'flat' must not fail");
// 9. Perform ! CreateDataPropertyOrThrow(unscopableList, "flatMap", true).
unscopable_list
.create_data_property_or_throw("flatMap", true, context)
.expect("CreateDataPropertyOrThrow for 'flatMap' must not fail");
// 10. Perform ! CreateDataPropertyOrThrow(unscopableList, "includes", true).
unscopable_list
.create_data_property_or_throw("includes", true, context)
.expect("CreateDataPropertyOrThrow for 'includes' must not fail");
// 11. Perform ! CreateDataPropertyOrThrow(unscopableList, "keys", true).
unscopable_list
.create_data_property_or_throw("keys", true, context)
.expect("CreateDataPropertyOrThrow for 'keys' must not fail");
// 12. Perform ! CreateDataPropertyOrThrow(unscopableList, "values", true).
unscopable_list
.create_data_property_or_throw("values", true, context)
.expect("CreateDataPropertyOrThrow for 'values' must not fail");

// 13. Return unscopableList.
unscopable_list
}
}

0 comments on commit dc54837

Please sign in to comment.