• 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         CString errorMsg =
148             "The type of \"callbackfn\" must be callable. Received value is: " + ConvertToString(*result);
149         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
150         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
151     }
152     // If thisArg was supplied, let T be thisArg; else let T be undefined.
153     JSHandle<JSTaggedValue> thisArgHandle = GetCallArg(argv, 1);
154 
155     JSMutableHandle<JSTaggedValue> key(thread, JSTaggedValue::Undefined());
156     uint32_t index = 0;
157     uint32_t k = 0;
158     JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
159     while (k < len) {
160         JSHandle<JSTaggedValue> kValue =
161             JSHandle<JSTaggedValue>(thread, queue->Get(thread, index));
162         index = queue->GetNextPosition(index);
163         key.Update(JSTaggedValue(k));
164         const int32_t argsLength = 3;
165         EcmaRuntimeCallInfo *info =
166             EcmaInterpreter::NewRuntimeCallInfo(thread, callbackFnHandle, thisArgHandle, undefined, argsLength);
167         RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
168         info->SetCallArg(kValue.GetTaggedValue(), key.GetTaggedValue(), thisHandle.GetTaggedValue());
169         JSTaggedValue funcResult = JSFunction::Call(info);
170         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, funcResult);
171         k++;
172     }
173     return JSTaggedValue::Undefined();
174 }
175 
GetIteratorObj(EcmaRuntimeCallInfo * argv)176 JSTaggedValue ContainersQueue::GetIteratorObj(EcmaRuntimeCallInfo *argv)
177 {
178     ASSERT(argv);
179     BUILTINS_API_TRACE(argv->GetThread(), Queue, GetIteratorObj);
180     JSThread *thread = argv->GetThread();
181     [[maybe_unused]] EcmaHandleScope handleScope(thread);
182     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
183     JSHandle<JSTaggedValue> self = GetThis(argv);
184     if (!self->IsJSAPIQueue()) {
185         if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIQueue()) {
186             self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
187         } else {
188             JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
189                                                                 "The remove method cannot be bound");
190             THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
191         }
192     }
193     JSHandle<JSAPIQueueIterator> iter(factory->NewJSAPIQueueIterator(JSHandle<JSAPIQueue>::Cast(self)));
194     return iter.GetTaggedValue();
195 }
196 
GetSize(EcmaRuntimeCallInfo * argv)197 JSTaggedValue ContainersQueue::GetSize(EcmaRuntimeCallInfo *argv)
198 {
199     ASSERT(argv);
200     BUILTINS_API_TRACE(argv->GetThread(), Queue, GetSize);
201     JSThread *thread = argv->GetThread();
202     [[maybe_unused]] EcmaHandleScope handleScope(thread);
203     JSHandle<JSTaggedValue> self = GetThis(argv);
204 
205     if (!self->IsJSAPIQueue()) {
206         if (self->IsJSProxy() && JSHandle<JSProxy>::Cast(self)->GetTarget().IsJSAPIQueue()) {
207             self = JSHandle<JSTaggedValue>(thread, JSHandle<JSProxy>::Cast(self)->GetTarget());
208         } else {
209             JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::BIND_ERROR,
210                                                                 "The Symbol.iterator method cannot be bound");
211             THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, JSTaggedValue::Exception());
212         }
213     }
214 
215     uint32_t length = JSHandle<JSAPIQueue>::Cast(self)->GetSize();
216     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
217 
218     return JSTaggedValue(length);
219 }
220 } // namespace panda::ecmascript::containers
221