1/* 2 * Copyright (c) 2021-2024 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_H 17#define ECMASCRIPT_OBJECT_FACTORY_H 18 19#include "ecmascript/base/error_type.h" 20#include "ecmascript/base/number_helper.h" 21#include "ecmascript/compiler/builtins/builtins_call_signature.h" 22#include "ecmascript/ecma_string.h" 23#include "ecmascript/js_function_kind.h" 24#include "ecmascript/js_handle.h" 25#include "ecmascript/js_hclass.h" 26#include "ecmascript/js_native_pointer.h" 27#include "ecmascript/js_tagged_value.h" 28#include "ecmascript/mem/heap_region_allocator.h" 29#include "ecmascript/mem/machine_code.h" 30#include "ecmascript/mem/native_area_allocator.h" 31#include "ecmascript/mem/space.h" 32#include "ecmascript/shared_objects/js_shared_object.h" 33#include "ecmascript/tagged_array.h" 34#include "ecmascript/byte_array.h" 35#include "ecmascript/pgo_profiler/pgo_extra_profiler.h" 36 37namespace panda::ecmascript { 38struct MethodLiteral; 39class Method; 40class JSObject; 41class JSArray; 42class JSAPIPlainArray; 43class JSSymbol; 44class JSFunctionBase; 45class JSFunction; 46class JSBoundFunction; 47class JSProxyRevocFunction; 48class JSAsyncAwaitStatusFunction; 49class JSPrimitiveRef; 50class GlobalEnv; 51class GlobalEnvConstants; 52class AccessorData; 53class JSGlobalObject; 54class LexicalEnv; 55class SendableEnv; 56class JSDate; 57class JSProxy; 58class JSRealm; 59class JSArguments; 60class TaggedQueue; 61class JSForInIterator; 62class JSSet; 63class JSSharedSet; 64class JSMap; 65class JSSharedMap; 66class JSRegExp; 67class JSSetIterator; 68class JSSharedSetIterator; 69class JSRegExpIterator; 70class JSMapIterator; 71class JSSharedMapIterator; 72class JSArrayIterator; 73class JSSharedArrayIterator; 74class JSAPIPlainArrayIterator; 75class JSStringIterator; 76class JSGeneratorObject; 77class CompletionRecord; 78class GeneratorContext; 79class JSArrayBuffer; 80class JSSendableArrayBuffer; 81class JSDataView; 82class JSPromise; 83class JSPromiseReactionsFunction; 84class JSPromiseExecutorFunction; 85class JSAsyncModuleFulfilledFunction; 86class JSAsyncModuleRejectedFunction; 87class JSPromiseAllResolveElementFunction; 88class JSAsyncGeneratorResNextRetProRstFtn; 89class JSPromiseAnyRejectElementFunction; 90class JSPromiseAllSettledElementFunction; 91class JSPromiseFinallyFunction; 92class JSPromiseValueThunkOrThrowerFunction; 93class PromiseReaction; 94class PromiseCapability; 95class PromiseIteratorRecord; 96class JSAsyncFuncObject; 97class JSAsyncFunction; 98class JSAsyncFromSyncIterUnwarpFunction; 99class PromiseRecord; 100class JSLocale; 101class ResolvingFunctionsRecord; 102class EcmaVM; 103class Heap; 104class SharedHeap; 105class ConstantPool; 106class Program; 107class LayoutInfo; 108class JSIntlBoundFunction; 109class FreeObject; 110class JSNativePointer; 111class JSAPIArrayList; 112class JSAPIArrayListIterator; 113class JSAPIDeque; 114class JSAPIDequeIterator; 115class TaggedHashArray; 116class LinkedNode; 117class RBTreeNode; 118class JSAPIHashMap; 119class JSAPIHashSet; 120class JSAPIHashMapIterator; 121class JSAPIHashSetIterator; 122class JSAPILightWeightMap; 123class JSAPILightWeightMapIterator; 124class JSAPILightWeightSet; 125class JSAPILightWeightSetIterator; 126class JSAPIQueue; 127class JSAPIQueueIterator; 128class JSAPIStack; 129class JSAPIStackIterator; 130class JSAPITreeSet; 131class JSAPITreeMap; 132class JSAPITreeSetIterator; 133class JSAPITreeMapIterator; 134class JSAPIVector; 135class JSAPIVectorIterator; 136class JSAPIBitVector; 137class JSAPIBitVectorIterator; 138class JSAPILinkedList; 139class JSAPIList; 140class JSAPILinkedListIterator; 141class JSAPIListIterator; 142class ModuleNamespace; 143class NativeModuleFailureInfo; 144class ImportEntry; 145class LocalExportEntry; 146class IndirectExportEntry; 147class StarExportEntry; 148class SourceTextModule; 149class CjsModule; 150class CjsRequire; 151class CjsExports; 152class ResolvedBinding; 153class ResolvedIndexBinding; 154class ResolvedRecordIndexBinding; 155class ResolvedRecordBinding; 156class BigInt; 157class AsyncGeneratorRequest; 158class AsyncIteratorRecord; 159class JSAsyncGeneratorFunction; 160class JSAsyncGeneratorObject; 161class CellRecord; 162class ClassLiteral; 163 164namespace job { 165class MicroJobQueue; 166class PendingJob; 167} // namespace job 168class TransitionHandler; 169class PrototypeHandler; 170class TransWithProtoHandler; 171class StoreTSHandler; 172class PropertyBox; 173class ProtoChangeMarker; 174class ProtoChangeDetails; 175class MarkerCell; 176class ProfileTypeInfo; 177class MachineCode; 178class ClassInfoExtractor; 179class AOTLiteralInfo; 180class ProfileTypeInfoCell; 181class FunctionTemplate; 182class VTable; 183namespace kungfu { 184class TSHClassGenerator; 185} // namespace kungfu 186 187enum class CompletionRecordType : uint8_t; 188enum class PrimitiveType : uint8_t; 189enum class IterationKind : uint8_t; 190enum class MethodIndex : uint8_t; 191enum class SharedTypes : uint8_t; 192using ErrorType = base::ErrorType; 193using base::ErrorType; 194using ExtraProfileTypeInfo = pgo::ExtraProfileTypeInfo; 195 196enum class RemoveSlots { YES, NO }; 197enum class GrowMode { KEEP, GROW }; 198enum class StackCheck { YES, NO }; 199 200class ObjectFactory { 201public: 202 static constexpr JSTaggedType FREE_MEMMORY_ADDRESS_ZAM_VALUE = 0xDEADFACE; 203 ObjectFactory(JSThread *thread, Heap *heap, SharedHeap *sHeap); 204 ~ObjectFactory() = default; 205 JSHandle<Method> NewMethodForNativeFunction(const void *func, FunctionKind kind = FunctionKind::NORMAL_FUNCTION, 206 kungfu::BuiltinsStubCSigns::ID builtinId = 207 kungfu::BuiltinsStubCSigns::INVALID, 208 MemSpaceType methodSpaceType = SHARED_OLD_SPACE); 209 210 JSHandle<ProfileTypeInfo> NewProfileTypeInfo(uint32_t length); 211 JSHandle<ConstantPool> NewConstantPool(uint32_t capacity); 212 JSHandle<Program> NewProgram(); 213 214 JSHandle<JSObject> PUBLIC_API GetJSError(const ErrorType &errorType, const char *data = nullptr, 215 StackCheck needCheckStack = StackCheck::YES); 216 217 JSHandle<JSObject> NewJSError(const ErrorType &errorType, const JSHandle<EcmaString> &message, 218 StackCheck needCheckStack = StackCheck::YES); 219 220 JSHandle<JSObject> NewJSAggregateError(); 221 222 JSHandle<TransitionHandler> NewTransitionHandler(); 223 224 JSHandle<PrototypeHandler> NewPrototypeHandler(); 225 226 JSHandle<TransWithProtoHandler> NewTransWithProtoHandler(); 227 228 JSHandle<StoreTSHandler> NewStoreTSHandler(); 229 230 JSHandle<JSObject> NewEmptyJSObject(uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 231 232 // use for others create, prototype is Function.prototype 233 // use for native function 234 JSHandle<JSFunction> NewJSFunction(const JSHandle<GlobalEnv> &env, const void *nativeFunc = nullptr, 235 FunctionKind kind = FunctionKind::NORMAL_FUNCTION, 236 kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID, 237 MemSpaceType methodSpaceType = SHARED_OLD_SPACE); 238 JSHandle<JSFunction> NewSFunction(const JSHandle<GlobalEnv> &env, 239 const void *nativeFunc = nullptr, 240 FunctionKind kind = FunctionKind::NORMAL_FUNCTION, 241 kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID, 242 MemSpaceType spaceType = SHARED_OLD_SPACE); 243 void InitializeMethod(const MethodLiteral *methodLiteral, JSHandle<Method> &method); 244 // use for method 245 JSHandle<JSFunction> NewJSFunction(const JSHandle<GlobalEnv> &env, const JSHandle<Method> &method); 246 247 JSHandle<JSFunction> NewJSFunction(const JSHandle<Method> &methodHandle); 248 249 JSHandle<JSFunction> NewJSFunction(const JSHandle<Method> &methodHandle, 250 const JSHandle<JSTaggedValue> &homeObject); 251 252 JSHandle<JSFunction> NewJSNativeErrorFunction(const JSHandle<GlobalEnv> &env, const void *nativeFunc = nullptr); 253 254 JSHandle<JSFunction> NewSpecificTypedArrayFunction(const JSHandle<GlobalEnv> &env, 255 const void *nativeFunc = nullptr, 256 kungfu::BuiltinsStubCSigns::ID builtinId = 257 kungfu::BuiltinsStubCSigns::INVALID); 258 259 JSHandle<JSObject> OrdinaryNewJSObjectCreate(const JSHandle<JSTaggedValue> &proto); 260 261 JSHandle<JSObject> CreateNullJSObject(); 262 263 JSHandle<JSFunction> NewAotFunction(uint32_t numArgs, uintptr_t codeEntry); 264 265 JSHandle<JSBoundFunction> NewJSBoundFunction(const JSHandle<JSTaggedValue> &target, 266 const JSHandle<JSTaggedValue> &boundThis, 267 const JSHandle<TaggedArray> &args); 268 269 JSHandle<JSIntlBoundFunction> NewJSIntlBoundFunction(MethodIndex idx, int functionLength = 1); 270 271 JSHandle<JSProxyRevocFunction> NewJSProxyRevocFunction(const JSHandle<JSProxy> &proxy); 272 273 JSHandle<JSAsyncAwaitStatusFunction> NewJSAsyncAwaitStatusFunction(MethodIndex idx); 274 275 JSHandle<JSGeneratorObject> NewJSGeneratorObject(JSHandle<JSTaggedValue> generatorFunction); 276 277 JSHandle<JSAsyncFuncObject> NewJSAsyncFuncObject(); 278 JSHandle<JSAsyncGeneratorObject> NewJSAsyncGeneratorObject(JSHandle<JSTaggedValue> generatorFunction); 279 280 JSHandle<JSPrimitiveRef> NewJSPrimitiveRef(const JSHandle<JSFunction> &function, 281 const JSHandle<JSTaggedValue> &object); 282 JSHandle<JSPrimitiveRef> NewJSPrimitiveRef(PrimitiveType type, const JSHandle<JSTaggedValue> &object); 283 284 // get JSHClass for Ecma ClassLinker 285 JSHandle<GlobalEnv> NewGlobalEnv(JSHClass *globalEnvClass); 286 287 // get JSHClass for Ecma ClassLinker 288 JSHandle<LexicalEnv> NewLexicalEnv(int numSlots); 289 290 inline LexicalEnv *InlineNewLexicalEnv(int numSlots); 291 292 JSHandle<JSSymbol> NewEmptySymbol(); 293 294 JSHandle<JSSymbol> NewJSSymbol(); 295 296 JSHandle<JSSymbol> NewPrivateSymbol(); 297 298 JSHandle<JSSymbol> NewPrivateNameSymbol(const JSHandle<JSTaggedValue> &name); 299 300 JSHandle<JSSymbol> NewWellKnownSymbol(const JSHandle<JSTaggedValue> &name); 301 302 JSHandle<JSSymbol> NewPublicSymbol(const JSHandle<JSTaggedValue> &name); 303 304 JSHandle<JSSymbol> NewSymbolWithTable(const JSHandle<JSTaggedValue> &name); 305 306 JSHandle<JSSymbol> NewPrivateNameSymbolWithChar(std::string_view description); 307 308 JSHandle<JSSymbol> NewWellKnownSymbolWithChar(std::string_view description); 309 310 JSHandle<JSSymbol> NewPublicSymbolWithChar(std::string_view description); 311 312 JSHandle<JSSymbol> NewSymbolWithTableWithChar(std::string_view description); 313 314 JSHandle<AccessorData> NewAccessorData(); 315 JSHandle<AccessorData> NewInternalAccessor(void *setter, void *getter); 316 317 JSHandle<PromiseCapability> NewPromiseCapability(); 318 319 JSHandle<PromiseReaction> NewPromiseReaction(); 320 321 JSHandle<PromiseRecord> NewPromiseRecord(); 322 JSHandle<AsyncGeneratorRequest> NewAsyncGeneratorRequest(); 323 324 JSHandle<AsyncIteratorRecord> NewAsyncIteratorRecord(const JSHandle<JSTaggedValue> &itor, 325 const JSHandle<JSTaggedValue> &next, bool done); 326 327 JSHandle<ResolvingFunctionsRecord> NewResolvingFunctionsRecord(); 328 329 JSHandle<PromiseIteratorRecord> NewPromiseIteratorRecord(const JSHandle<JSTaggedValue> &itor, bool done); 330 331 JSHandle<job::MicroJobQueue> NewMicroJobQueue(); 332 333 JSHandle<job::PendingJob> NewPendingJob(const JSHandle<JSFunction> &func, const JSHandle<TaggedArray> &argv); 334 335 JSHandle<JSArray> NewJSArray(); 336 JSHandle<JSSharedArray> PUBLIC_API NewJSSArray(); 337 JSHandle<JSArray> PUBLIC_API NewJSArray(size_t length, JSHandle<JSHClass> &hclass); 338 JSHandle<TaggedArray> PUBLIC_API NewJsonFixedArray(size_t start, size_t length, 339 const std::vector<JSHandle<JSTaggedValue>> &vec); 340 JSHandle<TaggedArray> PUBLIC_API NewSJsonFixedArray(size_t start, size_t length, 341 const std::vector<JSHandle<JSTaggedValue>> &vec); 342 343 JSHandle<JSProxy> NewJSProxy(const JSHandle<JSTaggedValue> &target, const JSHandle<JSTaggedValue> &handler); 344 JSHandle<JSRealm> NewJSRealm(); 345 346 JSHandle<JSArguments> NewJSArguments(); 347 348 JSHandle<JSPrimitiveRef> NewJSString(const JSHandle<JSTaggedValue> &str, const JSHandle<JSTaggedValue> &newTarget); 349 350 template <typename Derived> 351 JSHandle<TaggedArray> ConvertListToArray(const JSThread *thread, const JSHandle<Derived> &list, 352 uint32_t numberOfNodes) 353 { 354 MemSpaceType spaceType = numberOfNodes < LENGTH_THRESHOLD ? MemSpaceType::SEMI_SPACE : MemSpaceType::OLD_SPACE; 355 JSHandle<TaggedArray> dstElements = NewTaggedArrayWithoutInit(numberOfNodes, spaceType); 356 if (numberOfNodes == 0) { 357 return dstElements; 358 } 359 int dataIndex = Derived::ELEMENTS_START_INDEX; 360 for (uint32_t i = 0; i < numberOfNodes; i++) { 361 dataIndex = list->GetElement(dataIndex + Derived::NEXT_PTR_OFFSET).GetInt(); 362 dstElements->Set(thread, i, list->GetElement(dataIndex)); 363 } 364 return dstElements; 365 } 366 367 JSHandle<JSObject> NewAndCopyJSArrayObject(JSHandle<JSObject> thisObjHandle, uint32_t newLength, 368 uint32_t oldLength, uint32_t k = 0); 369 JSHandle<TaggedArray> NewAndCopyTaggedArray(JSHandle<TaggedArray> &srcElements, uint32_t newLength, 370 uint32_t oldLength, uint32_t k = 0); 371 JSHandle<TaggedArray> NewAndCopyTaggedArraySkipBarrier(JSHandle<TaggedArray> &srcElements, uint32_t newLength, 372 uint32_t oldLength, uint32_t k = 0); 373 JSHandle<TaggedArray> NewAndCopySNameDictionary(JSHandle<TaggedArray> &srcElements, uint32_t length); 374 JSHandle<TaggedArray> NewAndCopyTaggedArrayByObject(JSHandle<JSObject> thisObjHandle, uint32_t newLength, 375 uint32_t oldLength, uint32_t k = 0); 376 JSHandle<MutantTaggedArray> NewAndCopyMutantTaggedArrayByObject(JSHandle<JSObject> thisObjHandle, 377 uint32_t newLength, uint32_t oldLength, 378 uint32_t k = 0); 379 JSHandle<TaggedArray> PUBLIC_API NewTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 380 JSHandle<TaggedArray> NewTaggedArray(uint32_t length, JSTaggedValue initVal, bool nonMovable); 381 JSHandle<TaggedArray> NewTaggedArray(uint32_t length, JSTaggedValue initVal, MemSpaceType spaceType); 382 // Copy on write array is allocated in nonmovable space by default. 383 JSHandle<COWTaggedArray> NewCOWTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 384 JSHandle<COWMutantTaggedArray> NewCOWMutantTaggedArray(uint32_t length, JSTaggedType initVal = base::SPECIAL_HOLE); 385 JSHandle<MutantTaggedArray> NewMutantTaggedArray(uint32_t length, JSTaggedType initVal = base::SPECIAL_HOLE); 386 JSHandle<TaggedArray> PUBLIC_API NewDictionaryArray(uint32_t length); 387 JSHandle<JSForInIterator> NewJSForinIterator(const JSHandle<JSTaggedValue> &obj, 388 const JSHandle<JSTaggedValue> keys, 389 const JSHandle<JSTaggedValue> cachedHclass); 390 391 JSHandle<ByteArray> NewByteArray(uint32_t length, uint32_t size, void *srcData = nullptr, 392 MemSpaceType spaceType = MemSpaceType::SEMI_SPACE); 393 394 JSHandle<PropertyBox> NewPropertyBox(const JSHandle<JSTaggedValue> &name); 395 396 JSHandle<ProtoChangeMarker> NewProtoChangeMarker(); 397 398 JSHandle<ProtoChangeDetails> NewProtoChangeDetails(); 399 400 JSHandle<MarkerCell> NewMarkerCell(); 401 JSHandle<BigInt> NewBigInt(uint32_t length); 402 // use for copy properties keys's array to another array 403 JSHandle<TaggedArray> ExtendArray(const JSHandle<TaggedArray> &old, uint32_t length, 404 JSTaggedValue initVal = JSTaggedValue::Hole(), 405 MemSpaceType type = MemSpaceType::SEMI_SPACE, 406 ElementsKind kind = ElementsKind::GENERIC); 407 JSHandle<TaggedArray> CopyPartArray(const JSHandle<TaggedArray> &old, uint32_t start, uint32_t end); 408 JSHandle<TaggedArray> PUBLIC_API CopyArray(const JSHandle<TaggedArray> &old, uint32_t oldLength, uint32_t newLength, 409 JSTaggedValue initVal = JSTaggedValue::Hole(), 410 MemSpaceType type = MemSpaceType::SEMI_SPACE, 411 ElementsKind kind = ElementsKind::GENERIC); 412 JSHandle<TaggedArray> CopyFromEnumCache(const JSHandle<TaggedArray> &old); 413 JSHandle<TaggedArray> CloneProperties(const JSHandle<TaggedArray> &old); 414 JSHandle<TaggedArray> CloneProperties(const JSHandle<TaggedArray> &old, const JSHandle<JSTaggedValue> &env, 415 const JSHandle<JSObject> &obj); 416 417 JSHandle<LayoutInfo> PUBLIC_API CreateLayoutInfo(int properties, MemSpaceType type = MemSpaceType::SEMI_SPACE, 418 GrowMode mode = GrowMode::GROW); 419 420 JSHandle<LayoutInfo> ExtendLayoutInfo(const JSHandle<LayoutInfo> &old, int properties); 421 422 JSHandle<LayoutInfo> CopyLayoutInfo(const JSHandle<LayoutInfo> &old); 423 424 JSHandle<LayoutInfo> CopyAndReSort(const JSHandle<LayoutInfo> &old, int end, int capacity); 425 426 JSHandle<EcmaString> GetEmptyString() const; 427 428 JSHandle<TaggedArray> EmptyArray() const; 429 430 JSHandle<MutantTaggedArray> EmptyMutantArray() const; 431 432 FreeObject *FillFreeObject(uintptr_t address, size_t size, RemoveSlots removeSlots = RemoveSlots::NO, 433 uintptr_t hugeObjectHead = 0); 434 435 TaggedObject *NewObject(const JSHandle<JSHClass> &hclass); 436 437 TaggedObject *NewNonMovableObject(const JSHandle<JSHClass> &hclass, uint32_t inobjPropCount = 0); 438 439 void InitializeExtraProperties(const JSHandle<JSHClass> &hclass, TaggedObject *obj, uint32_t inobjPropCount); 440 441 JSHandle<TaggedQueue> NewTaggedQueue(uint32_t length); 442 443 JSHandle<TaggedQueue> GetEmptyTaggedQueue() const; 444 445 JSHandle<JSSetIterator> NewJSSetIterator(const JSHandle<JSSet> &set, IterationKind kind); 446 JSHandle<JSSharedSetIterator> NewJSSetIterator(const JSHandle<JSSharedSet> &set, IterationKind kind); 447 448 JSHandle<JSRegExpIterator> NewJSRegExpIterator(const JSHandle<JSTaggedValue> &matcher, 449 const JSHandle<EcmaString> &inputStr, bool global, 450 bool fullUnicode); 451 452 JSHandle<JSMapIterator> NewJSMapIterator(const JSHandle<JSMap> &map, IterationKind kind); 453 454 JSHandle<JSSharedMapIterator> NewJSMapIterator(const JSHandle<JSSharedMap> &map, IterationKind kind); 455 456 JSHandle<JSArrayIterator> NewJSArrayIterator(const JSHandle<JSObject> &array, IterationKind kind); 457 JSHandle<JSSharedArrayIterator> NewJSSharedArrayIterator(const JSHandle<JSObject> &array, IterationKind kind); 458 459 JSHandle<CompletionRecord> NewCompletionRecord(CompletionRecordType type, JSHandle<JSTaggedValue> value); 460 461 JSHandle<GeneratorContext> NewGeneratorContext(); 462 463 JSHandle<JSPromiseReactionsFunction> CreateJSPromiseReactionsFunction(MethodIndex idx); 464 465 JSHandle<JSPromiseExecutorFunction> CreateJSPromiseExecutorFunction(); 466 467 JSHandle<JSAsyncModuleFulfilledFunction> CreateJSAsyncModuleFulfilledFunction(); 468 469 JSHandle<JSAsyncModuleRejectedFunction> CreateJSAsyncModuleRejectedFunction(); 470 471 JSHandle<JSPromiseAllResolveElementFunction> NewJSPromiseAllResolveElementFunction(); 472 473 JSHandle<JSPromiseAnyRejectElementFunction> NewJSPromiseAnyRejectElementFunction(); 474 475 JSHandle<JSPromiseAllSettledElementFunction> NewJSPromiseAllSettledResolveElementFunction(); 476 477 JSHandle<JSPromiseAllSettledElementFunction> NewJSPromiseAllSettledRejectElementFunction(); 478 479 JSHandle<JSPromiseFinallyFunction> NewJSPromiseThenFinallyFunction(); 480 481 JSHandle<JSPromiseFinallyFunction> NewJSPromiseCatchFinallyFunction(); 482 483 JSHandle<JSPromiseValueThunkOrThrowerFunction> NewJSPromiseValueThunkFunction(); 484 485 JSHandle<JSPromiseValueThunkOrThrowerFunction> NewJSPromiseThrowerFunction(); 486 487 JSHandle<JSAsyncGeneratorResNextRetProRstFtn> NewJSAsyGenResNextRetProRstFulfilledFtn(); 488 489 JSHandle<JSAsyncGeneratorResNextRetProRstFtn> NewJSAsyGenResNextRetProRstRejectedFtn(); 490 491 JSHandle<JSAsyncFromSyncIterUnwarpFunction> NewJSAsyncFromSyncIterUnwarpFunction(); 492 493 JSHandle<JSObject> CloneObjectLiteral(JSHandle<JSObject> object, const JSHandle<JSTaggedValue> &env, 494 bool canShareHClass = true); 495 JSHandle<JSObject> CloneObjectLiteral(JSHandle<JSObject> object); 496 JSHandle<JSArray> CloneArrayLiteral(JSHandle<JSArray> object); 497 JSHandle<JSFunction> CloneJSFunction(JSHandle<JSFunction> func); 498 JSHandle<JSFunction> CloneSFunction(JSHandle<JSFunction> func); 499 JSHandle<JSFunction> CloneClassCtor(JSHandle<JSFunction> ctor, const JSHandle<JSTaggedValue> &lexenv, 500 bool canShareHClass); 501 JSHandle<JSFunction> CreateJSFunctionFromTemplate(JSHandle<FunctionTemplate> funcTemp); 502 JSHandle<JSFunction> CreateSFunctionFromTemplate(JSHandle<FunctionTemplate> funcTemp); 503 504 void NewJSArrayBufferData(const JSHandle<JSArrayBuffer> &array, int32_t length); 505 void NewJSSendableArrayBufferData(const JSHandle<JSSendableArrayBuffer> &array, int32_t length); 506 JSHandle<JSSendableArrayBuffer> NewJSSendableArrayBuffer(int32_t length); 507 JSHandle<JSSendableArrayBuffer> NewJSSendableArrayBuffer(void *buffer, int32_t length, 508 const NativePointerCallback &deleter, void *data); 509 510 JSHandle<JSArrayBuffer> NewJSArrayBuffer(int32_t length); 511 512 JSHandle<JSArrayBuffer> NewJSArrayBuffer(void *buffer, int32_t length, const NativePointerCallback &deleter, 513 void *data, bool share = false); 514 515 JSHandle<JSDataView> NewJSDataView(JSHandle<JSArrayBuffer> buffer, uint32_t offset, uint32_t length); 516 517 void NewJSSharedArrayBufferData(const JSHandle<JSArrayBuffer> &array, int32_t length); 518 519 JSHandle<JSArrayBuffer> NewJSSharedArrayBuffer(int32_t length); 520 521 JSHandle<JSArrayBuffer> NewJSSharedArrayBuffer(void *buffer, int32_t length); 522 523 void NewJSRegExpByteCodeData(const JSHandle<JSRegExp> ®exp, void *buffer, size_t size); 524 525 template<typename T, typename S> 526 inline void NewJSIntlIcuData(const JSHandle<T> &obj, const S &icu, const NativePointerCallback &callback); 527 528 EcmaString *PUBLIC_API InternString(const JSHandle<JSTaggedValue> &key); 529 530 inline JSHandle<JSNativePointer> NewJSNativePointer(void *externalPointer, 531 const NativePointerCallback &callBack = nullptr, 532 void *data = nullptr, 533 bool nonMovable = false, 534 size_t nativeBindingsize = 0, 535 Concurrent isConcurrent = Concurrent::NO, 536 NativeFlag flag = NativeFlag::NO_DIV); 537 538 JSHandle<JSObject> NewOldSpaceObjLiteralByHClass(const JSHandle<JSHClass> &hclass); 539 JSHandle<JSHClass> SetLayoutInObjHClass(const JSHandle<TaggedArray> &properties, size_t length, 540 const JSHandle<JSHClass> &objClass); 541 static bool CanObjectLiteralHClassCache(size_t length); 542 JSHandle<JSHClass> CreateObjectLiteralRootHClass(size_t length); 543 JSHandle<JSHClass> GetObjectLiteralRootHClass(size_t length); 544 JSHandle<JSHClass> GetObjectLiteralHClass(const JSHandle<TaggedArray> &properties, size_t length); 545 // only use for creating Function.prototype and Function 546 JSHandle<JSFunction> NewJSFunctionByHClass(const JSHandle<Method> &method, const JSHandle<JSHClass> &clazz, 547 MemSpaceType type = MemSpaceType::SEMI_SPACE); 548 JSHandle<JSFunction> NewJSFunctionByHClass(const void *func, const JSHandle<JSHClass> &clazz, 549 FunctionKind kind = FunctionKind::NORMAL_FUNCTION); 550 JSHandle<JSFunction> NewJSFunctionByHClassWithoutAccessor(const void *func, 551 const JSHandle<JSHClass> &clazz, FunctionKind kind = FunctionKind::NORMAL_FUNCTION); 552 JSHandle<Method> NewMethod(const MethodLiteral *methodLiteral, MemSpaceType spaceType = OLD_SPACE); 553 554 JSHandle<Method> NewMethod(const JSPandaFile *jsPandaFile, MethodLiteral *methodLiteral, 555 JSHandle<ConstantPool> constpool, uint32_t entryIndex, 556 bool needSetAotFlag, bool *canFastCall = nullptr); 557 558 // used for creating jsobject by constructor 559 JSHandle<JSObject> PUBLIC_API NewJSObjectByConstructor(const JSHandle<JSFunction> &constructor, 560 const JSHandle<JSTaggedValue> &newTarget); 561 JSHandle<JSObject> NewJSObjectByConstructor(JSHandle<GlobalEnv> env, 562 const JSHandle<JSFunction> &constructor, uint32_t inlinedProps); 563 JSHandle<JSObject> NewJSObjectByConstructor(const JSHandle<JSFunction> &constructor, 564 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 565 void InitializeJSObject(const JSHandle<JSObject> &obj, const JSHandle<JSHClass> &jshclass); 566 567 JSHandle<JSObject> NewJSObjectWithInit(const JSHandle<JSHClass> &jshclass); 568 uintptr_t NewSpaceBySnapshotAllocator(size_t size); 569 TaggedObject *NewMachineCodeObject(size_t length, MachineCodeDesc &desc); 570 JSHandle<MachineCode> SetMachineCodeObjectData(TaggedObject *obj, size_t length, 571 const MachineCodeDesc &desc, JSHandle<Method> &method); 572 JSHandle<MachineCode> NewMachineCodeObject(size_t length, const MachineCodeDesc &desc, JSHandle<Method> &method); 573 JSHandle<ClassInfoExtractor> NewClassInfoExtractor(JSHandle<JSTaggedValue> method); 574 JSHandle<ClassLiteral> NewClassLiteral(); 575 576 // ----------------------------------- new string ---------------------------------------- 577 JSHandle<EcmaString> PUBLIC_API NewFromASCII(std::string_view data); 578 JSHandle<EcmaString> NewFromUtf8WithoutStringTable(std::string_view data); 579 JSHandle<EcmaString> PUBLIC_API NewFromUtf8(std::string_view data); 580 JSHandle<EcmaString> NewFromUtf8(std::string_view data, bool canBeCompress); 581 JSHandle<EcmaString> NewFromUtf8ReadOnly(std::string_view data); 582 JSHandle<EcmaString> NewFromASCIISkippingStringTable(std::string_view data); 583 JSHandle<EcmaString> NewFromUtf16WithoutStringTable(std::u16string_view data); 584 JSHandle<EcmaString> NewFromUtf16(std::u16string_view data); 585 586 JSHandle<EcmaString> PUBLIC_API NewFromStdString(const std::string &data); 587 588 JSHandle<EcmaString> NewFromUtf8WithoutStringTable(const uint8_t *utf8Data, uint32_t utf8Len); 589 JSHandle<EcmaString> NewFromUtf8(const uint8_t *utf8Data, uint32_t utf8Len); 590 591 JSHandle<EcmaString> NewFromUtf16WithoutStringTable(const uint16_t *utf16Data, uint32_t utf16Len); 592 JSHandle<EcmaString> PUBLIC_API NewFromUtf16(const uint16_t *utf16Data, uint32_t utf16Len); 593 JSHandle<EcmaString> NewFromUtf16Compress(const uint16_t *utf16Data, uint32_t utf16Len); 594 JSHandle<EcmaString> NewFromUtf16NotCompress(const uint16_t *utf16Data, uint32_t utf16Len); 595 596 JSHandle<EcmaString> NewFromUtf8Literal(const uint8_t *utf8Data, uint32_t utf8Len); 597 JSHandle<EcmaString> PUBLIC_API NewFromUtf8LiteralCompress(const uint8_t *utf8Data, uint32_t utf8Len); 598 JSHandle<EcmaString> PUBLIC_API NewFromUtf8LiteralCompressSubString(const JSHandle<EcmaString> &string, 599 uint32_t offset, uint32_t utf8Len); 600 JSHandle<EcmaString> PUBLIC_API NewCompressedUtf8(const uint8_t *utf8Data, uint32_t utf8Len); 601 JSHandle<EcmaString> PUBLIC_API NewCompressedUtf8SubString(const JSHandle<EcmaString> &string, 602 uint32_t offset, uint32_t utf8Len); 603 JSHandle<EcmaString> NewFromUtf16Literal(const uint16_t *utf16Data, uint32_t utf16Len); 604 JSHandle<EcmaString> NewFromUtf16LiteralCompress(const uint16_t *utf16Data, uint32_t utf16Len); 605 JSHandle<EcmaString> PUBLIC_API NewFromUtf16LiteralNotCompress(const uint16_t *utf16Data, uint32_t utf16Len); 606 607 inline EcmaString *AllocLineStringObject(size_t size); 608 inline EcmaString *AllocLineStringObjectNoGC(size_t size); 609 inline EcmaString *AllocOldSpaceLineStringObject(size_t size); 610 inline EcmaString *AllocReadOnlyLineStringObject(size_t size); 611 inline EcmaString *AllocNonMovableLineStringObject(size_t size); 612 inline EcmaString *AllocSlicedStringObject(MemSpaceType type); 613 inline EcmaString *AllocConstantStringObject(MemSpaceType type); 614 inline EcmaString *AllocTreeStringObject(); 615 616 JSHandle<EcmaString> ConcatFromString(const JSHandle<EcmaString> &firstString, 617 const JSHandle<EcmaString> &secondString); 618 619 // used for creating Function 620 JSHandle<JSObject> PUBLIC_API NewJSObject(const JSHandle<JSHClass> &jshclass); 621 622 // used for creating jshclass in Builtins, Function, Class_Linker 623 JSHandle<JSHClass> NewEcmaHClass(uint32_t size, JSType type, const JSHandle<JSTaggedValue> &prototype); 624 JSHandle<JSHClass> NewEcmaHClass(uint32_t size, uint32_t inlinedProps, JSType type, 625 const JSHandle<JSTaggedValue> &prototype); 626 627 // used for creating jshclass in Builtins, Function, Class_Linker 628 JSHandle<JSHClass> PUBLIC_API NewEcmaHClass(uint32_t size, JSType type, 629 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 630 631 JSHandle<JSHClass> PUBLIC_API NewEcmaHClass(uint32_t size, uint32_t inlinedProps, JSType type, 632 const JSHandle<JSTaggedValue> &prototype, const JSHandle<JSTaggedValue> &layout); 633 634 // It is used to provide iterators for non ECMA standard jsapi containers. 635 JSHandle<JSAPIPlainArray> NewJSAPIPlainArray(uint32_t capacity); 636 JSHandle<JSAPIPlainArrayIterator> NewJSAPIPlainArrayIterator(const JSHandle<JSAPIPlainArray> &plainarray, 637 IterationKind kind); 638 JSHandle<JSAPIArrayList> NewJSAPIArrayList(uint32_t capacity); 639 640 JSHandle<JSAPILightWeightMapIterator> NewJSAPILightWeightMapIterator(const JSHandle<JSAPILightWeightMap> &obj, 641 IterationKind kind); 642 JSHandle<JSAPILightWeightSetIterator> NewJSAPILightWeightSetIterator(const JSHandle<JSAPILightWeightSet> &obj, 643 IterationKind kind); 644 JSHandle<TaggedArray> CopyQueue(const JSHandle<TaggedArray> &old, uint32_t newLength, 645 uint32_t front, uint32_t tail); 646 JSHandle<JSAPIQueueIterator> NewJSAPIQueueIterator(const JSHandle<JSAPIQueue> &queue); 647 JSHandle<TaggedArray> CopyDeque(const JSHandle<TaggedArray> &old, uint32_t newLength, uint32_t oldLength, 648 uint32_t first, uint32_t last); 649 JSHandle<JSAPIDequeIterator> NewJSAPIDequeIterator(const JSHandle<JSAPIDeque> &deque); 650 JSHandle<JSAPIArrayListIterator> NewJSAPIArrayListIterator(const JSHandle<JSAPIArrayList> &arrayList); 651 JSHandle<JSAPIList> NewJSAPIList(); 652 JSHandle<JSAPILinkedList> NewJSAPILinkedList(); 653 JSHandle<JSAPILinkedListIterator> NewJSAPILinkedListIterator(const JSHandle<JSAPILinkedList> &linkedList); 654 JSHandle<JSAPIListIterator> NewJSAPIListIterator(const JSHandle<JSAPIList> &list); 655 JSHandle<JSAPITreeMapIterator> NewJSAPITreeMapIterator(const JSHandle<JSAPITreeMap> &map, IterationKind kind); 656 JSHandle<JSAPITreeSetIterator> NewJSAPITreeSetIterator(const JSHandle<JSAPITreeSet> &set, IterationKind kind); 657 JSHandle<JSAPIStackIterator> NewJSAPIStackIterator(const JSHandle<JSAPIStack> &stack); 658 JSHandle<JSAPIVector> NewJSAPIVector(uint32_t capacity); 659 JSHandle<JSAPIVectorIterator> NewJSAPIVectorIterator(const JSHandle<JSAPIVector> &vector); 660 JSHandle<JSAPIBitVector> NewJSAPIBitVector(uint32_t capacity); 661 JSHandle<JSAPIBitVectorIterator> NewJSAPIBitVectorIterator(const JSHandle<JSAPIBitVector> &bitVector); 662 JSHandle<JSAPIHashMapIterator> NewJSAPIHashMapIterator(const JSHandle<JSAPIHashMap> &hashMap, IterationKind kind); 663 JSHandle<JSAPIHashSetIterator> NewJSAPIHashSetIterator(const JSHandle<JSAPIHashSet> &hashSet, IterationKind kind); 664 JSHandle<TaggedHashArray> NewTaggedHashArray(uint32_t length); 665 JSHandle<LinkedNode> NewLinkedNode(int hash, const JSHandle<JSTaggedValue> &key, 666 const JSHandle<JSTaggedValue> &value, 667 const JSHandle<LinkedNode> &next); 668 JSHandle<RBTreeNode> NewTreeNode(int hash, const JSHandle<JSTaggedValue> &key, 669 const JSHandle<JSTaggedValue> &value); 670 // --------------------------------------module-------------------------------------------- 671 JSHandle<ModuleNamespace> NewModuleNamespace(); 672 JSHandle<NativeModuleFailureInfo> NewNativeModuleFailureInfo(); 673 JSHandle<ImportEntry> NewImportEntry(); 674 JSHandle<ImportEntry> NewImportEntry(const JSHandle<JSTaggedValue> &moduleRequest, 675 const JSHandle<JSTaggedValue> &importName, 676 const JSHandle<JSTaggedValue> &localName, 677 SharedTypes sharedTypes); 678 JSHandle<LocalExportEntry> NewLocalExportEntry(); 679 JSHandle<LocalExportEntry> NewLocalExportEntry( 680 const JSHandle<JSTaggedValue> &exportName, const JSHandle<JSTaggedValue> &localName, 681 const uint32_t index, SharedTypes sharedTypes); 682 JSHandle<IndirectExportEntry> NewIndirectExportEntry(); 683 JSHandle<IndirectExportEntry> NewIndirectExportEntry(const JSHandle<JSTaggedValue> &exportName, 684 const JSHandle<JSTaggedValue> &moduleRequest, 685 const JSHandle<JSTaggedValue> &importName, 686 SharedTypes sharedTypes); 687 JSHandle<StarExportEntry> NewStarExportEntry(); 688 JSHandle<StarExportEntry> NewStarExportEntry(const JSHandle<JSTaggedValue> &moduleRequest, 689 SharedTypes sharedTypes); 690 JSHandle<SourceTextModule> NewSourceTextModule(); 691 JSHandle<ResolvedBinding> NewResolvedBindingRecord(); 692 JSHandle<ResolvedBinding> NewResolvedBindingRecord(const JSHandle<SourceTextModule> &module, 693 const JSHandle<JSTaggedValue> &bindingName); 694 JSHandle<ResolvedIndexBinding> NewResolvedIndexBindingRecord(); 695 JSHandle<ResolvedIndexBinding> NewResolvedIndexBindingRecord( 696 const JSHandle<SourceTextModule> &module, int32_t index); 697 698 JSHandle<CellRecord> NewCellRecord(); 699 JSHandle<JSFunction> NewJSAsyncGeneratorFunction(const JSHandle<Method> &method); 700 // --------------------------------------require-------------------------------------------- 701 JSHandle<CjsModule> NewCjsModule(); 702 JSHandle<CjsExports> NewCjsExports(); 703 JSHandle<CjsRequire> NewCjsRequire(); 704 705 JSHandle<JSHClass> CreateIteratorResultInstanceClass(const JSHandle<GlobalEnv> &env); 706 707 // --------------------------------------old space object-------------------------------------------- 708 JSHandle<JSObject> NewOldSpaceJSObject(const JSHandle<JSHClass> &jshclass); 709 TaggedObject *NewOldSpaceObject(const JSHandle<JSHClass> &hclass); 710 JSHandle<TaggedArray> NewOldSpaceTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 711 712 // ---------------------------------New objects used internally-------------------------------------- 713 JSHandle<JSArray> NewJSStableArrayWithElements(const JSHandle<TaggedArray> &elements); 714 715 // ---------------------------------------Used by AOT------------------------------------------------ 716 JSHandle<AOTLiteralInfo> NewAOTLiteralInfo(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 717 JSHandle<ExtraProfileTypeInfo> NewExtraProfileTypeInfo(); 718 JSHandle<ProfileTypeInfoCell> NewProfileTypeInfoCell(const JSHandle<JSTaggedValue> &value); 719 JSHandle<FunctionTemplate> NewFunctionTemplate( 720 const JSHandle<Method> &method, const JSHandle<JSTaggedValue> &module, int32_t length); 721 JSHandle<VTable> NewVTable(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 722 JSHandle<JSHClass> NewEcmaHClass(JSHClass *hclass, uint32_t size, JSType type, 723 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 724 725 // napi interface to create object with initial inline properties 726 JSHandle<JSTaggedValue> CreateJSObjectWithProperties(size_t propertyCount, const Local<JSValueRef> *keys, 727 const PropertyDescriptor *attributes); 728 JSHandle<JSTaggedValue> CreateJSObjectWithNamedProperties(size_t propertyCount, const char **keys, 729 const Local<JSValueRef> *values); 730 // Fill the given free memory range with special zam value. 731 void FillFreeMemoryRange(uintptr_t start, uintptr_t end); 732 733 // -----------------------------------shared object----------------------------------------- 734 JSHandle<JSObject> PUBLIC_API NewSharedOldSpaceJSObject(const JSHandle<JSHClass> &jshclass); 735 736 JSHandle<JSObject> PUBLIC_API NewSharedOldSpaceJSObjectWithInit(const JSHandle<JSHClass> &jshclass); 737 738 TaggedObject *NewSharedOldSpaceObject(const JSHandle<JSHClass> &hclass); 739 740 JSHandle<JSTaggedValue> CreateSObjectWithProperties(std::vector<PropertyDescriptor> &descs); 741 742 JSHandle<JSHClass> PUBLIC_API NewSEcmaHClass(uint32_t size, JSType type, uint32_t inlinedProps); 743 744 JSHandle<JSHClass> PUBLIC_API NewSEcmaHClass(JSHClass *hclass, uint32_t size, JSType type, 745 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 746 747 JSHandle<JSHClass> PUBLIC_API NewSEcmaHClass(uint32_t size, uint32_t inlinedProps, JSType type, 748 const JSHandle<JSTaggedValue> &prototype, const JSHandle<JSTaggedValue> &layout); 749 750 JSHandle<JSHClass> PUBLIC_API NewSEcmaHClassDictMode(uint32_t size, uint32_t inlinedProps, JSType type, 751 const JSHandle<JSTaggedValue> &prototype); 752 753 JSHandle<JSHClass> NewSEcmaHClassClass(JSHClass *hclass, uint32_t size, JSType type); 754 755 JSHandle<JSHClass> InitSClassClass(); 756 757 JSHandle<JSHClass> NewSEcmaReadOnlyHClass(JSHClass *hclass, uint32_t size, JSType type, 758 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 759 760 JSHandle<TaggedArray> SharedEmptyArray() const; 761 762 JSHandle<TaggedArray> CopySArray(const JSHandle<TaggedArray> &old, uint32_t oldLength, uint32_t newLength, 763 JSTaggedValue initVal = JSTaggedValue::Hole(), 764 ElementsKind kind = ElementsKind::GENERIC); 765 766 JSHandle<TaggedArray> ExtendSArray(const JSHandle<TaggedArray> &old, uint32_t length, 767 JSTaggedValue initVal, [[maybe_unused]] ElementsKind kind); 768 769 JSHandle<Method> NewSMethodForNativeFunction(const void *func, FunctionKind kind = FunctionKind::NORMAL_FUNCTION, 770 kungfu::BuiltinsStubCSigns::ID builtinId = 771 kungfu::BuiltinsStubCSigns::INVALID, 772 MemSpaceType spaceType = SHARED_OLD_SPACE); 773 774 JSHandle<JSFunction> NewSFunctionByHClass(const JSHandle<Method> &methodHandle, 775 const JSHandle<JSHClass> &hclass); 776 JSHandle<JSFunction> NewSFunctionByHClass(const void *func, const JSHandle<JSHClass> &hclass, 777 FunctionKind kind, 778 kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID, 779 MemSpaceType spaceType = SHARED_OLD_SPACE); 780 781 JSHandle<JSFunction> NewSFunctionWithAccessor( 782 const void *func, 783 const JSHandle<JSHClass> &hclass, 784 FunctionKind kind, 785 kungfu::BuiltinsStubCSigns::ID builtinId = kungfu::BuiltinsStubCSigns::INVALID, 786 MemSpaceType spaceType = SHARED_OLD_SPACE); 787 788 JSHandle<Method> NewSMethod(const MethodLiteral *methodLiteral, MemSpaceType methodSpaceType = SHARED_OLD_SPACE); 789 790 JSHandle<Method> PUBLIC_API NewSMethod(const JSPandaFile *jsPandaFile, 791 MethodLiteral *methodLiteral, 792 JSHandle<ConstantPool> constpool, 793 uint32_t entryIndex, 794 bool needSetAotFlag, 795 bool *canFastCall = nullptr); 796 797 JSHandle<ConstantPool> NewSConstantPool(uint32_t capacity); 798 799 JSHandle<AOTLiteralInfo> NewSAOTLiteralInfo(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 800 801 JSHandle<COWTaggedArray> NewSCOWTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 802 803 JSHandle<ClassLiteral> NewSClassLiteral(); 804 805 JSHandle<ClassInfoExtractor> NewSClassInfoExtractor(JSHandle<JSTaggedValue> method); 806 807 JSHandle<TaggedArray> NewSOldSpaceTaggedArray(uint32_t length, JSTaggedValue initVal = JSTaggedValue::Hole()); 808 809 JSHandle<TaggedArray> PUBLIC_API NewSTaggedArray(uint32_t length, 810 JSTaggedValue initVal = JSTaggedValue::Hole(), 811 MemSpaceType spaceType = SHARED_OLD_SPACE); 812 813 JSHandle<AccessorData> NewSAccessorData(); 814 815 JSHandle<SourceTextModule> NewSSourceTextModule(); 816 817 JSHandle<ModuleNamespace> NewSModuleNamespace(); 818 819 JSHandle<ImportEntry> NewSImportEntry(const JSHandle<JSTaggedValue> &moduleRequest, 820 const JSHandle<JSTaggedValue> &importName, 821 const JSHandle<JSTaggedValue> &localName); 822 823 JSHandle<LocalExportEntry> NewSLocalExportEntry(const JSHandle<JSTaggedValue> &exportName, 824 const JSHandle<JSTaggedValue> &localName, const uint32_t index); 825 826 JSHandle<IndirectExportEntry> NewSIndirectExportEntry(const JSHandle<JSTaggedValue> &exportName, 827 const JSHandle<JSTaggedValue> &moduleRequest, 828 const JSHandle<JSTaggedValue> &importName); 829 830 JSHandle<StarExportEntry> NewSStarExportEntry(const JSHandle<JSTaggedValue> &moduleRequest); 831 832 JSHandle<ResolvedIndexBinding> NewSResolvedIndexBindingRecord(); 833 834 JSHandle<ResolvedIndexBinding> NewSResolvedIndexBindingRecord(const JSHandle<SourceTextModule> &module, 835 int32_t index); 836 837 JSHandle<ResolvedBinding> NewSResolvedBindingRecord(); 838 839 JSHandle<ResolvedBinding> NewSResolvedBindingRecord(const JSHandle<SourceTextModule> &module, 840 const JSHandle<JSTaggedValue> &bindingName); 841 842 JSHandle<ResolvedRecordIndexBinding> NewSResolvedRecordIndexBindingRecord(); 843 844 JSHandle<ResolvedRecordIndexBinding> NewSResolvedRecordIndexBindingRecord( 845 const JSHandle<EcmaString> &moduleRecord, const JSHandle<EcmaString> &abcFileName, int32_t index); 846 847 JSHandle<ResolvedRecordBinding> NewSResolvedRecordBindingRecord(); 848 849 JSHandle<ResolvedRecordBinding> NewSResolvedRecordBindingRecord( 850 const JSHandle<EcmaString> &moduleRecord, const JSHandle<JSTaggedValue> &bindingName); 851 852 JSHandle<LayoutInfo> CopyAndReSortSLayoutInfo(const JSHandle<LayoutInfo> &old, int end, int capacity); 853 854 JSHandle<LayoutInfo> PUBLIC_API CreateSLayoutInfo(uint32_t properties); 855 856 JSHandle<LayoutInfo> PUBLIC_API NewSEmptyLayoutInfo(); 857 858 JSHandle<ProfileTypeInfoCell> NewSEmptyProfileTypeInfoCell(); 859 860 JSHandle<FunctionTemplate> NewSFunctionTemplate( 861 const JSHandle<Method> &method, const JSHandle<JSTaggedValue> &module, int32_t length); 862 863 JSHandle<TaggedArray> NewSEmptyArray(); // only used for EcmaVM. 864 865 JSHandle<MutantTaggedArray> NewSEmptyMutantArray(); 866 867 JSHandle<TaggedArray> PUBLIC_API NewSDictionaryArray(uint32_t length); 868 869 JSHandle<TaggedArray> NewSTaggedArrayWithoutInit(uint32_t length, 870 MemSpaceType spaceType = MemSpaceType::SHARED_OLD_SPACE); 871 872 JSHandle<JSHClass> CreateSFunctionClass(uint32_t size, JSType type, 873 const JSHandle<JSTaggedValue> &prototype, 874 bool isAccessor = true, bool setProto = false); 875 876 JSHandle<JSNativePointer> NewSJSNativePointer(void *externalPointer, 877 const NativePointerCallback &callBack, 878 void *data = nullptr, 879 bool nonMovable = false, 880 size_t nativeBindingsize = 0, 881 NativeFlag flag = NativeFlag::NO_DIV); 882 883 JSHandle<JSNativePointer> NewSReadOnlyJSNativePointer(void *externalPointer); 884 885 JSHandle<AccessorData> NewSInternalAccessor(void *setter, void *getter); 886 887 JSHandle<JSSymbol> NewSWellKnownSymbol(const JSHandle<JSTaggedValue> &name); 888 JSHandle<JSSymbol> NewSEmptySymbol(); 889 JSHandle<JSSymbol> NewSWellKnownSymbolWithChar(std::string_view description); 890 JSHandle<JSSymbol> NewSPublicSymbolWithChar(std::string_view description); 891 JSHandle<JSSymbol> NewSPublicSymbol(const JSHandle<JSTaggedValue> &name); 892 JSHandle<SendableEnv> NewSendableEnv(int numSlots); 893 JSHandle<JSFunction> NewJSSendableFunction(const JSHandle<Method> &methodHandle); 894 895private: 896 friend class GlobalEnv; 897 friend class GlobalEnvConstants; 898 friend class EcmaString; 899 friend class SnapshotProcessor; 900 friend class SingleCharTable; 901 void InitObjectFields(const TaggedObject *object); 902 903 JSThread *thread_ {nullptr}; 904 bool isTriggerGc_ {false}; 905 bool triggerSemiGC_ {false}; 906 907 EcmaVM *vm_ {nullptr}; 908 Heap *heap_ {nullptr}; 909 SharedHeap *sHeap_ {nullptr}; 910 911 static constexpr uint32_t LENGTH_THRESHOLD = 50; 912 static constexpr int MAX_LITERAL_HCLASS_CACHE_SIZE = 63; 913 914 NO_COPY_SEMANTIC(ObjectFactory); 915 NO_MOVE_SEMANTIC(ObjectFactory); 916 917 void NewObjectHook() const; 918 void NewSObjectHook() const; 919 // used for creating jshclass in GlobalEnv, EcmaVM 920 JSHandle<JSHClass> NewEcmaHClassClass(JSHClass *hclass, uint32_t size, JSType type); 921 922 JSHandle<JSHClass> NewEcmaReadOnlyHClass(JSHClass *hclass, uint32_t size, JSType type, 923 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 924 JSHandle<JSHClass> InitClassClass(); 925 926 // used to create nonmovable js_object 927 JSHandle<JSObject> NewNonMovableJSObject(const JSHandle<JSHClass> &jshclass); 928 929 // used to create nonmovable utf8 string at global constants 930 JSHandle<EcmaString> NewFromASCIINonMovable(std::string_view data); 931 // used to create read only utf8 string at global constants 932 JSHandle<EcmaString> NewFromASCIIReadOnly(std::string_view data); 933 934 // used for creating Function 935 JSHandle<JSFunction> NewJSFunction(const JSHandle<GlobalEnv> &env, const JSHandle<JSHClass> &hclass); 936 JSHandle<JSHClass> CreateObjectClass(const JSHandle<TaggedArray> &keys, const JSHandle<TaggedArray> &values); 937 JSHandle<JSHClass> CreateObjectClass(const JSHandle<TaggedArray> &properties, size_t length); 938 JSHandle<JSHClass> CreateFunctionClass(FunctionKind kind, uint32_t size, JSType type, 939 const JSHandle<JSTaggedValue> &prototype); 940 JSHandle<JSHClass> CreateBoundFunctionClass(); 941 JSHandle<JSHClass> CreateDefaultClassPrototypeHClass(JSHClass *hclass); 942 JSHandle<JSHClass> CreateDefaultClassConstructorHClass(JSHClass *hclass); 943 944 // used for creating ref.prototype in buildins, such as Number.prototype 945 JSHandle<JSPrimitiveRef> NewJSPrimitiveRef(const JSHandle<JSHClass> &hclass, 946 const JSHandle<JSTaggedValue> &object); 947 948 JSHandle<EcmaString> GetStringFromStringTable(const uint8_t *utf8Data, uint32_t utf8Len, bool canBeCompress) const; 949 JSHandle<EcmaString> GetCompressedSubStringFromStringTable(const JSHandle<EcmaString> &string, uint32_t offset, 950 uint32_t utf8Len) const; 951 JSHandle<EcmaString> GetStringFromStringTableNonMovable(const uint8_t *utf8Data, uint32_t utf8Len) const; 952 JSHandle<EcmaString> GetStringFromStringTableReadOnly(const uint8_t *utf8Data, uint32_t utf8Len, 953 bool canBeCompress = true) const; 954 // For MUtf-8 string data 955 EcmaString *PUBLIC_API GetRawStringFromStringTable(StringData sd, 956 MemSpaceType type = MemSpaceType::SHARED_OLD_SPACE, 957 bool isConstantString = false, uint32_t idOffset = 0) const; 958 EcmaString *GetRawStringFromStringTableWithoutJSHandle(StringData sd, 959 MemSpaceType type = MemSpaceType::SHARED_OLD_SPACE, 960 bool isConstantString = false, 961 uint32_t idOffset = 0) const; 962 963 JSHandle<EcmaString> GetStringFromStringTable(const uint16_t *utf16Data, uint32_t utf16Len, 964 bool canBeCompress) const; 965 966 JSHandle<EcmaString> GetStringFromStringTable(EcmaString *string) const; 967 968 JSHandle<EcmaString> GetStringFromStringTable(const JSHandle<EcmaString> &firstString, 969 const JSHandle<EcmaString> &secondString); 970 971 JSHandle<JSHClass> CreateJSArguments(const JSHandle<GlobalEnv> &env); 972 JSHandle<JSHClass> CreateJSArrayInstanceClass(JSHandle<JSTaggedValue> proto, 973 uint32_t inlinedProps = JSHClass::DEFAULT_CAPACITY_OF_IN_OBJECTS); 974 JSHandle<JSHClass> CreateJSRegExpInstanceClass(JSHandle<JSTaggedValue> proto); 975 976 inline TaggedObject *AllocObjectWithSpaceType(size_t size, JSHClass *cls, MemSpaceType type); 977 JSHandle<TaggedArray> NewTaggedArrayWithoutInit(uint32_t length, MemSpaceType spaceType); 978 979 // For object with many properties, directly create new HClass instead of searching on transitions 980 JSHandle<JSTaggedValue> CreateLargeJSObjectWithProperties(size_t propertyCount, 981 const Local<JSValueRef> *keys, 982 const PropertyDescriptor *descs); 983 JSHandle<JSTaggedValue> CreateLargeJSObjectWithNamedProperties(size_t propertyCount, const char **keys, 984 const Local<JSValueRef> *values); 985 // For object with numerous properties, directly create it in dictionary mode 986 JSHandle<JSTaggedValue> CreateDictionaryJSObjectWithProperties(size_t propertyCount, 987 const Local<JSValueRef> *keys, 988 const PropertyDescriptor *descs); 989 JSHandle<JSTaggedValue> CreateDictionaryJSObjectWithNamedProperties(size_t propertyCount, const char **keys, 990 const Local<JSValueRef> *values); 991 992 JSHandle<MutantTaggedArray> NewMutantTaggedArrayWithoutInit(uint32_t length, MemSpaceType spaceType); 993 994 friend class Builtins; // create builtins object need hclass 995 friend class JSFunction; // create prototype_or_hclass need hclass 996 friend class JSHClass; // HC transition need hclass 997 friend class EcmaVM; // hold the factory instance 998 friend class JsVerificationTest; 999 friend class PandaFileTranslator; 1000 friend class LiteralDataExtractor; 1001 friend class RuntimeStubs; 1002 friend class ClassInfoExtractor; 1003 friend class ModuleDataExtractor; 1004 friend class ModuleDataAccessor; 1005 friend class ConstantPool; 1006 friend class EcmaContext; 1007 friend class kungfu::TSHClassGenerator; 1008 friend class panda::FunctionRef; 1009}; 1010 1011class ClassLinkerFactory { 1012private: 1013 friend class GlobalEnv; // root class in class_linker need hclass 1014 friend class EcmaVM; // root class in class_linker need hclass 1015}; 1016} // namespace panda::ecmascript 1017#endif // ECMASCRIPT_OBJECT_FACTORY_H 1018