• 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/js_tagged_value.h"
20 #include "ecmascript/object_factory.h"
21 
22 namespace panda::ecmascript {
23 using ContainerError = containers::ContainerError;
24 using ErrorFlag = containers::ErrorFlag;
Empty()25 bool JSAPIStack::Empty()
26 {
27     if (this->GetTop() == -1) {
28         return true;
29     }
30     return false;
31 }
32 
Push(JSThread * thread,const JSHandle<JSAPIStack> & stack,const JSHandle<JSTaggedValue> & value)33 JSTaggedValue JSAPIStack::Push(JSThread *thread, const JSHandle<JSAPIStack> &stack,
34                                const JSHandle<JSTaggedValue> &value)
35 {
36     int top = static_cast<int>(stack->GetTop());
37     JSHandle<TaggedArray> elements = GrowCapacity(thread, stack, top + 1);
38 
39     ASSERT(!elements->IsDictionaryMode());
40     elements->Set(thread, top + 1, value);
41     stack->SetTop(++top);
42     return value.GetTaggedValue();
43 }
44 
Peek()45 JSTaggedValue JSAPIStack::Peek()
46 {
47     int top = this->GetTop();
48     if (top == -1) {
49         return JSTaggedValue::Undefined();
50     }
51 
52     TaggedArray *elements = TaggedArray::Cast(this->GetElements().GetTaggedObject());
53     ASSERT(!elements->IsDictionaryMode());
54     return elements->Get(top);
55 }
56 
Pop()57 JSTaggedValue JSAPIStack::Pop()
58 {
59     int top = this->GetTop();
60     if (top == -1) {
61         return JSTaggedValue::Undefined();
62     }
63     TaggedArray *elements = TaggedArray::Cast(this->GetElements().GetTaggedObject());
64     ASSERT(!elements->IsDictionaryMode());
65     this->SetTop(--top);
66     return elements->Get(top + 1);
67 }
68 
Search(const JSHandle<JSTaggedValue> & value)69 int JSAPIStack::Search(const JSHandle<JSTaggedValue> &value)
70 {
71     int top = this->GetTop();
72     TaggedArray *elements = TaggedArray::Cast(this->GetElements().GetTaggedObject());
73     ASSERT(!elements->IsDictionaryMode());
74     for (int i = 0; i <= top; i++) {
75         if (value.GetTaggedValue() == elements->Get(i)) {
76             return i;
77         }
78     }
79     return -1;
80 }
81 
GrowCapacity(const JSThread * thread,const JSHandle<JSAPIStack> & obj,uint32_t capacity)82 JSHandle<TaggedArray> JSAPIStack::GrowCapacity(const JSThread *thread, const JSHandle<JSAPIStack> &obj,
83                                                uint32_t capacity)
84 {
85     JSHandle<TaggedArray> oldElements(thread, obj->GetElements());
86     uint32_t oldLength = oldElements->GetLength();
87     if (capacity < oldLength) {
88         return oldElements;
89     }
90     uint32_t newCapacity = ComputeCapacity(capacity);
91     JSHandle<TaggedArray> newElements =
92         thread->GetEcmaVM()->GetFactory()->CopyArray(oldElements, oldLength, newCapacity);
93 
94     obj->SetElements(thread, newElements);
95     return newElements;
96 }
97 
98 
Get(const uint32_t index)99 JSTaggedValue JSAPIStack::Get(const uint32_t index)
100 {
101     ASSERT(static_cast<int>(index) <= GetTop());
102     TaggedArray *elements = TaggedArray::Cast(GetElements().GetTaggedObject());
103     return elements->Get(index);
104 }
105 
Set(JSThread * thread,const uint32_t index,JSTaggedValue value)106 JSTaggedValue JSAPIStack::Set(JSThread *thread, const uint32_t index, JSTaggedValue value)
107 {
108     TaggedArray *elements = TaggedArray::Cast(GetElements().GetTaggedObject());
109     elements->Set(thread, index, value);
110     return JSTaggedValue::Undefined();
111 }
112 
Has(JSTaggedValue value) const113 bool JSAPIStack::Has(JSTaggedValue value) const
114 {
115     TaggedArray *elements = TaggedArray::Cast(GetElements().GetTaggedObject());
116     int top = static_cast<int>(GetTop());
117     if (top == -1) {
118         return false;
119     }
120 
121     for (int i = 0; i < top + 1; i++) {
122         if (JSTaggedValue::SameValue(elements->Get(i), value)) {
123             return true;
124         }
125     }
126     return false;
127 }
128 
OwnKeys(JSThread * thread,const JSHandle<JSAPIStack> & obj)129 JSHandle<TaggedArray> JSAPIStack::OwnKeys(JSThread *thread, const JSHandle<JSAPIStack> &obj)
130 {
131     uint32_t top = static_cast<uint32_t>(obj->GetTop());
132     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
133     JSHandle<TaggedArray> keys = factory->NewTaggedArray(top + 1);
134 
135     for (uint32_t i = 0; i < top + 1; i++) {
136         keys->Set(thread, i, JSTaggedValue(i));
137     }
138 
139     return keys;
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         CString errorMsg =
149             "The type of \"index\" can not obtain attributes of no-number type. Received value is: "
150             + ConvertToString(*result);
151         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
152         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
153     }
154 
155     uint32_t length = static_cast<uint32_t>(obj->GetTop() + 1);
156     if (index >= length) {
157         std::ostringstream oss;
158         oss << "The value of \"index\" is out of range. It must be > " << (length - 1)
159             << ". Received value is: " << index;
160         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
161         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
162     }
163 
164     obj->Get(index);
165     return true;
166 }
167 
GetProperty(JSThread * thread,const JSHandle<JSAPIStack> & obj,const JSHandle<JSTaggedValue> & key)168 OperationResult JSAPIStack::GetProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj,
169                                         const JSHandle<JSTaggedValue> &key)
170 {
171     int length = obj->GetTop() + 1;
172     int index = key->GetInt();
173     if (index < 0 || index >= length) {
174         std::ostringstream oss;
175         oss << "The value of \"index\" is out of range. It must be >= 0 && <= " << (length - 1)
176             << ". Received value is: " << index;
177         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
178         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, OperationResult(thread,
179                                                                         JSTaggedValue::Exception(),
180                                                                         PropertyMetaData(false)));
181     }
182 
183     return OperationResult(thread, obj->Get(index), PropertyMetaData(false));
184 }
185 
SetProperty(JSThread * thread,const JSHandle<JSAPIStack> & obj,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)186 bool JSAPIStack::SetProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj,
187                              const JSHandle<JSTaggedValue> &key,
188                              const JSHandle<JSTaggedValue> &value)
189 {
190     int length = obj->GetTop() + 1;
191     int index = key->GetInt();
192     if (index < 0 || index >= length) {
193         return false;
194     }
195 
196     obj->Set(thread, index, value.GetTaggedValue());
197     return true;
198 }
199 }  // namespace panda::ecmascript
200