• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/js_generator_object.h"
17 
18 #include "ecmascript/generator_helper.h"
19 #include "ecmascript/js_iterator.h"
20 #include "ecmascript/js_tagged_value-inl.h"
21 
22 namespace panda::ecmascript {
GeneratorValidate(JSThread * thread,const JSHandle<JSTaggedValue> & obj)23 JSGeneratorState JSGeneratorObject::GeneratorValidate(JSThread *thread, const JSHandle<JSTaggedValue> &obj)
24 {
25     // 1.Perform ? RequireInternalSlot(generator, [[GeneratorState]]).
26     // 2.Assert: generator also has a [[GeneratorContext]] internal slot.
27     if (!obj->IsECMAObject()) {
28         THROW_TYPE_ERROR_AND_RETURN(thread, "", JSGeneratorState::UNDEFINED);
29     }
30     JSHandle<JSObject> toObj = JSTaggedValue::ToObject(thread, obj);
31     if (!toObj->IsGeneratorObject()) {
32         THROW_TYPE_ERROR_AND_RETURN(thread, "", JSGeneratorState::UNDEFINED);
33     }
34 
35     // 3.Let state be generator.[[GeneratorState]].
36     JSHandle<JSGeneratorObject> generator(thread, JSGeneratorObject::Cast(*(toObj)));
37     JSGeneratorState state = generator->GetGeneratorState();
38     // 4.If state is executing, throw a TypeError exception.
39     if (state == JSGeneratorState::EXECUTING) {
40         THROW_TYPE_ERROR_AND_RETURN(thread, "", JSGeneratorState::UNDEFINED);
41     }
42     // 5.Return state.
43     return state;
44 }
45 
GeneratorResume(JSThread * thread,const JSHandle<JSGeneratorObject> & generator,JSTaggedValue value)46 JSHandle<JSObject> JSGeneratorObject::GeneratorResume(JSThread *thread, const JSHandle<JSGeneratorObject> &generator,
47                                                       JSTaggedValue value)
48 {
49     // 1.Let state be ? GeneratorValidate(generator).
50     JSHandle<JSTaggedValue> gen(thread, generator.GetTaggedValue());
51     JSGeneratorState state = GeneratorValidate(thread, gen);
52     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSObject, thread);
53 
54     // 2.If state is completed, return CreateIterResultObject(undefined, true).
55     if (state == JSGeneratorState::COMPLETED) {
56         JSHandle<JSTaggedValue> valueHandle(thread, JSTaggedValue::Undefined());
57         return JSIterator::CreateIterResultObject(thread, valueHandle, true);
58     }
59 
60     // 3.Assert: state is either suspendedStart or suspendedYield.
61     ASSERT_PRINT(state == JSGeneratorState::SUSPENDED_START ||
62                      state == JSGeneratorState::SUSPENDED_YIELD,
63                  "state is neither suspendedStart nor suspendedYield");
64 
65     // 4.Let genContext be generator.[[GeneratorContext]].
66     JSHandle<GeneratorContext> genContext(thread, generator->GetGeneratorContext());
67 
68     // 5.Let methodContext be the running execution context.
69     // 6.Suspend methodContext.
70 
71     // 7.Set generator.[[GeneratorState]] to executing.
72     generator->SetGeneratorState(JSGeneratorState::EXECUTING);
73 
74     // 8.Push genContext onto the execution context stack; genContext is now the running execution context.
75     // 9.Resume the suspended evaluation of genContext using NormalCompletion(value) as the result of the operation
76     //   that suspended it. Let result be the value returned by the resumed computation.
77     // 10.Assert: When we return here, genContext has already been removed from the execution context stack and
78     //    methodContext is the currently running execution context.
79     // 11.Return Completion(result).
80     JSHandle<JSObject> result = GeneratorHelper::Next(thread, genContext, value);
81     return result;
82 }
83 
GeneratorResumeAbrupt(JSThread * thread,const JSHandle<JSGeneratorObject> & generator,const JSHandle<CompletionRecord> & abruptCompletion)84 JSHandle<JSObject> JSGeneratorObject::GeneratorResumeAbrupt(JSThread *thread,
85                                                             const JSHandle<JSGeneratorObject> &generator,
86                                                             const JSHandle<CompletionRecord> &abruptCompletion)
87 {
88     // 1.Let state be ? GeneratorValidate(generator).
89     JSHandle<JSTaggedValue> gen(thread, generator.GetTaggedValue());
90     JSGeneratorState state = GeneratorValidate(thread, gen);
91     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSObject, thread);
92 
93     // 2.If state is suspendedStart, then
94     //     a.Set generator.[[GeneratorState]] to completed.
95     //     b.Once a generator enters the completed state it never leaves it and its associated execution context is
96     //       never resumed. Any execution state associated with generator can be discarded at this point.
97     //     c.Set state to completed.
98     if (state == JSGeneratorState::SUSPENDED_START) {
99         state = JSGeneratorState::COMPLETED;
100         generator->SetGeneratorState(state);
101     }
102 
103     // 3.If state is completed, then
104     //     a.If abruptCompletion.[[Type]] is return, then
105     //         i.Return CreateIterResultObject(abruptCompletion.[[Value]], true).
106     //     b.Return Completion(abruptCompletion).
107     if (state == JSGeneratorState::COMPLETED) {
108         JSHandle<JSTaggedValue> valueHandle(thread, abruptCompletion->GetValue());
109         JSHandle<JSObject> result = JSIterator::CreateIterResultObject(thread, valueHandle, true);
110         if (abruptCompletion->GetType() == CompletionRecordType::RETURN) {
111             return result;
112         }
113         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, valueHandle.GetTaggedValue(), result);
114     }
115 
116     // 4.Assert: state is suspendedYield.
117     ASSERT_PRINT(state == JSGeneratorState::SUSPENDED_YIELD, "state is not suspendedYield");
118 
119     // 5.Let genContext be generator.[[GeneratorContext]].
120     JSHandle<GeneratorContext> genContext(thread, generator->GetGeneratorContext());
121 
122     // 6.Let methodContext be the running execution context.
123     // 7.Suspend methodContext.
124 
125     // 8.Set generator.[[GeneratorState]] to executing.
126     generator->SetGeneratorState(JSGeneratorState::EXECUTING);
127 
128     // 9.Push genContext onto the execution context stack; genContext is now the running execution context.
129     // 10.Resume the suspended evaluation of genContext using abruptCompletion as the result of the operation that
130     //    suspended it. Let result be the completion record returned by the resumed computation.
131     // 11.Assert: When we return here, genContext has already been removed from the execution context stack and
132     //    methodContext is the currently running execution context.
133     // 12.Return Completion(result).
134     JSHandle<JSObject> result;
135     if (abruptCompletion->GetType() == CompletionRecordType::RETURN) {
136         result = GeneratorHelper::Return(thread, genContext, abruptCompletion->GetValue());
137     } else {
138         result = GeneratorHelper::Throw(thread, genContext, abruptCompletion->GetValue());
139     }
140     return result;
141 }
142 }  // namespace panda::ecmascript
143