/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ECMASCRIPT_JS_API_LIGHTWEIGHTSET_H #define ECMASCRIPT_JS_API_LIGHTWEIGHTSET_H #include "ecmascript/js_object.h" #include "ecmascript/js_tagged_value-inl.h" namespace panda::ecmascript { class JSAPILightWeightSet : public JSObject { public: static constexpr int DEFAULT_CAPACITY_LENGTH = 8; static constexpr int32_t HASH_REBELLION = 0xFFFFFFFF; static JSAPILightWeightSet *Cast(TaggedObject *object) { ASSERT(JSTaggedValue(object).IsJSAPILightWeightSet()); return static_cast(object); } static bool Add(JSThread *thread, const JSHandle &obj, const JSHandle &value); static JSHandle CreateSlot(const JSThread *thread, const uint32_t capacity); static void EnsureCapacity(const JSThread *thread, const JSHandle &obj, uint32_t minimumCapacity); static void SizeCopy(const JSThread *thread, const JSHandle &obj, uint32_t capacity, uint32_t newCapacity); static bool AddAll(JSThread *thread, const JSHandle &obj, const JSHandle &value); static bool Equal(JSThread *thread, const JSHandle &obj, const JSHandle &value); static void IncreaseCapacityTo(JSThread *thread, const JSHandle &obj, int32_t minCapacity); static JSTaggedValue ToString(JSThread *thread, const JSHandle &obj); static JSTaggedValue ForEach(JSThread *thread, const JSHandle &thisHandle, const JSHandle &callbackFn, const JSHandle &thisArg); static JSHandle GetIteratorObj(JSThread *thread, const JSHandle &obj, IterationKind kind); void AdjustArray(JSThread *thread, JSHandle srcArray, uint32_t fromIndex, uint32_t toIndex, bool direction); void Clear(JSThread *thread); JSTaggedValue Get(const uint32_t index); JSTaggedValue GetHashAt(int32_t index); JSTaggedValue GetValueAt(int32_t index); JSTaggedValue Remove(JSThread *thread, JSHandle &value); bool Has(const JSHandle &value); bool HasHash(const JSHandle &hashCode); bool HasAll(const JSHandle &value); bool RemoveAt(JSThread *thread, int32_t index); void RemoveValue(const JSThread *thread, JSHandle &taggedArray, uint32_t index); bool IsEmpty(); int32_t GetIndexOf(JSHandle &value); int32_t BinarySearchHashes(uint32_t hash, int32_t size); int32_t GetHashIndex(const JSHandle &value, int32_t size); uint32_t Hash(JSTaggedValue key); inline uint32_t GetSize() const { return GetLength(); } static constexpr size_t HASHES_OFFSET = JSObject::SIZE; ACCESSORS(Hashes, HASHES_OFFSET, VALUES_OFFSET); ACCESSORS(Values, VALUES_OFFSET, LENGTH_OFFSET); ACCESSORS_PRIMITIVE_FIELD(Length, uint32_t, LENGTH_OFFSET, LAST_OFFSET); DEFINE_ALIGN_SIZE(LAST_OFFSET); DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, HASHES_OFFSET, LENGTH_OFFSET) DECL_DUMP() }; } // namespace panda::ecmascript #endif // ECMASCRIPT_JS_API_LIGHTWEIGHTSET_H