Browse Source

Implement missing species getters (#1331)

* Implement get Array [ @@species ]

* Implement get Map [ @@species ]

* Adjust get Set [ @@species ]
pull/1334/head
raskad 3 years ago committed by GitHub
parent
commit
0825fdaa5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      boa/src/builtins/array/mod.rs
  2. 27
      boa/src/builtins/map/mod.rs
  3. 22
      boa/src/builtins/set/mod.rs

27
boa/src/builtins/array/mod.rs

@ -45,6 +45,12 @@ impl BuiltIn for Array {
let symbol_iterator = WellKnownSymbols::iterator(); let symbol_iterator = WellKnownSymbols::iterator();
let get_species = FunctionBuilder::new(context, Self::get_species)
.name("get [Symbol.species]")
.constructable(false)
.callable(true)
.build();
let values_function = FunctionBuilder::new(context, Self::values) let values_function = FunctionBuilder::new(context, Self::values)
.name("values") .name("values")
.length(0) .length(0)
@ -59,6 +65,12 @@ impl BuiltIn for Array {
) )
.name(Self::NAME) .name(Self::NAME)
.length(Self::LENGTH) .length(Self::LENGTH)
.static_accessor(
WellKnownSymbols::species(),
Some(get_species),
None,
Attribute::CONFIGURABLE,
)
.property( .property(
"length", "length",
0, 0,
@ -263,6 +275,21 @@ impl Array {
Ok(array_obj_ptr) Ok(array_obj_ptr)
} }
/// `get Array [ @@species ]`
///
/// The Array[@@species] accessor property returns the Array constructor.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-get-array-@@species
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/@@species
fn get_species(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
// 1. Return the this value.
Ok(this.clone())
}
/// Utility function used to specify the creation of a new Array object using a constructor /// Utility function used to specify the creation of a new Array object using a constructor
/// function that is derived from original_array. /// function that is derived from original_array.
/// ///

27
boa/src/builtins/map/mod.rs

@ -44,6 +44,12 @@ impl BuiltIn for Map {
let to_string_tag = WellKnownSymbols::to_string_tag(); let to_string_tag = WellKnownSymbols::to_string_tag();
let iterator_symbol = WellKnownSymbols::iterator(); let iterator_symbol = WellKnownSymbols::iterator();
let get_species = FunctionBuilder::new(context, Self::get_species)
.name("get [Symbol.species]")
.constructable(false)
.callable(true)
.build();
let entries_function = FunctionBuilder::new(context, Self::entries) let entries_function = FunctionBuilder::new(context, Self::entries)
.name("entries") .name("entries")
.length(0) .length(0)
@ -58,6 +64,12 @@ impl BuiltIn for Map {
) )
.name(Self::NAME) .name(Self::NAME)
.length(Self::LENGTH) .length(Self::LENGTH)
.static_accessor(
WellKnownSymbols::species(),
Some(get_species),
None,
Attribute::CONFIGURABLE,
)
.property( .property(
"entries", "entries",
entries_function.clone(), entries_function.clone(),
@ -160,6 +172,21 @@ impl Map {
Ok(this) Ok(this)
} }
/// `get Map [ @@species ]`
///
/// The Map[@@species] accessor property returns the Map constructor.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-get-map-@@species
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/@@species
fn get_species(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
// 1. Return the this value.
Ok(this.clone())
}
/// `Map.prototype.entries()` /// `Map.prototype.entries()`
/// ///
/// Returns a new Iterator object that contains the [key, value] pairs for each element in the Map object in insertion order. /// Returns a new Iterator object that contains the [key, value] pairs for each element in the Map object in insertion order.

22
boa/src/builtins/set/mod.rs

@ -39,12 +39,10 @@ impl BuiltIn for Set {
fn init(context: &mut Context) -> (&'static str, Value, Attribute) { fn init(context: &mut Context) -> (&'static str, Value, Attribute) {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); let _timer = BoaProfiler::global().start_event(Self::NAME, "init");
let species = WellKnownSymbols::species(); let get_species = FunctionBuilder::new(context, Self::get_species)
let species_getter = FunctionBuilder::new(context, Self::species_getter)
.callable(true)
.constructable(false)
.name("get [Symbol.species]") .name("get [Symbol.species]")
.constructable(false)
.callable(true)
.build(); .build();
let size_getter = FunctionBuilder::new(context, Self::size_getter) let size_getter = FunctionBuilder::new(context, Self::size_getter)
@ -71,7 +69,12 @@ impl BuiltIn for Set {
) )
.name(Self::NAME) .name(Self::NAME)
.length(Self::LENGTH) .length(Self::LENGTH)
.static_accessor(species, Some(species_getter), None, Attribute::CONFIGURABLE) .static_accessor(
WellKnownSymbols::species(),
Some(get_species),
None,
Attribute::CONFIGURABLE,
)
.method(Self::add, "add", 1) .method(Self::add, "add", 1)
.method(Self::clear, "clear", 0) .method(Self::clear, "clear", 0)
.method(Self::delete, "delete", 1) .method(Self::delete, "delete", 1)
@ -178,13 +181,16 @@ impl Set {
/// `get Set [ @@species ]` /// `get Set [ @@species ]`
/// ///
/// get accessor for the @@species property of Set /// The Set[Symbol.species] accessor property returns the Set constructor.
/// ///
/// More information: /// More information:
/// - [ECMAScript reference][spec] /// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
/// ///
/// [spec]: https://tc39.es/ecma262/#sec-get-set-@@species /// [spec]: https://tc39.es/ecma262/#sec-get-set-@@species
fn species_getter(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> { /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/@@species
fn get_species(this: &Value, _: &[Value], _: &mut Context) -> Result<Value> {
// 1. Return the this value.
Ok(this.clone()) Ok(this.clone())
} }

Loading…
Cancel
Save