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},
gc::{Finalize, Trace},
object::{GcObject, ObjectData},
property::PropertyDescriptor,
property::{PropertyDescriptor, PropertyNameKind},
symbol::WellKnownSymbols,
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.
///
/// More information:
@ -24,13 +17,13 @@ pub enum ArrayIterationKind {
pub struct ArrayIterator {
array: JsValue,
next_index: u32,
kind: ArrayIterationKind,
kind: PropertyNameKind,
}
impl ArrayIterator {
pub(crate) const NAME: &'static str = "ArrayIterator";
fn new(array: JsValue, kind: ArrayIterationKind) -> Self {
fn new(array: JsValue, kind: PropertyNameKind) -> Self {
ArrayIterator {
array,
kind,
@ -49,7 +42,7 @@ impl ArrayIterator {
pub(crate) fn create_array_iterator(
context: &Context,
array: JsValue,
kind: ArrayIterationKind,
kind: PropertyNameKind,
) -> JsValue {
let array_iterator = JsValue::new_object(context);
array_iterator.set_data(ObjectData::ArrayIterator(Self::new(array, kind)));
@ -96,14 +89,14 @@ impl ArrayIterator {
}
array_iterator.next_index = index + 1;
match array_iterator.kind {
ArrayIterationKind::Key => {
PropertyNameKind::Key => {
Ok(create_iter_result_object(context, index.into(), false))
}
ArrayIterationKind::Value => {
PropertyNameKind::Value => {
let element_value = array_iterator.array.get_field(index, context)?;
Ok(create_iter_result_object(context, element_value, false))
}
ArrayIterationKind::KeyAndValue => {
PropertyNameKind::KeyAndValue => {
let element_value = array_iterator.array.get_field(index, context)?;
let result =
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;
use crate::{
builtins::array::array_iterator::{ArrayIterationKind, ArrayIterator},
builtins::array::array_iterator::ArrayIterator,
builtins::BuiltIn,
builtins::Number,
object::{ConstructorBuilder, FunctionBuilder, GcObject, ObjectData, PROTOTYPE},
property::{Attribute, PropertyDescriptor},
property::{Attribute, PropertyDescriptor, PropertyNameKind},
symbol::WellKnownSymbols,
value::{IntegerOrInfinity, JsValue},
BoaProfiler, Context, JsResult, JsString,
@ -2350,7 +2350,7 @@ impl Array {
Ok(ArrayIterator::create_array_iterator(
context,
this.clone(),
ArrayIterationKind::Value,
PropertyNameKind::Value,
))
}
@ -2368,7 +2368,7 @@ impl Array {
Ok(ArrayIterator::create_array_iterator(
context,
this.clone(),
ArrayIterationKind::Key,
PropertyNameKind::Key,
))
}
@ -2390,7 +2390,7 @@ impl Array {
Ok(ArrayIterator::create_array_iterator(
context,
this.clone(),
ArrayIterationKind::KeyAndValue,
PropertyNameKind::KeyAndValue,
))
}

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

@ -1,21 +1,13 @@
use crate::{
builtins::{function::make_builtin_fn, iterable::create_iter_result_object, Array, JsValue},
object::{GcObject, ObjectData},
property::PropertyDescriptor,
property::{PropertyDescriptor, PropertyNameKind},
symbol::WellKnownSymbols,
BoaProfiler, Context, JsResult,
};
use gc::{Finalize, Trace};
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.
///
/// More information:
@ -26,7 +18,7 @@ pub enum MapIterationKind {
pub struct MapIterator {
iterated_map: JsValue,
map_next_index: usize,
map_iteration_kind: MapIterationKind,
map_iteration_kind: PropertyNameKind,
lock: MapLock,
}
@ -34,7 +26,7 @@ impl MapIterator {
pub(crate) const NAME: &'static str = "MapIterator";
/// 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)?;
Ok(MapIterator {
iterated_map: map,
@ -55,7 +47,7 @@ impl MapIterator {
pub(crate) fn create_map_iterator(
context: &mut Context,
map: JsValue,
kind: MapIterationKind,
kind: PropertyNameKind,
) -> JsResult<JsValue> {
let map_iterator = JsValue::new_object(context);
map_iterator.set_data(ObjectData::MapIterator(Self::new(map, kind, context)?));
@ -99,21 +91,21 @@ impl MapIterator {
map_iterator.map_next_index = index;
if let Some((key, value)) = e {
match item_kind {
MapIterationKind::Key => {
PropertyNameKind::Key => {
return Ok(create_iter_result_object(
context,
key.clone(),
false,
));
}
MapIterationKind::Value => {
PropertyNameKind::Value => {
return Ok(create_iter_result_object(
context,
value.clone(),
false,
));
}
MapIterationKind::KeyAndValue => {
PropertyNameKind::KeyAndValue => {
let result = Array::create_array_from_list(
[key.clone(), value.clone()],
context,

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

@ -15,14 +15,14 @@
use crate::{
builtins::BuiltIn,
object::{ConstructorBuilder, FunctionBuilder, ObjectData, PROTOTYPE},
property::{Attribute, PropertyDescriptor},
property::{Attribute, PropertyDescriptor, PropertyNameKind},
symbol::WellKnownSymbols,
BoaProfiler, Context, JsResult, JsValue,
};
use ordered_map::OrderedMap;
pub mod map_iterator;
use map_iterator::{MapIterationKind, MapIterator};
use map_iterator::MapIterator;
use self::ordered_map::MapLock;
@ -202,7 +202,7 @@ impl Map {
_: &[JsValue],
context: &mut Context,
) -> JsResult<JsValue> {
MapIterator::create_map_iterator(context, this.clone(), MapIterationKind::KeyAndValue)
MapIterator::create_map_iterator(context, this.clone(), PropertyNameKind::KeyAndValue)
}
/// `Map.prototype.keys()`
@ -216,7 +216,7 @@ impl Map {
/// [spec]: https://tc39.es/ecma262/#sec-map.prototype.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> {
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.
@ -470,7 +470,7 @@ impl Map {
_: &[JsValue],
context: &mut Context,
) -> 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.

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

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

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

@ -4,18 +4,12 @@ use crate::{
builtins::Array,
builtins::JsValue,
object::{GcObject, ObjectData},
property::PropertyDescriptor,
property::{PropertyDescriptor, PropertyNameKind},
symbol::WellKnownSymbols,
BoaProfiler, Context, JsResult,
};
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.
///
/// More information:
@ -26,14 +20,14 @@ pub enum SetIterationKind {
pub struct SetIterator {
iterated_set: JsValue,
next_index: usize,
iteration_kind: SetIterationKind,
iteration_kind: PropertyNameKind,
}
impl SetIterator {
pub(crate) const NAME: &'static str = "SetIterator";
/// 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 {
iterated_set: set,
next_index: 0,
@ -52,7 +46,7 @@ impl SetIterator {
pub(crate) fn create_set_iterator(
context: &Context,
set: JsValue,
kind: SetIterationKind,
kind: PropertyNameKind,
) -> JsValue {
let set_iterator = JsValue::new_object(context);
set_iterator.set_data(ObjectData::SetIterator(Self::new(set, kind)));
@ -96,14 +90,14 @@ impl SetIterator {
set_iterator.next_index = index;
if let Some(value) = e {
match item_kind {
SetIterationKind::Value => {
PropertyNameKind::Value => {
return Ok(create_iter_result_object(
context,
value.clone(),
false,
));
}
SetIterationKind::KeyAndValue => {
PropertyNameKind::KeyAndValue => {
let result = Array::create_array_from_list(
[value.clone(), value.clone()],
context,
@ -114,6 +108,9 @@ impl SetIterator {
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;
pub use attribute::Attribute;
use gc::unsafe_empty_trace;
/// 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 {
Key,
Value,
KeyAndValue,
}
unsafe impl Trace for PropertyNameKind {
unsafe_empty_trace!();
}

Loading…
Cancel
Save