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 22namespace panda::ecmascript { 23class JSAPIBitVector : public JSObject { 24public: 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; 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(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); 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 DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, NATIVE_POINTER_OFFSET, LENGTH_OFFSET); 85 DECL_DUMP() 86 87private: 88 inline static uint32_t ComputeCapacity(uint32_t oldCapacity) 89 { 90 uint32_t newCapacity = oldCapacity + (oldCapacity >> 1U); 91 return newCapacity > 0 ? newCapacity : 0; 92 } 93 inline static std::pair<uint32_t, uint32_t> ComputeElementIdAndBitId(uint32_t index) 94 { 95 uint32_t elementId = index >> TAGGED_VALUE_BIT_SIZE; 96 uint32_t bitId = index & TAGGED_VALUE_BIT_OFFSET; 97 return std::make_pair(elementId, bitId); 98 } 99 inline static void SetBit(std::vector<std::bitset<BIT_SET_LENGTH>> *elements, uint32_t index, 100 const JSTaggedValue &value) 101 { 102 std::pair<uint32_t, uint32_t> pair = ComputeElementIdAndBitId(index); 103 uint32_t elementId = pair.first; 104 uint32_t bitId = pair.second; 105 if (value.IsZero()) { 106 (*elements)[elementId].reset(bitId); 107 } else { 108 (*elements)[elementId].set(bitId); 109 } 110 return; 111 } 112 inline static JSTaggedValue GetBit(std::vector<std::bitset<BIT_SET_LENGTH>> *elements, uint32_t index) 113 { 114 std::pair<uint32_t, uint32_t> pair = ComputeElementIdAndBitId(index); 115 uint32_t elementId = pair.first; 116 uint32_t bitId = pair.second; 117 int32_t bit = (*elements)[elementId].test(bitId); 118 return JSTaggedValue(bit); 119 } 120}; 121} // namespace panda::ecmascript 122 123#endif // ECMASCRIPT_JS_API_JS_API_BITVECTOR_H 124