• 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_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         // In some cases, the size of JS/TS object is too small and the native binding size is too large.
88         // Check and try trigger concurrent mark here.
89         heap_->TryTriggerFullMarkByNativeSize();
90     }
91     return obj;
92 }
93 
InlineNewLexicalEnv(int numSlots)94 LexicalEnv *ObjectFactory::InlineNewLexicalEnv(int numSlots)
95 {
96     NewObjectHook();
97     size_t size = LexicalEnv::ComputeSize(numSlots);
98     auto header = heap_->TryAllocateYoungGeneration(
99         JSHClass::Cast(thread_->GlobalConstants()->GetEnvClass().GetTaggedObject()), size);
100     if (UNLIKELY(header == nullptr)) {
101         return nullptr;
102     }
103     LexicalEnv *array = LexicalEnv::Cast(header);
104     array->InitializeWithSpecialValue(JSTaggedValue::Hole(), numSlots + LexicalEnv::RESERVED_ENV_LENGTH);
105     return array;
106 }
107 
108 template<typename T, typename S>
NewJSIntlIcuData(const JSHandle<T> & obj,const S & icu,const DeleteEntryPoint & callback)109 void ObjectFactory::NewJSIntlIcuData(const JSHandle<T> &obj, const S &icu, const DeleteEntryPoint &callback)
110 {
111     S *icuPoint = vm_->GetNativeAreaAllocator()->New<S>(icu);
112     ASSERT(icuPoint != nullptr);
113     JSTaggedValue data = obj->GetIcuField();
114     if (data.IsHeapObject() && data.IsJSNativePointer()) {
115         JSNativePointer *native = JSNativePointer::Cast(data.GetTaggedObject());
116         native->ResetExternalPointer(icuPoint);
117         return;
118     }
119     JSHandle<JSNativePointer> pointer = NewJSNativePointer(icuPoint, callback, vm_);
120     obj->SetIcuField(thread_, pointer.GetTaggedValue());
121 }
122 
AllocObjectWithSpaceType(size_t size,JSHClass * cls,MemSpaceType type)123 TaggedObject *ObjectFactory::AllocObjectWithSpaceType(size_t size, JSHClass *cls, MemSpaceType type)
124 {
125     switch (type) {
126         case MemSpaceType::SEMI_SPACE:
127             return heap_->AllocateYoungOrHugeObject(cls, size);
128         case MemSpaceType::OLD_SPACE:
129             return heap_->AllocateOldOrHugeObject(cls, size);
130         case MemSpaceType::NON_MOVABLE:
131             return heap_->AllocateNonMovableOrHugeObject(cls, size);
132         default:
133             LOG_ECMA(FATAL) << "this branch is unreachable";
134             UNREACHABLE();
135     }
136 }
137 }  // namespace panda::ecmascript
138 #endif  // ECMASCRIPT_OBJECT_FACTORY_INL_H
139