• 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_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, "JSIterator::GetIterator: iter is not object", 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_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
98         return asyncIterator;
99     }
100 
101     // 4.Let iterator be Call(method,obj).
102     JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
103     EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, method, obj, undefined, 0);
104     JSTaggedValue ret = JSFunction::Call(info);
105     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
106     JSHandle<JSTaggedValue> iterator(thread, ret);
107     // 5.If Type(iterator) is not Object, throw a TypeError exception
108     if (!iterator->IsECMAObject()) {
109         THROW_TYPE_ERROR_AND_RETURN(thread, "Is not object", undefined);
110     }
111     return iterator;
112 }
113 
114 
115 // 7.4.2
IteratorNext(JSThread * thread,const JSHandle<JSTaggedValue> & iter)116 JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<JSTaggedValue> &iter)
117 {
118     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
119 
120     // 1.If value was not passed, then Let result be Invoke(iterator, "next", «‍ »).
121     JSHandle<JSTaggedValue> key(globalConst->GetHandledNextString());
122     JSHandle<JSTaggedValue> next(JSObject::GetMethod(thread, iter, key));
123     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
124     JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
125     EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iter, undefined, 0);
126     JSTaggedValue ret = JSFunction::Call(info);
127     // 3.ReturnIfAbrupt(result)
128     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
129     // 4.If Type(result) is not Object, throw a TypeError exception.
130     if (!ret.IsECMAObject()) {
131         THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
132     }
133     JSHandle<JSTaggedValue> result(thread, ret);
134     return result;
135 }
136 
IteratorNext(JSThread * thread,const JSHandle<JSTaggedValue> & iter,const JSHandle<JSTaggedValue> & value)137 JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<JSTaggedValue> &iter,
138                                                  const JSHandle<JSTaggedValue> &value)
139 {
140     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
141     // 2.Let result be Invoke(iterator, "next", «‍value»).
142     JSHandle<JSTaggedValue> key(globalConst->GetHandledNextString());
143     JSHandle<JSTaggedValue> next(JSObject::GetMethod(thread, iter, key));
144     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
145     JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
146     EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iter, undefined, 1);
147     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
148     info->SetCallArg(value.GetTaggedValue());
149     JSTaggedValue ret = JSFunction::Call(info);
150     // 3.ReturnIfAbrupt(result)
151     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
152     // 4.If Type(result) is not Object, throw a TypeError exception.
153     if (!ret.IsECMAObject()) {
154         THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
155     }
156     JSHandle<JSTaggedValue> result(thread, ret);
157     return result;
158 }
159 
IteratorNext(JSThread * thread,const JSHandle<AsyncIteratorRecord> & iter,const JSHandle<JSTaggedValue> & value)160 JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<AsyncIteratorRecord> &iter,
161                                                  const JSHandle<JSTaggedValue> &value)
162 {
163     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
164     // 2.Let result be Invoke(iterator, "next", «‍value»).
165     JSHandle<JSTaggedValue> iterator(thread, iter->GetIterator());
166     JSHandle<JSTaggedValue> next(thread, iter->GetNextMethod());
167     JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
168     EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iterator, undefined, 1);
169     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
170     info->SetCallArg(value.GetTaggedValue());
171     JSTaggedValue ret = JSFunction::Call(info);
172     // 3.ReturnIfAbrupt(result)
173     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
174     // 4.If Type(result) is not Object, throw a TypeError exception.
175     if (!ret.IsECMAObject()) {
176         THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
177     }
178     JSHandle<JSTaggedValue> result(thread, ret);
179     return result;
180 }
181 
IteratorNext(JSThread * thread,const JSHandle<AsyncIteratorRecord> & iter)182 JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<AsyncIteratorRecord> &iter)
183 {
184     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
185     // 2.Let result be Invoke(iterator, "next", «‍value»).
186     JSHandle<JSTaggedValue> iterator(thread, iter->GetIterator());
187     JSHandle<JSTaggedValue> next(thread, iter->GetNextMethod());
188     JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
189     EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iterator, undefined, 0);
190     JSTaggedValue ret = JSFunction::Call(info);
191     // 3.ReturnIfAbrupt(result)
192     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
193     // 4.If Type(result) is not Object, throw a TypeError exception.
194     if (!ret.IsECMAObject()) {
195         THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
196     }
197     JSHandle<JSTaggedValue> result(thread, ret);
198     return result;
199 }
200 // 7.4.3
IteratorComplete(JSThread * thread,const JSHandle<JSTaggedValue> & iterResult)201 bool JSIterator::IteratorComplete(JSThread *thread, const JSHandle<JSTaggedValue> &iterResult)
202 {
203     ASSERT_PRINT(iterResult->IsECMAObject(), "iterResult must be JSObject");
204     // Return ToBoolean(Get(iterResult, "done")).
205     JSHandle<JSTaggedValue> doneStr = thread->GlobalConstants()->GetHandledDoneString();
206     JSHandle<JSTaggedValue> done = JSTaggedValue::GetProperty(thread, iterResult, doneStr).GetValue();
207     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
208     return done->ToBoolean();
209 }
210 // 7.4.4
IteratorValue(JSThread * thread,const JSHandle<JSTaggedValue> & iterResult)211 JSHandle<JSTaggedValue> JSIterator::IteratorValue(JSThread *thread, const JSHandle<JSTaggedValue> &iterResult)
212 {
213     ASSERT_PRINT(iterResult->IsECMAObject(), "iterResult must be JSObject");
214     // Return Get(iterResult, "value").
215     JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
216     JSHandle<JSTaggedValue> value = JSTaggedValue::GetProperty(thread, iterResult, valueStr).GetValue();
217     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
218     return value;
219 }
220 // 7.4.5
IteratorStep(JSThread * thread,const JSHandle<JSTaggedValue> & iter)221 JSHandle<JSTaggedValue> JSIterator::IteratorStep(JSThread *thread, const JSHandle<JSTaggedValue> &iter)
222 {
223     // 1.Let result be IteratorNext(iterator).
224     JSHandle<JSTaggedValue> result = IteratorNext(thread, iter);
225     // 2.ReturnIfAbrupt(result).
226     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, result);
227     // 3.Let done be IteratorComplete(result).
228     bool done = IteratorComplete(thread, result);
229     // 4.ReturnIfAbrupt(done).
230     JSHandle<JSTaggedValue> doneHandle(thread, JSTaggedValue(done));
231     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, doneHandle);
232     // 5.If done is true, return false.
233     if (done) {
234         JSHandle<JSTaggedValue> falseHandle(thread, JSTaggedValue::False());
235         return falseHandle;
236     }
237     return result;
238 }
239 // 7.4.6
IteratorClose(JSThread * thread,const JSHandle<JSTaggedValue> & iter,const JSHandle<JSTaggedValue> & completion)240 JSHandle<JSTaggedValue> JSIterator::IteratorClose(JSThread *thread, const JSHandle<JSTaggedValue> &iter,
241                                                   const JSHandle<JSTaggedValue> &completion)
242 {
243     // 1.Assert: Type(iterator) is Object.
244     ASSERT_PRINT(iter->IsECMAObject(), "iter must be JSObject");
245     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
246     JSHandle<JSTaggedValue> exceptionOnThread;
247     if (thread->HasPendingException()) {
248         exceptionOnThread = JSHandle<JSTaggedValue>(thread, thread->GetException());
249         thread->ClearException();
250     }
251     JSTaggedValue returnStr = globalConst->GetReturnString();
252     // 3.Let return be GetMethod(iterator, "return").
253     JSTaggedValue func = ObjectFastOperator::FastGetPropertyByName(thread, iter.GetTaggedValue(), returnStr);
254     // 4.ReturnIfAbrupt(return).
255     JSHandle<JSTaggedValue> returnFunc(thread, func);
256     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, returnFunc);
257 
258     // 5.If return is undefined, return Completion(completion).
259     if (returnFunc->IsUndefined() || returnFunc->IsNull()) {
260         if (!exceptionOnThread.IsEmpty()) {
261             thread->SetException(exceptionOnThread.GetTaggedValue());
262         }
263         return completion;
264     }
265 
266     if (!returnFunc->IsCallable()) {
267         THROW_TYPE_ERROR_AND_RETURN(thread, "return function is not Callable", returnFunc);
268     }
269     // 6.Let innerResult be Call(return, iterator, «‍ »).
270     JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
271     EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, returnFunc, iter, undefined, 0);
272     JSTaggedValue ret = JSFunction::Call(info);
273     RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
274     if (!exceptionOnThread.IsEmpty()) {
275         thread->SetException(exceptionOnThread.GetTaggedValue());
276     }
277     JSHandle<JSTaggedValue> innerResult(thread, ret);
278     JSHandle<CompletionRecord> completionRecord(thread, globalConst->GetUndefined());
279     if (completion->IsCompletionRecord()) {
280         completionRecord = JSHandle<CompletionRecord>::Cast(completion);
281     }
282     // 7.If completion.[[type]] is throw, return Completion(completion).
283     if (!completionRecord.GetTaggedValue().IsUndefined() && completionRecord->IsThrow()) {
284         if (!exceptionOnThread.IsEmpty()) {
285             thread->SetException(exceptionOnThread.GetTaggedValue());
286         }
287         return completion;
288     }
289     // 8.If innerResult.[[type]] is throw, return Completion(innerResult).
290     if (thread->HasPendingException()) {
291         return innerResult;
292     }
293     // 9.If Type(innerResult.[[value]]) is not Object, throw a TypeError exception.
294     if (!innerResult->IsECMAObject()) {
295         THROW_TYPE_ERROR_AND_RETURN(thread, "Is not object", undefined);
296     }
297     if (!exceptionOnThread.IsEmpty()) {
298         thread->SetException(exceptionOnThread.GetTaggedValue());
299     }
300     return completion;
301 }
302 // 7.4.7
CreateIterResultObject(JSThread * thread,const JSHandle<JSTaggedValue> & value,bool done)303 JSHandle<JSObject> JSIterator::CreateIterResultObject(JSThread *thread, const JSHandle<JSTaggedValue> &value, bool done)
304 {
305     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
306     auto globalConst = thread->GlobalConstants();
307     // 2. Let obj be OrdinaryObjectCreate(%Object.prototype%).
308     JSHandle<JSHClass> klass = JSHandle<JSHClass>::Cast(globalConst->GetHandledIteratorResultClass());
309     JSHandle<JSObject> obj = factory->NewJSObject(klass);
310 
311     // 3. Perform ! CreateDataPropertyOrThrow(obj, "value", value).
312     // 4. Perform ! CreateDataPropertyOrThrow(obj, "done", done).
313     obj->SetPropertyInlinedProps(thread, VALUE_INLINE_PROPERTY_INDEX, value.GetTaggedValue());
314     obj->SetPropertyInlinedProps(thread, DONE_INLINE_PROPERTY_INDEX, JSTaggedValue(done));
315     // 5. Return obj.
316     return obj;
317 }
318 }  // namespace panda::ecmascript
319