• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_OBJECT_FACTORY_INL_H
17 #define ECMASCRIPT_OBJECT_FACTORY_INL_H
18 
19 #include "ecmascript/global_env_constants-inl.h"
20 #include "ecmascript/global_env_constants.h"
21 #include "ecmascript/js_hclass-inl.h"
22 #include "ecmascript/js_thread.h"
23 #include "ecmascript/lexical_env.h"
24 #include "ecmascript/mem/heap-inl.h"
25 #include "ecmascript/mem/barriers-inl.h"
26 #include "ecmascript/object_factory.h"
27 #include "ecmascript/tagged_array-inl.h"
28 
29 namespace panda::ecmascript {
AllocNonMovableLineStringObject(size_t size)30 EcmaString *ObjectFactory::AllocNonMovableLineStringObject(size_t size)
31 {
32     NewObjectHook();
33     return reinterpret_cast<EcmaString *>(heap_->AllocateNonMovableOrHugeObject(
34         JSHClass::Cast(thread_->GlobalConstants()->GetLineStringClass().GetTaggedObject()), size));
35 }
36 
AllocLineStringObject(size_t size)37 EcmaString *ObjectFactory::AllocLineStringObject(size_t size)
38 {
39     NewObjectHook();
40     return reinterpret_cast<EcmaString *>(heap_->AllocateYoungOrHugeObject(
41         JSHClass::Cast(thread_->GlobalConstants()->GetLineStringClass().GetTaggedObject()), size));
42 }
43 
AllocOldSpaceLineStringObject(size_t size)44 EcmaString *ObjectFactory::AllocOldSpaceLineStringObject(size_t size)
45 {
46     NewObjectHook();
47     return reinterpret_cast<EcmaString *>(heap_->AllocateOldOrHugeObject(
48         JSHClass::Cast(thread_->GlobalConstants()->GetLineStringClass().GetTaggedObject()), size));
49 }
50 
AllocSlicedStringObject(MemSpaceType type)51 EcmaString *ObjectFactory::AllocSlicedStringObject(MemSpaceType type)
52 {
53     NewObjectHook();
54     return reinterpret_cast<EcmaString *>(AllocObjectWithSpaceType(SlicedString::SIZE,
55         JSHClass::Cast(thread_->GlobalConstants()->GetSlicedStringClass().GetTaggedObject()), type));
56 }
57 
AllocConstantStringObject(MemSpaceType type)58 EcmaString *ObjectFactory::AllocConstantStringObject(MemSpaceType type)
59 {
60     NewObjectHook();
61     return reinterpret_cast<EcmaString *>(AllocObjectWithSpaceType(ConstantString::SIZE,
62         JSHClass::Cast(thread_->GlobalConstants()->GetConstantStringClass().GetTaggedObject()), type));
63 }
64 
AllocTreeStringObject()65 EcmaString *ObjectFactory::AllocTreeStringObject()
66 {
67     NewObjectHook();
68     return reinterpret_cast<EcmaString *>(heap_->AllocateYoungOrHugeObject(
69         JSHClass::Cast(thread_->GlobalConstants()->GetTreeStringClass().GetTaggedObject()), TreeEcmaString::SIZE));
70 }
71 
NewJSNativePointer(void * externalPointer,const DeleteEntryPoint & callBack,void * data,bool nonMovable,size_t nativeBindingsize,NativeFlag flag)72 JSHandle<JSNativePointer> ObjectFactory::NewJSNativePointer(void *externalPointer,
73                                                             const DeleteEntryPoint &callBack,
74                                                             void *data,
75                                                             bool nonMovable,
76                                                             size_t nativeBindingsize,
77                                                             NativeFlag flag)
78 {
79     NewObjectHook();
80     TaggedObject *header;
81     auto jsNativePointerClass = JSHClass::Cast(thread_->GlobalConstants()->GetJSNativePointerClass().GetTaggedObject());
82     if (nonMovable) {
83         header = heap_->AllocateNonMovableOrHugeObject(jsNativePointerClass);
84     } else {
85         header = heap_->AllocateOldOrHugeObject(jsNativePointerClass);
86     }
87     JSHandle<JSNativePointer> obj(thread_, header);
88     obj->SetExternalPointer(externalPointer);
89     obj->SetDeleter(callBack);
90     obj->SetData(data);
91     obj->SetBindingSize(nativeBindingsize);
92     obj->SetNativeFlag(flag);
93 
94     if (callBack != nullptr) {
95         heap_->IncreaseNativeBindingSize(nativeBindingsize);
96         vm_->PushToNativePointerList(static_cast<JSNativePointer *>(header));
97         // In some cases, the size of JS/TS object is too small and the native binding size is too large.
98         // Check and try trigger concurrent mark here.
99         heap_->TryTriggerFullMarkByNativeSize();
100     }
101     return obj;
102 }
103 
InlineNewLexicalEnv(int numSlots)104 LexicalEnv *ObjectFactory::InlineNewLexicalEnv(int numSlots)
105 {
106     NewObjectHook();
107     size_t size = LexicalEnv::ComputeSize(numSlots);
108     auto header = heap_->TryAllocateYoungGeneration(
109         JSHClass::Cast(thread_->GlobalConstants()->GetEnvClass().GetTaggedObject()), size);
110     if (UNLIKELY(header == nullptr)) {
111         return nullptr;
112     }
113     LexicalEnv *array = LexicalEnv::Cast(header);
114     array->InitializeWithSpecialValue(JSTaggedValue::Hole(), numSlots + LexicalEnv::RESERVED_ENV_LENGTH);
115     return array;
116 }
117 
118 template<typename T, typename S>
NewJSIntlIcuData(const JSHandle<T> & obj,const S & icu,const DeleteEntryPoint & callback)119 void ObjectFactory::NewJSIntlIcuData(const JSHandle<T> &obj, const S &icu, const DeleteEntryPoint &callback)
120 {
121     S *icuPoint = vm_->GetNativeAreaAllocator()->New<S>(icu);
122     ASSERT(icuPoint != nullptr);
123     JSTaggedValue data = obj->GetIcuField();
124     if (data.IsHeapObject() && data.IsJSNativePointer()) {
125         JSNativePointer *native = JSNativePointer::Cast(data.GetTaggedObject());
126         native->ResetExternalPointer(icuPoint);
127         return;
128     }
129     JSHandle<JSNativePointer> pointer = NewJSNativePointer(icuPoint, callback, vm_);
130     obj->SetIcuField(thread_, pointer.GetTaggedValue());
131 }
132 
AllocObjectWithSpaceType(size_t size,JSHClass * cls,MemSpaceType type)133 TaggedObject *ObjectFactory::AllocObjectWithSpaceType(size_t size, JSHClass *cls, MemSpaceType type)
134 {
135     switch (type) {
136         case MemSpaceType::SEMI_SPACE:
137             return heap_->AllocateYoungOrHugeObject(cls, size);
138         case MemSpaceType::OLD_SPACE:
139             return heap_->AllocateOldOrHugeObject(cls, size);
140         case MemSpaceType::NON_MOVABLE:
141             return heap_->AllocateNonMovableOrHugeObject(cls, size);
142         default:
143             LOG_ECMA(FATAL) << "this branch is unreachable";
144             UNREACHABLE();
145     }
146 }
147 }  // namespace panda::ecmascript
148 #endif  // ECMASCRIPT_OBJECT_FACTORY_INL_H
149