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 "JSInternalFieldObjectImpl.h"
29
30namespace JSC {
31
32class JSGenerator final : public JSInternalFieldObjectImpl<5> {
33public:
34 using Base = JSInternalFieldObjectImpl<5>;
35
36 // JSGenerator has one inline storage slot, which is pointing internalField(0).
37 static size_t allocationSize(Checked<size_t> inlineCapacity)
38 {
39 ASSERT_UNUSED(inlineCapacity, inlineCapacity == 0U);
40 return sizeof(JSGenerator);
41 }
42
43 enum class GeneratorResumeMode : int32_t {
44 NormalMode = 0,
45 ReturnMode = 1,
46 ThrowMode = 2
47 };
48
49 enum class GeneratorState : int32_t {
50 Completed = -1,
51 Executing = -2,
52 Init = 0,
53 };
54
55 // [this], @generator, @generatorState, @generatorValue, @generatorResumeMode, @generatorFrame.
56 enum class GeneratorArgument : int32_t {
57 ThisValue = 0,
58 Generator = 1,
59 State = 2,
60 Value = 3,
61 ResumeMode = 4,
62 Frame = 5,
63 };
64
65 enum class Field : uint32_t {
66 // FIXME: JSGenerator should support PolyProto, since generator tends to be created with poly proto mode.
67 // 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.
68 PolyProto = 0,
69 State,
70 Next,
71 This,
72 Frame,
73 };
74 static_assert(numberOfInternalFields == 5);
75 static std::array<JSValue, numberOfInternalFields> initialValues()
76 {
77 return { {
78 jsNull(),
79 jsNumber(static_cast<int32_t>(GeneratorState::Init)),
80 jsUndefined(),
81 jsUndefined(),
82 jsUndefined(),
83 } };
84 }
85
86 static JSGenerator* create(VM&, Structure*);
87 static Structure* createStructure(VM&, JSGlobalObject*, JSValue);
88
89 DECLARE_EXPORT_INFO;
90
91 static void visitChildren(JSCell*, SlotVisitor&);
92
93protected:
94 JSGenerator(VM&, Structure*);
95 void finishCreation(VM&);
96};
97
98} // namespace JSC
99