1/*
2 * Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>.
3 * Copyright (C) 2016 Apple Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include "JSCJSValue.h"
30#include "JSObject.h"
31#include "ThrowScope.h"
32
33namespace JSC {
34
35struct IterationRecord {
36 JSValue iterator;
37 JSValue nextMethod;
38};
39
40JSValue iteratorNext(ExecState*, IterationRecord, JSValue argument = JSValue());
41JS_EXPORT_PRIVATE JSValue iteratorValue(ExecState*, JSValue iterResult);
42bool iteratorComplete(ExecState*, JSValue iterResult);
43JS_EXPORT_PRIVATE JSValue iteratorStep(ExecState*, IterationRecord);
44JS_EXPORT_PRIVATE void iteratorClose(ExecState*, IterationRecord);
45JS_EXPORT_PRIVATE JSObject* createIteratorResultObject(ExecState*, JSValue, bool done);
46
47Structure* createIteratorResultObjectStructure(VM&, JSGlobalObject&);
48
49JS_EXPORT_PRIVATE JSValue iteratorMethod(ExecState&, JSObject*);
50JS_EXPORT_PRIVATE IterationRecord iteratorForIterable(ExecState&, JSObject*, JSValue iteratorMethod);
51JS_EXPORT_PRIVATE IterationRecord iteratorForIterable(ExecState*, JSValue iterable);
52
53JS_EXPORT_PRIVATE JSValue iteratorMethod(ExecState&, JSObject*);
54JS_EXPORT_PRIVATE bool hasIteratorMethod(ExecState&, JSValue);
55
56template<typename CallBackType>
57void forEachInIterable(ExecState* exec, JSValue iterable, const CallBackType& callback)
58{
59 auto& vm = exec->vm();
60 auto scope = DECLARE_THROW_SCOPE(vm);
61
62 IterationRecord iterationRecord = iteratorForIterable(exec, iterable);
63 RETURN_IF_EXCEPTION(scope, void());
64 while (true) {
65 JSValue next = iteratorStep(exec, iterationRecord);
66 if (UNLIKELY(scope.exception()) || next.isFalse())
67 return;
68
69 JSValue nextValue = iteratorValue(exec, next);
70 RETURN_IF_EXCEPTION(scope, void());
71
72 callback(vm, exec, nextValue);
73 if (UNLIKELY(scope.exception())) {
74 scope.release();
75 iteratorClose(exec, iterationRecord);
76 return;
77 }
78 }
79}
80
81template<typename CallBackType>
82void forEachInIterable(ExecState& state, JSObject* iterable, JSValue iteratorMethod, const CallBackType& callback)
83{
84 auto& vm = state.vm();
85 auto scope = DECLARE_THROW_SCOPE(vm);
86
87 auto iterationRecord = iteratorForIterable(state, iterable, iteratorMethod);
88 RETURN_IF_EXCEPTION(scope, void());
89 while (true) {
90 JSValue next = iteratorStep(&state, iterationRecord);
91 if (UNLIKELY(scope.exception()) || next.isFalse())
92 return;
93
94 JSValue nextValue = iteratorValue(&state, next);
95 RETURN_IF_EXCEPTION(scope, void());
96
97 callback(vm, state, nextValue);
98 if (UNLIKELY(scope.exception())) {
99 scope.release();
100 iteratorClose(&state, iterationRecord);
101 return;
102 }
103 }
104}
105
106} // namespace JSC
107