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