2021-09-04 08:06:49 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 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.
|
|
|
|
*/
|
|
|
|
|
2021-09-07 14:24:16 +00:00
|
|
|
#ifndef ECMASCRIPT_WEAK_VECTOR_H
|
|
|
|
#define ECMASCRIPT_WEAK_VECTOR_H
|
2021-09-04 08:06:49 +00:00
|
|
|
|
|
|
|
#include "ecmascript/js_handle.h"
|
|
|
|
#include "ecmascript/js_thread.h"
|
2022-04-05 13:28:18 +00:00
|
|
|
#include "ecmascript/tagged_array-inl.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
|
|
|
|
namespace panda::ecmascript {
|
|
|
|
class WeakVector : public TaggedArray {
|
|
|
|
public:
|
2023-03-08 02:22:03 +00:00
|
|
|
enum ElementType {
|
|
|
|
NORMAL = 0,
|
|
|
|
WEAK,
|
|
|
|
};
|
|
|
|
|
2022-06-16 15:10:50 +00:00
|
|
|
static WeakVector *Cast(TaggedObject *object)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
|
|
|
return static_cast<WeakVector *>(object);
|
|
|
|
}
|
|
|
|
|
2022-03-19 06:14:33 +00:00
|
|
|
static constexpr uint32_t DEFAULT_CAPACITY = 4;
|
2023-05-04 13:07:13 +00:00
|
|
|
static constexpr uint32_t DEFAULT_GROW_SIZE = 4;
|
2023-11-03 09:13:33 +00:00
|
|
|
static JSHandle<WeakVector> Create(const JSThread *thread, uint32_t capacity = DEFAULT_CAPACITY,
|
2023-10-31 06:38:13 +00:00
|
|
|
MemSpaceType type = MemSpaceType::SEMI_SPACE);
|
2022-01-14 13:50:26 +00:00
|
|
|
static JSHandle<WeakVector> Grow(const JSThread *thread, const JSHandle<WeakVector> &old, uint32_t newCapacity);
|
2023-03-08 02:22:03 +00:00
|
|
|
static JSHandle<WeakVector> Append(const JSThread *thread, const JSHandle<WeakVector> &vec,
|
|
|
|
const JSHandle<JSTaggedValue> &value, ElementType type = ElementType::NORMAL);
|
|
|
|
static JSHandle<WeakVector> FillOrAppend(const JSThread *thread, const JSHandle<WeakVector> &vec,
|
|
|
|
const JSHandle<JSTaggedValue> &value,
|
|
|
|
ElementType type = ElementType::NORMAL);
|
|
|
|
static JSHandle<WeakVector> Copy(const JSThread *thread, const JSHandle<WeakVector> &vec, bool needExtend = false);
|
2022-01-14 13:50:26 +00:00
|
|
|
uint32_t PushBack(const JSThread *thread, JSTaggedValue value);
|
2021-09-04 08:06:49 +00:00
|
|
|
// just set index value to Hole
|
2022-01-14 13:50:26 +00:00
|
|
|
bool Delete(const JSThread *thread, uint32_t index);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2022-04-05 13:28:18 +00:00
|
|
|
inline uint32_t GetEnd() const
|
|
|
|
{
|
2023-04-10 01:46:39 +00:00
|
|
|
return TaggedArray::GetExtraLength();
|
2022-04-05 13:28:18 +00:00
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2022-04-05 13:28:18 +00:00
|
|
|
inline bool Full() const
|
|
|
|
{
|
|
|
|
return GetEnd() == GetCapacity();
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2022-04-05 13:28:18 +00:00
|
|
|
inline bool Empty() const
|
|
|
|
{
|
|
|
|
return GetEnd() == 0;
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2022-04-05 13:28:18 +00:00
|
|
|
inline uint32_t GetCapacity() const
|
|
|
|
{
|
2023-04-10 01:46:39 +00:00
|
|
|
return TaggedArray::GetLength();
|
2022-04-05 13:28:18 +00:00
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2022-04-05 13:28:18 +00:00
|
|
|
inline JSTaggedValue Get(uint32_t index) const
|
|
|
|
{
|
|
|
|
ASSERT(index < GetCapacity());
|
|
|
|
return TaggedArray::Get(VectorToArrayIndex(index));
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2022-04-05 13:28:18 +00:00
|
|
|
inline void Set(const JSThread *thread, uint32_t index, JSTaggedValue value)
|
|
|
|
{
|
|
|
|
ASSERT(index < GetCapacity());
|
|
|
|
TaggedArray::Set(thread, VectorToArrayIndex(index), value);
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2023-03-08 02:22:03 +00:00
|
|
|
template <class Callback>
|
2023-03-21 03:27:53 +00:00
|
|
|
void Iterate(const Callback &cb)
|
|
|
|
{
|
2023-03-08 02:22:03 +00:00
|
|
|
uint32_t end = GetEnd();
|
|
|
|
for (uint32_t index = 0; index < end; ++index) {
|
|
|
|
cb(Get(index));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
private:
|
2022-01-14 13:50:26 +00:00
|
|
|
static const uint32_t MIN_CAPACITY = 2;
|
2023-04-10 01:46:39 +00:00
|
|
|
static const uint32_t MAX_VECTOR_INDEX = TaggedArray::MAX_ARRAY_INDEX;
|
2021-09-04 08:06:49 +00:00
|
|
|
|
2022-01-14 13:50:26 +00:00
|
|
|
inline static constexpr uint32_t VectorToArrayIndex(uint32_t index)
|
2021-09-04 08:06:49 +00:00
|
|
|
{
|
2023-04-10 01:46:39 +00:00
|
|
|
return index;
|
2021-09-04 08:06:49 +00:00
|
|
|
}
|
|
|
|
|
2023-04-10 01:46:39 +00:00
|
|
|
inline void SetEnd([[maybe_unused]] const JSThread *thread, uint32_t end)
|
2022-04-05 13:28:18 +00:00
|
|
|
{
|
|
|
|
ASSERT(end <= GetCapacity());
|
2023-04-10 01:46:39 +00:00
|
|
|
SetExtraLength(end);
|
2022-04-05 13:28:18 +00:00
|
|
|
}
|
2023-03-08 02:22:03 +00:00
|
|
|
|
|
|
|
static JSTaggedValue GetStoreVal(const JSHandle<JSTaggedValue> &value, ElementType type);
|
|
|
|
|
|
|
|
static uint32_t CheckHole(const JSHandle<WeakVector> &vec);
|
|
|
|
|
|
|
|
static JSHandle<WeakVector> AppendToFullVec(const JSThread *thread, const JSHandle<WeakVector> &vec,
|
|
|
|
const JSHandle<JSTaggedValue> &value, ElementType type);
|
2021-09-04 08:06:49 +00:00
|
|
|
};
|
|
|
|
} // namespace panda::ecmascript
|
2021-09-07 14:24:16 +00:00
|
|
|
#endif // ECMASCRIPT_WEAK_VECTOR_H
|