1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use crate::{
builtins::{function::make_builtin_fn, iterable::create_iter_result_object, Array, Value},
object::{GcObject, ObjectData},
property::{Attribute, DataDescriptor},
BoaProfiler, Context, Result,
};
use gc::{Finalize, Trace};
#[derive(Debug, Clone, Finalize, Trace)]
pub enum MapIterationKind {
Key,
Value,
KeyAndValue,
}
#[derive(Debug, Clone, Finalize, Trace)]
pub struct MapIterator {
iterated_map: Value,
map_next_index: usize,
map_iteration_kind: MapIterationKind,
}
impl MapIterator {
pub(crate) const NAME: &'static str = "MapIterator";
fn new(map: Value, kind: MapIterationKind) -> Self {
MapIterator {
iterated_map: map,
map_next_index: 0,
map_iteration_kind: kind,
}
}
pub(crate) fn create_map_iterator(
context: &Context,
map: Value,
kind: MapIterationKind,
) -> Result<Value> {
let map_iterator = Value::new_object(context);
map_iterator.set_data(ObjectData::MapIterator(Self::new(map, kind)));
map_iterator
.as_object()
.expect("map iterator object")
.set_prototype_instance(context.iterator_prototypes().map_iterator().into());
Ok(map_iterator)
}
pub(crate) fn next(this: &Value, _: &[Value], context: &mut Context) -> Result<Value> {
if let Value::Object(ref object) = this {
let mut object = object.borrow_mut();
if let Some(map_iterator) = object.as_map_iterator_mut() {
let m = &map_iterator.iterated_map;
let mut index = map_iterator.map_next_index;
let item_kind = &map_iterator.map_iteration_kind;
if map_iterator.iterated_map.is_undefined() {
return Ok(create_iter_result_object(context, Value::undefined(), true));
}
if let Value::Object(ref object) = m {
if let Some(entries) = object.borrow().as_map_ref() {
let num_entries = entries.len();
while index < num_entries {
let e = entries.get_index(index);
index += 1;
map_iterator.map_next_index = index;
if let Some((key, value)) = e {
match item_kind {
MapIterationKind::Key => {
return Ok(create_iter_result_object(
context,
key.clone(),
false,
));
}
MapIterationKind::Value => {
return Ok(create_iter_result_object(
context,
value.clone(),
false,
));
}
MapIterationKind::KeyAndValue => {
let result = Array::construct_array(
&Array::new_array(context)?,
&[key.clone(), value.clone()],
context,
)?;
return Ok(create_iter_result_object(
context, result, false,
));
}
}
}
}
} else {
return Err(context.construct_type_error("'this' is not a Map"));
}
} else {
return Err(context.construct_type_error("'this' is not a Map"));
}
map_iterator.iterated_map = Value::undefined();
Ok(create_iter_result_object(context, Value::undefined(), true))
} else {
context.throw_type_error("`this` is not an MapIterator")
}
} else {
context.throw_type_error("`this` is not an MapIterator")
}
}
pub(crate) fn create_prototype(context: &mut Context, iterator_prototype: Value) -> GcObject {
let _timer = BoaProfiler::global().start_event(Self::NAME, "init");
let mut map_iterator = context.construct_object();
make_builtin_fn(Self::next, "next", &map_iterator, 0, context);
map_iterator.set_prototype_instance(iterator_prototype);
let to_string_tag = context.well_known_symbols().to_string_tag_symbol();
let to_string_tag_property = DataDescriptor::new("Map Iterator", Attribute::CONFIGURABLE);
map_iterator.insert(to_string_tag, to_string_tag_property);
map_iterator
}
}