1 /*
2 * Copyright (c) 2023 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_ELEMENT_ACCESSOR_INL_H
17 #define ECMASCRIPT_ELEMENT_ACCESSOR_INL_H
18
19 #include "ecmascript/element_accessor.h"
20
21 #include "ecmascript/mem/barriers-inl.h"
22 #include "ecmascript/js_tagged_value-inl.h"
23 #include "ecmascript/js_thread.h"
24 #include "ecmascript/tagged_array.h"
25
26 namespace panda::ecmascript {
27
28 template<typename T>
Set(const JSThread * thread,JSHandle<JSObject> receiver,uint32_t idx,const JSHandle<T> & value,bool needTransition,ElementsKind extraKind)29 inline void ElementAccessor::Set(const JSThread *thread, JSHandle<JSObject> receiver, uint32_t idx,
30 const JSHandle<T> &value, bool needTransition, ElementsKind extraKind)
31 {
32 // Change elementsKind
33 ElementsKind oldKind = receiver->GetClass()->GetElementsKind();
34 if (needTransition && JSHClass::TransitToElementsKind(thread, receiver,
35 JSHandle<JSTaggedValue>(value), extraKind)) {
36 ElementsKind newKind = receiver->GetClass()->GetElementsKind();
37 // GC might happen when migrating Array
38 Elements::MigrateArrayWithKind(thread, receiver, oldKind, newKind);
39 }
40
41 TaggedArray *elements = TaggedArray::Cast(receiver->GetElements(thread));
42 ASSERT(idx < elements->GetLength());
43 if (UNLIKELY(thread->IsEnableMutantArray())) {
44 ElementsKind kind = receiver->GetClass()->GetElementsKind();
45 if (!elements->GetClass()->IsMutantTaggedArray()) {
46 kind = ElementsKind::GENERIC;
47 }
48 JSTaggedType convertedValue = ConvertTaggedValueWithElementsKind(value.GetTaggedValue(), kind);
49 size_t offset = JSTaggedValue::TaggedTypeSize() * idx;
50 // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon)
51 if (value.GetTaggedValue().IsHeapObject()) {
52 Barriers::SetObject<true>(thread, reinterpret_cast<void*>(elements),
53 TaggedArray::DATA_OFFSET + offset, convertedValue);
54 } else { // NOLINTNEXTLINE(readability-misleading-indentation)
55 Barriers::SetPrimitive<JSTaggedType>(elements->GetData(), offset, convertedValue);
56 }
57 } else {
58 elements->Set(thread, idx, value);
59 }
60 }
61
62 template<typename T>
FastSet(const JSThread * thread,JSHandle<TaggedArray> elements,uint32_t idx,const JSHandle<T> & value,ElementsKind kind)63 void ElementAccessor::FastSet(const JSThread *thread, JSHandle<TaggedArray> elements, uint32_t idx,
64 const JSHandle<T> &value, ElementsKind kind)
65 {
66 ASSERT(idx < elements->GetLength());
67 size_t offset = JSTaggedValue::TaggedTypeSize() * idx;
68 JSTaggedValue rawValue = value.GetTaggedValue();
69 switch (kind) {
70 case ElementsKind::INT:
71 Barriers::SetPrimitive<JSTaggedType>(elements->GetData(), offset,
72 static_cast<JSTaggedType>(rawValue.GetInt()));
73 break;
74 case ElementsKind::NUMBER:
75 if (rawValue.IsInt()) {
76 int intValue = rawValue.GetInt();
77 Barriers::SetPrimitive<JSTaggedType>(elements->GetData(), offset,
78 base::bit_cast<JSTaggedType>(static_cast<double>(intValue)));
79 } else {
80 Barriers::SetPrimitive<JSTaggedType>(elements->GetData(), offset,
81 base::bit_cast<JSTaggedType>(rawValue.GetDouble()));
82 }
83 break;
84 case ElementsKind::TAGGED:
85 if (value.GetTaggedValue().IsHeapObject()) {
86 Barriers::SetObject<true>(thread, elements->GetThis(),
87 TaggedArray::DATA_OFFSET + offset, rawValue.GetRawData());
88 } else { // NOLINTNEXTLINE(readability-misleading-indentation)
89 Barriers::SetPrimitive<JSTaggedType>(elements->GetData(), offset, rawValue.GetRawData());
90 }
91 break;
92 default:
93 LOG_ECMA(FATAL) << "Trying to Convert TaggedValue With Unknown ElementsKind";
94 UNREACHABLE();
95 break;
96 }
97 }
98 } // namespace panda::ecmascript
99 #endif // ECMASCRIPT_ELEMENT_ACCESSOR_INL_H
100