Browse Source

Reuse `PropertyNameKind` for all iterators (#1494)

pull/1506/head
jedel1043 3 years ago committed by GitHub
parent
commit
d3a70e8a81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      boa/src/builtins/array/array_iterator.rs
  2. 10
      boa/src/builtins/array/mod.rs
  3. 22
      boa/src/builtins/map/map_iterator.rs
  4. 10
      boa/src/builtins/map/mod.rs
  5. 8
      boa/src/builtins/set/mod.rs
  6. 21
      boa/src/builtins/set/set_iterator.rs
  7. 7
      boa/src/property/mod.rs

21
boa/src/builtins/array/array_iterator.rs

@ -2,18 +2,11 @@ use crate::{
builtins::{function::make_builtin_fn, iterable::create_iter_result_object, Array, JsValue}, builtins::{function::make_builtin_fn, iterable::create_iter_result_object, Array, JsValue},
gc::{Finalize, Trace}, gc::{Finalize, Trace},
object::{GcObject, ObjectData}, object::{GcObject, ObjectData},
property::PropertyDescriptor, property::{PropertyDescriptor, PropertyNameKind},
symbol::WellKnownSymbols, symbol::WellKnownSymbols,
BoaProfiler, Context, JsResult, BoaProfiler, Context, JsResult,
}; };
#[derive(Debug, Clone, Finalize, Trace)]
pub enum ArrayIterationKind {
Key,
Value,
KeyAndValue,
}
/// The Array Iterator object represents an iteration over an array. It implements the iterator protocol. /// The Array Iterator object represents an iteration over an array. It implements the iterator protocol.
/// ///
/// More information: /// More information:
@ -24,13 +17,13 @@ pub enum ArrayIterationKind {
pub struct ArrayIterator { pub struct ArrayIterator {
array: JsValue, array: JsValue,
next_index: u32, next_index: u32,
kind: ArrayIterationKind, kind: PropertyNameKind,
} }
impl ArrayIterator { impl ArrayIterator {
pub(crate) const NAME: &'static str = "ArrayIterator"; pub(crate) const NAME: &'static str = "ArrayIterator";
fn new(array: JsValue, kind: ArrayIterationKind) -> Self { fn new(array: JsValue, kind: PropertyNameKind) -> Self {
ArrayIterator { ArrayIterator {
array, array,
kind, kind,
@ -49,7 +42,7 @@ impl ArrayIterator {
pub(crate) fn create_array_iterator( pub(crate) fn create_array_iterator(
context: &Context, context: &Context,
array: JsValue, array: JsValue,
kind: ArrayIterationKind, kind: PropertyNameKind,
) -> JsValue { ) -> JsValue {
let array_iterator = JsValue::new_object(context); let array_iterator = JsValue::new_object(context);
array_iterator.set_data(ObjectData::ArrayIterator(Self::new(array, kind))); array_iterator.set_data(ObjectData::ArrayIterator(Self::new(array, kind)));
@ -96,14 +89,14 @@ impl ArrayIterator {
} }
array_iterator.next_index = index + 1; array_iterator.next_index = index + 1;
match array_iterator.kind { match array_iterator.kind {
ArrayIterationKind::Key => { PropertyNameKind::Key => {
Ok(create_iter_result_object(context, index.into(), false)) Ok(create_iter_result_object(context, index.into(), false))
} }
ArrayIterationKind::Value => { PropertyNameKind::Value => {
let element_value = array_iterator.array.get_field(index, context)?; let element_value = array_iterator.array.get_field(index, context)?;
Ok(create_iter_result_object(context, element_value, false)) Ok(create_iter_result_object(context, element_value, false))
} }
ArrayIterationKind::KeyAndValue => { PropertyNameKind::KeyAndValue => {
let element_value = array_iterator.array.get_field(index, context)?; let element_value = array_iterator.array.get_field(index, context)?;
let result = let result =
Array::create_array_from_list([index.into(), element_value], context); Array::create_array_from_list([index.into(), element_value], context);

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

@ -14,11 +14,11 @@ pub mod array_iterator;
mod tests; mod tests;
use crate::{ use crate::{
builtins::array::array_iterator::{ArrayIterationKind, ArrayIterator}, builtins::array::array_iterator::ArrayIterator,
builtins::BuiltIn, builtins::BuiltIn,
builtins::Number, builtins::Number,
object::{ConstructorBuilder, FunctionBuilder, GcObject, ObjectData, PROTOTYPE}, object::{ConstructorBuilder, FunctionBuilder, GcObject, ObjectData, PROTOTYPE},
property::{Attribute, PropertyDescriptor}, property::{Attribute, PropertyDescriptor, PropertyNameKind},
symbol::WellKnownSymbols, symbol::WellKnownSymbols,
value::{IntegerOrInfinity, JsValue}, value::{IntegerOrInfinity, JsValue},
BoaProfiler, Context, JsResult, JsString, BoaProfiler, Context, JsResult, JsString,
@ -2350,7 +2350,7 @@ impl Array {
Ok(ArrayIterator::create_array_iterator( Ok(ArrayIterator::create_array_iterator(
context, context,
this.clone(), this.clone(),
ArrayIterationKind::Value, PropertyNameKind::Value,
)) ))
} }
@ -2368,7 +2368,7 @@ impl Array {
Ok(ArrayIterator::create_array_iterator( Ok(ArrayIterator::create_array_iterator(
context, context,
this.clone(), this.clone(),
ArrayIterationKind::Key, PropertyNameKind::Key,
)) ))
} }
@ -2390,7 +2390,7 @@ impl Array {
Ok(ArrayIterator::create_array_iterator( Ok(ArrayIterator::create_array_iterator(
context, context,
this.clone(), this.clone(),
ArrayIterationKind::KeyAndValue, PropertyNameKind::KeyAndValue,
)) ))
} }

22
boa/src/builtins/map/map_iterator.rs

@ -1,21 +1,13 @@
use crate::{ use crate::{
builtins::{function::make_builtin_fn, iterable::create_iter_result_object, Array, JsValue}, builtins::{function::make_builtin_fn, iterable::create_iter_result_object, Array, JsValue},
object::{GcObject, ObjectData}, object::{GcObject, ObjectData},
property::PropertyDescriptor, property::{PropertyDescriptor, PropertyNameKind},
symbol::WellKnownSymbols, symbol::WellKnownSymbols,
BoaProfiler, Context, JsResult, BoaProfiler, Context, JsResult,
}; };
use gc::{Finalize, Trace}; use gc::{Finalize, Trace};
use super::{ordered_map::MapLock, Map}; use super::{ordered_map::MapLock, Map};
#[derive(Debug, Clone, Finalize, Trace)]
pub enum MapIterationKind {
Key,
Value,
KeyAndValue,
}
/// The Map Iterator object represents an iteration over a map. It implements the iterator protocol. /// The Map Iterator object represents an iteration over a map. It implements the iterator protocol.
/// ///
/// More information: /// More information:
@ -26,7 +18,7 @@ pub enum MapIterationKind {
pub struct MapIterator { pub struct MapIterator {
iterated_map: JsValue, iterated_map: JsValue,
map_next_index: usize, map_next_index: usize,
map_iteration_kind: MapIterationKind, map_iteration_kind: PropertyNameKind,
lock: MapLock, lock: MapLock,
} }
@ -34,7 +26,7 @@ impl MapIterator {
pub(crate) const NAME: &'static str = "MapIterator"; pub(crate) const NAME: &'static str = "MapIterator";
/// Constructs a new `MapIterator`, that will iterate over `map`, starting at index 0 /// Constructs a new `MapIterator`, that will iterate over `map`, starting at index 0
fn new(map: JsValue, kind: MapIterationKind, context: &mut Context) -> JsResult<Self> { fn new(map: JsValue, kind: PropertyNameKind, context: &mut Context) -> JsResult<Self> {
let lock = Map::lock(&map, context)?; let lock = Map::lock(&map, context)?;
Ok(MapIterator { Ok(MapIterator {
iterated_map: map, iterated_map: map,
@ -55,7 +47,7 @@ impl MapIterator {
pub(crate) fn create_map_iterator( pub(crate) fn create_map_iterator(
context: &mut Context, context: &mut Context,
map: JsValue, map: JsValue,
kind: MapIterationKind, kind: PropertyNameKind,
) -> JsResult<JsValue> { ) -> JsResult<JsValue> {
let map_iterator = JsValue::new_object(context); let map_iterator = JsValue::new_object(context);
map_iterator.set_data(ObjectData::MapIterator(Self::new(map, kind, context)?)); map_iterator.set_data(ObjectData::MapIterator(Self::new(map, kind, context)?));
@ -99,21 +91,21 @@ impl MapIterator {
map_iterator.map_next_index = index; map_iterator.map_next_index = index;
if let Some((key, value)) = e { if let Some((key, value)) = e {
match item_kind { match item_kind {
MapIterationKind::Key => { PropertyNameKind::Key => {
return Ok(create_iter_result_object( return Ok(create_iter_result_object(
context, context,
key.clone(), key.clone(),
false, false,
)); ));
} }
MapIterationKind::Value => { PropertyNameKind::Value => {
return Ok(create_iter_result_object( return Ok(create_iter_result_object(
context, context,
value.clone(), value.clone(),
false, false,
)); ));
} }
MapIterationKind::KeyAndValue => { PropertyNameKind::KeyAndValue => {
let result = Array::create_array_from_list( let result = Array::create_array_from_list(
[key.clone(), value.clone()], [key.clone(), value.clone()],
context, context,

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

@ -15,14 +15,14 @@
use crate::{ use crate::{
builtins::BuiltIn, builtins::BuiltIn,
object::{ConstructorBuilder, FunctionBuilder, ObjectData, PROTOTYPE}, object::{ConstructorBuilder, FunctionBuilder, ObjectData, PROTOTYPE},
property::{Attribute, PropertyDescriptor}, property::{Attribute, PropertyDescriptor, PropertyNameKind},
symbol::WellKnownSymbols, symbol::WellKnownSymbols,
BoaProfiler, Context, JsResult, JsValue, BoaProfiler, Context, JsResult, JsValue,
}; };
use ordered_map::OrderedMap; use ordered_map::OrderedMap;
pub mod map_iterator; pub mod map_iterator;
use map_iterator::{MapIterationKind, MapIterator}; use map_iterator::MapIterator;
use self::ordered_map::MapLock; use self::ordered_map::MapLock;
@ -202,7 +202,7 @@ impl Map {
_: &[JsValue], _: &[JsValue],
context: &mut Context, context: &mut Context,
) -> JsResult<JsValue> { ) -> JsResult<JsValue> {
MapIterator::create_map_iterator(context, this.clone(), MapIterationKind::KeyAndValue) MapIterator::create_map_iterator(context, this.clone(), PropertyNameKind::KeyAndValue)
} }
/// `Map.prototype.keys()` /// `Map.prototype.keys()`
@ -216,7 +216,7 @@ impl Map {
/// [spec]: https://tc39.es/ecma262/#sec-map.prototype.keys /// [spec]: https://tc39.es/ecma262/#sec-map.prototype.keys
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys
pub(crate) fn keys(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> { pub(crate) fn keys(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
MapIterator::create_map_iterator(context, this.clone(), MapIterationKind::Key) MapIterator::create_map_iterator(context, this.clone(), PropertyNameKind::Key)
} }
/// Helper function to set the size property. /// Helper function to set the size property.
@ -470,7 +470,7 @@ impl Map {
_: &[JsValue], _: &[JsValue],
context: &mut Context, context: &mut Context,
) -> JsResult<JsValue> { ) -> JsResult<JsValue> {
MapIterator::create_map_iterator(context, this.clone(), MapIterationKind::Value) MapIterator::create_map_iterator(context, this.clone(), PropertyNameKind::Value)
} }
/// Helper function to get a key-value pair from an array. /// Helper function to get a key-value pair from an array.

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

@ -13,14 +13,14 @@
use crate::{ use crate::{
builtins::{iterable::get_iterator, BuiltIn}, builtins::{iterable::get_iterator, BuiltIn},
object::{ConstructorBuilder, FunctionBuilder, ObjectData, PROTOTYPE}, object::{ConstructorBuilder, FunctionBuilder, ObjectData, PROTOTYPE},
property::Attribute, property::{Attribute, PropertyNameKind},
symbol::WellKnownSymbols, symbol::WellKnownSymbols,
BoaProfiler, Context, JsResult, JsValue, BoaProfiler, Context, JsResult, JsValue,
}; };
use ordered_set::OrderedSet; use ordered_set::OrderedSet;
pub mod set_iterator; pub mod set_iterator;
use set_iterator::{SetIterationKind, SetIterator}; use set_iterator::SetIterator;
pub mod ordered_set; pub mod ordered_set;
#[cfg(test)] #[cfg(test)]
@ -308,7 +308,7 @@ impl Set {
Ok(SetIterator::create_set_iterator( Ok(SetIterator::create_set_iterator(
context, context,
this.clone(), this.clone(),
SetIterationKind::KeyAndValue, PropertyNameKind::KeyAndValue,
)) ))
} }
@ -422,7 +422,7 @@ impl Set {
Ok(SetIterator::create_set_iterator( Ok(SetIterator::create_set_iterator(
context, context,
this.clone(), this.clone(),
SetIterationKind::Value, PropertyNameKind::Value,
)) ))
} }

21
boa/src/builtins/set/set_iterator.rs

@ -4,18 +4,12 @@ use crate::{
builtins::Array, builtins::Array,
builtins::JsValue, builtins::JsValue,
object::{GcObject, ObjectData}, object::{GcObject, ObjectData},
property::PropertyDescriptor, property::{PropertyDescriptor, PropertyNameKind},
symbol::WellKnownSymbols, symbol::WellKnownSymbols,
BoaProfiler, Context, JsResult, BoaProfiler, Context, JsResult,
}; };
use gc::{Finalize, Trace}; use gc::{Finalize, Trace};
#[derive(Debug, Clone, Finalize, Trace)]
pub enum SetIterationKind {
Value,
KeyAndValue,
}
/// The Set Iterator object represents an iteration over a set. It implements the iterator protocol. /// The Set Iterator object represents an iteration over a set. It implements the iterator protocol.
/// ///
/// More information: /// More information:
@ -26,14 +20,14 @@ pub enum SetIterationKind {
pub struct SetIterator { pub struct SetIterator {
iterated_set: JsValue, iterated_set: JsValue,
next_index: usize, next_index: usize,
iteration_kind: SetIterationKind, iteration_kind: PropertyNameKind,
} }
impl SetIterator { impl SetIterator {
pub(crate) const NAME: &'static str = "SetIterator"; pub(crate) const NAME: &'static str = "SetIterator";
/// Constructs a new `SetIterator`, that will iterate over `set`, starting at index 0 /// Constructs a new `SetIterator`, that will iterate over `set`, starting at index 0
fn new(set: JsValue, kind: SetIterationKind) -> Self { fn new(set: JsValue, kind: PropertyNameKind) -> Self {
SetIterator { SetIterator {
iterated_set: set, iterated_set: set,
next_index: 0, next_index: 0,
@ -52,7 +46,7 @@ impl SetIterator {
pub(crate) fn create_set_iterator( pub(crate) fn create_set_iterator(
context: &Context, context: &Context,
set: JsValue, set: JsValue,
kind: SetIterationKind, kind: PropertyNameKind,
) -> JsValue { ) -> JsValue {
let set_iterator = JsValue::new_object(context); let set_iterator = JsValue::new_object(context);
set_iterator.set_data(ObjectData::SetIterator(Self::new(set, kind))); set_iterator.set_data(ObjectData::SetIterator(Self::new(set, kind)));
@ -96,14 +90,14 @@ impl SetIterator {
set_iterator.next_index = index; set_iterator.next_index = index;
if let Some(value) = e { if let Some(value) = e {
match item_kind { match item_kind {
SetIterationKind::Value => { PropertyNameKind::Value => {
return Ok(create_iter_result_object( return Ok(create_iter_result_object(
context, context,
value.clone(), value.clone(),
false, false,
)); ));
} }
SetIterationKind::KeyAndValue => { PropertyNameKind::KeyAndValue => {
let result = Array::create_array_from_list( let result = Array::create_array_from_list(
[value.clone(), value.clone()], [value.clone(), value.clone()],
context, context,
@ -114,6 +108,9 @@ impl SetIterator {
false, false,
)); ));
} }
PropertyNameKind::Key => {
panic!("tried to collect only keys of Set")
}
} }
} }
} }

7
boa/src/property/mod.rs

@ -22,6 +22,7 @@ use std::{convert::TryFrom, fmt};
mod attribute; mod attribute;
pub use attribute::Attribute; pub use attribute::Attribute;
use gc::unsafe_empty_trace;
/// This represents a JavaScript Property AKA The Property Descriptor. /// This represents a JavaScript Property AKA The Property Descriptor.
/// ///
@ -663,9 +664,13 @@ impl PartialEq<&str> for PropertyKey {
} }
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy, Finalize)]
pub(crate) enum PropertyNameKind { pub(crate) enum PropertyNameKind {
Key, Key,
Value, Value,
KeyAndValue, KeyAndValue,
} }
unsafe impl Trace for PropertyNameKind {
unsafe_empty_trace!();
}

Loading…
Cancel
Save