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<typename T>
Set(const JSThread * thread,uint32_t idx,const JSHandle<T> & value)24 inline void TaggedArray::Set(const JSThread *thread, uint32_t idx, const JSHandle<T> &value)
25 {
26 ASSERT(idx < GetLength());
27 size_t offset = JSTaggedValue::TaggedTypeSize() * idx;
28
29 // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon)
30 if (value.GetTaggedValue().IsHeapObject()) {
31 Barriers::SetObject<true>(thread, GetData(), offset, value.GetTaggedValue().GetRawData());
32 } else { // NOLINTNEXTLINE(readability-misleading-indentation)
33 Barriers::SetPrimitive<JSTaggedType>(GetData(), offset, value.GetTaggedValue().GetRawData());
34 }
35 }
36
37 #ifndef ECMASCRIPT_TAGGED_ARRAY_CPP
38 // `Get` is inlined when possible, including the case when it's used outside libark_jsruntime.
39 // For other cases `Get` is defined with external linkage in tagged_array.cpp
40 #define MAYBE_INLINE inline
41 #else
42 #define MAYBE_INLINE
43 #endif // ECMASCRIPT_TAGGED_ARRAY_CPP
44
Get(uint32_t idx)45 MAYBE_INLINE JSTaggedValue TaggedArray::Get(uint32_t idx) const
46 {
47 ASSERT(idx < GetLength());
48 // Note: Here we can't statically decide the element type is a primitive or heap object, especially for
49 // dynamically-typed languages like JavaScript. So we simply skip the read-barrier.
50 size_t offset = JSTaggedValue::TaggedTypeSize() * idx;
51 // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon)
52 return JSTaggedValue(Barriers::GetValue<JSTaggedType>(GetData(), offset));
53 }
54
55 #undef MAYBE_INLINE
56
57 template <bool needBarrier>
Set(const JSThread * thread,uint32_t idx,const JSTaggedValue & value)58 inline void TaggedArray::Set(const JSThread *thread, uint32_t idx, const JSTaggedValue &value)
59 {
60 ASSERT(idx < GetLength());
61 size_t offset = JSTaggedValue::TaggedTypeSize() * idx;
62
63 // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon)
64 if (needBarrier && value.IsHeapObject()) {
65 Barriers::SetObject<true>(thread, GetData(), offset, value.GetRawData());
66 } else { // NOLINTNEXTLINE(readability-misleading-indentation)
67 Barriers::SetPrimitive<JSTaggedType>(GetData(), offset, value.GetRawData());
68 }
69 }
70
71 } // namespace panda::ecmascript
72 #endif // ECMASCRIPT_TAGGED_ARRAY_INL_H
73