• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_JS_API_BITVECTOR_H
17 #define ECMASCRIPT_JS_API_JS_API_BITVECTOR_H
18 
19 #include "ecmascript/js_object.h"
20 #include "ecmascript/js_tagged_value-inl.h"
21 
22 namespace panda::ecmascript {
23 class JSAPIBitVector : public JSObject {
24 public:
25     static constexpr int32_t BIT_SET_LENGTH = 64;
26     static constexpr int32_t TAGGED_VALUE_BIT_SIZE = 6;
27     static constexpr uint64_t TAGGED_VALUE_BIT_OFFSET = 0x3FULL;
Cast(TaggedObject * object)28     static JSAPIBitVector* Cast(TaggedObject* object)
29     {
30         ASSERT(JSTaggedValue(object).IsJSAPIBitVector());
31         return static_cast<JSAPIBitVector*>(object);
32     }
33 
34     static bool Push(JSThread* thread, const JSHandle<JSAPIBitVector>& bitVector, const JSHandle<JSTaggedValue>& value);
35     static JSTaggedValue Pop(JSThread* thread, const JSHandle<JSAPIBitVector>& bitVector);
36 
37     static JSTaggedValue GetIteratorObj(JSThread* thread, const JSHandle<JSAPIBitVector>& obj);
38 
39     static JSTaggedValue SetBitsByRange(JSThread* thread, const JSHandle<JSAPIBitVector>& bitVector,
40         const JSHandle<JSTaggedValue>& value, const JSHandle<JSTaggedValue>& value1,
41         const JSHandle<JSTaggedValue>& value2);
42     static JSTaggedValue GetBitsByRange(JSThread* thread, const JSHandle<JSAPIBitVector>& bitVector,
43         const JSHandle<JSTaggedValue>& value1, const JSHandle<JSTaggedValue>& value2);
44     static JSTaggedValue SetAllBits(
45         JSThread* thread, const JSHandle<JSAPIBitVector>& bitVector, const JSHandle<JSTaggedValue>& value);
46     static void Resize(JSThread* thread, const JSHandle<JSAPIBitVector>& bitVector, int capacity);
47     static JSTaggedValue GetBitCountByRange(JSThread* thread, const JSHandle<JSAPIBitVector>& bitVector,
48         const JSHandle<JSTaggedValue>& value, const JSHandle<JSTaggedValue>& value1,
49         const JSHandle<JSTaggedValue>& value2);
50     static int GetIndexOf(JSThread* thread, const JSHandle<JSAPIBitVector>& bitVector,
51         const JSHandle<JSTaggedValue>& value, const JSHandle<JSTaggedValue>& value1,
52         const JSHandle<JSTaggedValue>& value2);
53     static int GetLastIndexOf(JSThread* thread, const JSHandle<JSAPIBitVector>& bitVector,
54         const JSHandle<JSTaggedValue>& value, const JSHandle<JSTaggedValue>& value1,
55         const JSHandle<JSTaggedValue>& value2);
56     static JSTaggedValue FlipBitByIndex(JSThread* thread, const JSHandle<JSAPIBitVector>& bitVector, const int index);
57     static JSTaggedValue FlipBitsByRange(JSThread* thread, const JSHandle<JSAPIBitVector>& bitVector,
58         const JSHandle<JSTaggedValue>& value1, const JSHandle<JSTaggedValue>& value2);
59     JSTaggedValue PUBLIC_API Set(JSThread* thread, const uint32_t index, JSTaggedValue value);
60     JSTaggedValue Get(JSThread* thread, const uint32_t index);
61     bool Has(JSThread* thread, const JSTaggedValue& value) const;
62     static bool Has(JSThread* thread, const JSHandle<JSAPIBitVector>& bitVector, const JSHandle<JSTaggedValue>& value,
63         const JSHandle<JSTaggedValue>& value1, const JSHandle<JSTaggedValue>& value2);
64 
65     static JSHandle<TaggedArray> OwnKeys(JSThread* thread, const JSHandle<JSAPIBitVector>& obj);
66     static JSHandle<TaggedArray> OwnEnumKeys(JSThread* thread, const JSHandle<JSAPIBitVector>& obj);
67     static bool GetOwnProperty(
68         JSThread* thread, const JSHandle<JSAPIBitVector>& obj, const JSHandle<JSTaggedValue>& key);
69     static OperationResult GetProperty(
70         JSThread* thread, const JSHandle<JSAPIBitVector>& obj, const JSHandle<JSTaggedValue>& key);
71     static bool SetProperty(JSThread* thread, const JSHandle<JSAPIBitVector>& obj, const JSHandle<JSTaggedValue>& key,
72         const JSHandle<JSTaggedValue>& value);
GetSize()73     inline uint32_t GetSize() const
74     {
75         return GetLength();
76     }
77 
78     static constexpr size_t NATIVE_POINTER_OFFSET = JSObject::SIZE;
79     ACCESSORS(NativePointer, NATIVE_POINTER_OFFSET, LENGTH_OFFSET);
80     ACCESSORS_PRIMITIVE_FIELD(Length, int32_t, LENGTH_OFFSET, MOD_RECORD_OFFSET);
81     ACCESSORS_SYNCHRONIZED_PRIMITIVE_FIELD(ModRecord, uint32_t, MOD_RECORD_OFFSET, LAST_OFFSET);
82     DEFINE_ALIGN_SIZE(LAST_OFFSET);
83 
84     static constexpr uint32_t MAX_INLINE = PropertyAttributes::MAX_FAST_PROPS_CAPACITY -
85         SIZE / JSTaggedValue::TaggedTypeSize() + 1;
86     DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, NATIVE_POINTER_OFFSET, LENGTH_OFFSET);
DECL_DUMP()87     DECL_DUMP()
88 
89 private:
90     inline static uint32_t ComputeCapacity(uint32_t oldCapacity)
91     {
92         uint32_t newCapacity = oldCapacity + (oldCapacity >> 1U);
93         return newCapacity > 0 ? newCapacity : 0;
94     }
ComputeElementIdAndBitId(uint32_t index)95     inline static std::pair<uint32_t, uint32_t> ComputeElementIdAndBitId(uint32_t index)
96     {
97         uint32_t elementId = index >> TAGGED_VALUE_BIT_SIZE;
98         uint32_t bitId = index & TAGGED_VALUE_BIT_OFFSET;
99         return std::make_pair(elementId, bitId);
100     }
SetBit(std::vector<std::bitset<BIT_SET_LENGTH>> * elements,uint32_t index,const JSTaggedValue & value)101     inline static void SetBit(std::vector<std::bitset<BIT_SET_LENGTH>> *elements, uint32_t index,
102                               const JSTaggedValue &value)
103     {
104         std::pair<uint32_t, uint32_t> pair = ComputeElementIdAndBitId(index);
105         uint32_t elementId = pair.first;
106         uint32_t bitId = pair.second;
107         if (value.IsZero()) {
108             (*elements)[elementId].reset(bitId);
109         } else {
110             (*elements)[elementId].set(bitId);
111         }
112         return;
113     }
GetBit(std::vector<std::bitset<BIT_SET_LENGTH>> * elements,uint32_t index)114     inline static JSTaggedValue GetBit(std::vector<std::bitset<BIT_SET_LENGTH>> *elements, uint32_t index)
115     {
116         std::pair<uint32_t, uint32_t> pair = ComputeElementIdAndBitId(index);
117         uint32_t elementId = pair.first;
118         uint32_t bitId = pair.second;
119         int32_t bit = (*elements)[elementId].test(bitId);
120         return JSTaggedValue(bit);
121     }
122 };
123 } // namespace panda::ecmascript
124 
125 #endif // ECMASCRIPT_JS_API_JS_API_BITVECTOR_H
126