1/*
2 * Copyright (C) 2019 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "JSGenerator.h"
29#include "JSInternalFieldObjectImpl.h"
30
31namespace JSC {
32
33class JSAsyncGenerator final : public JSInternalFieldObjectImpl<8> {
34public:
35 using Base = JSInternalFieldObjectImpl<8>;
36
37 // JSAsyncGenerator has one inline storage slot, which is pointing internalField(0).
38 static size_t allocationSize(Checked<size_t> inlineCapacity)
39 {
40 ASSERT_UNUSED(inlineCapacity, inlineCapacity == 0U);
41 return sizeof(JSAsyncGenerator);
42 }
43
44 enum class AsyncGeneratorState : int32_t {
45 Completed = -1,
46 Executing = -2,
47 SuspendedStart = -3,
48 SuspendedYield = -4,
49 AwaitingReturn = -5,
50 };
51 static_assert(static_cast<int32_t>(AsyncGeneratorState::Completed) == static_cast<int32_t>(JSGenerator::GeneratorState::Completed));
52 static_assert(static_cast<int32_t>(AsyncGeneratorState::Executing) == static_cast<int32_t>(JSGenerator::GeneratorState::Executing));
53
54 enum class AsyncGeneratorSuspendReason : int32_t {
55 None = 0,
56 Yield = -1,
57 Await = -2
58 };
59
60 enum class Field : uint32_t {
61 // FIXME: JSAsyncGenerator should support PolyProto, since generator tends to be created with poly proto mode.
62 // We reserve the first internal field for PolyProto property. This offset is identical to JSFinalObject's first inline storage slot which will be used for PolyProto.
63 PolyProto = 0,
64 State,
65 Next,
66 This,
67 Frame,
68 SuspendReason,
69 QueueFirst,
70 QueueLast,
71 };
72 static_assert(numberOfInternalFields == 8);
73 static std::array<JSValue, numberOfInternalFields> initialValues()
74 {
75 return { {
76 jsNull(),
77 jsNumber(static_cast<int32_t>(AsyncGeneratorState::SuspendedStart)),
78 jsUndefined(),
79 jsUndefined(),
80 jsUndefined(),
81 jsNumber(static_cast<int32_t>(AsyncGeneratorSuspendReason::None)),
82 jsNull(),
83 jsNull(),
84 } };
85 }
86
87 static JSAsyncGenerator* create(VM&, Structure*);
88 static Structure* createStructure(VM&, JSGlobalObject*, JSValue);
89
90 DECLARE_EXPORT_INFO;
91
92 static void visitChildren(JSCell*, SlotVisitor&);
93
94protected:
95 JSAsyncGenerator(VM&, Structure*);
96 void finishCreation(VM&);
97};
98
99} // namespace JSC
100