• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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/containers/containers_queue.h"
17 
18 #include "ecmascript/containers/containers_errors.h"
19 #include "ecmascript/ecma_vm.h"
20 #include "ecmascript/interpreter/interpreter.h"
21 #include "ecmascript/js_api/js_api_queue.h"
22 #include "ecmascript/js_function.h"
23 #include "ecmascript/object_factory.h"
24 #include "ecmascript/tagged_array-inl.h"
25 
26 namespace panda::ecmascript::containers {
QueueConstructor(EcmaRuntimeCallInfo * argv)27 JSTaggedValue ContainersQueue::QueueConstructor(EcmaRuntimeCallInfo *argv)
28 {
29     ASSERT(argv);
30     BUILTINS_API_TRACE(argv->GetThread(), Queue, Constructor);
31     JSThread *thread = argv->GetThread();
32     [[maybe_unused]] EcmaHandleScope handleScope(thread);
33     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
34     JSHandle<JSTaggedValue> newTarget = GetNewTarget(argv);
35     if (newTarget->IsUndefined()) {
36         JSTaggedValue error =
37             ContainerError::BusinessError(thread, ErrorFlag::IS_NULL_ERROR,
38                                           "The Queue's constructor cannot be directly invoked");
39         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
40     }
41     JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
42     JSHandle<JSObject> obj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
43     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
44     JSHandle<TaggedArray> newTaggedArray = factory->NewTaggedArray(JSAPIQueue::DEFAULT_CAPACITY_LENGTH);
45     obj->SetElements(thread, newTaggedArray);
46 
47     return obj.GetTaggedValue();
48 }
49 
Add(EcmaRuntimeCallInfo * argv)50 JSTaggedValue ContainersQueue::Add(EcmaRuntimeCallInfo *argv)
51 {
52     ASSERT(argv);
53     BUILTINS_API_TRACE(argv->GetThread(), Queue, Add);
54     JSThread *thread = argv->GetThread();
55     [[maybe_unused]] EcmaHandleScope handleScope(thread);
56     JSHandle<JSTaggedValue> self = GetThis(argv);
57 
58     if (!self->IsJSAPIQueue()) {
59         if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIQueue()) {
60             self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
61         } else {
62             JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
63                                                                 "The add method cannot be bound");
64             THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
65         }
66     }
67 
68     JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
69     JSAPIQueue::Add(thread, JSHandle<JSAPIQueue>::Cast(self), value);
70     return JSTaggedValue::True();
71 }
72 
GetFirst(EcmaRuntimeCallInfo * argv)73 JSTaggedValue ContainersQueue::GetFirst(EcmaRuntimeCallInfo *argv)
74 {
75     ASSERT(argv);
76     BUILTINS_API_TRACE(argv->GetThread(), Queue, Add);
77     JSThread *thread = argv->GetThread();
78     [[maybe_unused]] EcmaHandleScope handleScope(thread);
79     JSHandle<JSTaggedValue> self = GetThis(argv);
80 
81     if (!self->IsJSAPIQueue()) {
82         if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIQueue()) {
83             self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
84         } else {
85             JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
86                                                                 "The getFirst method cannot be bound");
87             THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
88         }
89     }
90 
91     JSTaggedValue value = JSAPIQueue::GetFirst(thread, JSHandle<JSAPIQueue>::Cast(self));
92     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::False());
93     return value;
94 }
95 
Pop(EcmaRuntimeCallInfo * argv)96 JSTaggedValue ContainersQueue::Pop(EcmaRuntimeCallInfo *argv)
97 {
98     ASSERT(argv);
99     BUILTINS_API_TRACE(argv->GetThread(), Queue, Add);
100     JSThread *thread = argv->GetThread();
101     [[maybe_unused]] EcmaHandleScope handleScope(thread);
102     JSHandle<JSTaggedValue> self = GetThis(argv);
103 
104     if (!self->IsJSAPIQueue()) {
105         if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIQueue()) {
106             self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
107         } else {
108             JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
109                                                                 "The pop method cannot be bound");
110             THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
111         }
112     }
113 
114     JSTaggedValue value = JSAPIQueue::Pop(thread, JSHandle<JSAPIQueue>::Cast(self));
115     RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::False());
116     return value;
117 }
118 
ForEach(EcmaRuntimeCallInfo * argv)119 JSTaggedValue ContainersQueue::ForEach(EcmaRuntimeCallInfo *argv)
120 {
121     ASSERT(argv);
122     BUILTINS_API_TRACE(argv->GetThread(), Queue, ForEach);
123     JSThread *thread = argv->GetThread();
124     [[maybe_unused]] EcmaHandleScope handleScope(thread);
125 
126     // Let O be ToObject(this value).
127     JSHandle<JSTaggedValue> thisHandle = GetThis(argv); // JSAPIQueue
128     if (!thisHandle->IsJSAPIQueue()) {
129         if (thisHandle->IsJSProxy() && JSHandle<JSProxy>::Cast(thisHandle)->GetTarget().IsJSAPIQueue()) {
130             thisHandle = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(thisHandle)->GetTarget());
131         } else {
132             JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
133                                                                 "The forEach method cannot be bound");
134             THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
135         }
136     }
137 
138     JSHandle<JSAPIQueue> queue = JSHandle<JSAPIQueue>::Cast(thisHandle);
139     // Let len be ToLength(Get(O, "length")).
140     uint32_t len = JSAPIQueue::GetArrayLength(thread, queue);
141     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
142 
143     JSHandle<JSTaggedValue> callbackFnHandle = GetCallArg(argv, 0);
144     // If IsCallable(callbackfn) is false, throw a TypeError exception.
145     if (!callbackFnHandle->IsCallable()) {
146         JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, callbackFnHandle.GetTaggedValue());
147         RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
148         CString errorMsg =
149             "The type of \"callbackfn\" must be callable. Received value is: " + ConvertToString(*result);
150         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
151         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
152     }
153     // If thisArg was supplied, let T be thisArg; else let T be undefined.
154     JSHandle<JSTaggedValue> thisArgHandle = GetCallArg(argv, 1);
155 
156     JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
157     uint32_t index = 0;
158     uint32_t k = 0;
159     JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
160     while (k < len) {
161         JSHandle<JSTaggedValue> kValue =
162             JSHandle<JSTaggedValue>(thread, queue->Get(thread, index));
163         index = queue->GetNextPosition(index);
164         key.Update(JSTaggedValue(k));
165         const uint32_t argsLength = 3;
166         EcmaRuntimeCallInfo *info =
167             EcmaInterpreter::NewRuntimeCallInfo(thread, callbackFnHandle, thisArgHandle, undefined, argsLength);
168         RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
169         info->SetCallArg(kValue.GetTaggedValue(), key.GetTaggedValue(), thisHandle.GetTaggedValue());
170         JSTaggedValue funcResult = JSFunction::Call(info);
171         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, funcResult);
172         k++;
173     }
174     return JSTaggedValue::Undefined();
175 }
176 
GetIteratorObj(EcmaRuntimeCallInfo * argv)177 JSTaggedValue ContainersQueue::GetIteratorObj(EcmaRuntimeCallInfo *argv)
178 {
179     ASSERT(argv);
180     BUILTINS_API_TRACE(argv->GetThread(), Queue, GetIteratorObj);
181     JSThread *thread = argv->GetThread();
182     [[maybe_unused]] EcmaHandleScope handleScope(thread);
183     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
184     JSHandle<JSTaggedValue> self = GetThis(argv);
185     if (!self->IsJSAPIQueue()) {
186         if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIQueue()) {
187             self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
188         } else {
189             JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
190                                                                 "The remove method cannot be bound");
191             THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
192         }
193     }
194     JSHandle<JSAPIQueueIterator> iter(factory->NewJSAPIQueueIterator(JSHandle<JSAPIQueue>::Cast(self)));
195     return iter.GetTaggedValue();
196 }
197 
GetSize(EcmaRuntimeCallInfo * argv)198 JSTaggedValue ContainersQueue::GetSize(EcmaRuntimeCallInfo *argv)
199 {
200     ASSERT(argv);
201     BUILTINS_API_TRACE(argv->GetThread(), Queue, GetSize);
202     JSThread *thread = argv->GetThread();
203     [[maybe_unused]] EcmaHandleScope handleScope(thread);
204     JSHandle<JSTaggedValue> self = GetThis(argv);
205 
206     if (!self->IsJSAPIQueue()) {
207         if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIQueue()) {
208             self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
209         } else {
210             JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
211                                                                 "The Symbol.iterator method cannot be bound");
212             THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
213         }
214     }
215 
216     uint32_t length = JSHandle<JSAPIQueue>::Cast(self)->GetSize();
217     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
218 
219     return JSTaggedValue(length);
220 }
221 } // namespace panda::ecmascript::containers
222