• 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/js_api/js_api_stack.h"
17 
18 #include "ecmascript/containers/containers_errors.h"
19 #include "ecmascript/object_factory-inl.h"
20 
21 namespace panda::ecmascript {
22 using ContainerError = containers::ContainerError;
23 using ErrorFlag = containers::ErrorFlag;
Empty()24 bool JSAPIStack::Empty()
25 {
26     if (this->GetTop() == -1) {
27         return true;
28     }
29     return false;
30 }
31 
Push(JSThread * thread,const JSHandle<JSAPIStack> & stack,const JSHandle<JSTaggedValue> & value)32 JSTaggedValue JSAPIStack::Push(JSThread *thread, const JSHandle<JSAPIStack> &stack,
33                                const JSHandle<JSTaggedValue> &value)
34 {
35     int top = static_cast<int>(stack->GetTop());
36     JSHandle<TaggedArray> elements = GrowCapacity(thread, stack, top + 1);
37 
38     ASSERT(!elements->IsDictionaryMode());
39     elements->Set(thread, top + 1, value);
40     stack->SetTop(++top);
41     return value.GetTaggedValue();
42 }
43 
Peek(JSThread * thread)44 JSTaggedValue JSAPIStack::Peek(JSThread *thread)
45 {
46     int top = this->GetTop();
47     if (top == -1) {
48         return JSTaggedValue::Undefined();
49     }
50 
51     TaggedArray *elements = TaggedArray::Cast(this->GetElements(thread).GetTaggedObject());
52     ASSERT(!elements->IsDictionaryMode());
53     return elements->Get(thread, top);
54 }
55 
Pop(JSThread * thread)56 JSTaggedValue JSAPIStack::Pop(JSThread *thread)
57 {
58     int top = this->GetTop();
59     if (top == -1) {
60         return JSTaggedValue::Undefined();
61     }
62     TaggedArray *elements = TaggedArray::Cast(this->GetElements(thread).GetTaggedObject());
63     ASSERT(!elements->IsDictionaryMode());
64     this->SetTop(--top);
65     return elements->Get(thread, top + 1);
66 }
67 
Search(JSThread * thread,const JSHandle<JSTaggedValue> & value)68 int JSAPIStack::Search(JSThread *thread, const JSHandle<JSTaggedValue> &value)
69 {
70     int top = this->GetTop();
71     TaggedArray *elements = TaggedArray::Cast(this->GetElements(thread).GetTaggedObject());
72     ASSERT(!elements->IsDictionaryMode());
73     for (int i = 0; i <= top; i++) {
74         if (value.GetTaggedValue() == elements->Get(thread, i)) {
75             return i;
76         }
77     }
78     return -1;
79 }
80 
GrowCapacity(const JSThread * thread,const JSHandle<JSAPIStack> & obj,uint32_t capacity)81 JSHandle<TaggedArray> JSAPIStack::GrowCapacity(const JSThread *thread, const JSHandle<JSAPIStack> &obj,
82                                                uint32_t capacity)
83 {
84     JSHandle<TaggedArray> oldElements(thread, obj->GetElements(thread));
85     uint32_t oldLength = oldElements->GetLength();
86     if (capacity < oldLength) {
87         return oldElements;
88     }
89     uint32_t newCapacity = ComputeCapacity(capacity);
90     JSHandle<TaggedArray> newElements =
91         thread->GetEcmaVM()->GetFactory()->CopyArray(oldElements, oldLength, newCapacity);
92 
93     obj->SetElements(thread, newElements);
94     return newElements;
95 }
96 
97 
Get(JSThread * thread,const uint32_t index)98 JSTaggedValue JSAPIStack::Get(JSThread *thread, const uint32_t index)
99 {
100     ASSERT(static_cast<int>(index) <= GetTop());
101     TaggedArray *elements = TaggedArray::Cast(GetElements(thread).GetTaggedObject());
102     return elements->Get(thread, index);
103 }
104 
Set(JSThread * thread,const uint32_t index,JSTaggedValue value)105 JSTaggedValue JSAPIStack::Set(JSThread *thread, const uint32_t index, JSTaggedValue value)
106 {
107     int32_t length = GetSize() + 1;
108     if (index >= static_cast<uint32_t>(length)) {
109         return JSTaggedValue::Undefined();
110     }
111     TaggedArray *elements = TaggedArray::Cast(GetElements(thread).GetTaggedObject());
112     elements->Set(thread, index, value);
113     return JSTaggedValue::Undefined();
114 }
115 
Has(JSThread * thread,JSTaggedValue value) const116 bool JSAPIStack::Has(JSThread *thread, JSTaggedValue value) const
117 {
118     TaggedArray *elements = TaggedArray::Cast(GetElements(thread).GetTaggedObject());
119     int top = static_cast<int>(GetTop());
120     if (top == -1) {
121         return false;
122     }
123 
124     for (int i = 0; i < top + 1; i++) {
125         if (JSTaggedValue::SameValue(thread, elements->Get(thread, i), value)) {
126             return true;
127         }
128     }
129     return false;
130 }
131 
OwnKeys(JSThread * thread,const JSHandle<JSAPIStack> & obj)132 JSHandle<TaggedArray> JSAPIStack::OwnKeys(JSThread *thread, const JSHandle<JSAPIStack> &obj)
133 {
134     return JSObject::GetOwnPropertyKeys(thread, JSHandle<JSObject>::Cast(obj));
135 }
136 
OwnEnumKeys(JSThread * thread,const JSHandle<JSAPIStack> & obj)137 JSHandle<TaggedArray> JSAPIStack::OwnEnumKeys(JSThread *thread, const JSHandle<JSAPIStack> &obj)
138 {
139     return JSObject::GetOwnEnumPropertyKeys(thread, JSHandle<JSObject>::Cast(obj));
140 }
141 
GetOwnProperty(JSThread * thread,const JSHandle<JSAPIStack> & obj,const JSHandle<JSTaggedValue> & key)142 bool JSAPIStack::GetOwnProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj,
143                                 const JSHandle<JSTaggedValue> &key)
144 {
145     uint32_t index = 0;
146     if (UNLIKELY(!JSTaggedValue::ToElementIndex(thread, key.GetTaggedValue(), &index))) {
147         JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, key.GetTaggedValue());
148         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
149         CString errorMsg =
150             "The type of \"index\" can not obtain attributes of no-number type. Received value is: "
151             + ConvertToString(thread, *result);
152         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
153         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
154     }
155 
156     uint32_t length = static_cast<uint32_t>(obj->GetTop() + 1);
157     if (length == 0) {
158         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, "Container is empty");
159         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
160     }
161     if (index >= length) {
162         std::ostringstream oss;
163         ASSERT(length > 0);
164         oss << "The value of \"index\" is out of range. It must be > " << (length - 1)
165             << ". Received value is: " << index;
166         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
167         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
168     }
169 
170     obj->Get(thread, index);
171     return true;
172 }
173 
GetProperty(JSThread * thread,const JSHandle<JSAPIStack> & obj,const JSHandle<JSTaggedValue> & key)174 OperationResult JSAPIStack::GetProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj,
175                                         const JSHandle<JSTaggedValue> &key)
176 {
177     int length = obj->GetTop() + 1;
178     if (length == 0) {
179         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, "Container is empty");
180         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, OperationResult(thread,
181                                                                         JSTaggedValue::Exception(),
182                                                                         PropertyMetaData(false)));
183     }
184     JSHandle<JSTaggedValue> indexKey = key;
185     if (indexKey->IsDouble()) {
186         // Math.floor(1) will produce TaggedDouble, we need to cast into TaggedInt
187         // For integer which is greater than INT32_MAX, it will remain TaggedDouble
188         indexKey = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(indexKey->GetDouble()));
189     }
190     if (!indexKey->IsInt()) {
191         CString errorMsg = "The type of \"index\" must be small integer.";
192         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
193         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error,
194                                          OperationResult(thread, JSTaggedValue::Exception(), PropertyMetaData(false)));
195     }
196 
197     int index = indexKey->GetInt();
198     if (index < 0 || index >= length) {
199         std::ostringstream oss;
200         oss << "The value of \"index\" is out of range. It must be >= 0 && <= " << (length - 1)
201             << ". Received value is: " << index;
202         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
203         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, OperationResult(thread,
204                                                                         JSTaggedValue::Exception(),
205                                                                         PropertyMetaData(false)));
206     }
207 
208     return OperationResult(thread, obj->Get(thread, index), PropertyMetaData(false));
209 }
210 
SetProperty(JSThread * thread,const JSHandle<JSAPIStack> & obj,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)211 bool JSAPIStack::SetProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj,
212                              const JSHandle<JSTaggedValue> &key,
213                              const JSHandle<JSTaggedValue> &value)
214 {
215     int length = obj->GetTop() + 1;
216     JSHandle<JSTaggedValue> indexKey = key;
217     if (indexKey->IsDouble()) {
218         // Math.floor(1) will produce TaggedDouble, we need to cast into TaggedInt
219         // For integer which is greater than INT32_MAX, it will remain TaggedDouble
220         indexKey = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(indexKey->GetDouble()));
221     }
222     if (!indexKey->IsInt()) {
223         return false;
224     }
225 
226     int index = indexKey->GetInt();
227     if (index < 0 || index >= length) {
228         return false;
229     }
230 
231     obj->Set(thread, index, value.GetTaggedValue());
232     return true;
233 }
234 }  // namespace panda::ecmascript
235