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_iterator.h"
17
18 #include "ecmascript/accessor_data.h"
19 #include "ecmascript/ecma_macros.h"
20 #include "ecmascript/ecma_vm.h"
21 #include "ecmascript/global_env.h"
22 #include "ecmascript/interpreter/interpreter.h"
23 #include "ecmascript/js_symbol.h"
24 #include "ecmascript/js_async_from_sync_iterator.h"
25 #include "ecmascript/object_factory.h"
26 #include "ecmascript/object_fast_operator-inl.h"
27
28 namespace panda::ecmascript {
IteratorCloseAndReturn(JSThread * thread,const JSHandle<JSTaggedValue> & iter)29 JSTaggedValue JSIterator::IteratorCloseAndReturn(JSThread *thread, const JSHandle<JSTaggedValue> &iter)
30 {
31 ASSERT(thread->HasPendingException());
32 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
33 JSTaggedValue exception = thread->GetException();
34 JSHandle<JSTaggedValue> record = JSHandle<JSTaggedValue>(factory->NewCompletionRecord(CompletionRecordType::THROW,
35 JSHandle<JSTaggedValue>(thread, exception)));
36 JSHandle<JSTaggedValue> result = JSIterator::IteratorClose(thread, iter, record);
37 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
38 if (result->IsCompletionRecord()) {
39 return CompletionRecord::Cast(result->GetTaggedObject())->GetValue();
40 }
41 return result.GetTaggedValue();
42 }
43
GetIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj)44 JSHandle<JSTaggedValue> JSIterator::GetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj)
45 {
46 // 1.ReturnIfAbrupt(obj).
47 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
48 // 2.If method was not passed, then
49 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
50 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
51 JSHandle<JSTaggedValue> func = JSObject::GetMethod(thread, obj, iteratorSymbol);
52 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
53
54 return GetIterator(thread, obj, func);
55 }
56
GetIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj,const JSHandle<JSTaggedValue> & method)57 JSHandle<JSTaggedValue> JSIterator::GetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
58 const JSHandle<JSTaggedValue> &method)
59 {
60 // 1.ReturnIfAbrupt(obj).
61 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
62 // 3.Let iterator be Call(method,obj).
63 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
64 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, method, obj, undefined, 0);
65 JSTaggedValue ret = JSFunction::Call(info);
66 JSHandle<JSTaggedValue> iter(thread, ret);
67 // 4.ReturnIfAbrupt(iterator).
68 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, iter);
69 // 5.If Type(iterator) is not Object, throw a TypeError exception
70 if (!iter->IsECMAObject()) {
71 THROW_TYPE_ERROR_AND_RETURN(thread, "", undefined);
72 }
73 return iter;
74 }
75
GetAsyncIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj)76 JSHandle<JSTaggedValue> JSIterator::GetAsyncIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj)
77 {
78 // 3.If method is not present, then
79 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
80 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
81 JSHandle<JSTaggedValue> asynciteratorSymbol = env->GetAsyncIteratorSymbol();
82 // i. Set method to ? GetMethod(obj, @@asyncIterator).
83 // ii. If method is undefined, then
84 JSHandle<JSTaggedValue> method = JSObject::GetMethod(thread, obj, asynciteratorSymbol);
85 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
86 if (method->IsUndefined()) {
87 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
88 JSHandle<JSTaggedValue> func = JSObject::GetMethod(thread, obj, iteratorSymbol);
89 JSHandle<JSTaggedValue> syncIterator = GetIterator(thread, obj, func);
90 JSHandle<JSTaggedValue> nextStr = thread->GlobalConstants()->GetHandledNextString();
91 JSHandle<JSTaggedValue> nextMethod = JSTaggedValue::GetProperty(thread, syncIterator, nextStr).GetValue();
92 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
93 JSHandle<AsyncIteratorRecord> syncIteratorRecord =
94 factory->NewAsyncIteratorRecord(syncIterator, nextMethod, false);
95 JSHandle<JSTaggedValue> asyncIterator =
96 JSAsyncFromSyncIterator::CreateAsyncFromSyncIterator(thread, syncIteratorRecord);
97 return asyncIterator;
98 }
99
100 // 4.Let iterator be Call(method,obj).
101 JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
102 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, method, obj, undefined, 0);
103 JSTaggedValue ret = JSFunction::Call(info);
104 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
105 JSHandle<JSTaggedValue> iterator(thread, ret);
106 // 5.If Type(iterator) is not Object, throw a TypeError exception
107 if (!iterator->IsECMAObject()) {
108 THROW_TYPE_ERROR_AND_RETURN(thread, "", undefined);
109 }
110 return iterator;
111 }
112
113
114 // 7.4.2
IteratorNext(JSThread * thread,const JSHandle<JSTaggedValue> & iter)115 JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<JSTaggedValue> &iter)
116 {
117 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
118
119 // 1.If value was not passed, then Let result be Invoke(iterator, "next", « »).
120 JSHandle<JSTaggedValue> key(globalConst->GetHandledNextString());
121 JSHandle<JSTaggedValue> next(JSObject::GetMethod(thread, iter, key));
122 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
123 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
124 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iter, undefined, 0);
125 JSTaggedValue ret = JSFunction::Call(info);
126 // 3.ReturnIfAbrupt(result)
127 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
128 // 4.If Type(result) is not Object, throw a TypeError exception.
129 if (!ret.IsECMAObject()) {
130 THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
131 }
132 JSHandle<JSTaggedValue> result(thread, ret);
133 return result;
134 }
135
IteratorNext(JSThread * thread,const JSHandle<JSTaggedValue> & iter,const JSHandle<JSTaggedValue> & value)136 JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<JSTaggedValue> &iter,
137 const JSHandle<JSTaggedValue> &value)
138 {
139 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
140 // 2.Let result be Invoke(iterator, "next", «value»).
141 JSHandle<JSTaggedValue> key(globalConst->GetHandledNextString());
142 JSHandle<JSTaggedValue> next(JSObject::GetMethod(thread, iter, key));
143 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
144 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
145 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iter, undefined, 1);
146 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
147 info->SetCallArg(value.GetTaggedValue());
148 JSTaggedValue ret = JSFunction::Call(info);
149 // 3.ReturnIfAbrupt(result)
150 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
151 // 4.If Type(result) is not Object, throw a TypeError exception.
152 if (!ret.IsECMAObject()) {
153 THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
154 }
155 JSHandle<JSTaggedValue> result(thread, ret);
156 return result;
157 }
158
IteratorNext(JSThread * thread,const JSHandle<AsyncIteratorRecord> & iter,const JSHandle<JSTaggedValue> & value)159 JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<AsyncIteratorRecord> &iter,
160 const JSHandle<JSTaggedValue> &value)
161 {
162 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
163 // 2.Let result be Invoke(iterator, "next", «value»).
164 JSHandle<JSTaggedValue> iterator(thread, iter->GetIterator());
165 JSHandle<JSTaggedValue> next(thread, iter->GetNextMethod());
166 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
167 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iterator, undefined, 1);
168 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
169 info->SetCallArg(value.GetTaggedValue());
170 JSTaggedValue ret = JSFunction::Call(info);
171 // 3.ReturnIfAbrupt(result)
172 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
173 // 4.If Type(result) is not Object, throw a TypeError exception.
174 if (!ret.IsECMAObject()) {
175 THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
176 }
177 JSHandle<JSTaggedValue> result(thread, ret);
178 return result;
179 }
180
IteratorNext(JSThread * thread,const JSHandle<AsyncIteratorRecord> & iter)181 JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<AsyncIteratorRecord> &iter)
182 {
183 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
184 // 2.Let result be Invoke(iterator, "next", «value»).
185 JSHandle<JSTaggedValue> iterator(thread, iter->GetIterator());
186 JSHandle<JSTaggedValue> next(thread, iter->GetNextMethod());
187 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
188 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iterator, undefined, 0);
189 JSTaggedValue ret = JSFunction::Call(info);
190 // 3.ReturnIfAbrupt(result)
191 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
192 // 4.If Type(result) is not Object, throw a TypeError exception.
193 if (!ret.IsECMAObject()) {
194 THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
195 }
196 JSHandle<JSTaggedValue> result(thread, ret);
197 return result;
198 }
199 // 7.4.3
IteratorComplete(JSThread * thread,const JSHandle<JSTaggedValue> & iterResult)200 bool JSIterator::IteratorComplete(JSThread *thread, const JSHandle<JSTaggedValue> &iterResult)
201 {
202 ASSERT_PRINT(iterResult->IsECMAObject(), "iterResult must be JSObject");
203 // Return ToBoolean(Get(iterResult, "done")).
204 JSHandle<JSTaggedValue> doneStr = thread->GlobalConstants()->GetHandledDoneString();
205 JSHandle<JSTaggedValue> done = JSTaggedValue::GetProperty(thread, iterResult, doneStr).GetValue();
206 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
207 return done->ToBoolean();
208 }
209 // 7.4.4
IteratorValue(JSThread * thread,const JSHandle<JSTaggedValue> & iterResult)210 JSHandle<JSTaggedValue> JSIterator::IteratorValue(JSThread *thread, const JSHandle<JSTaggedValue> &iterResult)
211 {
212 ASSERT_PRINT(iterResult->IsECMAObject(), "iterResult must be JSObject");
213 // Return Get(iterResult, "value").
214 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
215 JSHandle<JSTaggedValue> value = JSTaggedValue::GetProperty(thread, iterResult, valueStr).GetValue();
216 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
217 return value;
218 }
219 // 7.4.5
IteratorStep(JSThread * thread,const JSHandle<JSTaggedValue> & iter)220 JSHandle<JSTaggedValue> JSIterator::IteratorStep(JSThread *thread, const JSHandle<JSTaggedValue> &iter)
221 {
222 // 1.Let result be IteratorNext(iterator).
223 JSHandle<JSTaggedValue> result = IteratorNext(thread, iter);
224 // 2.ReturnIfAbrupt(result).
225 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, result);
226 // 3.Let done be IteratorComplete(result).
227 bool done = IteratorComplete(thread, result);
228 // 4.ReturnIfAbrupt(done).
229 JSHandle<JSTaggedValue> doneHandle(thread, JSTaggedValue(done));
230 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, doneHandle);
231 // 5.If done is true, return false.
232 if (done) {
233 JSHandle<JSTaggedValue> falseHandle(thread, JSTaggedValue::False());
234 return falseHandle;
235 }
236 return result;
237 }
238 // 7.4.6
IteratorClose(JSThread * thread,const JSHandle<JSTaggedValue> & iter,const JSHandle<JSTaggedValue> & completion)239 JSHandle<JSTaggedValue> JSIterator::IteratorClose(JSThread *thread, const JSHandle<JSTaggedValue> &iter,
240 const JSHandle<JSTaggedValue> &completion)
241 {
242 // 1.Assert: Type(iterator) is Object.
243 ASSERT_PRINT(iter->IsECMAObject(), "iter must be JSObject");
244 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
245 JSHandle<JSTaggedValue> exceptionOnThread;
246 if (thread->HasPendingException()) {
247 exceptionOnThread = JSHandle<JSTaggedValue>(thread, thread->GetException());
248 thread->ClearException();
249 }
250 JSTaggedValue returnStr = globalConst->GetReturnString();
251 // 3.Let return be GetMethod(iterator, "return").
252 JSTaggedValue func = ObjectFastOperator::FastGetPropertyByName(thread, iter.GetTaggedValue(), returnStr);
253 // 4.ReturnIfAbrupt(return).
254 JSHandle<JSTaggedValue> returnFunc(thread, func);
255 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, returnFunc);
256
257 // 5.If return is undefined, return Completion(completion).
258 if (returnFunc->IsUndefined() || returnFunc->IsNull()) {
259 if (!exceptionOnThread.IsEmpty()) {
260 thread->SetException(exceptionOnThread.GetTaggedValue());
261 }
262 return completion;
263 }
264
265 if (!returnFunc->IsCallable()) {
266 THROW_TYPE_ERROR_AND_RETURN(thread, "return function is not Callable", returnFunc);
267 }
268 // 6.Let innerResult be Call(return, iterator, « »).
269 JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
270 EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, returnFunc, iter, undefined, 0);
271 JSTaggedValue ret = JSFunction::Call(info);
272 RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
273 if (!exceptionOnThread.IsEmpty()) {
274 thread->SetException(exceptionOnThread.GetTaggedValue());
275 }
276 JSHandle<JSTaggedValue> innerResult(thread, ret);
277 JSHandle<CompletionRecord> completionRecord(thread, globalConst->GetUndefined());
278 if (completion->IsCompletionRecord()) {
279 completionRecord = JSHandle<CompletionRecord>::Cast(completion);
280 }
281 // 7.If completion.[[type]] is throw, return Completion(completion).
282 if (!completionRecord.GetTaggedValue().IsUndefined() && completionRecord->IsThrow()) {
283 if (!exceptionOnThread.IsEmpty()) {
284 thread->SetException(exceptionOnThread.GetTaggedValue());
285 }
286 return completion;
287 }
288 // 8.If innerResult.[[type]] is throw, return Completion(innerResult).
289 if (thread->HasPendingException()) {
290 return innerResult;
291 }
292 // 9.If Type(innerResult.[[value]]) is not Object, throw a TypeError exception.
293 if (!innerResult->IsECMAObject()) {
294 THROW_TYPE_ERROR_AND_RETURN(thread, "", undefined);
295 }
296 if (!exceptionOnThread.IsEmpty()) {
297 thread->SetException(exceptionOnThread.GetTaggedValue());
298 }
299 return completion;
300 }
301 // 7.4.7
CreateIterResultObject(JSThread * thread,const JSHandle<JSTaggedValue> & value,bool done)302 JSHandle<JSObject> JSIterator::CreateIterResultObject(JSThread *thread, const JSHandle<JSTaggedValue> &value, bool done)
303 {
304 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
305 auto globalConst = thread->GlobalConstants();
306 // 2. Let obj be OrdinaryObjectCreate(%Object.prototype%).
307 JSHandle<JSHClass> klass = JSHandle<JSHClass>::Cast(globalConst->GetHandledIteratorResultClass());
308 JSHandle<JSObject> obj = factory->NewJSObject(klass);
309
310 // 3. Perform ! CreateDataPropertyOrThrow(obj, "value", value).
311 // 4. Perform ! CreateDataPropertyOrThrow(obj, "done", done).
312 obj->SetPropertyInlinedProps(thread, VALUE_INLINE_PROPERTY_INDEX, value.GetTaggedValue());
313 obj->SetPropertyInlinedProps(thread, DONE_INLINE_PROPERTY_INDEX, JSTaggedValue(done));
314 // 5. Return obj.
315 return obj;
316 }
317 } // namespace panda::ecmascript
318