From 0825fdaa5b99dae0bf086985b1ba47188eab6dad Mon Sep 17 00:00:00 2001 From: raskad <32105367+raskad@users.noreply.github.com> Date: Fri, 18 Jun 2021 16:55:21 +0200 Subject: [PATCH] Implement missing species getters (#1331) * Implement get Array [ @@species ] * Implement get Map [ @@species ] * Adjust get Set [ @@species ] --- boa/src/builtins/array/mod.rs | 27 +++++++++++++++++++++++++++ boa/src/builtins/map/mod.rs | 27 +++++++++++++++++++++++++++ boa/src/builtins/set/mod.rs | 22 ++++++++++++++-------- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/boa/src/builtins/array/mod.rs b/boa/src/builtins/array/mod.rs index 8cdbf435f2..159fe4c744 100644 --- a/boa/src/builtins/array/mod.rs +++ b/boa/src/builtins/array/mod.rs @@ -45,6 +45,12 @@ impl BuiltIn for Array { 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) .name("values") .length(0) @@ -59,6 +65,12 @@ impl BuiltIn for Array { ) .name(Self::NAME) .length(Self::LENGTH) + .static_accessor( + WellKnownSymbols::species(), + Some(get_species), + None, + Attribute::CONFIGURABLE, + ) .property( "length", 0, @@ -263,6 +275,21 @@ impl Array { 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 { + // 1. Return the this value. + Ok(this.clone()) + } + /// Utility function used to specify the creation of a new Array object using a constructor /// function that is derived from original_array. /// diff --git a/boa/src/builtins/map/mod.rs b/boa/src/builtins/map/mod.rs index 711579574d..bf578a79a7 100644 --- a/boa/src/builtins/map/mod.rs +++ b/boa/src/builtins/map/mod.rs @@ -44,6 +44,12 @@ impl BuiltIn for Map { let to_string_tag = WellKnownSymbols::to_string_tag(); 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) .name("entries") .length(0) @@ -58,6 +64,12 @@ impl BuiltIn for Map { ) .name(Self::NAME) .length(Self::LENGTH) + .static_accessor( + WellKnownSymbols::species(), + Some(get_species), + None, + Attribute::CONFIGURABLE, + ) .property( "entries", entries_function.clone(), @@ -160,6 +172,21 @@ impl Map { 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 { + // 1. Return the this value. + Ok(this.clone()) + } + /// `Map.prototype.entries()` /// /// Returns a new Iterator object that contains the [key, value] pairs for each element in the Map object in insertion order. diff --git a/boa/src/builtins/set/mod.rs b/boa/src/builtins/set/mod.rs index d8df3c6b37..8f7543a063 100644 --- a/boa/src/builtins/set/mod.rs +++ b/boa/src/builtins/set/mod.rs @@ -39,12 +39,10 @@ impl BuiltIn for Set { fn init(context: &mut Context) -> (&'static str, Value, Attribute) { let _timer = BoaProfiler::global().start_event(Self::NAME, "init"); - let species = WellKnownSymbols::species(); - - let species_getter = FunctionBuilder::new(context, Self::species_getter) - .callable(true) - .constructable(false) + let get_species = FunctionBuilder::new(context, Self::get_species) .name("get [Symbol.species]") + .constructable(false) + .callable(true) .build(); let size_getter = FunctionBuilder::new(context, Self::size_getter) @@ -71,7 +69,12 @@ impl BuiltIn for Set { ) .name(Self::NAME) .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::clear, "clear", 0) .method(Self::delete, "delete", 1) @@ -178,13 +181,16 @@ impl Set { /// `get Set [ @@species ]` /// - /// get accessor for the @@species property of Set + /// The Set[Symbol.species] accessor property returns the Set constructor. /// /// More information: /// - [ECMAScript reference][spec] + /// - [MDN documentation][mdn] /// /// [spec]: https://tc39.es/ecma262/#sec-get-set-@@species - fn species_getter(this: &Value, _: &[Value], _: &mut Context) -> Result { + /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/@@species + fn get_species(this: &Value, _: &[Value], _: &mut Context) -> Result { + // 1. Return the this value. Ok(this.clone()) }