1 /*
2 * Copyright (c) 2021-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_TAGGED_ARRAY_INL_H
17 #define ECMASCRIPT_TAGGED_ARRAY_INL_H
18
19 #include "ecmascript/tagged_array.h"
20 #include "ecmascript/mem/barriers-inl.h"
21
22 namespace panda::ecmascript {
23 template<bool needBarrier, typename T>
Set(const JSThread * thread,uint32_t idx,const T & value)24 inline void TaggedArray::Set(const JSThread *thread, uint32_t idx, const T &value)
25 {
26 ASSERT(idx < GetLength());
27 size_t offset = JSTaggedValue::TaggedTypeSize() * idx;
28
29 if constexpr (std::is_same_v<T, JSTaggedValue>) {
30 if (needBarrier && value.IsHeapObject()) {
31 Barriers::SetObject<true>(thread, reinterpret_cast<void*>(this), offset + DATA_OFFSET, value.GetRawData());
32 } else {
33 Barriers::SetPrimitive<JSTaggedType>(GetData(), offset, value.GetRawData());
34 }
35 } else if constexpr (IsJSHandle<T>::value) {
36 auto taggedValue = value.GetTaggedValue();
37 if (taggedValue.IsHeapObject()) {
38 Barriers::SetObject<true>(thread, reinterpret_cast<void*>(this),
39 offset + DATA_OFFSET, taggedValue.GetRawData());
40 } else {
41 Barriers::SetPrimitive<JSTaggedType>(GetData(), offset, taggedValue.GetRawData());
42 }
43 } else {
44 static_assert(!std::is_same_v<T, T>, "T must be either JSTaggedValue or JSHandle<>");
45 }
46 }
47
48 #ifndef ECMASCRIPT_TAGGED_ARRAY_CPP
49 // `Get` is inlined when possible, including the case when it's used outside libark_jsruntime.
50 // For other cases `Get` is defined with external linkage in tagged_array.cpp
51 #define MAYBE_INLINE inline
52 #else
53 #define MAYBE_INLINE
54 #endif // ECMASCRIPT_TAGGED_ARRAY_CPP
55
56 #undef MAYBE_INLINE
57
58 template <bool needBarrier, bool maybeOverlap>
Copy(const JSThread * thread,uint32_t dstStart,uint32_t srcStart,const TaggedArray * srcArray,uint32_t count)59 inline void TaggedArray::Copy(const JSThread* thread, uint32_t dstStart, uint32_t srcStart,
60 const TaggedArray* srcArray, uint32_t count)
61 {
62 DISALLOW_GARBAGE_COLLECTION;
63 ASSERT((dstStart + count <= GetLength()) && "TaggedArray::Copy dst count is out of range");
64 ASSERT((srcStart + count <= srcArray->GetLength()) && "TaggedArray::Copy src count is out of range");
65 size_t taggedTypeSize = JSTaggedValue::TaggedTypeSize();
66 JSTaggedValue* to = reinterpret_cast<JSTaggedValue*>(ToUintPtr(GetData()) + taggedTypeSize * dstStart);
67 JSTaggedValue* from = reinterpret_cast<JSTaggedValue*>(ToUintPtr(srcArray->GetData()) + taggedTypeSize * srcStart);
68 Barriers::CopyObject<needBarrier, maybeOverlap>(thread, this, to, from, count);
69 }
70 } // namespace panda::ecmascript
71 #endif // ECMASCRIPT_TAGGED_ARRAY_INL_H
72