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 "js_iterator.h"
17 #include "ecma_macros.h"
18 #include "ecma_vm.h"
19 #include "ecmascript/accessor_data.h"
20 #include "ecmascript/internal_call_params.h"
21 #include "global_env.h"
22 #include "js_invoker.h"
23 #include "js_symbol.h"
24 #include "object_factory.h"
25
26 namespace panda::ecmascript {
IteratorCloseAndReturn(JSThread * thread,const JSHandle<JSTaggedValue> & iter,const JSHandle<JSTaggedValue> & status)27 JSTaggedValue JSIterator::IteratorCloseAndReturn(JSThread *thread, const JSHandle<JSTaggedValue> &iter,
28 const JSHandle<JSTaggedValue> &status)
29 {
30 ASSERT(thread->HasPendingException());
31 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
32 JSTaggedValue exception = thread->GetException();
33 JSHandle<JSTaggedValue> record = JSHandle<JSTaggedValue>(factory->NewCompletionRecord(CompletionRecordType::THROW,
34 JSHandle<JSTaggedValue>(thread, exception)));
35 JSHandle<JSTaggedValue> result = JSIterator::IteratorClose(thread, iter, record);
36 if (result->IsCompletionRecord()) {
37 return CompletionRecord::Cast(result->GetTaggedObject())->GetValue();
38 }
39 return result.GetTaggedValue();
40 }
41
GetIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj)42 JSHandle<JSTaggedValue> JSIterator::GetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj)
43 {
44 // 1.ReturnIfAbrupt(obj).
45 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
46 // 2.If method was not passed, then
47 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
48 JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
49 JSHandle<JSTaggedValue> func = JSObject::GetMethod(thread, obj, iteratorSymbol);
50 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
51
52 return GetIterator(thread, obj, func);
53 }
54
GetIterator(JSThread * thread,const JSHandle<JSTaggedValue> & obj,const JSHandle<JSTaggedValue> & method)55 JSHandle<JSTaggedValue> JSIterator::GetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
56 const JSHandle<JSTaggedValue> &method)
57 {
58 // 1.ReturnIfAbrupt(obj).
59 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
60 // 3.Let iterator be Call(method,obj).
61 JSTaggedValue ret = JSFunction::Call(thread, method, obj, 0, nullptr);
62 JSHandle<JSTaggedValue> iter(thread, ret);
63 // 4.ReturnIfAbrupt(iterator).
64 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, iter);
65 // 5.If Type(iterator) is not Object, throw a TypeError exception
66 if (!iter->IsECMAObject()) {
67 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
68 THROW_TYPE_ERROR_AND_RETURN(thread, "", undefinedHandle);
69 }
70 return iter;
71 } // namespace panda::ecmascript
72 // 7.4.2
IteratorNext(JSThread * thread,const JSHandle<JSTaggedValue> & iter)73 JSHandle<JSObject> JSIterator::IteratorNext(JSThread *thread, const JSHandle<JSTaggedValue> &iter)
74 {
75 // 1.If value was not passed, then Let result be Invoke(iterator, "next", « »).
76 JSHandle<JSTaggedValue> key(thread->GlobalConstants()->GetHandledNextString());
77 JSHandle<JSTaggedValue> next(JSObject::GetMethod(thread, iter, key));
78 ASSERT(next->IsCallable());
79 JSTaggedValue ret = JSFunction::Call(thread, next, iter, 0, nullptr);
80 JSHandle<JSObject> result(thread, ret);
81 // 3.ReturnIfAbrupt(result)
82 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, result);
83 // 4.If Type(result) is not Object, throw a TypeError exception.
84 if (!result->IsECMAObject()) {
85 THROW_TYPE_ERROR_AND_RETURN(thread, "", result);
86 }
87 return result;
88 }
89
IteratorNext(JSThread * thread,const JSHandle<JSTaggedValue> & iter,const JSHandle<JSTaggedValue> & value)90 JSHandle<JSObject> JSIterator::IteratorNext(JSThread *thread, const JSHandle<JSTaggedValue> &iter,
91 const JSHandle<JSTaggedValue> &value)
92 {
93 // 2.Let result be Invoke(iterator, "next", «value»).
94 JSHandle<JSTaggedValue> key(thread->GlobalConstants()->GetHandledNextString());
95 JSHandle<JSTaggedValue> next(JSObject::GetMethod(thread, iter, key));
96 ASSERT(next->IsCallable());
97 InternalCallParams *arguments = thread->GetInternalCallParams();
98 arguments->MakeArgv(value);
99 JSTaggedValue ret = JSFunction::Call(thread, next, iter, 1, arguments->GetArgv());
100
101 JSHandle<JSObject> result(thread, ret);
102 // 3.ReturnIfAbrupt(result)
103 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, result);
104 // 4.If Type(result) is not Object, throw a TypeError exception.
105 if (!result->IsECMAObject()) {
106 THROW_TYPE_ERROR_AND_RETURN(thread, "", result);
107 }
108 return result;
109 }
110 // 7.4.3
IteratorComplete(JSThread * thread,const JSHandle<JSTaggedValue> & iterResult)111 bool JSIterator::IteratorComplete(JSThread *thread, const JSHandle<JSTaggedValue> &iterResult)
112 {
113 ASSERT_PRINT(iterResult->IsECMAObject(), "iterResult must be JSObject");
114 // Return ToBoolean(Get(iterResult, "done")).
115 JSHandle<JSTaggedValue> doneStr = thread->GlobalConstants()->GetHandledDoneString();
116 JSHandle<JSTaggedValue> done = JSTaggedValue::GetProperty(thread, iterResult, doneStr).GetValue();
117 return done->ToBoolean();
118 }
119 // 7.4.4
IteratorValue(JSThread * thread,const JSHandle<JSTaggedValue> & iterResult)120 JSHandle<JSTaggedValue> JSIterator::IteratorValue(JSThread *thread, const JSHandle<JSTaggedValue> &iterResult)
121 {
122 ASSERT_PRINT(iterResult->IsECMAObject(), "iterResult must be JSObject");
123 // Return Get(iterResult, "value").
124 JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
125 JSHandle<JSTaggedValue> value = JSTaggedValue::GetProperty(thread, iterResult, valueStr).GetValue();
126 return value;
127 }
128 // 7.4.5
IteratorStep(JSThread * thread,const JSHandle<JSTaggedValue> & iter)129 JSHandle<JSTaggedValue> JSIterator::IteratorStep(JSThread *thread, const JSHandle<JSTaggedValue> &iter)
130 {
131 // 1.Let result be IteratorNext(iterator).
132 JSHandle<JSTaggedValue> result(IteratorNext(thread, iter));
133 // 2.ReturnIfAbrupt(result).
134 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, result);
135 // 3.Let done be IteratorComplete(result).
136 bool done = IteratorComplete(thread, result);
137 // 4.ReturnIfAbrupt(done).
138 JSHandle<JSTaggedValue> doneHandle(thread, JSTaggedValue(done));
139 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, doneHandle);
140 // 5.If done is true, return false.
141 if (done) {
142 JSHandle<JSTaggedValue> falseHandle(thread, JSTaggedValue::False());
143 return falseHandle;
144 }
145 return result;
146 }
147 // 7.4.6
IteratorClose(JSThread * thread,const JSHandle<JSTaggedValue> & iter,const JSHandle<JSTaggedValue> & completion)148 JSHandle<JSTaggedValue> JSIterator::IteratorClose(JSThread *thread, const JSHandle<JSTaggedValue> &iter,
149 const JSHandle<JSTaggedValue> &completion)
150 {
151 // 1.Assert: Type(iterator) is Object.
152 ASSERT_PRINT(iter->IsECMAObject(), "iter must be JSObject");
153 const GlobalEnvConstants *globalConst = thread->GlobalConstants();
154 JSHandle<JSTaggedValue> exceptionOnThread;
155 if (thread->HasPendingException()) {
156 exceptionOnThread = JSHandle<JSTaggedValue>(thread, thread->GetException());
157 thread->ClearException();
158 }
159 JSHandle<JSTaggedValue> returnStr(globalConst->GetHandledReturnString());
160 // 3.Let return be GetMethod(iterator, "return").
161 JSHandle<JSTaggedValue> returnFunc(JSObject::GetMethod(thread, iter, returnStr));
162 // 4.ReturnIfAbrupt(return).
163 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, returnFunc);
164 // 5.If return is undefined, return Completion(completion).
165 if (returnFunc->IsUndefined()) {
166 if (!exceptionOnThread.IsEmpty()) {
167 thread->SetException(exceptionOnThread.GetTaggedValue());
168 }
169 return completion;
170 }
171 // 6.Let innerResult be Call(return, iterator, « »).
172 JSTaggedValue ret = JSFunction::Call(thread, returnFunc, iter, 0, nullptr);
173 if (!exceptionOnThread.IsEmpty()) {
174 thread->SetException(exceptionOnThread.GetTaggedValue());
175 }
176 JSHandle<JSTaggedValue> innerResult(thread, ret);
177 JSHandle<CompletionRecord> completionRecord(thread, globalConst->GetUndefined());
178 if (completion->IsCompletionRecord()) {
179 completionRecord = JSHandle<CompletionRecord>::Cast(completion);
180 }
181 // 7.If completion.[[type]] is throw, return Completion(completion).
182 if (!completionRecord.GetTaggedValue().IsUndefined() && completionRecord->IsThrow()) {
183 if (!exceptionOnThread.IsEmpty()) {
184 thread->SetException(exceptionOnThread.GetTaggedValue());
185 }
186 return completion;
187 }
188 // 8.If innerResult.[[type]] is throw, return Completion(innerResult).
189 if (thread->HasPendingException()) {
190 return innerResult;
191 }
192 // 9.If Type(innerResult.[[value]]) is not Object, throw a TypeError exception.
193 if (!innerResult->IsECMAObject()) {
194 JSHandle<JSTaggedValue> undefinedHandle(thread, JSTaggedValue::Undefined());
195 THROW_TYPE_ERROR_AND_RETURN(thread, "", undefinedHandle);
196 }
197 if (!exceptionOnThread.IsEmpty()) {
198 thread->SetException(exceptionOnThread.GetTaggedValue());
199 }
200 return completion;
201 }
202 // 7.4.7
CreateIterResultObject(JSThread * thread,const JSHandle<JSTaggedValue> & value,bool done)203 JSHandle<JSObject> JSIterator::CreateIterResultObject(JSThread *thread, const JSHandle<JSTaggedValue> &value, bool done)
204 {
205 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
206 JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
207 auto globalConst = thread->GlobalConstants();
208 JSHandle<JSTaggedValue> constructor(env->GetObjectFunction());
209 JSHandle<JSTaggedValue> valueStr = globalConst->GetHandledValueString();
210 JSHandle<JSTaggedValue> doneStr = globalConst->GetHandledDoneString();
211 JSHandle<JSTaggedValue> doneValue(thread, JSTaggedValue(done));
212 // 2. Let obj be OrdinaryObjectCreate(%Object.prototype%).
213 JSHandle<JSObject> obj(factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), constructor));
214 // 3. Perform ! CreateDataPropertyOrThrow(obj, "value", value).
215 // 4. Perform ! CreateDataPropertyOrThrow(obj, "done", done).
216 JSObject::CreateDataPropertyOrThrow(thread, obj, valueStr, value);
217 JSObject::CreateDataPropertyOrThrow(thread, obj, doneStr, doneValue);
218 ASSERT_NO_ABRUPT_COMPLETION(thread);
219 // 5. Return obj.
220 return obj;
221 }
222 } // namespace panda::ecmascript
223