• 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     return JSObject::GetOwnPropertyKeys(thread, JSHandle<JSObject>::Cast(obj));
132 }
133 
OwnEnumKeys(JSThread * thread,const JSHandle<JSAPIStack> & obj)134 JSHandle<TaggedArray> JSAPIStack::OwnEnumKeys(JSThread *thread, const JSHandle<JSAPIStack> &obj)
135 {
136     return JSObject::GetOwnEnumPropertyKeys(thread, JSHandle<JSObject>::Cast(obj));
137 }
138 
GetOwnProperty(JSThread * thread,const JSHandle<JSAPIStack> & obj,const JSHandle<JSTaggedValue> & key)139 bool JSAPIStack::GetOwnProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj,
140                                 const JSHandle<JSTaggedValue> &key)
141 {
142     uint32_t index = 0;
143     if (UNLIKELY(!JSTaggedValue::ToElementIndex(key.GetTaggedValue(), &index))) {
144         JSHandle<EcmaString> result = JSTaggedValue::ToString(thread, key.GetTaggedValue());
145         RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
146         CString errorMsg =
147             "The type of \"index\" can not obtain attributes of no-number type. Received value is: "
148             + ConvertToString(*result);
149         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::TYPE_ERROR, errorMsg.c_str());
150         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
151     }
152 
153     uint32_t length = static_cast<uint32_t>(obj->GetTop() + 1);
154     if (index >= length) {
155         std::ostringstream oss;
156         oss << "The value of \"index\" is out of range. It must be > " << (length - 1)
157             << ". Received value is: " << index;
158         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
159         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, false);
160     }
161 
162     obj->Get(index);
163     return true;
164 }
165 
GetProperty(JSThread * thread,const JSHandle<JSAPIStack> & obj,const JSHandle<JSTaggedValue> & key)166 OperationResult JSAPIStack::GetProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj,
167                                         const JSHandle<JSTaggedValue> &key)
168 {
169     int length = obj->GetTop() + 1;
170     int index = key->GetInt();
171     if (index < 0 || index >= length) {
172         std::ostringstream oss;
173         oss << "The value of \"index\" is out of range. It must be >= 0 && <= " << (length - 1)
174             << ". Received value is: " << index;
175         JSTaggedValue error = ContainerError::BusinessError(thread, ErrorFlag::RANGE_ERROR, oss.str().c_str());
176         THROW_NEW_ERROR_AND_RETURN_VALUE(thread, error, OperationResult(thread,
177                                                                         JSTaggedValue::Exception(),
178                                                                         PropertyMetaData(false)));
179     }
180 
181     return OperationResult(thread, obj->Get(index), PropertyMetaData(false));
182 }
183 
SetProperty(JSThread * thread,const JSHandle<JSAPIStack> & obj,const JSHandle<JSTaggedValue> & key,const JSHandle<JSTaggedValue> & value)184 bool JSAPIStack::SetProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj,
185                              const JSHandle<JSTaggedValue> &key,
186                              const JSHandle<JSTaggedValue> &value)
187 {
188     int length = obj->GetTop() + 1;
189     int index = key->GetInt();
190     if (index < 0 || index >= length) {
191         return false;
192     }
193 
194     obj->Set(thread, index, value.GetTaggedValue());
195     return true;
196 }
197 }  // namespace panda::ecmascript
198