/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ECMASCRIPT_OBJECT_FACTORY_H #define ECMASCRIPT_OBJECT_FACTORY_H #include "ecmascript/base/error_type.h" #include "ecmascript/base/number_helper.h" #include "ecmascript/ecma_string.h" #include "ecmascript/js_function_kind.h" #include "ecmascript/js_handle.h" #include "ecmascript/js_hclass.h" #include "ecmascript/js_native_pointer.h" #include "ecmascript/js_tagged_value.h" #include "ecmascript/mem/heap_region_allocator.h" #include "ecmascript/mem/machine_code.h" #include "ecmascript/mem/native_area_allocator.h" #include "ecmascript/mem/space.h" #include "ecmascript/shared_objects/js_shared_object.h" #include "ecmascript/tagged_array.h" #include "ecmascript/byte_array.h" namespace panda::ecmascript { struct MethodLiteral; class Method; class JSObject; class JSArray; class JSAPIPlainArray; class JSSymbol; class JSFunctionBase; class JSFunction; class JSBoundFunction; class JSProxyRevocFunction; class JSAsyncAwaitStatusFunction; class JSPrimitiveRef; class GlobalEnv; class GlobalEnvConstants; class AccessorData; class JSGlobalObject; class LexicalEnv; class SendableEnv; class JSDate; class JSProxy; class JSRealm; class JSArguments; class TaggedQueue; class JSForInIterator; class JSSet; class JSSharedSet; class JSMap; class JSSharedMap; class JSRegExp; class JSSetIterator; class JSSharedSetIterator; class JSRegExpIterator; class JSMapIterator; class JSSharedMapIterator; class JSArrayIterator; class JSSharedArrayIterator; class JSAPIPlainArrayIterator; class JSStringIterator; class JSGeneratorObject; class CompletionRecord; class GeneratorContext; class JSArrayBuffer; class JSSendableArrayBuffer; class JSDataView; class JSPromise; class JSPromiseReactionsFunction; class JSPromiseExecutorFunction; class JSAsyncModuleFulfilledFunction; class JSAsyncModuleRejectedFunction; class JSPromiseAllResolveElementFunction; class JSAsyncGeneratorResNextRetProRstFtn; class JSPromiseAnyRejectElementFunction; class JSPromiseAllSettledElementFunction; class JSPromiseFinallyFunction; class JSPromiseValueThunkOrThrowerFunction; class PromiseReaction; class PromiseCapability; class PromiseIteratorRecord; class JSAsyncFuncObject; class JSAsyncFunction; class JSAsyncFromSyncIterUnwarpFunction; class PromiseRecord; class JSLocale; class ResolvingFunctionsRecord; class EcmaVM; class Heap; class SharedHeap; class ConstantPool; class Program; class LayoutInfo; class JSIntlBoundFunction; class FreeObject; class JSNativePointer; class JSAPIArrayList; class JSAPIArrayListIterator; class JSAPIDeque; class JSAPIDequeIterator; class TaggedHashArray; class LinkedNode; class RBTreeNode; class JSAPIHashMap; class JSAPIHashSet; class JSAPIHashMapIterator; class JSAPIHashSetIterator; class JSAPILightWeightMap; class JSAPILightWeightMapIterator; class JSAPILightWeightSet; class JSAPILightWeightSetIterator; class JSAPIQueue; class JSAPIQueueIterator; class JSAPIStack; class JSAPIStackIterator; class JSAPITreeSet; class JSAPITreeMap; class JSAPITreeSetIterator; class JSAPITreeMapIterator; class JSAPIVector; class JSAPIVectorIterator; class JSAPIBitVector; class JSAPIBitVectorIterator; class JSAPILinkedList; class JSAPIList; class JSAPILinkedListIterator; class JSAPIListIterator; class ModuleNamespace; class NativeModuleError; class ImportEntry; class LocalExportEntry; class IndirectExportEntry; class StarExportEntry; class SourceTextModule; class CjsModule; class CjsRequire; class CjsExports; class ResolvedBinding; class ResolvedIndexBinding; class ResolvedRecordIndexBinding; class ResolvedRecordBinding; class BigInt; class AsyncGeneratorRequest; class AsyncIteratorRecord; class JSAsyncGeneratorFunction; class JSAsyncGeneratorObject; class CellRecord; class ClassLiteral; namespace job { class MicroJobQueue; class PendingJob; } // namespace job class TransitionHandler; class PrototypeHandler; class TransWithProtoHandler; class StoreTSHandler; class PropertyBox; class ProtoChangeMarker; class ProtoChangeDetails; class MarkerCell; class ProfileTypeInfo; class MachineCode; class ClassInfoExtractor; class AOTLiteralInfo; class ProfileTypeInfoCell; class VTable; namespace kungfu { class TSHClassGenerator; } // namespace kungfu enum class CompletionRecordType : uint8_t; enum class PrimitiveType : uint8_t; enum class IterationKind : uint8_t; enum class MethodIndex : uint8_t; enum class SharedTypes : uint8_t; using ErrorType = base::ErrorType; using base::ErrorType; enum class RemoveSlots { YES, NO }; enum class GrowMode { KEEP, GROW }; enum class StackCheck { YES, NO }; class ObjectFactory { public: static constexpr JSTaggedType FREE_MEMMORY_ADDRESS_ZAM_VALUE = 0xDEADFACE; ObjectFactory(JSThread *thread, Heap *heap, SharedHeap *sHeap); ~ObjectFactory() = default; JSHandle NewMethodForNativeFunction(const void *func, FunctionKind kind = FunctionKind::NORMAL_FUNCTION, kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID, MemSpaceType methodSpaceType = SHARED_OLD_SPACE); JSHandle NewProfileTypeInfo(uint32_t length); JSHandle NewConstantPool(uint32_t capacity); JSHandle NewProgram(); JSHandle PUBLIC_API GetJSError(const ErrorType &errorType, const char *data = nullptr, StackCheck needCheckStack = StackCheck::YES); JSHandle NewJSError(const ErrorType &errorType, const JSHandle &message, StackCheck needCheckStack = StackCheck::YES); JSHandle NewJSAggregateError(); JSHandle NewTransitionHandler(); JSHandle NewPrototypeHandler(); JSHandle NewTransWithProtoHandler(); JSHandle NewStoreTSHandler(); JSHandle NewEmptyJSObject(uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); // use for others create, prototype is Function.prototype // use for native function JSHandle NewJSFunction(const JSHandle &env, const void *nativeFunc = nullptr, FunctionKind kind = FunctionKind::NORMAL_FUNCTION, kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID, MemSpaceType methodSpaceType = SHARED_OLD_SPACE); JSHandle NewSFunction(const JSHandle &env, const void *nativeFunc = nullptr, FunctionKind kind = FunctionKind::NORMAL_FUNCTION, kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID, MemSpaceType spaceType = SHARED_OLD_SPACE); void InitializeMethod(const MethodLiteral *methodLiteral, JSHandle &method); // use for method JSHandle NewJSFunction(const JSHandle &env, const JSHandle &method); JSHandle NewJSFunction(const JSHandle &methodHandle); JSHandle NewJSFunction(const JSHandle &methodHandle, const JSHandle &homeObject); JSHandle NewJSNativeErrorFunction(const JSHandle &env, const void *nativeFunc = nullptr); JSHandle NewSpecificTypedArrayFunction(const JSHandle &env, const void *nativeFunc = nullptr); JSHandle OrdinaryNewJSObjectCreate(const JSHandle &proto); JSHandle CreateNullJSObject(); JSHandle NewAotFunction(uint32_t numArgs, uintptr_t codeEntry); JSHandle NewJSBoundFunction(const JSHandle &target, const JSHandle &boundThis, const JSHandle &args); JSHandle NewJSIntlBoundFunction(MethodIndex idx, int functionLength = 1); JSHandle NewJSProxyRevocFunction(const JSHandle &proxy); JSHandle NewJSAsyncAwaitStatusFunction(MethodIndex idx); JSHandle NewJSGeneratorObject(JSHandle generatorFunction); JSHandle NewJSAsyncFuncObject(); JSHandle NewJSAsyncGeneratorObject(JSHandle generatorFunction); JSHandle NewJSPrimitiveRef(const JSHandle &function, const JSHandle &object); JSHandle NewJSPrimitiveRef(PrimitiveType type, const JSHandle &object); // get JSHClass for Ecma ClassLinker JSHandle NewGlobalEnv(JSHClass *globalEnvClass); // get JSHClass for Ecma ClassLinker JSHandle NewLexicalEnv(int numSlots); inline LexicalEnv *InlineNewLexicalEnv(int numSlots); JSHandle NewEmptySymbol(); JSHandle NewJSSymbol(); JSHandle NewPrivateSymbol(); JSHandle NewPrivateNameSymbol(const JSHandle &name); JSHandle NewWellKnownSymbol(const JSHandle &name); JSHandle NewPublicSymbol(const JSHandle &name); JSHandle NewSymbolWithTable(const JSHandle &name); JSHandle NewPrivateNameSymbolWithChar(std::string_view description); JSHandle NewWellKnownSymbolWithChar(std::string_view description); JSHandle NewPublicSymbolWithChar(std::string_view description); JSHandle NewSymbolWithTableWithChar(std::string_view description); JSHandle NewAccessorData(); JSHandle NewInternalAccessor(void *setter, void *getter); JSHandle NewPromiseCapability(); JSHandle NewPromiseReaction(); JSHandle NewPromiseRecord(); JSHandle NewAsyncGeneratorRequest(); JSHandle NewAsyncIteratorRecord(const JSHandle &itor, const JSHandle &next, bool done); JSHandle NewResolvingFunctionsRecord(); JSHandle NewPromiseIteratorRecord(const JSHandle &itor, bool done); JSHandle NewMicroJobQueue(); JSHandle NewPendingJob(const JSHandle &func, const JSHandle &argv); JSHandle NewJSArray(); JSHandle PUBLIC_API NewJSSArray(); JSHandle PUBLIC_API NewJSArray(size_t length, JSHandle &hclass); JSHandle PUBLIC_API NewJsonFixedArray(size_t start, size_t length, const std::vector> &vec); JSHandle PUBLIC_API NewSJsonFixedArray(size_t start, size_t length, const std::vector> &vec); JSHandle NewJSProxy(const JSHandle &target, const JSHandle &handler); JSHandle NewJSRealm(); JSHandle NewJSArguments(); JSHandle NewJSString(const JSHandle &str, const JSHandle &newTarget); template JSHandle ConvertListToArray(const JSThread *thread, const JSHandle &list, uint32_t numberOfNodes) { MemSpaceType spaceType = numberOfNodes < LENGTH_THRESHOLD ? MemSpaceType::SEMI_SPACE : MemSpaceType::OLD_SPACE; JSHandle dstElements = NewTaggedArrayWithoutInit(numberOfNodes, spaceType); if (numberOfNodes == 0) { return dstElements; } int dataIndex = Derived::ELEMENTS_START_INDEX; for (uint32_t i = 0; i < numberOfNodes; i++) { dataIndex = list->GetElement(dataIndex + Derived::NEXT_PTR_OFFSET).GetInt(); dstElements->Set(thread, i, list->GetElement(dataIndex)); } return dstElements; } JSHandle NewAndCopyJSArrayObject(JSHandle thisObjHandle, uint32_t newLength, uint32_t oldLength, uint32_t k = 0); JSHandle NewAndCopyTaggedArray(JSHandle &srcElements, uint32_t newLength, uint32_t oldLength, uint32_t k = 0); JSHandle NewAndCopyTaggedArraySkipBarrier(JSHandle &srcElements, uint32_t newLength, uint32_t oldLength, uint32_t k = 0); JSHandle NewAndCopySNameDictionary(JSHandle &srcElements, uint32_t length); JSHandle NewAndCopyTaggedArrayByObject(JSHandle thisObjHandle, uint32_t newLength, uint32_t oldLength, uint32_t k = 0); JSHandle NewAndCopyMutantTaggedArrayByObject(JSHandle thisObjHandle, uint32_t newLength, uint32_t oldLength, uint32_t k = 0); JSHandle PUBLIC_API NewTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); JSHandle NewTaggedArray(uint32_t length, JSTaggedValue initVal, bool nonMovable); JSHandle NewTaggedArray(uint32_t length, JSTaggedValue initVal, MemSpaceType spaceType); // Copy on write array is allocated in nonmovable space by default. JSHandle NewCOWTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); JSHandle NewCOWMutantTaggedArray(uint32_t length, JSTaggedType initVal = base::SPECIAL_HOLE); JSHandle NewMutantTaggedArray(uint32_t length, JSTaggedType initVal = base::SPECIAL_HOLE); JSHandle PUBLIC_API NewDictionaryArray(uint32_t length); JSHandle NewJSForinIterator(const JSHandle &obj, const JSHandle keys, const JSHandle cachedHclass); JSHandle NewByteArray(uint32_t length, uint32_t size, void *srcData = nullptr, MemSpaceType spaceType = MemSpaceType::SEMI_SPACE); JSHandle NewPropertyBox(const JSHandle &name); JSHandle NewProtoChangeMarker(); JSHandle NewProtoChangeDetails(); JSHandle NewMarkerCell(); JSHandle NewBigInt(uint32_t length); // use for copy properties keys's array to another array JSHandle ExtendArray(const JSHandle &old, uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole(), MemSpaceType type = MemSpaceType::SEMI_SPACE, ElementsKind kind = ElementsKind::GENERIC); JSHandle CopyPartArray(const JSHandle &old, uint32_t start, uint32_t end); JSHandle PUBLIC_API CopyArray(const JSHandle &old, uint32_t oldLength, uint32_t newLength, JSTaggedValue initVal = JSTaggedValue::Hole(), MemSpaceType type = MemSpaceType::SEMI_SPACE, ElementsKind kind = ElementsKind::GENERIC); JSHandle CopyFromEnumCache(const JSHandle &old); JSHandle CloneProperties(const JSHandle &old); JSHandle CloneProperties(const JSHandle &old, const JSHandle &env, const JSHandle &obj); JSHandle PUBLIC_API CreateLayoutInfo(int properties, MemSpaceType type = MemSpaceType::SEMI_SPACE, GrowMode mode = GrowMode::GROW); JSHandle ExtendLayoutInfo(const JSHandle &old, int properties); JSHandle CopyLayoutInfo(const JSHandle &old); JSHandle CopyAndReSort(const JSHandle &old, int end, int capacity); JSHandle GetEmptyString() const; JSHandle EmptyArray() const; JSHandle EmptyMutantArray() const; FreeObject *FillFreeObject(uintptr_t address, size_t size, RemoveSlots removeSlots = RemoveSlots::NO, uintptr_t hugeObjectHead = 0); TaggedObject *NewObject(const JSHandle &hclass); TaggedObject *NewNonMovableObject(const JSHandle &hclass, uint32_t inobjPropCount = 0); void InitializeExtraProperties(const JSHandle &hclass, TaggedObject *obj, uint32_t inobjPropCount); JSHandle NewTaggedQueue(uint32_t length); JSHandle GetEmptyTaggedQueue() const; JSHandle NewJSSetIterator(const JSHandle &set, IterationKind kind); JSHandle NewJSSetIterator(const JSHandle &set, IterationKind kind); JSHandle NewJSRegExpIterator(const JSHandle &matcher, const JSHandle &inputStr, bool global, bool fullUnicode); JSHandle NewJSMapIterator(const JSHandle &map, IterationKind kind); JSHandle NewJSMapIterator(const JSHandle &map, IterationKind kind); JSHandle NewJSArrayIterator(const JSHandle &array, IterationKind kind); JSHandle NewJSSharedArrayIterator(const JSHandle &array, IterationKind kind); JSHandle NewCompletionRecord(CompletionRecordType type, JSHandle value); JSHandle NewGeneratorContext(); JSHandle CreateJSPromiseReactionsFunction(MethodIndex idx); JSHandle CreateJSPromiseExecutorFunction(); JSHandle CreateJSAsyncModuleFulfilledFunction(); JSHandle CreateJSAsyncModuleRejectedFunction(); JSHandle NewJSPromiseAllResolveElementFunction(); JSHandle NewJSPromiseAnyRejectElementFunction(); JSHandle NewJSPromiseAllSettledResolveElementFunction(); JSHandle NewJSPromiseAllSettledRejectElementFunction(); JSHandle NewJSPromiseThenFinallyFunction(); JSHandle NewJSPromiseCatchFinallyFunction(); JSHandle NewJSPromiseValueThunkFunction(); JSHandle NewJSPromiseThrowerFunction(); JSHandle NewJSAsyGenResNextRetProRstFulfilledFtn(); JSHandle NewJSAsyGenResNextRetProRstRejectedFtn(); JSHandle NewJSAsyncFromSyncIterUnwarpFunction(); JSHandle CloneObjectLiteral(JSHandle object, const JSHandle &env, bool canShareHClass = true); JSHandle CloneObjectLiteral(JSHandle object); JSHandle CloneArrayLiteral(JSHandle object); JSHandle CloneJSFunction(JSHandle func); JSHandle CloneSFunction(JSHandle func); JSHandle CloneClassCtor(JSHandle ctor, const JSHandle &lexenv, bool canShareHClass); void NewJSArrayBufferData(const JSHandle &array, int32_t length); void NewJSSendableArrayBufferData(const JSHandle &array, int32_t length); JSHandle NewJSSendableArrayBuffer(int32_t length); JSHandle NewJSSendableArrayBuffer(void *buffer, int32_t length, const NativePointerCallback &deleter, void *data); JSHandle NewJSArrayBuffer(int32_t length); JSHandle NewJSArrayBuffer(void *buffer, int32_t length, const NativePointerCallback &deleter, void *data, bool share = false); JSHandle NewJSDataView(JSHandle buffer, uint32_t offset, uint32_t length); void NewJSSharedArrayBufferData(const JSHandle &array, int32_t length); JSHandle NewJSSharedArrayBuffer(int32_t length); JSHandle NewJSSharedArrayBuffer(void *buffer, int32_t length); void NewJSRegExpByteCodeData(const JSHandle ®exp, void *buffer, size_t size); template inline void NewJSIntlIcuData(const JSHandle &obj, const S &icu, const NativePointerCallback &callback); EcmaString *PUBLIC_API InternString(const JSHandle &key); inline JSHandle NewJSNativePointer(void *externalPointer, const NativePointerCallback &callBack = nullptr, void *data = nullptr, bool nonMovable = false, size_t nativeBindingsize = 0, Concurrent isConcurrent = Concurrent::NO, NativeFlag flag = NativeFlag::NO_DIV); JSHandle NewOldSpaceObjLiteralByHClass(const JSHandle &hclass); JSHandle SetLayoutInObjHClass(const JSHandle &properties, size_t length, const JSHandle &objClass); JSHandle CreateObjectLiteralRootHClass(size_t length); JSHandle GetObjectLiteralRootHClass(size_t length); JSHandle GetObjectLiteralHClass(const JSHandle &properties, size_t length); // only use for creating Function.prototype and Function JSHandle NewJSFunctionByHClass(const JSHandle &method, const JSHandle &clazz, MemSpaceType type = MemSpaceType::SEMI_SPACE); JSHandle NewJSFunctionByHClass(const void *func, const JSHandle &clazz, FunctionKind kind = FunctionKind::NORMAL_FUNCTION); JSHandle NewJSFunctionByHClassWithoutAccessor(const void *func, const JSHandle &clazz, FunctionKind kind = FunctionKind::NORMAL_FUNCTION); JSHandle NewMethod(const MethodLiteral *methodLiteral, MemSpaceType spaceType = OLD_SPACE); JSHandle NewMethod(const JSPandaFile *jsPandaFile, MethodLiteral *methodLiteral, JSHandle constpool, uint32_t entryIndex, bool needSetAotFlag, bool *canFastCall = nullptr); // used for creating jsobject by constructor JSHandle NewJSObjectByConstructor(const JSHandle &constructor, const JSHandle &newTarget); JSHandle NewJSObjectByConstructor(JSHandle env, const JSHandle &constructor, uint32_t inlinedProps); JSHandle NewJSObjectByConstructor(const JSHandle &constructor, uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); void InitializeJSObject(const JSHandle &obj, const JSHandle &jshclass); JSHandle NewJSObjectWithInit(const JSHandle &jshclass); uintptr_t NewSpaceBySnapshotAllocator(size_t size); #ifdef ENABLE_JITFORT TaggedObject *NewMachineCodeObject(size_t length, MachineCodeDesc &desc); JSHandle SetMachineCodeObjectData(TaggedObject *obj, size_t length, MachineCodeDesc &desc, JSHandle &method); #else JSHandle NewMachineCodeObject(size_t length, const MachineCodeDesc &desc, JSHandle &method); #endif JSHandle NewClassInfoExtractor(JSHandle method); JSHandle NewClassLiteral(); // ----------------------------------- new string ---------------------------------------- JSHandle PUBLIC_API NewFromASCII(std::string_view data); JSHandle NewFromUtf8WithoutStringTable(std::string_view data); JSHandle PUBLIC_API NewFromUtf8(std::string_view data); JSHandle NewFromUtf8ReadOnly(std::string_view data); JSHandle NewFromASCIISkippingStringTable(std::string_view data); JSHandle NewFromUtf16WithoutStringTable(std::u16string_view data); JSHandle NewFromUtf16(std::u16string_view data); JSHandle NewFromStdString(const std::string &data); JSHandle NewFromUtf8WithoutStringTable(const uint8_t *utf8Data, uint32_t utf8Len); JSHandle NewFromUtf8(const uint8_t *utf8Data, uint32_t utf8Len); JSHandle NewFromUtf16WithoutStringTable(const uint16_t *utf16Data, uint32_t utf16Len); JSHandle PUBLIC_API NewFromUtf16(const uint16_t *utf16Data, uint32_t utf16Len); JSHandle NewFromUtf16Compress(const uint16_t *utf16Data, uint32_t utf16Len); JSHandle NewFromUtf16NotCompress(const uint16_t *utf16Data, uint32_t utf16Len); JSHandle NewFromUtf8Literal(const uint8_t *utf8Data, uint32_t utf8Len); JSHandle PUBLIC_API NewFromUtf8LiteralCompress(const uint8_t *utf8Data, uint32_t utf8Len); JSHandle PUBLIC_API NewFromUtf8LiteralCompressSubString(const JSHandle &string, uint32_t offset, uint32_t utf8Len); JSHandle PUBLIC_API NewCompressedUtf8(const uint8_t *utf8Data, uint32_t utf8Len); JSHandle PUBLIC_API NewCompressedUtf8SubString(const JSHandle &string, uint32_t offset, uint32_t utf8Len); JSHandle NewFromUtf16Literal(const uint16_t *utf16Data, uint32_t utf16Len); JSHandle NewFromUtf16LiteralCompress(const uint16_t *utf16Data, uint32_t utf16Len); JSHandle PUBLIC_API NewFromUtf16LiteralNotCompress(const uint16_t *utf16Data, uint32_t utf16Len); inline EcmaString *AllocLineStringObject(size_t size); inline EcmaString *AllocLineStringObjectNoGC(size_t size); inline EcmaString *AllocOldSpaceLineStringObject(size_t size); inline EcmaString *AllocReadOnlyLineStringObject(size_t size); inline EcmaString *AllocNonMovableLineStringObject(size_t size); inline EcmaString *AllocSlicedStringObject(MemSpaceType type); inline EcmaString *AllocConstantStringObject(MemSpaceType type); inline EcmaString *AllocTreeStringObject(); JSHandle ConcatFromString(const JSHandle &firstString, const JSHandle &secondString); // used for creating Function JSHandle PUBLIC_API NewJSObject(const JSHandle &jshclass); // used for creating jshclass in Builtins, Function, Class_Linker JSHandle NewEcmaHClass(uint32_t size, JSType type, const JSHandle &prototype); JSHandle NewEcmaHClass(uint32_t size, uint32_t inlinedProps, JSType type, const JSHandle &prototype); // used for creating jshclass in Builtins, Function, Class_Linker JSHandle PUBLIC_API NewEcmaHClass(uint32_t size, JSType type, uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); JSHandle PUBLIC_API NewEcmaHClass(uint32_t size, uint32_t inlinedProps, JSType type, const JSHandle &prototype, const JSHandle &layout); // It is used to provide iterators for non ECMA standard jsapi containers. JSHandle NewJSAPIPlainArray(uint32_t capacity); JSHandle NewJSAPIPlainArrayIterator(const JSHandle &plainarray, IterationKind kind); JSHandle NewJSAPIArrayList(uint32_t capacity); JSHandle NewJSAPILightWeightMapIterator(const JSHandle &obj, IterationKind kind); JSHandle NewJSAPILightWeightSetIterator(const JSHandle &obj, IterationKind kind); JSHandle CopyQueue(const JSHandle &old, uint32_t newLength, uint32_t front, uint32_t tail); JSHandle NewJSAPIQueueIterator(const JSHandle &queue); JSHandle CopyDeque(const JSHandle &old, uint32_t newLength, uint32_t oldLength, uint32_t first, uint32_t last); JSHandle NewJSAPIDequeIterator(const JSHandle &deque); JSHandle NewJSAPIArrayListIterator(const JSHandle &arrayList); JSHandle NewJSAPIList(); JSHandle NewJSAPILinkedList(); JSHandle NewJSAPILinkedListIterator(const JSHandle &linkedList); JSHandle NewJSAPIListIterator(const JSHandle &list); JSHandle NewJSAPITreeMapIterator(const JSHandle &map, IterationKind kind); JSHandle NewJSAPITreeSetIterator(const JSHandle &set, IterationKind kind); JSHandle NewJSAPIStackIterator(const JSHandle &stack); JSHandle NewJSAPIVector(uint32_t capacity); JSHandle NewJSAPIVectorIterator(const JSHandle &vector); JSHandle NewJSAPIBitVector(uint32_t capacity); JSHandle NewJSAPIBitVectorIterator(const JSHandle &bitVector); JSHandle NewJSAPIHashMapIterator(const JSHandle &hashMap, IterationKind kind); JSHandle NewJSAPIHashSetIterator(const JSHandle &hashSet, IterationKind kind); JSHandle NewTaggedHashArray(uint32_t length); JSHandle NewLinkedNode(int hash, const JSHandle &key, const JSHandle &value, const JSHandle &next); JSHandle NewTreeNode(int hash, const JSHandle &key, const JSHandle &value); // --------------------------------------module-------------------------------------------- JSHandle NewModuleNamespace(); JSHandle NewNativeModuleError(); JSHandle NewImportEntry(); JSHandle NewImportEntry(const JSHandle &moduleRequest, const JSHandle &importName, const JSHandle &localName, SharedTypes sharedTypes); JSHandle NewLocalExportEntry(); JSHandle NewLocalExportEntry( const JSHandle &exportName, const JSHandle &localName, const uint32_t index, SharedTypes sharedTypes); JSHandle NewIndirectExportEntry(); JSHandle NewIndirectExportEntry(const JSHandle &exportName, const JSHandle &moduleRequest, const JSHandle &importName, SharedTypes sharedTypes); JSHandle NewStarExportEntry(); JSHandle NewStarExportEntry(const JSHandle &moduleRequest, SharedTypes sharedTypes); JSHandle NewSourceTextModule(); JSHandle NewResolvedBindingRecord(); JSHandle NewResolvedBindingRecord(const JSHandle &module, const JSHandle &bindingName); JSHandle NewResolvedIndexBindingRecord(); JSHandle NewResolvedIndexBindingRecord( const JSHandle &module, int32_t index); JSHandle NewCellRecord(); JSHandle NewJSAsyncGeneratorFunction(const JSHandle &method); // --------------------------------------require-------------------------------------------- JSHandle NewCjsModule(); JSHandle NewCjsExports(); JSHandle NewCjsRequire(); JSHandle CreateIteratorResultInstanceClass(const JSHandle &env); // --------------------------------------old space object-------------------------------------------- JSHandle NewOldSpaceJSObject(const JSHandle &jshclass); TaggedObject *NewOldSpaceObject(const JSHandle &hclass); JSHandle NewOldSpaceTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); // ---------------------------------New objects used internally-------------------------------------- JSHandle NewJSStableArrayWithElements(const JSHandle &elements); // ---------------------------------------Used by AOT------------------------------------------------ JSHandle NewAOTLiteralInfo(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); JSHandle NewProfileTypeInfoCell(const JSHandle &value); JSHandle NewVTable(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); JSHandle NewEcmaHClass(JSHClass *hclass, uint32_t size, JSType type, uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); // napi interface to create object with initial inline properties JSHandle CreateJSObjectWithProperties(size_t propertyCount, const Local *keys, const PropertyDescriptor *attributes); JSHandle CreateJSObjectWithNamedProperties(size_t propertyCount, const char **keys, const Local *values); // Fill the given free memory range with special zam value. void FillFreeMemoryRange(uintptr_t start, uintptr_t end); // -----------------------------------shared object----------------------------------------- JSHandle PUBLIC_API NewSharedOldSpaceJSObject(const JSHandle &jshclass); JSHandle PUBLIC_API NewSharedOldSpaceJSObjectWithInit(const JSHandle &jshclass); TaggedObject *NewSharedOldSpaceObject(const JSHandle &hclass); JSHandle CreateSObjectWithProperties(std::vector &descs); JSHandle PUBLIC_API NewSEcmaHClass(uint32_t size, JSType type, uint32_t inlinedProps); JSHandle PUBLIC_API NewSEcmaHClass(JSHClass *hclass, uint32_t size, JSType type, uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); JSHandle PUBLIC_API NewSEcmaHClass(uint32_t size, uint32_t inlinedProps, JSType type, const JSHandle &prototype, const JSHandle &layout); JSHandle PUBLIC_API NewSEcmaHClassDictMode(uint32_t size, uint32_t inlinedProps, JSType type, const JSHandle &prototype); JSHandle NewSEcmaHClassClass(JSHClass *hclass, uint32_t size, JSType type); JSHandle InitSClassClass(); JSHandle NewSEcmaReadOnlyHClass(JSHClass *hclass, uint32_t size, JSType type, uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); JSHandle SharedEmptyArray() const; JSHandle CopySArray(const JSHandle &old, uint32_t oldLength, uint32_t newLength, JSTaggedValue initVal = JSTaggedValue::Hole(), ElementsKind kind = ElementsKind::GENERIC); JSHandle ExtendSArray(const JSHandle &old, uint32_t length, JSTaggedValue initVal, [[maybe_unused]] ElementsKind kind); JSHandle NewSMethodForNativeFunction(const void *func, FunctionKind kind = FunctionKind::NORMAL_FUNCTION, kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID, MemSpaceType spaceType = SHARED_OLD_SPACE); JSHandle NewSFunctionByHClass(const JSHandle &methodHandle, const JSHandle &hclass); JSHandle NewSFunctionByHClass(const void *func, const JSHandle &hclass, FunctionKind kind, kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID, MemSpaceType spaceType = SHARED_OLD_SPACE); JSHandle NewSFunctionWithAccessor( const void *func, const JSHandle &hclass, FunctionKind kind, kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID, MemSpaceType spaceType = SHARED_OLD_SPACE); JSHandle NewSMethod(const MethodLiteral *methodLiteral, MemSpaceType methodSpaceType = SHARED_OLD_SPACE); JSHandle PUBLIC_API NewSMethod(const JSPandaFile *jsPandaFile, MethodLiteral *methodLiteral, JSHandle constpool, uint32_t entryIndex, bool needSetAotFlag, bool *canFastCall = nullptr); JSHandle NewSConstantPool(uint32_t capacity); JSHandle NewSAOTLiteralInfo(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); JSHandle NewSCOWTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); JSHandle NewSClassLiteral(); JSHandle NewSClassInfoExtractor(JSHandle method); JSHandle NewSOldSpaceTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); JSHandle PUBLIC_API NewSTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole(), MemSpaceType spaceType = SHARED_OLD_SPACE); JSHandle NewSAccessorData(); JSHandle NewSSourceTextModule(); JSHandle NewSModuleNamespace(); JSHandle NewSImportEntry(const JSHandle &moduleRequest, const JSHandle &importName, const JSHandle &localName); JSHandle NewSLocalExportEntry(const JSHandle &exportName, const JSHandle &localName, const uint32_t index); JSHandle NewSIndirectExportEntry(const JSHandle &exportName, const JSHandle &moduleRequest, const JSHandle &importName); JSHandle NewSStarExportEntry(const JSHandle &moduleRequest); JSHandle NewSResolvedIndexBindingRecord(); JSHandle NewSResolvedIndexBindingRecord(const JSHandle &module, int32_t index); JSHandle NewSResolvedBindingRecord(); JSHandle NewSResolvedBindingRecord(const JSHandle &module, const JSHandle &bindingName); JSHandle NewSResolvedRecordIndexBindingRecord(); JSHandle NewSResolvedRecordIndexBindingRecord( const JSHandle &moduleRecord, const JSHandle &abcFileName, int32_t index); JSHandle NewSResolvedRecordBindingRecord(); JSHandle NewSResolvedRecordBindingRecord( const JSHandle &moduleRecord, const JSHandle &bindingName); JSHandle CopyAndReSortSLayoutInfo(const JSHandle &old, int end, int capacity); JSHandle PUBLIC_API CreateSLayoutInfo(uint32_t properties); JSHandle NewSEmptyProfileTypeInfoCell(); JSHandle NewSEmptyArray(); // only used for EcmaVM. JSHandle NewSEmptyMutantArray(); JSHandle PUBLIC_API NewSDictionaryArray(uint32_t length); JSHandle NewSTaggedArrayWithoutInit(uint32_t length); JSHandle CreateSFunctionClass(uint32_t size, JSType type, const JSHandle &prototype, bool isAccessor = true); JSHandle NewSJSNativePointer(void *externalPointer, const NativePointerCallback &callBack, void *data = nullptr, bool nonMovable = false, size_t nativeBindingsize = 0, NativeFlag flag = NativeFlag::NO_DIV); JSHandle NewSInternalAccessor(void *setter, void *getter); JSHandle NewSWellKnownSymbol(const JSHandle &name); JSHandle NewSEmptySymbol(); JSHandle NewSWellKnownSymbolWithChar(std::string_view description); JSHandle NewSPublicSymbolWithChar(std::string_view description); JSHandle NewSPublicSymbol(const JSHandle &name); JSHandle CloneMethodTemporaryForJIT(JSHandle method); JSHandle NewSendableEnv(int numSlots); private: friend class GlobalEnv; friend class GlobalEnvConstants; friend class EcmaString; friend class SnapshotProcessor; friend class SingleCharTable; void InitObjectFields(const TaggedObject *object); JSThread *thread_ {nullptr}; bool isTriggerGc_ {false}; bool triggerSemiGC_ {false}; EcmaVM *vm_ {nullptr}; Heap *heap_ {nullptr}; SharedHeap *sHeap_ {nullptr}; static constexpr uint32_t LENGTH_THRESHOLD = 50; static constexpr int MAX_LITERAL_HCLASS_CACHE_SIZE = 63; NO_COPY_SEMANTIC(ObjectFactory); NO_MOVE_SEMANTIC(ObjectFactory); void NewObjectHook() const; void NewSObjectHook() const; // used for creating jshclass in GlobalEnv, EcmaVM JSHandle NewEcmaHClassClass(JSHClass *hclass, uint32_t size, JSType type); JSHandle NewEcmaReadOnlyHClass(JSHClass *hclass, uint32_t size, JSType type, uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); JSHandle InitClassClass(); // used to create nonmovable js_object JSHandle NewNonMovableJSObject(const JSHandle &jshclass); // used to create nonmovable utf8 string at global constants JSHandle NewFromASCIINonMovable(std::string_view data); // used to create read only utf8 string at global constants JSHandle NewFromASCIIReadOnly(std::string_view data); // used for creating Function JSHandle NewJSFunction(const JSHandle &env, const JSHandle &hclass); JSHandle CreateObjectClass(const JSHandle &keys, const JSHandle &values); JSHandle CreateObjectClass(const JSHandle &properties, size_t length); JSHandle CreateFunctionClass(FunctionKind kind, uint32_t size, JSType type, const JSHandle &prototype); JSHandle CreateBoundFunctionClass(); JSHandle CreateDefaultClassPrototypeHClass(JSHClass *hclass); JSHandle CreateDefaultClassConstructorHClass(JSHClass *hclass); // used for creating ref.prototype in buildins, such as Number.prototype JSHandle NewJSPrimitiveRef(const JSHandle &hclass, const JSHandle &object); JSHandle GetStringFromStringTable(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress) const; JSHandle GetCompressedSubStringFromStringTable(const JSHandle &string, uint32_t offset, uint32_t utf8Len) const; JSHandle GetStringFromStringTableNonMovable(const uint8_t *utf8Data, uint32_t utf8Len) const; JSHandle GetStringFromStringTableReadOnly(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress = true) const; // For MUtf-8 string data EcmaString *PUBLIC_API GetRawStringFromStringTable(StringData sd, MemSpaceType type = MemSpaceType::SHARED_OLD_SPACE, bool isConstantString = false, uint32_t idOffset = 0) const; JSHandle GetStringFromStringTable(const uint16_t *utf16Data, uint32_t utf16Len, bool canBeCompress) const; JSHandle GetStringFromStringTable(EcmaString *string) const; JSHandle GetStringFromStringTable(const JSHandle &firstString, const JSHandle &secondString); JSHandle CreateJSArguments(const JSHandle &env); JSHandle CreateJSArrayInstanceClass(JSHandle proto, uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); JSHandle CreateJSRegExpInstanceClass(JSHandle proto); inline TaggedObject *AllocObjectWithSpaceType(size_t size, JSHClass *cls, MemSpaceType type); JSHandle NewTaggedArrayWithoutInit(uint32_t length, MemSpaceType spaceType); // For object with many properties, directly create new HClass instead of searching on transitions JSHandle CreateLargeJSObjectWithProperties(size_t propertyCount, const Local *keys, const PropertyDescriptor *descs); JSHandle CreateLargeJSObjectWithNamedProperties(size_t propertyCount, const char **keys, const Local *values); // For object with numerous properties, directly create it in dictionary mode JSHandle CreateDictionaryJSObjectWithProperties(size_t propertyCount, const Local *keys, const PropertyDescriptor *descs); JSHandle CreateDictionaryJSObjectWithNamedProperties(size_t propertyCount, const char **keys, const Local *values); JSHandle NewMutantTaggedArrayWithoutInit(uint32_t length, MemSpaceType spaceType); friend class Builtins; // create builtins object need hclass friend class JSFunction; // create prototype_or_hclass need hclass friend class JSHClass; // HC transition need hclass friend class EcmaVM; // hold the factory instance friend class JsVerificationTest; friend class PandaFileTranslator; friend class LiteralDataExtractor; friend class RuntimeStubs; friend class ClassInfoExtractor; friend class ModuleDataExtractor; friend class ModuleDataAccessor; friend class ConstantPool; friend class EcmaContext; friend class kungfu::TSHClassGenerator; friend class panda::FunctionRef; }; class ClassLinkerFactory { private: friend class GlobalEnv; // root class in class_linker need hclass friend class EcmaVM; // root class in class_linker need hclass }; } // namespace panda::ecmascript #endif // ECMASCRIPT_OBJECT_FACTORY_H