logo
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
155
156
157
158
159
160
161
use crate::{
    builtins::function::make_builtin_fn,
    builtins::iterable::create_iter_result_object,
    builtins::Array,
    builtins::JsValue,
    object::{JsObject, ObjectData},
    property::{PropertyDescriptor, PropertyNameKind},
    symbol::WellKnownSymbols,
    BoaProfiler, Context, JsResult,
};
use gc::{Finalize, Trace};

/// The Set Iterator object represents an iteration over a set. It implements the iterator protocol.
///
/// More information:
///  - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#sec-set-iterator-objects
#[derive(Debug, Clone, Finalize, Trace)]
pub struct SetIterator {
    iterated_set: JsValue,
    next_index: usize,
    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: PropertyNameKind) -> Self {
        SetIterator {
            iterated_set: set,
            next_index: 0,
            iteration_kind: kind,
        }
    }

    /// Abstract operation CreateSetIterator( set, kind )
    ///
    /// Creates a new iterator over the given set.
    ///
    /// More information:
    ///  - [ECMA reference][spec]
    ///
    /// [spec]: https://www.ecma-international.org/ecma-262/11.0/index.html#sec-createsetiterator
    pub(crate) fn create_set_iterator(
        set: JsValue,
        kind: PropertyNameKind,
        context: &Context,
    ) -> JsValue {
        let set_iterator = JsValue::new_object(context);
        set_iterator.set_data(ObjectData::set_iterator(Self::new(set, kind)));
        set_iterator
            .as_object()
            .expect("set iterator object")
            .set_prototype_instance(context.iterator_prototypes().set_iterator().into());
        set_iterator
    }

    /// %SetIteratorPrototype%.next( )
    ///
    /// Advances the iterator and gets the next result in the set.
    ///
    /// More information:
    ///  - [ECMA reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-%setiteratorprototype%.next
    pub(crate) fn next(this: &JsValue, _: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
        if let JsValue::Object(ref object) = this {
            let mut object = object.borrow_mut();
            if let Some(set_iterator) = object.as_set_iterator_mut() {
                let m = &set_iterator.iterated_set;
                let mut index = set_iterator.next_index;
                let item_kind = &set_iterator.iteration_kind;

                if set_iterator.iterated_set.is_undefined() {
                    return Ok(create_iter_result_object(
                        JsValue::undefined(),
                        true,
                        context,
                    ));
                }

                if let JsValue::Object(ref object) = m {
                    if let Some(entries) = object.borrow().as_set_ref() {
                        let num_entries = entries.size();
                        while index < num_entries {
                            let e = entries.get_index(index);
                            index += 1;
                            set_iterator.next_index = index;
                            if let Some(value) = e {
                                match item_kind {
                                    PropertyNameKind::Value => {
                                        return Ok(create_iter_result_object(
                                            value.clone(),
                                            false,
                                            context,
                                        ));
                                    }
                                    PropertyNameKind::KeyAndValue => {
                                        let result = Array::create_array_from_list(
                                            [value.clone(), value.clone()],
                                            context,
                                        );
                                        return Ok(create_iter_result_object(
                                            result.into(),
                                            false,
                                            context,
                                        ));
                                    }
                                    PropertyNameKind::Key => {
                                        panic!("tried to collect only keys of Set")
                                    }
                                }
                            }
                        }
                    } else {
                        return Err(context.construct_type_error("'this' is not a Set"));
                    }
                } else {
                    return Err(context.construct_type_error("'this' is not a Set"));
                }

                set_iterator.iterated_set = JsValue::undefined();
                Ok(create_iter_result_object(
                    JsValue::undefined(),
                    true,
                    context,
                ))
            } else {
                context.throw_type_error("`this` is not an SetIterator")
            }
        } else {
            context.throw_type_error("`this` is not an SetIterator")
        }
    }

    /// Create the %SetIteratorPrototype% object
    ///
    /// More information:
    ///  - [ECMA reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#sec-%setiteratorprototype%-object
    pub(crate) fn create_prototype(iterator_prototype: JsValue, context: &mut Context) -> JsObject {
        let _timer = BoaProfiler::global().start_event(Self::NAME, "init");

        // Create prototype
        let set_iterator = context.construct_object();
        make_builtin_fn(Self::next, "next", &set_iterator, 0, context);
        set_iterator.set_prototype_instance(iterator_prototype);

        let to_string_tag = WellKnownSymbols::to_string_tag();
        let to_string_tag_property = PropertyDescriptor::builder()
            .value("Set Iterator")
            .writable(false)
            .enumerable(false)
            .configurable(true);
        set_iterator.insert(to_string_tag, to_string_tag_property);
        set_iterator
    }
}