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, GetData(), 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, GetData(), offset, taggedValue.GetRawData());
39         } else {
40             Barriers::SetPrimitive<JSTaggedType>(GetData(), offset, taggedValue.GetRawData());
41         }
42     } else {
43         static_assert(!std::is_same_v<T, T>, "T must be either JSTaggedValue or JSHandle<>");
44     }
45 }
46 
47 #ifndef ECMASCRIPT_TAGGED_ARRAY_CPP
48 // `Get` is inlined when possible, including the case when it's used outside libark_jsruntime.
49 // For other cases `Get` is defined with external linkage in tagged_array.cpp
50     #define MAYBE_INLINE inline
51 #else
52     #define MAYBE_INLINE
53 #endif // ECMASCRIPT_TAGGED_ARRAY_CPP
54 
55 MAYBE_INLINE JSTaggedValue TaggedArray::Get(uint32_t idx) const
56 {
57     ASSERT(idx < GetLength());
58     // Note: Here we can't statically decide the element type is a primitive or heap object, especially for
59     //       dynamically-typed languages like JavaScript. So we simply skip the read-barrier.
60     size_t offset = JSTaggedValue::TaggedTypeSize() * idx;
61     // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon)
62     return JSTaggedValue(Barriers::GetValue<JSTaggedType>(GetData(), offset));
63 }
64 
65 #undef MAYBE_INLINE
66 
67 template <bool needBarrier>
68 inline void TaggedArray::Copy(const JSThread* thread, uint32_t dstStart, uint32_t srcStart,
69                               const TaggedArray* srcArray, uint32_t count)
70 {
71     DISALLOW_GARBAGE_COLLECTION;
72     ASSERT((dstStart + count <= GetLength()) && "TaggedArray::Copy dst count is out of range");
73     ASSERT((srcStart + count <= srcArray->GetLength()) && "TaggedArray::Copy src count is out of range");
74     size_t taggedTypeSize = JSTaggedValue::TaggedTypeSize();
75     JSTaggedValue* to = reinterpret_cast<JSTaggedValue*>(ToUintPtr(GetData()) + taggedTypeSize * dstStart);
76     JSTaggedValue* from = reinterpret_cast<JSTaggedValue*>(ToUintPtr(srcArray->GetData()) + taggedTypeSize * srcStart);
77     Barriers::CopyObject<needBarrier, false>(thread, to, from, count);
78 }
79 }  // namespace panda::ecmascript
80 #endif  // ECMASCRIPT_TAGGED_ARRAY_INL_H
81