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());
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, elements->GetData(), offset, convertedValue);
53 } else { // NOLINTNEXTLINE(readability-misleading-indentation)
54 Barriers::SetPrimitive<JSTaggedType>(elements->GetData(), offset, convertedValue);
55 }
56 } else {
57 elements->Set(thread, idx, value);
58 }
59 }
60
61 template<typename T>
FastSet(const JSThread * thread,JSHandle<TaggedArray> elements,uint32_t idx,const JSHandle<T> & value,ElementsKind kind)62 void ElementAccessor::FastSet(const JSThread *thread, JSHandle<TaggedArray> elements, uint32_t idx,
63 const JSHandle<T> &value, ElementsKind kind)
64 {
65 ASSERT(idx < elements->GetLength());
66 size_t offset = JSTaggedValue::TaggedTypeSize() * idx;
67 JSTaggedValue rawValue = value.GetTaggedValue();
68 switch (kind) {
69 case ElementsKind::INT:
70 Barriers::SetPrimitive<JSTaggedType>(elements->GetData(), offset,
71 static_cast<JSTaggedType>(rawValue.GetInt()));
72 break;
73 case ElementsKind::NUMBER:
74 if (rawValue.IsInt()) {
75 int intValue = rawValue.GetInt();
76 Barriers::SetPrimitive<JSTaggedType>(elements->GetData(), offset,
77 base::bit_cast<JSTaggedType>(static_cast<double>(intValue)));
78 } else {
79 Barriers::SetPrimitive<JSTaggedType>(elements->GetData(), offset,
80 base::bit_cast<JSTaggedType>(rawValue.GetDouble()));
81 }
82 break;
83 case ElementsKind::TAGGED:
84 if (value.GetTaggedValue().IsHeapObject()) {
85 Barriers::SetObject<true>(thread, elements->GetData(), offset, rawValue.GetRawData());
86 } else { // NOLINTNEXTLINE(readability-misleading-indentation)
87 Barriers::SetPrimitive<JSTaggedType>(elements->GetData(), offset, rawValue.GetRawData());
88 }
89 break;
90 default:
91 LOG_ECMA(FATAL) << "Trying to Convert TaggedValue With Unknown ElementsKind";
92 UNREACHABLE();
93 break;
94 }
95 }
96 } // namespace panda::ecmascript
97 #endif // ECMASCRIPT_ELEMENT_ACCESSOR_INL_H
98