1 | /* |
2 | * Copyright (C) 2013, 2016 Apple, Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #include "IterationKind.h" |
29 | #include "JSMap.h" |
30 | #include "JSObject.h" |
31 | |
32 | namespace JSC { |
33 | |
34 | // Now, it is only used for serialization. |
35 | class JSMapIterator final : public JSCell { |
36 | typedef HashMapBucket<HashMapBucketDataKeyValue> HashMapBucketType; |
37 | public: |
38 | using Base = JSCell; |
39 | |
40 | DECLARE_EXPORT_INFO; |
41 | |
42 | static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
43 | { |
44 | return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info()); |
45 | } |
46 | |
47 | static JSMapIterator* create(VM& vm, Structure* structure, JSMap* iteratedObject, IterationKind kind) |
48 | { |
49 | JSMapIterator* instance = new (NotNull, allocateCell<JSMapIterator>(vm.heap)) JSMapIterator(vm, structure, iteratedObject, kind); |
50 | instance->finishCreation(vm, iteratedObject); |
51 | return instance; |
52 | } |
53 | |
54 | ALWAYS_INLINE HashMapBucketType* advanceIter(JSGlobalObject* globalObject) |
55 | { |
56 | HashMapBucketType* prev = m_iter.get(); |
57 | if (!prev) |
58 | return nullptr; |
59 | VM& vm = getVM(globalObject); |
60 | HashMapBucketType* bucket = m_iter->next(); |
61 | while (bucket && bucket->deleted()) |
62 | bucket = bucket->next(); |
63 | if (!bucket) { |
64 | setIterator(vm, nullptr); |
65 | return nullptr; |
66 | } |
67 | setIterator(vm, bucket); // We keep m_iter on the last value since the first thing we do in this function is call next(). |
68 | return bucket; |
69 | } |
70 | bool next(JSGlobalObject* globalObject, JSValue& value) |
71 | { |
72 | HashMapBucketType* bucket = advanceIter(globalObject); |
73 | if (!bucket) |
74 | return false; |
75 | |
76 | if (m_kind == IterateValue) |
77 | value = bucket->value(); |
78 | else if (m_kind == IterateKey) |
79 | value = bucket->key(); |
80 | else |
81 | value = createPair(globalObject, bucket->key(), bucket->value()); |
82 | return true; |
83 | } |
84 | |
85 | bool nextKeyValue(JSGlobalObject* globalObject, JSValue& key, JSValue& value) |
86 | { |
87 | HashMapBucketType* bucket = advanceIter(globalObject); |
88 | if (!bucket) |
89 | return false; |
90 | |
91 | key = bucket->key(); |
92 | value = bucket->value(); |
93 | return true; |
94 | } |
95 | |
96 | IterationKind kind() const { return m_kind; } |
97 | JSValue iteratedValue() const { return m_map.get(); } |
98 | |
99 | private: |
100 | JSMapIterator(VM& vm, Structure* structure, JSMap*, IterationKind kind) |
101 | : Base(vm, structure) |
102 | , m_kind(kind) |
103 | { } |
104 | |
105 | void setIterator(VM& vm, HashMapBucketType* bucket) |
106 | { |
107 | m_iter.setMayBeNull(vm, this, bucket); |
108 | } |
109 | |
110 | JS_EXPORT_PRIVATE void finishCreation(VM&, JSMap*); |
111 | JSValue createPair(JSGlobalObject*, JSValue, JSValue); |
112 | static void visitChildren(JSCell*, SlotVisitor&); |
113 | |
114 | WriteBarrier<JSMap> m_map; |
115 | WriteBarrier<HashMapBucketType> m_iter; |
116 | IterationKind m_kind; |
117 | }; |
118 | STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSMapIterator); |
119 | |
120 | } // namespace JSC |
121 | |