• 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 "object_factory.h"
20 #include "ecmascript/global_env_constants-inl.h"
21 #include "ecmascript/global_env_constants.h"
22 #include "ecmascript/js_thread.h"
23 #include "ecmascript/lexical_env.h"
24 #include "ecmascript/mem/heap-inl.h"
25 #include "ecmascript/tagged_array-inl.h"
26 
27 namespace panda::ecmascript {
AllocNonMovableStringObject(size_t size)28 EcmaString *ObjectFactory::AllocNonMovableStringObject(size_t size)
29 {
30     NewObjectHook();
31     return reinterpret_cast<EcmaString *>(heap_->AllocateNonMovableOrHugeObject(
32         JSHClass::Cast(thread_->GlobalConstants()->GetStringClass().GetTaggedObject()), size));
33 }
34 
AllocStringObject(size_t size)35 EcmaString *ObjectFactory::AllocStringObject(size_t size)
36 {
37     NewObjectHook();
38     return reinterpret_cast<EcmaString *>(heap_->AllocateYoungOrHugeObject(
39         JSHClass::Cast(thread_->GlobalConstants()->GetStringClass().GetTaggedObject()), size));
40 }
41 
NewJSNativePointer(void * externalPointer,const DeleteEntryPoint & callBack,void * data,bool nonMovable)42 JSHandle<JSNativePointer> ObjectFactory::NewJSNativePointer(void *externalPointer,
43                                                             const DeleteEntryPoint &callBack,
44                                                             void *data,
45                                                             bool nonMovable)
46 {
47     NewObjectHook();
48     TaggedObject *header;
49     auto jsNativePointerClass = JSHClass::Cast(thread_->GlobalConstants()->GetJSNativePointerClass().GetTaggedObject());
50     if (nonMovable) {
51         header = heap_->AllocateNonMovableOrHugeObject(jsNativePointerClass);
52     } else {
53         header = heap_->AllocateYoungOrHugeObject(jsNativePointerClass);
54     }
55     JSHandle<JSNativePointer> obj(thread_, header);
56     obj->SetExternalPointer(externalPointer);
57     obj->SetDeleter(callBack);
58     obj->SetData(data);
59     return obj;
60 }
61 
InlineNewLexicalEnv(int numSlots)62 LexicalEnv *ObjectFactory::InlineNewLexicalEnv(int numSlots)
63 {
64     NewObjectHook();
65     size_t size = LexicalEnv::ComputeSize(numSlots);
66     auto header = heap_->TryAllocateYoungGeneration(
67         JSHClass::Cast(thread_->GlobalConstants()->GetEnvClass().GetTaggedObject()), size);
68     if (UNLIKELY(header == nullptr)) {
69         return nullptr;
70     }
71     LexicalEnv *array = LexicalEnv::Cast(header);
72     array->InitializeWithSpecialValue(JSTaggedValue::Hole(), numSlots + LexicalEnv::RESERVED_ENV_LENGTH);
73     return array;
74 }
75 
76 template<typename T, typename S>
NewJSIntlIcuData(const JSHandle<T> & obj,const S & icu,const DeleteEntryPoint & callback)77 void ObjectFactory::NewJSIntlIcuData(const JSHandle<T> &obj, const S &icu, const DeleteEntryPoint &callback)
78 {
79     S *icuPoint = vm_->GetNativeAreaAllocator()->New<S>(icu);
80     ASSERT(icuPoint != nullptr);
81     JSTaggedValue data = obj->GetIcuField();
82     if (data.IsHeapObject() && data.IsJSNativePointer()) {
83         JSNativePointer *native = JSNativePointer::Cast(data.GetTaggedObject());
84         native->ResetExternalPointer(icuPoint);
85         return;
86     }
87     JSHandle<JSNativePointer> pointer(thread_, NewJSNativePointer(icuPoint, callback, nullptr).GetTaggedValue());
88     obj->SetIcuField(thread_, pointer.GetTaggedValue());
89     // push uint8_t* to ecma array_data_list
90     vm_->PushToArrayDataList(*pointer);
91 }
92 }  // namespace panda::ecmascript
93 #endif  // ECMASCRIPT_OBJECT_FACTORY_INL_H
94