]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSArrayIterator.cpp
c6220f5f9916cd1767c5f72496b1c1d7abe59e44
[apple/javascriptcore.git] / runtime / JSArrayIterator.cpp
1 /*
2 * Copyright (C) 2013 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 #include "config.h"
27 #include "JSArrayIterator.h"
28
29 #include "JSCJSValueInlines.h"
30 #include "JSCellInlines.h"
31 #include "SlotVisitorInlines.h"
32 #include "StructureInlines.h"
33
34 namespace JSC {
35
36 const ClassInfo JSArrayIterator::s_info = { "ArrayIterator", &Base::s_info, 0, 0, CREATE_METHOD_TABLE(JSArrayIterator) };
37
38 static EncodedJSValue JSC_HOST_CALL arrayIteratorNextKey(ExecState*);
39 static EncodedJSValue JSC_HOST_CALL arrayIteratorNextValue(ExecState*);
40 static EncodedJSValue JSC_HOST_CALL arrayIteratorNextGeneric(ExecState*);
41
42 void JSArrayIterator::finishCreation(VM& vm, JSGlobalObject* globalObject, ArrayIterationKind kind, JSObject* iteratedObject)
43 {
44 Base::finishCreation(vm);
45 ASSERT(inherits(info()));
46 m_iterationKind = kind;
47 m_iteratedObject.set(vm, this, iteratedObject);
48 switch (kind) {
49 case ArrayIterateKey:
50 JSC_NATIVE_INTRINSIC_FUNCTION(vm.propertyNames->iteratorNextPrivateName, arrayIteratorNextKey, DontEnum, 0, ArrayIteratorNextKeyIntrinsic);
51 break;
52 case ArrayIterateValue:
53 JSC_NATIVE_INTRINSIC_FUNCTION(vm.propertyNames->iteratorNextPrivateName, arrayIteratorNextValue, DontEnum, 0, ArrayIteratorNextValueIntrinsic);
54 break;
55 default:
56 JSC_NATIVE_INTRINSIC_FUNCTION(vm.propertyNames->iteratorNextPrivateName, arrayIteratorNextGeneric, DontEnum, 0, ArrayIteratorNextGenericIntrinsic);
57 break;
58 }
59
60 }
61
62
63 void JSArrayIterator::visitChildren(JSCell* cell, SlotVisitor& visitor)
64 {
65 JSArrayIterator* thisObject = jsCast<JSArrayIterator*>(cell);
66 ASSERT_GC_OBJECT_INHERITS(thisObject, info());
67 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
68 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
69
70 Base::visitChildren(thisObject, visitor);
71 visitor.append(&thisObject->m_iteratedObject);
72
73 }
74
75 static EncodedJSValue createIteratorResult(CallFrame* callFrame, ArrayIterationKind kind, size_t index, JSValue result, bool done)
76 {
77 if (done)
78 return JSValue::encode(callFrame->vm().iterationTerminator.get());
79
80 switch (kind & ~ArrayIterateSparseTag) {
81 case ArrayIterateKey:
82 return JSValue::encode(jsNumber(index));
83
84 case ArrayIterateValue:
85 return JSValue::encode(result);
86
87 case ArrayIterateKeyValue: {
88 MarkedArgumentBuffer args;
89 args.append(jsNumber(index));
90 args.append(result);
91 JSGlobalObject* globalObject = callFrame->callee()->globalObject();
92 return JSValue::encode(constructArray(callFrame, 0, globalObject, args));
93
94 }
95 default:
96 RELEASE_ASSERT_NOT_REACHED();
97 }
98 return JSValue::encode(JSValue());
99 }
100
101 static inline EncodedJSValue JSC_HOST_CALL arrayIteratorNext(CallFrame* callFrame)
102 {
103 JSArrayIterator* iterator = jsDynamicCast<JSArrayIterator*>(callFrame->thisValue());
104 if (!iterator) {
105 ASSERT_NOT_REACHED();
106 return JSValue::encode(throwTypeError(callFrame, ASCIILiteral("Cannot call ArrayIterator.next() on a non-ArrayIterator object")));
107 }
108 JSObject* iteratedObject = iterator->iteratedObject();
109 size_t index = iterator->nextIndex();
110 ArrayIterationKind kind = iterator->iterationKind();
111 JSValue jsLength = JSValue(iteratedObject).get(callFrame, callFrame->propertyNames().length);
112 if (callFrame->hadException())
113 return JSValue::encode(jsNull());
114
115 size_t length = jsLength.toUInt32(callFrame);
116 if (callFrame->hadException())
117 return JSValue::encode(jsNull());
118
119 if (index >= length) {
120 iterator->finish();
121 return createIteratorResult(callFrame, kind, index, jsUndefined(), true);
122 }
123 if (JSValue result = iteratedObject->tryGetIndexQuickly(index)) {
124 iterator->setNextIndex(index + 1);
125 return createIteratorResult(callFrame, kind, index, result, false);
126 }
127
128 JSValue result = jsUndefined();
129 PropertySlot slot(iteratedObject);
130 if (kind > ArrayIterateSparseTag) {
131 // We assume that the indexed property will be an own property so cache the getOwnProperty
132 // method locally
133 auto getOwnPropertySlotByIndex = iteratedObject->methodTable()->getOwnPropertySlotByIndex;
134 while (index < length) {
135 if (getOwnPropertySlotByIndex(iteratedObject, callFrame, index, slot)) {
136 result = slot.getValue(callFrame, index);
137 break;
138 }
139 if (iteratedObject->getPropertySlot(callFrame, index, slot)) {
140 result = slot.getValue(callFrame, index);
141 break;
142 }
143 index++;
144 }
145 } else if (iteratedObject->getPropertySlot(callFrame, index, slot))
146 result = slot.getValue(callFrame, index);
147
148 if (index == length)
149 iterator->finish();
150 else
151 iterator->setNextIndex(index + 1);
152 return createIteratorResult(callFrame, kind, index, jsUndefined(), index == length);
153 }
154
155 EncodedJSValue JSC_HOST_CALL arrayIteratorNextKey(CallFrame* callFrame)
156 {
157 return arrayIteratorNext(callFrame);
158 }
159
160 EncodedJSValue JSC_HOST_CALL arrayIteratorNextValue(CallFrame* callFrame)
161 {
162 return arrayIteratorNext(callFrame);
163 }
164
165 EncodedJSValue JSC_HOST_CALL arrayIteratorNextGeneric(CallFrame* callFrame)
166 {
167 return arrayIteratorNext(callFrame);
168 }
169
170 }