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()44 JSTaggedValue JSAPIStack::Peek()
45 {
46 int top = this->GetTop();
47 if (top == -1) {
48 return JSTaggedValue::Undefined();
49 }
50
51 TaggedArray *elements = TaggedArray::Cast(this->GetElements().GetTaggedObject());
52 ASSERT(!elements->IsDictionaryMode());
53 return elements->Get(top);
54 }
55
Pop()56 JSTaggedValue JSAPIStack::Pop()
57 {
58 int top = this->GetTop();
59 if (top == -1) {
60 return JSTaggedValue::Undefined();
61 }
62 TaggedArray *elements = TaggedArray::Cast(this->GetElements().GetTaggedObject());
63 ASSERT(!elements->IsDictionaryMode());
64 this->SetTop(--top);
65 return elements->Get(top + 1);
66 }
67
Search(const JSHandle<JSTaggedValue> & value)68 int JSAPIStack::Search(const JSHandle<JSTaggedValue> &value)
69 {
70 int top = this->GetTop();
71 TaggedArray *elements = TaggedArray::Cast(this->GetElements().GetTaggedObject());
72 ASSERT(!elements->IsDictionaryMode());
73 for (int i = 0; i <= top; i++) {
74 if (value.GetTaggedValue() == elements->Get(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());
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(const uint32_t index)98 JSTaggedValue JSAPIStack::Get(const uint32_t index)
99 {
100 ASSERT(static_cast<int>(index) <= GetTop());
101 TaggedArray *elements = TaggedArray::Cast(GetElements().GetTaggedObject());
102 return elements->Get(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 uint32_t length = GetSize() + 1;
108 if (index >= length) {
109 return JSTaggedValue::Undefined();
110 }
111 TaggedArray *elements = TaggedArray::Cast(GetElements().GetTaggedObject());
112 elements->Set(thread, index, value);
113 return JSTaggedValue::Undefined();
114 }
115
Has(JSTaggedValue value) const116 bool JSAPIStack::Has(JSTaggedValue value) const
117 {
118 TaggedArray *elements = TaggedArray::Cast(GetElements().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(elements->Get(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(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(*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 (index >= length) {
158 std::ostringstream oss;
159 ASSERT(length > 0);
160 oss << "The value of \"index\" is out of range. It must be > " << (length - 1)
161 << ". Received value is: " << index;
162 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
163 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
164 }
165
166 obj->Get(index);
167 return true;
168 }
169
GetProperty(JSThread * thread,const JSHandle<JSAPIStack> & obj,const JSHandle<JSTaggedValue> & key)170 OperationResult JSAPIStack::GetProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj,
171 const JSHandle<JSTaggedValue> &key)
172 {
173 int length = obj->GetTop() + 1;
174 if (length == 0) {
175 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, "Container is empty");
176 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, OperationResult(thread,
177 JSTaggedValue::Exception(),
178 PropertyMetaData(false)));
179 }
180 JSHandle<JSTaggedValue> indexKey = key;
181 if (indexKey->IsDouble()) {
182 // Math.floor(1) will produce TaggedDouble, we need to cast into TaggedInt
183 // For integer which is greater than INT32_MAX, it will remain TaggedDouble
184 indexKey = JSHandle<JSTaggedValue>(thread, JSTaggedValue::TryCastDoubleToInt32(indexKey->GetDouble()));
185 }
186 if (!indexKey->IsInt()) {
187 CString errorMsg = "The type of \"index\" must be small integer.";
188 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
189 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error,
190 OperationResult(thread, JSTaggedValue::Exception(), PropertyMetaData(false)));
191 }
192
193 int index = indexKey->GetInt();
194 if (index < 0 || index >= length) {
195 std::ostringstream oss;
196 oss << "The value of \"index\" is out of range. It must be >= 0 && <= " << (length - 1)
197 << ". Received value is: " << index;
198 JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
199 THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, OperationResult(thread,
200 JSTaggedValue::Exception(),
201 PropertyMetaData(false)));
202 }
203
204 return OperationResult(thread, obj->Get(index), PropertyMetaData(false));
205 }
206
SetProperty(JSThread * thread,const JSHandle<JSAPIStack> & obj,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)207 bool JSAPIStack::SetProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj,
208 const JSHandle<JSTaggedValue> &key,
209 const JSHandle<JSTaggedValue> &value)
210 {
211 int length = obj->GetTop() + 1;
212 int index = key->GetInt();
213 if (index < 0 || index >= length) {
214 return false;
215 }
216
217 obj->Set(thread, index, value.GetTaggedValue());
218 return true;
219 }
220 } // namespace panda::ecmascript
221