• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, value, extraKind)) {
35         ElementsKind newKind = receiver->GetClass()->GetElementsKind();
36         Elements::MigrateArrayWithKind(thread, receiver, oldKind, newKind);
37     }
38 
39     TaggedArray *elements = TaggedArray::Cast(receiver->GetElements());
40     ASSERT(idx < elements->GetLength());
41     size_t offset = JSTaggedValue::TaggedTypeSize() * idx;
42 
43     ElementsKind kind = receiver->GetClass()->GetElementsKind();
44     if (!elements->GetClass()->IsMutantTaggedArray()) {
45         kind = ElementsKind::GENERIC;
46     }
47 
48     JSTaggedType convertedValue = ConvertTaggedValueWithElementsKind(value.GetTaggedValue(), kind);
49     // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon)
50     if (value.GetTaggedValue().IsHeapObject()) {
51         Barriers::SetObject<true>(thread, elements->GetData(), offset, convertedValue);
52     } else {  // NOLINTNEXTLINE(readability-misleading-indentation)
53         Barriers::SetPrimitive<JSTaggedType>(elements->GetData(), offset, convertedValue);
54     }
55 }
56 
57 template <bool needBarrier>
Set(const JSThread * thread,JSHandle<JSObject> receiver,uint32_t idx,const JSTaggedValue & value,bool needTransition,ElementsKind extraKind)58 inline void ElementAccessor::Set(const JSThread *thread, JSHandle<JSObject> receiver, uint32_t idx,
59                                  const JSTaggedValue &value, bool needTransition, ElementsKind extraKind)
60 {
61     // Change elementsKind
62     ElementsKind oldKind = receiver->GetClass()->GetElementsKind();
63     if (needTransition &&
64         JSHClass::TransitToElementsKind(thread, receiver, JSHandle<JSTaggedValue>(thread, value), extraKind)) {
65         ElementsKind newKind = receiver->GetClass()->GetElementsKind();
66         Elements::MigrateArrayWithKind(thread, receiver, oldKind, newKind);
67     }
68 
69     TaggedArray *elements = TaggedArray::Cast(receiver->GetElements());
70     ASSERT(idx < elements->GetLength());
71     size_t offset = JSTaggedValue::TaggedTypeSize() * idx;
72 
73     ElementsKind kind = receiver->GetClass()->GetElementsKind();
74     if (!elements->GetClass()->IsMutantTaggedArray()) {
75         kind = ElementsKind::GENERIC;
76     }
77 
78     JSTaggedType convertedValue = ConvertTaggedValueWithElementsKind(value, kind);
79     // NOLINTNEXTLINE(readability-braces-around-statements, bugprone-suspicious-semicolon)
80     if (needBarrier && value.IsHeapObject()) {
81         Barriers::SetObject<true>(thread, elements->GetData(), offset, convertedValue);
82     } else {  // NOLINTNEXTLINE(readability-misleading-indentation)
83         Barriers::SetPrimitive<JSTaggedType>(elements->GetData(), offset, convertedValue);
84     }
85 }
86 }  // namespace panda::ecmascript
87 #endif  // ECMASCRIPT_ELEMENT_ACCESSOR_INL_H
88