• 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 {
AllocNonMovableStringObject(size_t size)29 EcmaString *ObjectFactory::AllocNonMovableStringObject(size_t size)
30 {
31     NewObjectHook();
32     return reinterpret_cast<EcmaString *>(heap_->AllocateNonMovableOrHugeObject(
33         JSHClass::Cast(thread_->GlobalConstants()->GetStringClass().GetTaggedObject()), size));
34 }
35 
AllocStringObject(size_t size)36 EcmaString *ObjectFactory::AllocStringObject(size_t size)
37 {
38     NewObjectHook();
39     return reinterpret_cast<EcmaString *>(heap_->AllocateYoungOrHugeObject(
40         JSHClass::Cast(thread_->GlobalConstants()->GetStringClass().GetTaggedObject()), size));
41 }
42 
AllocOldSpaceStringObject(size_t size)43 EcmaString *ObjectFactory::AllocOldSpaceStringObject(size_t size)
44 {
45     NewObjectHook();
46     return reinterpret_cast<EcmaString *>(heap_->AllocateOldOrHugeObject(
47         JSHClass::Cast(thread_->GlobalConstants()->GetStringClass().GetTaggedObject()), size));
48 }
49 
NewJSNativePointer(void * externalPointer,const DeleteEntryPoint & callBack,void * data,bool nonMovable,size_t nativeBindingsize)50 JSHandle<JSNativePointer> ObjectFactory::NewJSNativePointer(void *externalPointer,
51                                                             const DeleteEntryPoint &callBack,
52                                                             void *data,
53                                                             bool nonMovable,
54                                                             size_t nativeBindingsize)
55 {
56     NewObjectHook();
57     TaggedObject *header;
58     auto jsNativePointerClass = JSHClass::Cast(thread_->GlobalConstants()->GetJSNativePointerClass().GetTaggedObject());
59     if (nonMovable) {
60         header = heap_->AllocateNonMovableOrHugeObject(jsNativePointerClass);
61     } else {
62         header = heap_->AllocateYoungOrHugeObject(jsNativePointerClass);
63     }
64     JSHandle<JSNativePointer> obj(thread_, header);
65     obj->SetExternalPointer(externalPointer);
66     obj->SetDeleter(callBack);
67     obj->SetData(data);
68     obj->SetBindingSize(nativeBindingsize);
69 
70     if (callBack != nullptr) {
71         heap_->IncreaseNativeBindingSize(nonMovable, nativeBindingsize);
72         vm_->PushToNativePointerList(static_cast<JSNativePointer *>(header));
73     }
74     return obj;
75 }
76 
InlineNewLexicalEnv(int numSlots)77 LexicalEnv *ObjectFactory::InlineNewLexicalEnv(int numSlots)
78 {
79     NewObjectHook();
80     size_t size = LexicalEnv::ComputeSize(numSlots);
81     auto header = heap_->TryAllocateYoungGeneration(
82         JSHClass::Cast(thread_->GlobalConstants()->GetEnvClass().GetTaggedObject()), size);
83     if (UNLIKELY(header == nullptr)) {
84         return nullptr;
85     }
86     LexicalEnv *array = LexicalEnv::Cast(header);
87     array->InitializeWithSpecialValue(JSTaggedValue::Hole(), numSlots + LexicalEnv::RESERVED_ENV_LENGTH);
88     return array;
89 }
90 
91 template<typename T, typename S>
NewJSIntlIcuData(const JSHandle<T> & obj,const S & icu,const DeleteEntryPoint & callback)92 void ObjectFactory::NewJSIntlIcuData(const JSHandle<T> &obj, const S &icu, const DeleteEntryPoint &callback)
93 {
94     S *icuPoint = vm_->GetNativeAreaAllocator()->New<S>(icu);
95     ASSERT(icuPoint != nullptr);
96     JSTaggedValue data = obj->GetIcuField();
97     if (data.IsHeapObject() && data.IsJSNativePointer()) {
98         JSNativePointer *native = JSNativePointer::Cast(data.GetTaggedObject());
99         native->ResetExternalPointer(icuPoint);
100         return;
101     }
102     JSHandle<JSNativePointer> pointer = NewJSNativePointer(icuPoint, callback, vm_);
103     obj->SetIcuField(thread_, pointer.GetTaggedValue());
104 }
105 
AllocObjectWithSpaceType(size_t size,JSHClass * cls,MemSpaceType type)106 TaggedObject *ObjectFactory::AllocObjectWithSpaceType(size_t size, JSHClass *cls, MemSpaceType type)
107 {
108     switch (type) {
109         case MemSpaceType::SEMI_SPACE:
110             return heap_->AllocateYoungOrHugeObject(cls, size);
111         case MemSpaceType::OLD_SPACE:
112             return heap_->AllocateOldOrHugeObject(cls, size);
113         case MemSpaceType::NON_MOVABLE:
114             return heap_->AllocateNonMovableOrHugeObject(cls, size);
115         default:
116             UNREACHABLE();
117     }
118 }
119 }  // namespace panda::ecmascript
120 #endif  // ECMASCRIPT_OBJECT_FACTORY_INL_H
121