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_CONTAINERS_CONTAINERS_BIT_VECTOR_H 17 #define ECMASCRIPT_CONTAINERS_CONTAINERS_BIT_VECTOR_H 18 19 #include "ecmascript/base/builtins_base.h" 20 #include "ecmascript/ecma_runtime_call_info.h" 21 22 #define BUILTIN_BITVECTOR_PROTOTYPE_FUNCTIONS(V) \ 23 V("push", Push, 1, INVALID) \ 24 V("pop", Pop, 0, INVALID) \ 25 V("has", Has, 3, INVALID) \ 26 V("setBitsByRange", SetBitsByRange, 3, INVALID) \ 27 V("getBitsByRange", GetBitsByRange, 2, INVALID) \ 28 V("setAllBits", SetAllBits, 1, INVALID) \ 29 V("resize", Resize, 1, INVALID) \ 30 V("getBitCountByRange", GetBitCountByRange, 3, INVALID) \ 31 V("getIndexOf", GetIndexOf, 3, INVALID) \ 32 V("getLastIndexOf", GetLastIndexOf, 3, INVALID) \ 33 V("flipBitByIndex", FlipBitByIndex, 1, INVALID) \ 34 V("flipBitsByRange", FlipBitsByRange, 2, INVALID) \ 35 V("values", GetIteratorObj, 0, INVALID) 36 37 namespace panda::ecmascript::containers { 38 class ContainersBitVector : public base::BuiltinsBase { 39 public: 40 static JSTaggedValue BitVectorConstructor(EcmaRuntimeCallInfo* argv); 41 static JSTaggedValue Push(EcmaRuntimeCallInfo* argv); 42 static JSTaggedValue Pop(EcmaRuntimeCallInfo* argv); 43 static JSTaggedValue Has(EcmaRuntimeCallInfo* argv); 44 static JSTaggedValue SetBitsByRange(EcmaRuntimeCallInfo* argv); 45 static JSTaggedValue GetBitsByRange(EcmaRuntimeCallInfo* argv); 46 static JSTaggedValue Resize(EcmaRuntimeCallInfo* argv); 47 static JSTaggedValue GetBitCountByRange(EcmaRuntimeCallInfo* argv); 48 static JSTaggedValue GetIndexOf(EcmaRuntimeCallInfo* argv); 49 static JSTaggedValue GetLastIndexOf(EcmaRuntimeCallInfo* argv); 50 static JSTaggedValue FlipBitByIndex(EcmaRuntimeCallInfo* argv); 51 static JSTaggedValue FlipBitsByRange(EcmaRuntimeCallInfo* argv); 52 static JSTaggedValue GetIteratorObj(EcmaRuntimeCallInfo* argv); 53 static JSTaggedValue GetSize(EcmaRuntimeCallInfo* argv); 54 static JSTaggedValue SetAllBits(EcmaRuntimeCallInfo* argv); 55 56 // Excluding the constructor and '@@' internal properties. GetBitVectorPrototypeFunctions()57 static Span<const base::BuiltinFunctionEntry> GetBitVectorPrototypeFunctions() 58 { 59 return Span<const base::BuiltinFunctionEntry>(BITVECTOR_PROTOTYPE_FUNCTIONS); 60 } 61 GetNumPrototypeInlinedProperties()62 static size_t GetNumPrototypeInlinedProperties() 63 { 64 // 4 : 4 more inline properties in BitVector.prototype 65 // (1) BitVector.prototype.constructor 66 // (2) BitVector.prototype [ @@toStringTag ] 67 // (3) BitVector.prototype [ @@iterator ] 68 // (4) get BitVector.prototype.size 69 // (5) get BitVector.prototype.length 70 return GetBitVectorPrototypeFunctions().Size() + 5; 71 } 72 GetPrototypeProperties()73 static Span<const std::pair<std::string_view, bool>> GetPrototypeProperties() 74 { 75 return Span<const std::pair<std::string_view, bool>>(BITVECTOR_PROTOTYPE_PROPERTIES); 76 } 77 GetFunctionProperties()78 static Span<const std::pair<std::string_view, bool>> GetFunctionProperties() 79 { 80 return Span<const std::pair<std::string_view, bool>>(BITVECTOR_FUNCTION_PROPERTIES); 81 } 82 private: 83 static void FreeBitsetVectorPointer([[maybe_unused]] void *env, void *pointer, void *data); 84 85 #define BUILTIN_BITVECTOR_FUNCTION_ENTRY(name, func, length, id) \ 86 base::BuiltinFunctionEntry::Create(name, ContainersBitVector::func, length, BUILTINS_STUB_ID(id)), 87 88 static constexpr std::array BITVECTOR_PROTOTYPE_FUNCTIONS = { 89 BUILTIN_BITVECTOR_PROTOTYPE_FUNCTIONS(BUILTIN_BITVECTOR_FUNCTION_ENTRY) 90 }; 91 #undef BUILTIN_BITVECTOR_FUNCTION_ENTRY 92 93 #define BITVECTOR_PROPERTIES_PAIR(name, func, length, id) \ 94 std::pair<std::string_view, bool>(name, false), 95 96 static constexpr std::array BITVECTOR_PROTOTYPE_PROPERTIES = { 97 std::pair<std::string_view, bool>("constructor", false), 98 BUILTIN_BITVECTOR_PROTOTYPE_FUNCTIONS(BITVECTOR_PROPERTIES_PAIR) 99 std::pair<std::string_view, bool>("[Symbol.toStringTag]", false), 100 std::pair<std::string_view, bool>("length", true), 101 std::pair<std::string_view, bool>("size", true), 102 std::pair<std::string_view, bool>("[Symbol.iterator]", false) 103 }; 104 105 static constexpr std::array BITVECTOR_FUNCTION_PROPERTIES = { 106 std::pair<std::string_view, bool>("length", false), 107 std::pair<std::string_view, bool>("name", false), 108 std::pair<std::string_view, bool>("prototype", false), 109 std::pair<std::string_view, bool>("[Symbol.species]", true), 110 }; 111 #undef BITVECTOR_PROPERTIES_PAIR 112 }; 113 } // namespace panda::ecmascript::containers 114 #endif // ECMASCRIPT_CONTAINERS_CONTAINERS_BIT_VECTOR_H 115