| 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 "JSObject.h" |
| 30 | #include "JSSet.h" |
| 31 | |
| 32 | namespace JSC { |
| 33 | |
| 34 | // Now, it is only used for serialization. |
| 35 | class JSSetIterator final : public JSCell { |
| 36 | typedef HashMapBucket<HashMapBucketDataKey> 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 JSSetIterator* create(VM& vm, Structure* structure, JSSet* iteratedObject, IterationKind kind) |
| 48 | { |
| 49 | JSSetIterator* instance = new (NotNull, allocateCell<JSSetIterator>(vm.heap)) JSSetIterator(vm, structure, iteratedObject, kind); |
| 50 | instance->finishCreation(vm, iteratedObject); |
| 51 | return instance; |
| 52 | } |
| 53 | |
| 54 | ALWAYS_INLINE HashMapBucketType* advanceIter(ExecState* exec) |
| 55 | { |
| 56 | HashMapBucketType* prev = m_iter.get(); |
| 57 | if (!prev) |
| 58 | return nullptr; |
| 59 | VM& vm = exec->vm(); |
| 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 | |
| 71 | bool next(ExecState* exec, JSValue& value) |
| 72 | { |
| 73 | HashMapBucketType* bucket = advanceIter(exec); |
| 74 | if (!bucket) |
| 75 | return false; |
| 76 | |
| 77 | if (m_kind == IterateValue || m_kind == IterateKey) |
| 78 | value = bucket->key(); |
| 79 | else |
| 80 | value = createPair(exec, bucket->key(), bucket->key()); |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | IterationKind kind() const { return m_kind; } |
| 85 | JSValue iteratedValue() const { return m_set.get(); } |
| 86 | |
| 87 | private: |
| 88 | JSSetIterator(VM& vm, Structure* structure, JSSet*, IterationKind kind) |
| 89 | : Base(vm, structure) |
| 90 | , m_kind(kind) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | void setIterator(VM& vm, HashMapBucketType* bucket) |
| 95 | { |
| 96 | m_iter.setMayBeNull(vm, this, bucket); |
| 97 | } |
| 98 | |
| 99 | JS_EXPORT_PRIVATE void finishCreation(VM&, JSSet*); |
| 100 | JS_EXPORT_PRIVATE JSValue createPair(CallFrame*, JSValue, JSValue); |
| 101 | static void visitChildren(JSCell*, SlotVisitor&); |
| 102 | |
| 103 | WriteBarrier<JSSet> m_set; |
| 104 | WriteBarrier<HashMapBucketType> m_iter; |
| 105 | IterationKind m_kind; |
| 106 | }; |
| 107 | STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSSetIterator); |
| 108 | |
| 109 | } // namespace JSC |
| 110 | |