• 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 #ifndef ECMASCRIPT_JS_API_STACK_H
17 #define ECMASCRIPT_JS_API_STACK_H
18 
19 #include "ecmascript/js_object.h"
20 #include "ecmascript/js_tagged_value-inl.h"
21 
22 namespace panda::ecmascript {
23 class JSAPIStack : public JSObject {
24 public:
25     static constexpr int DEFAULT_CAPACITY_LENGTH = 10;
Cast(TaggedObject * object)26     static JSAPIStack *Cast(TaggedObject *object)
27     {
28         ASSERT(JSTaggedValue(object).IsJSAPIStack());
29         return static_cast<JSAPIStack *>(object);
30     }
31 
32     static JSTaggedValue Push(JSThread *thread, const JSHandle<JSAPIStack> &stack,
33                               const JSHandle<JSTaggedValue> &value);
34 
35     static JSHandle<TaggedArray> OwnKeys(JSThread *thread, const JSHandle<JSAPIStack> &obj);
36 
37     static bool GetOwnProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj, const JSHandle<JSTaggedValue> &key);
38     static OperationResult GetProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj,
39                                        const JSHandle<JSTaggedValue> &key);
40     static bool SetProperty(JSThread *thread, const JSHandle<JSAPIStack> &obj,
41                             const JSHandle<JSTaggedValue> &key,
42                             const JSHandle<JSTaggedValue> &value);
43     bool Empty();
44 
45     bool Has(JSTaggedValue value) const;
46 
47     int Search(const JSHandle<JSTaggedValue> &value);
48 
GetSize()49     inline int GetSize() const
50     {
51         return GetTop();
52     }
53 
54     JSTaggedValue Peek();
55 
56     JSTaggedValue Pop();
57 
58     JSTaggedValue Get(const uint32_t index);
59 
60     JSTaggedValue Set(JSThread *thread, const uint32_t index, JSTaggedValue value);
61 
62     static constexpr size_t TOP_OFFSET = JSObject::SIZE;
63     ACCESSORS_PRIMITIVE_FIELD(Top, int, TOP_OFFSET, LAST_OFFSET)
64     DEFINE_ALIGN_SIZE(LAST_OFFSET);
65 
DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject,LAST_OFFSET,LAST_OFFSET)66     DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LAST_OFFSET, LAST_OFFSET)
67     DECL_DUMP()
68 private:
69     inline static uint32_t ComputeCapacity(uint32_t oldCapacity)
70     {
71         uint32_t newCapacity = oldCapacity + (oldCapacity >> 1U);
72         return newCapacity > DEFAULT_CAPACITY_LENGTH ? newCapacity : DEFAULT_CAPACITY_LENGTH;
73     }
74     static JSHandle<TaggedArray> GrowCapacity(const JSThread *thread, const JSHandle<JSAPIStack> &obj,
75                                               uint32_t capacity);
76 };
77 }  // namespace panda::ecmascript
78 
79 #endif  // ECMASCRIPT_JS_API_STACK_H
80