diff --git a/ecmascript/builtins.cpp b/ecmascript/builtins.cpp index a93f2f17..bbb5e691 100644 --- a/ecmascript/builtins.cpp +++ b/ecmascript/builtins.cpp @@ -400,9 +400,9 @@ void Builtins::InitializeFunction(const JSHandle &env, const JSHandle { [[maybe_unused]] EcmaHandleScope scope(thread_); // Initialize Function.prototype - JSMethod *invokeSelf = - vm_->GetMethodForNativeFunction(reinterpret_cast(Function::FunctionPrototypeInvokeSelf)); - JSHandle funcFuncPrototype = factory_->NewJSFunctionByDynClass(invokeSelf, emptyFuncDynclass); + JSHandle funcFuncPrototype = factory_->NewJSFunctionByDynClass( + reinterpret_cast(Function::FunctionPrototypeInvokeSelf), + emptyFuncDynclass); // ecma 19.2.3 The value of the name property of the Function prototype object is the empty String. JSHandle emptyString(thread_->GlobalConstants()->GetHandledEmptyString()); JSHandle undefinedString(thread_, JSTaggedValue::Undefined()); @@ -419,9 +419,9 @@ void Builtins::InitializeFunction(const JSHandle &env, const JSHandle function->SetBuiltinsCtorMode(); // Function = new Function() (forbidden use NewBuiltinConstructor) - JSMethod *ctor = vm_->GetMethodForNativeFunction(reinterpret_cast(Function::FunctionConstructor)); JSHandle funcFunc = - factory_->NewJSFunctionByDynClass(ctor, funcFuncIntanceDynclass, FunctionKind::BUILTIN_CONSTRUCTOR); + factory_->NewJSFunctionByDynClass(reinterpret_cast(Function::FunctionConstructor), + funcFuncIntanceDynclass, FunctionKind::BUILTIN_CONSTRUCTOR); auto funcFuncPrototypeObj = JSHandle(funcFuncPrototype); InitializeCtor(env, funcFuncPrototypeObj, funcFunc, "Function", FunctionLength::ONE); diff --git a/ecmascript/builtins/builtins_collator.cpp b/ecmascript/builtins/builtins_collator.cpp index f463881a..6605a0e1 100644 --- a/ecmascript/builtins/builtins_collator.cpp +++ b/ecmascript/builtins/builtins_collator.cpp @@ -100,7 +100,7 @@ JSTaggedValue BuiltinsCollator::Compare(EcmaRuntimeCallInfo *argv) if (boundCompare->IsUndefined()) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle intlBoundFunc = factory->NewJSIntlBoundFunction( - reinterpret_cast(BuiltinsCollator::AnonymousCollator), FUNCTION_LENGTH_TWO); + MethodIndex::BUILTINS_COLLATOR_ANONYMOUS_COLLATOR, FUNCTION_LENGTH_TWO); intlBoundFunc->SetCollator(thread, collator); collator->SetBoundCompare(thread, intlBoundFunc); } diff --git a/ecmascript/builtins/builtins_collator.h b/ecmascript/builtins/builtins_collator.h index 42917494..1f7dc6e2 100644 --- a/ecmascript/builtins/builtins_collator.h +++ b/ecmascript/builtins/builtins_collator.h @@ -34,6 +34,7 @@ public: static JSTaggedValue ResolvedOptions(EcmaRuntimeCallInfo *argv); private: + friend class panda::ecmascript::ObjectFactory; static JSTaggedValue AnonymousCollator(EcmaRuntimeCallInfo *argv); }; } // namespace panda::ecmascript::builtins diff --git a/ecmascript/builtins/builtins_date_time_format.cpp b/ecmascript/builtins/builtins_date_time_format.cpp index 884e6cc2..76f3f4ac 100644 --- a/ecmascript/builtins/builtins_date_time_format.cpp +++ b/ecmascript/builtins/builtins_date_time_format.cpp @@ -125,8 +125,8 @@ JSTaggedValue BuiltinsDateTimeFormat::Format(EcmaRuntimeCallInfo *argv) JSHandle boundFormat(thread, dtf->GetBoundFormat()); if (boundFormat->IsUndefined()) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); - JSHandle intlBoundFunc = - factory->NewJSIntlBoundFunction(reinterpret_cast(BuiltinsDateTimeFormat::AnonymousDateTimeFormat)); + JSHandle intlBoundFunc = factory->NewJSIntlBoundFunction( + MethodIndex::BUILTINS_DATE_TIME_FORMAT_ANONYMOUS_DATE_TIME_FORMAT); intlBoundFunc->SetDateTimeFormat(thread, dtf); dtf->SetBoundFormat(thread, intlBoundFunc); } diff --git a/ecmascript/builtins/builtins_date_time_format.h b/ecmascript/builtins/builtins_date_time_format.h index ac6172fa..d450c457 100644 --- a/ecmascript/builtins/builtins_date_time_format.h +++ b/ecmascript/builtins/builtins_date_time_format.h @@ -44,6 +44,7 @@ public: static JSTaggedValue FormatRangeToParts(EcmaRuntimeCallInfo *argv); private: + friend class panda::ecmascript::ObjectFactory; // 13.1.5 DateTime Format Functions static JSTaggedValue AnonymousDateTimeFormat(EcmaRuntimeCallInfo *argv); }; diff --git a/ecmascript/builtins/builtins_number_format.cpp b/ecmascript/builtins/builtins_number_format.cpp index 32278562..720b2860 100644 --- a/ecmascript/builtins/builtins_number_format.cpp +++ b/ecmascript/builtins/builtins_number_format.cpp @@ -125,7 +125,7 @@ JSTaggedValue BuiltinsNumberFormat::Format(EcmaRuntimeCallInfo *argv) if (boundFunc->IsUndefined()) { ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); JSHandle intlBoundFunc = factory->NewJSIntlBoundFunction( - reinterpret_cast(BuiltinsNumberFormat::NumberFormatInternalFormatNumber)); + MethodIndex::BUILTINS_NUMBER_FORMAT_NUMBER_FORMAT_INTERNAL_FORMAT_NUMBER); intlBoundFunc->SetNumberFormat(thread, typpedNf); typpedNf->SetBoundFormat(thread, intlBoundFunc); } diff --git a/ecmascript/builtins/builtins_object.cpp b/ecmascript/builtins/builtins_object.cpp index 8c452531..77cb9557 100644 --- a/ecmascript/builtins/builtins_object.cpp +++ b/ecmascript/builtins/builtins_object.cpp @@ -962,9 +962,9 @@ JSTaggedValue BuiltinsObject::FromEntries(EcmaRuntimeCallInfo *argv) // 5. Let lengthDefine be the number of non-optional parameters of the function definition in // CreateDataPropertyOnObject Functions. // 6. Let adder be ! CreateBuiltinFunction(stepsDefine, lengthDefine, "", « »). + JSMethod* method = factory->GetMethodByIndex(MethodIndex::BUILTINS_OBJECT_CREATE_DATA_PROPERTY_ON_OBJECT_FUNCTIONS); JSHandle addrFunc = - factory->NewJSFunction(env, reinterpret_cast(CreateDataPropertyOnObjectFunctions), - FunctionKind::NORMAL_FUNCTION); + factory->NewJSFunction(env, method, FunctionKind::NORMAL_FUNCTION); JSHandle adder(thread, addrFunc.GetTaggedValue()); diff --git a/ecmascript/builtins/builtins_promise.cpp b/ecmascript/builtins/builtins_promise.cpp index 08509bd1..996f2c80 100644 --- a/ecmascript/builtins/builtins_promise.cpp +++ b/ecmascript/builtins/builtins_promise.cpp @@ -520,8 +520,7 @@ JSHandle BuiltinsPromise::PerformPromiseAll(JSThread *thread, RETURN_COMPLETION_IF_ABRUPT(thread, nextPromise); // k. Let resolveElement be a new built-in function object as defined in Promise.all // Resolve Element Functions. - JSHandle resoleveElement = factory->NewJSPromiseAllResolveElementFunction( - reinterpret_cast(BuiltinsPromiseHandler::ResolveElementFunction)); + JSHandle resoleveElement = factory->NewJSPromiseAllResolveElementFunction(); // l. Set the [[AlreadyCalled]] internal slot of resolveElement to a new Record {[[value]]: false }. JSHandle falseRecord = factory->NewPromiseRecord(); falseRecord->SetValue(thread, JSTaggedValue::False()); diff --git a/ecmascript/builtins/builtins_proxy.cpp b/ecmascript/builtins/builtins_proxy.cpp index 9d25096b..1869fb2f 100644 --- a/ecmascript/builtins/builtins_proxy.cpp +++ b/ecmascript/builtins/builtins_proxy.cpp @@ -56,8 +56,7 @@ JSTaggedValue BuiltinsProxy::Revocable([[maybe_unused]] EcmaRuntimeCallInfo *arg RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread); // 3 ~ 4 new revoker function and set the [[RevocableProxy]] internal slot - JSHandle revoker = thread->GetEcmaVM()->GetFactory()->NewJSProxyRevocFunction( - proxy, reinterpret_cast(InvalidateProxyFunction)); + JSHandle revoker = thread->GetEcmaVM()->GetFactory()->NewJSProxyRevocFunction(proxy); // 5.Let result be ObjectCreate(%ObjectPrototype%). JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); diff --git a/ecmascript/compiler/tests/stub_tests.cpp b/ecmascript/compiler/tests/stub_tests.cpp index 13f3fb17..34b17671 100644 --- a/ecmascript/compiler/tests/stub_tests.cpp +++ b/ecmascript/compiler/tests/stub_tests.cpp @@ -79,7 +79,7 @@ public: auto ecmaVm = thread->GetEcmaVM(); ObjectFactory *factory = ecmaVm->GetFactory(); JSHandle env = ecmaVm->GetGlobalEnv(); - JSMethod *method = ecmaVm->GetMethodForNativeFunction(reinterpret_cast(codeEntry)); + JSMethod *method = factory->NewMethodForNativeFunction(reinterpret_cast(codeEntry)); method->SetAotCodeBit(true); method->SetNativeBit(false); method->SetNumArgsWithCallField(numArgs); @@ -1157,8 +1157,8 @@ HWTEST_F_L0(StubTest, FastTypeOfTest) EXPECT_EQ(resultVal7, expectResult7); // obj is callable - JSHandle resolveCallable = - factory->CreateJSPromiseReactionsFunction(reinterpret_cast(BuiltinsPromiseHandler::Resolve)); + JSHandle resolveCallable = factory->CreateJSPromiseReactionsFunction( + MethodIndex::BUILTINS_PROMISE_HANDLER_RESOLVE); JSTaggedValue expectResult8= FastRuntimeStub::FastTypeOf(thread, resolveCallable.GetTaggedValue()); JSTaggedValue resultVal8 = typeOfPtr(thread->GetGlueAddr(), resolveCallable.GetTaggedValue().GetRawData()); EXPECT_EQ(resultVal8, globalConst->GetFunctionString()); diff --git a/ecmascript/ecma_vm.cpp b/ecmascript/ecma_vm.cpp index 523c4650..75272dc2 100644 --- a/ecmascript/ecma_vm.cpp +++ b/ecmascript/ecma_vm.cpp @@ -107,8 +107,7 @@ EcmaVM::EcmaVM(JSRuntimeOptions options) nativeAreaAllocator_(std::make_unique()), heapRegionAllocator_(std::make_unique()), chunk_(nativeAreaAllocator_.get()), - nativePointerList_(&chunk_), - nativeMethods_(&chunk_) + nativePointerList_(&chunk_) { options_ = std::move(options); icEnabled_ = options_.EnableIC(); @@ -146,7 +145,7 @@ bool EcmaVM::Initialize() heap_ = new Heap(this); heap_->Initialize(); gcStats_ = chunk_.New(heap_); - factory_ = chunk_.New(thread_, heap_); + factory_ = chunk_.New(thread_, heap_, &chunk_); if (UNLIKELY(factory_ == nullptr)) { LOG_ECMA(FATAL) << "alloc factory_ failed"; UNREACHABLE(); @@ -167,6 +166,7 @@ bool EcmaVM::Initialize() microJobQueue_ = factory_->NewMicroJobQueue().GetTaggedValue(); Builtins builtins; builtins.Initialize(globalEnv, thread_); + factory_->GenerateInternalNativeMethods(); thread_->SetGlobalObject(GetGlobalEnv()->GetGlobalObject()); moduleManager_ = new ModuleManager(this); debuggerManager_->Initialize(); @@ -244,7 +244,6 @@ EcmaVM::~EcmaVM() { vmInitialized_ = false; Taskpool::GetCurrentTaskpool()->Destroy(); - ClearNativeMethodsData(); if (runtimeStat_ != nullptr && runtimeStat_->IsRuntimeStatEnabled()) { runtimeStat_->Print(); @@ -346,34 +345,12 @@ EcmaVM::CpuProfilingScope::~CpuProfilingScope() #endif } -JSMethod *EcmaVM::GetMethodForNativeFunction(const void *func) -{ - uint32_t numArgs = 2; // function object and this - auto method = chunk_.New(nullptr, panda_file::File::EntityId(0)); - method->SetNativePointer(const_cast(func)); - - method->SetNativeBit(true); - method->SetNumArgsWithCallField(numArgs); - nativeMethods_.push_back(method); - return nativeMethods_.back(); -} - -JSMethod *EcmaVM::GenerateMethodForAOTFunction(const void *func, size_t numArgs) -{ - auto method = chunk_.New(nullptr, panda_file::File::EntityId(0)); // 0 : temporary file id - method->SetNativePointer(const_cast(func)); - method->SetAotCodeBit(true); - method->SetNativeBit(false); - method->SetNumArgsWithCallField(numArgs); - nativeMethods_.push_back(method); - return nativeMethods_.back(); -} - void EcmaVM::UpdateMethodInFunc(JSHandle mainFunc, const JSPandaFile *jsPandaFile) { const std::string funcName = "func_main_0"; auto mainEntry = static_cast(aotInfo_->GetAOTFuncEntry(funcName)); - JSMethod *mainMethod = GenerateMethodForAOTFunction(reinterpret_cast(mainEntry), 1); // 1 : default paras + // 1 : default paras + JSMethod *mainMethod = factory_->NewMethodForAOTFunction(reinterpret_cast(mainEntry), 1); mainFunc->SetCallTarget(thread_, mainMethod); mainFunc->SetCodeEntry(reinterpret_cast(mainEntry)); JSHandle constPool(thread_, mainFunc->GetConstantPool()); @@ -388,7 +365,7 @@ void EcmaVM::UpdateMethodInFunc(JSHandle mainFunc, const JSPandaFile value.GetConstpoolType() == ConstPoolType::METHOD) { auto id = value.GetConstpoolIndex(); auto codeEntry = static_cast(aotInfo_->GetAOTFuncEntry(id)); - JSMethod *curMethod = GenerateMethodForAOTFunction(reinterpret_cast(codeEntry), 1); + JSMethod *curMethod = factory_->NewMethodForAOTFunction(reinterpret_cast(codeEntry), 1); auto curFunction = JSFunction::Cast(curPool->GetObjectFromCache(id).GetTaggedObject()); curFunction->SetCallTarget(thread_, curMethod); curFunction->SetCodeEntry(reinterpret_cast(codeEntry)); @@ -661,14 +638,6 @@ void EcmaVM::SetMicroJobQueue(job::MicroJobQueue *queue) microJobQueue_ = JSTaggedValue(queue); } -void EcmaVM::ClearNativeMethodsData() -{ - for (auto iter : nativeMethods_) { - chunk_.Delete(iter); - } - nativeMethods_.clear(); -} - void EcmaVM::LoadStubs() { std::string comStubFile = options_.GetComStubFile(); diff --git a/ecmascript/ecma_vm.h b/ecmascript/ecma_vm.h index 671be2c8..6c61f441 100644 --- a/ecmascript/ecma_vm.h +++ b/ecmascript/ecma_vm.h @@ -127,9 +127,6 @@ public: ASSERT(regExpParserCache_ != nullptr); return regExpParserCache_; } - - JSMethod *GetMethodForNativeFunction(const void *func); - JSMethod *GenerateMethodForAOTFunction(const void *func, size_t numArgs); void UpdateMethodInFunc(JSHandle mainFunc, const JSPandaFile *jsPandaFile); EcmaStringTable *GetEcmaStringTable() const @@ -318,8 +315,6 @@ private: void ClearBufferData(); - void ClearNativeMethodsData(); - void LoadAOTFile(const std::string &fileName); NO_MOVE_SEMANTIC(EcmaVM); @@ -359,7 +354,6 @@ private: CMap cachedConstpools_ {}; // VM resources. - ChunkVector nativeMethods_; ModuleManager *moduleManager_ {nullptr}; TSLoader *tsLoader_ {nullptr}; SnapshotEnv *snapshotEnv_ {nullptr}; diff --git a/ecmascript/js_async_function.cpp b/ecmascript/js_async_function.cpp index 50c50598..165a53d2 100644 --- a/ecmascript/js_async_function.cpp +++ b/ecmascript/js_async_function.cpp @@ -33,12 +33,13 @@ void JSAsyncFunction::AsyncFunctionAwait(JSThread *thread, const JSHandle &value) { // 1.Let asyncContext be the running execution context. - ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); + auto vm = thread->GetEcmaVM(); + ObjectFactory *factory = vm->GetFactory(); JSHandle asyncCtxt(thread, asyncFuncObj->GetGeneratorContext()); // 2.Let promiseCapability be ! NewPromiseCapability(%Promise%). - JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); + JSHandle env = vm->GetGlobalEnv(); const GlobalEnvConstants *globalConst = thread->GlobalConstants(); JSHandle pcap = JSPromise::NewPromiseCapability(thread, JSHandle::Cast(env->GetPromiseFunction())); @@ -54,12 +55,12 @@ void JSAsyncFunction::AsyncFunctionAwait(JSThread *thread, const JSHandle fulFunc = - factory->NewJSAsyncAwaitStatusFunction(reinterpret_cast(BuiltinsPromiseHandler::AsyncAwaitFulfilled)); + JSHandle fulFunc = factory->NewJSAsyncAwaitStatusFunction( + MethodIndex::BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_FULFILLED); // 5.Let onRejected be a new built-in function object as defined in AsyncFunction Awaited Rejected. - JSHandle rejFunc = - factory->NewJSAsyncAwaitStatusFunction(reinterpret_cast(BuiltinsPromiseHandler::AsyncAwaitRejected)); + JSHandle rejFunc = factory->NewJSAsyncAwaitStatusFunction( + MethodIndex::BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_REJECTED); // 6.Set onFulfilled.[[AsyncContext]] to asyncContext. // 7.Set onRejected.[[AsyncContext]] to asyncContext. diff --git a/ecmascript/js_method.cpp b/ecmascript/js_method.cpp index 9275637b..bcbcb1c9 100644 --- a/ecmascript/js_method.cpp +++ b/ecmascript/js_method.cpp @@ -68,7 +68,7 @@ void JSMethod::InitializeCallField(uint32_t numVregs, uint32_t numArgs) } }); // Needed info for call can be got by loading callField only once. - // Native bit will be set in GetMethodForNativeFunction(); + // Native bit will be set in NewMethodForNativeFunction(); callField_ = (callType & CALL_TYPE_MASK) | NumVregsBits::Encode(numVregs) | NumArgsBits::Encode(numArgs - HaveFuncBit::Decode(callType) // exclude func diff --git a/ecmascript/js_promise.cpp b/ecmascript/js_promise.cpp index e5275573..f35b2d3a 100644 --- a/ecmascript/js_promise.cpp +++ b/ecmascript/js_promise.cpp @@ -37,15 +37,15 @@ JSHandle JSPromise::CreateResolvingFunctions(JSThread record->SetValue(thread, JSTaggedValue::False()); // 2. Let resolve be a new built-in function object as defined in Promise Resolve Functions (25.4.1.3.2). - JSHandle resolve = - factory->CreateJSPromiseReactionsFunction(reinterpret_cast(BuiltinsPromiseHandler::Resolve)); + JSHandle resolve = factory->CreateJSPromiseReactionsFunction( + MethodIndex::BUILTINS_PROMISE_HANDLER_RESOLVE); // 3. Set the [[Promise]] internal slot of resolve to promise. resolve->SetPromise(thread, promise); // 4. Set the [[AlreadyResolved]] internal slot of resolve to alreadyResolved. resolve->SetAlreadyResolved(thread, record); // 5. Let reject be a new built-in function object as defined in Promise Reject Functions (25.4.1.3.1). - JSHandle reject = - factory->CreateJSPromiseReactionsFunction(reinterpret_cast(BuiltinsPromiseHandler::Reject)); + JSHandle reject = factory->CreateJSPromiseReactionsFunction( + MethodIndex::BUILTINS_PROMISE_HANDLER_REJECT); // 6. Set the [[Promise]] internal slot of reject to promise. reject->SetPromise(thread, promise); // 7. Set the [[AlreadyResolved]] internal slot of reject to alreadyResolved. @@ -92,8 +92,7 @@ JSHandle JSPromise::NewPromiseCapability(JSThread *thread, co JSHandle promiseCapability = factory->NewPromiseCapability(); // 4. Let executor be a new built-in function object as defined in GetCapabilitiesExecutor Functions // (25.4.1.5.1). - JSHandle executor = - factory->CreateJSPromiseExecutorFunction(reinterpret_cast(BuiltinsPromiseHandler::Executor)); + JSHandle executor = factory->CreateJSPromiseExecutorFunction(); // 5. Set the [[Capability]] internal slot of executor to promiseCapability. executor->SetCapability(thread, promiseCapability.GetTaggedValue()); // 6. Let promise be Construct(C, «executor»). diff --git a/ecmascript/napi/jsnapi.cpp b/ecmascript/napi/jsnapi.cpp index dad56e24..e1c9bbdf 100644 --- a/ecmascript/napi/jsnapi.cpp +++ b/ecmascript/napi/jsnapi.cpp @@ -944,10 +944,9 @@ Local FunctionRef::NewClassFunction(EcmaVM *vm, FunctionCallback na ObjectFactory *factory = vm->GetFactory(); JSHandle env = vm->GetGlobalEnv(); JSHandle dynclass = JSHandle::Cast(env->GetFunctionClassWithoutName()); - JSMethod *method = - vm->GetMethodForNativeFunction(reinterpret_cast(Callback::RegisterCallback)); JSHandle current = - factory->NewJSFunctionByDynClass(method, dynclass, ecmascript::FunctionKind::CLASS_CONSTRUCTOR); + factory->NewJSFunctionByDynClass(reinterpret_cast(Callback::RegisterCallback), + dynclass, ecmascript::FunctionKind::CLASS_CONSTRUCTOR); auto globalConst = thread->GlobalConstants(); JSHandle accessor = globalConst->GetHandledFunctionPrototypeAccessor(); diff --git a/ecmascript/napi/test/jsnapi_tests.cpp b/ecmascript/napi/test/jsnapi_tests.cpp index 7f98c2a4..73f2e3be 100644 --- a/ecmascript/napi/test/jsnapi_tests.cpp +++ b/ecmascript/napi/test/jsnapi_tests.cpp @@ -769,7 +769,7 @@ HWTEST_F_L0(JSNApiTests, InheritPrototype_003) auto factory = vm_->GetFactory(); JSMethod *invokeSelf = - vm_->GetMethodForNativeFunction(reinterpret_cast(BuiltinsFunction::FunctionPrototypeInvokeSelf)); + factory->NewMethodForNativeFunction(reinterpret_cast(BuiltinsFunction::FunctionPrototypeInvokeSelf)); // father type JSHandle protoDynclass = JSHandle::Cast(env->GetFunctionClassWithProto()); JSHandle protoFunc = factory->NewJSFunctionByDynClass(invokeSelf, protoDynclass); @@ -815,8 +815,9 @@ HWTEST_F_L0(JSNApiTests, InheritPrototype_004) JSHandle addMethod = JSObject::GetMethod(thread_, weakSet, addString); JSMethod *invokeSelf = - vm_->GetMethodForNativeFunction(reinterpret_cast(BuiltinsFunction::FunctionPrototypeInvokeSelf)); - JSMethod *ctor = vm_->GetMethodForNativeFunction(reinterpret_cast(BuiltinsFunction::FunctionConstructor)); + factory->NewMethodForNativeFunction(reinterpret_cast(BuiltinsFunction::FunctionPrototypeInvokeSelf)); + JSMethod *ctor = + factory->NewMethodForNativeFunction(reinterpret_cast(BuiltinsFunction::FunctionConstructor)); JSHandle protoDynclass = JSHandle::Cast(env->GetFunctionClassWithProto()); JSHandle funcFuncPrototype = factory->NewJSFunctionByDynClass(invokeSelf, protoDynclass); @@ -845,8 +846,8 @@ HWTEST_F_L0(JSNApiTests, InheritPrototype_004) JSHandle funcFuncNoProtoProtoIntanceDynclass = factory->NewEcmaDynClass(JSFunction::SIZE, JSType::JS_FUNCTION, funcFuncNoProtoPrototypeValue); // new with NewJSFunctionByDynClass::function DynClass - JSHandle noProtoFunc = - factory->NewJSFunctionByDynClass(ctor, funcFuncNoProtoProtoIntanceDynclass, FunctionKind::BUILTIN_CONSTRUCTOR); + JSHandle noProtoFunc = factory->NewJSFunctionByDynClass(ctor, + funcFuncNoProtoProtoIntanceDynclass, FunctionKind::BUILTIN_CONSTRUCTOR); EXPECT_TRUE(*noProtoFunc != nullptr); // set property that has same key with fater type PropertyDescriptor desc2 = PropertyDescriptor(thread_, defaultString); diff --git a/ecmascript/object_factory.cpp b/ecmascript/object_factory.cpp index d29b01f1..eb76e365 100644 --- a/ecmascript/object_factory.cpp +++ b/ecmascript/object_factory.cpp @@ -18,8 +18,15 @@ #include "ecmascript/accessor_data.h" #include "ecmascript/base/error_helper.h" #include "ecmascript/builtins.h" +#include "ecmascript/builtins/builtins_collator.h" +#include "ecmascript/builtins/builtins_date_time_format.h" #include "ecmascript/builtins/builtins_errors.h" #include "ecmascript/builtins/builtins_global.h" +#include "ecmascript/builtins/builtins_number_format.h" +#include "ecmascript/builtins/builtins_promise.h" +#include "ecmascript/builtins/builtins_promise_handler.h" +#include "ecmascript/builtins/builtins_object.h" +#include "ecmascript/builtins/builtins_proxy.h" #include "ecmascript/ecma_macros.h" #include "ecmascript/free_object.h" #include "ecmascript/global_env.h" @@ -112,11 +119,52 @@ using EvalError = builtins::BuiltinsEvalError; using ErrorType = base::ErrorType; using ErrorHelper = base::ErrorHelper; -ObjectFactory::ObjectFactory(JSThread *thread, Heap *heap) - : thread_(thread), vm_(thread->GetEcmaVM()), heap_(heap) +ObjectFactory::ObjectFactory(JSThread *thread, Heap *heap, Chunk *chunk) + : thread_(thread), vm_(thread->GetEcmaVM()), heap_(heap), + nativeMethods_(chunk), internalNativeMethods_(chunk) { } +ObjectFactory::~ObjectFactory() +{ + ClearNativeMethodsData(); +} + +void ObjectFactory::ClearNativeMethodsData() +{ + for (auto iter : nativeMethods_) { + vm_->GetChunk()->Delete(iter); + } + nativeMethods_.clear(); + for (auto iter : internalNativeMethods_) { + vm_->GetChunk()->Delete(iter); + } + internalNativeMethods_.clear(); +} + +JSMethod *ObjectFactory::NewMethodForNativeFunction(const void *func) +{ + uint32_t numArgs = 2; // function object and this + auto method = vm_->GetChunk()->New(nullptr, panda_file::File::EntityId(0)); + method->SetNativePointer(const_cast(func)); + + method->SetNativeBit(true); + method->SetNumArgsWithCallField(numArgs); + nativeMethods_.push_back(method); + return nativeMethods_.back(); +} + +JSMethod *ObjectFactory::NewMethodForAOTFunction(const void *func, size_t numArgs) +{ + auto method = vm_->GetChunk()->New(nullptr, panda_file::File::EntityId(0)); // 0 : temporary file id + method->SetNativePointer(const_cast(func)); + method->SetAotCodeBit(true); + method->SetNativeBit(false); + method->SetNumArgsWithCallField(numArgs); + nativeMethods_.push_back(method); + return nativeMethods_.back(); +} + JSHandle ObjectFactory::NewEcmaDynClassClass(JSHClass *hclass, uint32_t size, JSType type) { NewObjectHook(); @@ -135,6 +183,41 @@ JSHandle ObjectFactory::InitClassClass() return dynClassClassHandle; } +// NOLINTNEXTLINE(modernize-avoid-c-arrays) +void * ObjectFactory::InternalMethodTable[] = { + reinterpret_cast(builtins::BuiltinsGlobal::CallJsBoundFunction), + reinterpret_cast(builtins::BuiltinsGlobal::CallJsProxy), + reinterpret_cast(builtins::BuiltinsObject::CreateDataPropertyOnObjectFunctions), + reinterpret_cast(builtins::BuiltinsCollator::AnonymousCollator), + reinterpret_cast(builtins::BuiltinsDateTimeFormat::AnonymousDateTimeFormat), + reinterpret_cast(builtins::BuiltinsNumberFormat::NumberFormatInternalFormatNumber), + reinterpret_cast(builtins::BuiltinsProxy::InvalidateProxyFunction), + reinterpret_cast(builtins::BuiltinsPromiseHandler::AsyncAwaitFulfilled), + reinterpret_cast(builtins::BuiltinsPromiseHandler::AsyncAwaitRejected), + reinterpret_cast(builtins::BuiltinsPromiseHandler::ResolveElementFunction), + reinterpret_cast(builtins::BuiltinsPromiseHandler::Resolve), + reinterpret_cast(builtins::BuiltinsPromiseHandler::Reject), + reinterpret_cast(builtins::BuiltinsPromiseHandler::Executor) +}; + +void ObjectFactory::GenerateInternalNativeMethods() +{ + size_t length = static_cast(MethodIndex::METHOD_END); + for (size_t i = 0; i < length; i++) { + uint32_t numArgs = 2; // function object and this + auto method = vm_->GetChunk()->New(nullptr, panda_file::File::EntityId(0)); + method->SetNativePointer(InternalMethodTable[i]); + method->SetNativeBit(true); + method->SetNumArgsWithCallField(numArgs); + internalNativeMethods_.emplace_back(method); + } +} + +JSMethod *ObjectFactory::GetMethodByIndex(MethodIndex idx) +{ + return internalNativeMethods_[static_cast(idx)]; +} + JSHandle ObjectFactory::NewEcmaDynClass(JSHClass *hclass, uint32_t size, JSType type, uint32_t inlinedProps) { NewObjectHook(); @@ -1145,7 +1228,7 @@ JSHandle ObjectFactory::OrdinaryNewJSObjectCreate(const JSHandle ObjectFactory::NewJSFunction(const JSHandle &env, const void *nativeFunc, FunctionKind kind) { - JSMethod *target = vm_->GetMethodForNativeFunction(nativeFunc); + JSMethod *target = NewMethodForNativeFunction(nativeFunc); return NewJSFunction(env, target, kind); } @@ -1237,9 +1320,20 @@ JSHandle ObjectFactory::NewJSFunctionByDynClass(JSMethod *method, co return function; } +JSHandle ObjectFactory::NewJSFunctionByDynClass(const void *func, const JSHandle &clazz, + FunctionKind kind) +{ + JSHandle function = JSHandle::Cast(NewJSObject(clazz)); + clazz->SetCallable(true); + clazz->SetExtensible(true); + JSFunction::InitializeJSFunction(thread_, function, kind); + function->SetCallTarget(thread_, NewMethodForNativeFunction(func)); + return function; +} + JSHandle ObjectFactory::NewJSNativeErrorFunction(const JSHandle &env, const void *nativeFunc) { - JSMethod *target = vm_->GetMethodForNativeFunction(nativeFunc); + JSMethod *target = NewMethodForNativeFunction(nativeFunc); JSHandle dynclass = JSHandle::Cast(env->GetNativeErrorFunctionClass()); return NewJSFunctionByDynClass(target, dynclass, FunctionKind::BUILTIN_CONSTRUCTOR); } @@ -1247,7 +1341,7 @@ JSHandle ObjectFactory::NewJSNativeErrorFunction(const JSHandle ObjectFactory::NewSpecificTypedArrayFunction(const JSHandle &env, const void *nativeFunc) { - JSMethod *target = vm_->GetMethodForNativeFunction(nativeFunc); + JSMethod *target = NewMethodForNativeFunction(nativeFunc); JSHandle dynclass = JSHandle::Cast(env->GetSpecificTypedArrayFunctionClass()); return NewJSFunctionByDynClass(target, dynclass, FunctionKind::BUILTIN_CONSTRUCTOR); } @@ -1255,7 +1349,7 @@ JSHandle ObjectFactory::NewSpecificTypedArrayFunction(const JSHandle JSHandle ObjectFactory::NewAotFunction(uint32_t numArgs, uintptr_t codeEntry) { JSHandle env = vm_->GetGlobalEnv(); - JSMethod *method = vm_->GetMethodForNativeFunction(reinterpret_cast(codeEntry)); + JSMethod *method = NewMethodForNativeFunction(reinterpret_cast(codeEntry)); method->SetAotCodeBit(true); method->SetNativeBit(false); method->SetNumArgsWithCallField(numArgs); @@ -1280,13 +1374,12 @@ JSHandle ObjectFactory::NewJSBoundFunction(const JSHandleSetConstructor(true); } - JSMethod *method = - vm_->GetMethodForNativeFunction(reinterpret_cast(builtins::BuiltinsGlobal::CallJsBoundFunction)); + JSMethod *method = GetMethodByIndex(MethodIndex::BUILTINS_GLOBAL_CALL_JS_BOUND_FUNCTION); bundleFunction->SetCallTarget(thread_, method); return bundleFunction; } -JSHandle ObjectFactory::NewJSIntlBoundFunction(const void *nativeFunc, int functionLength) +JSHandle ObjectFactory::NewJSIntlBoundFunction(MethodIndex idx, int functionLength) { JSHandle env = vm_->GetGlobalEnv(); JSHandle dynclass = JSHandle::Cast(env->GetJSIntlBoundFunctionClass()); @@ -1295,8 +1388,7 @@ JSHandle ObjectFactory::NewJSIntlBoundFunction(const void * intlBoundFunc->SetNumberFormat(JSTaggedValue::Undefined()); intlBoundFunc->SetDateTimeFormat(JSTaggedValue::Undefined()); intlBoundFunc->SetCollator(JSTaggedValue::Undefined()); - JSMethod *method = vm_->GetMethodForNativeFunction(nativeFunc); - intlBoundFunc->SetCallTarget(thread_, method); + intlBoundFunc->SetCallTarget(thread_, GetMethodByIndex(idx)); JSHandle function = JSHandle::Cast(intlBoundFunc); JSFunction::InitializeJSFunction(thread_, function, FunctionKind::NORMAL_FUNCTION); JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(functionLength)); @@ -1308,8 +1400,7 @@ JSHandle ObjectFactory::NewJSIntlBoundFunction(const void * return intlBoundFunc; } -JSHandle ObjectFactory::NewJSProxyRevocFunction(const JSHandle &proxy, - const void *nativeFunc) +JSHandle ObjectFactory::NewJSProxyRevocFunction(const JSHandle &proxy) { JSHandle env = vm_->GetGlobalEnv(); const GlobalEnvConstants *globalConst = thread_->GlobalConstants(); @@ -1318,9 +1409,7 @@ JSHandle ObjectFactory::NewJSProxyRevocFunction(const JSHa JSHandle revocFunction = JSHandle::Cast(NewJSObject(dynclass)); revocFunction->SetRevocableProxy(JSTaggedValue::Undefined()); revocFunction->SetRevocableProxy(thread_, proxy); - - JSMethod *target = vm_->GetMethodForNativeFunction(nativeFunc); - revocFunction->SetCallTarget(thread_, target); + revocFunction->SetCallTarget(thread_, GetMethodByIndex(MethodIndex::BUILTINS_PROXY_INVALIDATE_PROXY_FUNCTION)); JSHandle function = JSHandle::Cast(revocFunction); JSFunction::InitializeJSFunction(thread_, function, FunctionKind::NORMAL_FUNCTION); JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(0)); @@ -1331,7 +1420,7 @@ JSHandle ObjectFactory::NewJSProxyRevocFunction(const JSHa return revocFunction; } -JSHandle ObjectFactory::NewJSAsyncAwaitStatusFunction(const void *nativeFunc) +JSHandle ObjectFactory::NewJSAsyncAwaitStatusFunction(MethodIndex idx) { JSHandle env = vm_->GetGlobalEnv(); JSHandle dynclass = JSHandle::Cast(env->GetAsyncAwaitStatusFunctionClass()); @@ -1340,8 +1429,7 @@ JSHandle ObjectFactory::NewJSAsyncAwaitStatusFunctio JSHandle::Cast(NewJSObject(dynclass)); awaitFunction->SetAsyncContext(JSTaggedValue::Undefined()); JSFunction::InitializeJSFunction(thread_, JSHandle::Cast(awaitFunction)); - JSMethod *target = vm_->GetMethodForNativeFunction(nativeFunc); - awaitFunction->SetCallTarget(thread_, target); + awaitFunction->SetCallTarget(thread_, GetMethodByIndex(idx)); return awaitFunction; } @@ -1703,8 +1791,7 @@ JSHandle ObjectFactory::NewJSProxy(const JSHandle &targe JSHandle proxy(thread_, header); JSMethod *method = nullptr; if (target->IsCallable()) { - JSMethod *nativeMethod = - vm_->GetMethodForNativeFunction(reinterpret_cast(builtins::BuiltinsGlobal::CallJsProxy)); + JSMethod *nativeMethod = GetMethodByIndex(MethodIndex::BUILTINS_GLOBAL_CALL_JS_PROXY); proxy->SetCallTarget(thread_, nativeMethod); } proxy->SetMethod(method); @@ -2184,7 +2271,7 @@ JSHandle ObjectFactory::NewJSArrayIterator(const JSHandle ObjectFactory::CreateJSPromiseReactionsFunction(const void *nativeFunc) +JSHandle ObjectFactory::CreateJSPromiseReactionsFunction(MethodIndex idx) { JSHandle env = vm_->GetGlobalEnv(); JSHandle dynclass = JSHandle::Cast(env->GetPromiseReactionFunctionClass()); @@ -2193,23 +2280,21 @@ JSHandle ObjectFactory::CreateJSPromiseReactionsFunc JSHandle::Cast(NewJSObject(dynclass)); reactionsFunction->SetPromise(thread_, JSTaggedValue::Hole()); reactionsFunction->SetAlreadyResolved(thread_, JSTaggedValue::Hole()); - JSMethod *method = vm_->GetMethodForNativeFunction(nativeFunc); - reactionsFunction->SetCallTarget(thread_, method); + reactionsFunction->SetCallTarget(thread_, GetMethodByIndex(idx)); JSHandle function = JSHandle::Cast(reactionsFunction); JSFunction::InitializeJSFunction(thread_, function); JSFunction::SetFunctionLength(thread_, function, JSTaggedValue(1)); return reactionsFunction; } -JSHandle ObjectFactory::CreateJSPromiseExecutorFunction(const void *nativeFunc) +JSHandle ObjectFactory::CreateJSPromiseExecutorFunction() { JSHandle env = vm_->GetGlobalEnv(); JSHandle dynclass = JSHandle::Cast(env->GetPromiseExecutorFunctionClass()); JSHandle executorFunction = JSHandle::Cast(NewJSObject(dynclass)); executorFunction->SetCapability(thread_, JSTaggedValue::Hole()); - JSMethod *method = vm_->GetMethodForNativeFunction(nativeFunc); - executorFunction->SetCallTarget(thread_, method); + executorFunction->SetCallTarget(thread_, GetMethodByIndex(MethodIndex::BUILTINS_PROMISE_HANDLER_EXECUTOR)); executorFunction->SetCapability(thread_, JSTaggedValue::Undefined()); JSHandle function = JSHandle::Cast(executorFunction); JSFunction::InitializeJSFunction(thread_, function, FunctionKind::NORMAL_FUNCTION); @@ -2217,16 +2302,14 @@ JSHandle ObjectFactory::CreateJSPromiseExecutorFuncti return executorFunction; } -JSHandle ObjectFactory::NewJSPromiseAllResolveElementFunction( - const void *nativeFunc) +JSHandle ObjectFactory::NewJSPromiseAllResolveElementFunction() { JSHandle env = vm_->GetGlobalEnv(); JSHandle dynclass = JSHandle::Cast(env->GetPromiseAllResolveElementFunctionClass()); JSHandle function = JSHandle::Cast(NewJSObject(dynclass)); JSFunction::InitializeJSFunction(thread_, JSHandle::Cast(function)); - JSMethod *method = vm_->GetMethodForNativeFunction(nativeFunc); - function->SetCallTarget(thread_, method); + function->SetCallTarget(thread_, GetMethodByIndex(MethodIndex::BUILTINS_PROMISE_HANDLER_RESOLVE_ELEMENT_FUNCTION)); function->SetIndex(JSTaggedValue::Undefined()); function->SetValues(JSTaggedValue::Undefined()); function->SetCapabilities(JSTaggedValue::Undefined()); diff --git a/ecmascript/object_factory.h b/ecmascript/object_factory.h index 7384e30f..400995a7 100644 --- a/ecmascript/object_factory.h +++ b/ecmascript/object_factory.h @@ -23,6 +23,7 @@ #include "ecmascript/js_hclass.h" #include "ecmascript/js_native_pointer.h" #include "ecmascript/js_tagged_value.h" +#include "ecmascript/mem/chunk_containers.h" #include "ecmascript/mem/heap_region_allocator.h" #include "ecmascript/mem/machine_code.h" #include "ecmascript/mem/native_area_allocator.h" @@ -139,9 +140,31 @@ using base::ErrorType; using DeleteEntryPoint = void (*)(void *, void *); enum class RemoveSlots { YES, NO }; +enum class MethodIndex : uint8_t { + BUILTINS_GLOBAL_CALL_JS_BOUND_FUNCTION = 0, + BUILTINS_GLOBAL_CALL_JS_PROXY, + BUILTINS_OBJECT_CREATE_DATA_PROPERTY_ON_OBJECT_FUNCTIONS, + BUILTINS_COLLATOR_ANONYMOUS_COLLATOR, + BUILTINS_DATE_TIME_FORMAT_ANONYMOUS_DATE_TIME_FORMAT, + BUILTINS_NUMBER_FORMAT_NUMBER_FORMAT_INTERNAL_FORMAT_NUMBER, + BUILTINS_PROXY_INVALIDATE_PROXY_FUNCTION, + BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_FULFILLED, + BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_REJECTED, + BUILTINS_PROMISE_HANDLER_RESOLVE_ELEMENT_FUNCTION, + BUILTINS_PROMISE_HANDLER_RESOLVE, + BUILTINS_PROMISE_HANDLER_REJECT, + BUILTINS_PROMISE_HANDLER_EXECUTOR, + METHOD_END +}; + class ObjectFactory { public: - explicit ObjectFactory(JSThread *thread, Heap *heap); + explicit ObjectFactory(JSThread *thread, Heap *heap, Chunk *chunk); + ~ObjectFactory(); + void GenerateInternalNativeMethods(); + JSMethod *GetMethodByIndex(MethodIndex idx); + JSMethod *NewMethodForNativeFunction(const void *func); + JSMethod *NewMethodForAOTFunction(const void *func, size_t numArgs); JSHandle NewProfileTypeInfo(uint32_t length); JSHandle NewConstantPool(uint32_t capacity); @@ -178,12 +201,11 @@ public: const JSHandle &boundThis, const JSHandle &args); - JSHandle NewJSIntlBoundFunction(const void *nativeFunc = nullptr, int functionLength = 1); + JSHandle NewJSIntlBoundFunction(MethodIndex idx, int functionLength = 1); - JSHandle NewJSProxyRevocFunction(const JSHandle &proxy, - const void *nativeFunc = nullptr); + JSHandle NewJSProxyRevocFunction(const JSHandle &proxy); - JSHandle NewJSAsyncAwaitStatusFunction(const void *nativeFunc = nullptr); + JSHandle NewJSAsyncAwaitStatusFunction(MethodIndex idx); JSHandle NewJSGeneratorFunction(JSMethod *method); JSHandle NewAsyncFunction(JSMethod *method); @@ -311,11 +333,11 @@ public: JSHandle NewGeneratorContext(); - JSHandle CreateJSPromiseReactionsFunction(const void *nativeFunc); + JSHandle CreateJSPromiseReactionsFunction(MethodIndex idx); - JSHandle CreateJSPromiseExecutorFunction(const void *nativeFunc); + JSHandle CreateJSPromiseExecutorFunction(); - JSHandle NewJSPromiseAllResolveElementFunction(const void *nativeFunc); + JSHandle NewJSPromiseAllResolveElementFunction(); JSHandle CloneObjectLiteral(JSHandle object, const JSHandle &env, const JSHandle &constpool, bool canShareHClass = true); @@ -359,6 +381,8 @@ public: // only use for creating Function.prototype and Function JSHandle NewJSFunctionByDynClass(JSMethod *method, const JSHandle &clazz, FunctionKind kind = FunctionKind::NORMAL_FUNCTION); + JSHandle NewJSFunctionByDynClass(const void *func, const JSHandle &clazz, + FunctionKind kind = FunctionKind::NORMAL_FUNCTION); // used for creating jsobject by constructor JSHandle NewJSObjectByConstructor(const JSHandle &constructor, @@ -382,8 +406,6 @@ public: JSHandle NewTSFunctionType(uint32_t length); JSHandle NewTSArrayType(); - ~ObjectFactory() = default; - // ----------------------------------- new string ---------------------------------------- JSHandle NewFromASCII(const CString &data); JSHandle NewFromUtf8(const CString &data); @@ -447,8 +469,10 @@ private: friend class GlobalEnv; friend class GlobalEnvConstants; friend class EcmaString; + friend class SnapshotProcessor; + void ClearNativeMethodsData(); JSHandle NewJSFunctionImpl(JSMethod *method); - + static void * InternalMethodTable[static_cast(MethodIndex::METHOD_END)]; void InitObjectFields(const TaggedObject *object); JSThread *thread_ {nullptr}; @@ -457,6 +481,8 @@ private: EcmaVM *vm_ {nullptr}; Heap *heap_ {nullptr}; + ChunkVector nativeMethods_; + ChunkVector internalNativeMethods_; NO_COPY_SEMANTIC(ObjectFactory); NO_MOVE_SEMANTIC(ObjectFactory); diff --git a/ecmascript/snapshot/mem/snapshot_processor.cpp b/ecmascript/snapshot/mem/snapshot_processor.cpp index 571fc9d1..d1303531 100644 --- a/ecmascript/snapshot/mem/snapshot_processor.cpp +++ b/ecmascript/snapshot/mem/snapshot_processor.cpp @@ -1007,7 +1007,7 @@ void *SnapshotProcessor::NativePointerEncodeBitToAddr(EncodeBit nativeBit) size_t nativeTableSize = GetNativeTableSize(); if (index < nativeTableSize - Constants::PROGRAM_NATIVE_METHOD_BEGIN) { - addr = reinterpret_cast(vm_->nativeMethods_.at(index)); + addr = reinterpret_cast(vm_->GetFactory()->nativeMethods_.at(index)); } else if (index < nativeTableSize) { addr = reinterpret_cast(g_nativeTable[index]); } else { @@ -1060,7 +1060,7 @@ void SnapshotProcessor::DeserializeNativePointer(uint64_t *value) return; } if (index < nativeTableSize - Constants::PROGRAM_NATIVE_METHOD_BEGIN) { - addr = reinterpret_cast(vm_->nativeMethods_.at(index)); + addr = reinterpret_cast(vm_->GetFactory()->nativeMethods_.at(index)); } else if (index < nativeTableSize) { addr = g_nativeTable[index]; } else { @@ -1158,7 +1158,7 @@ void SnapshotProcessor::GeneratedNativeMethod() // NOLINT(readability-function- { size_t nativeMethodSize = GetNativeTableSize() - Constants::PROGRAM_NATIVE_METHOD_BEGIN; for (size_t i = 0; i < nativeMethodSize; i++) { - vm_->GetMethodForNativeFunction(reinterpret_cast(g_nativeTable[i])); + vm_->GetFactory()->NewMethodForNativeFunction(reinterpret_cast(g_nativeTable[i])); } } diff --git a/ecmascript/stubs/runtime_stubs-inl.h b/ecmascript/stubs/runtime_stubs-inl.h index 63fbed47..abaa0a51 100644 --- a/ecmascript/stubs/runtime_stubs-inl.h +++ b/ecmascript/stubs/runtime_stubs-inl.h @@ -1426,15 +1426,16 @@ JSTaggedValue RuntimeStubs::RuntimeDefineGeneratorFunc(JSThread *thread, JSFunct JSTaggedValue RuntimeStubs::RuntimeDefineGeneratorFuncWithMethodId(JSThread *thread, JSTaggedValue methodId) { - auto aotCodeInfo = thread->GetEcmaVM()->GetAotCodeInfo(); + auto vm = thread->GetEcmaVM(); + ObjectFactory *factory = vm->GetFactory(); + auto aotCodeInfo = vm->GetAotCodeInfo(); auto codeEntry = aotCodeInfo->GetAOTFuncEntry(methodId.GetInt()); - JSMethod *method = thread->GetEcmaVM()->GetMethodForNativeFunction(reinterpret_cast(codeEntry)); + JSMethod *method = factory->NewMethodForNativeFunction(reinterpret_cast(codeEntry)); method->SetAotCodeBit(true); method->SetNativeBit(false); method->SetNumArgsWithCallField(1); - JSHandle env = thread->GetEcmaVM()->GetGlobalEnv(); - ObjectFactory *factory = thread->GetEcmaVM()->GetFactory(); + JSHandle env = vm->GetGlobalEnv(); JSHandle jsFunc = factory->NewJSGeneratorFunction(method); ASSERT_NO_ABRUPT_COMPLETION(thread); diff --git a/ecmascript/stubs/test_runtime_stubs.cpp b/ecmascript/stubs/test_runtime_stubs.cpp index c4804b6c..5569866b 100644 --- a/ecmascript/stubs/test_runtime_stubs.cpp +++ b/ecmascript/stubs/test_runtime_stubs.cpp @@ -65,7 +65,7 @@ DEF_RUNTIME_STUBS(DefineAotFunc) ObjectFactory *factory = ecmaVm->GetFactory(); JSHandle env = ecmaVm->GetGlobalEnv(); auto codeEntry = thread->GetFastStubEntry(funcIndex.GetInt()); - JSMethod *method = ecmaVm->GetMethodForNativeFunction(reinterpret_cast(codeEntry)); + JSMethod *method = factory->NewMethodForNativeFunction(reinterpret_cast(codeEntry)); method->SetAotCodeBit(true); method->SetNativeBit(false); method->SetNumArgsWithCallField(numArgs.GetInt()); diff --git a/ecmascript/tests/dump_test.cpp b/ecmascript/tests/dump_test.cpp index a1bb10cd..e99a0fe6 100644 --- a/ecmascript/tests/dump_test.cpp +++ b/ecmascript/tests/dump_test.cpp @@ -363,13 +363,15 @@ HWTEST_F_L0(EcmaDumpTest, HeapProfileDump) } case JSType::JS_INTL_BOUND_FUNCTION: { CHECK_DUMP_FIELDS(JSFunction::SIZE, JSIntlBoundFunction::SIZE, 3U) - JSHandle intlBoundFunc = factory->NewJSIntlBoundFunction(); + JSHandle intlBoundFunc = factory->NewJSIntlBoundFunction( + MethodIndex::BUILTINS_NUMBER_FORMAT_NUMBER_FORMAT_INTERNAL_FORMAT_NUMBER); DUMP_FOR_HANDLE(intlBoundFunc) break; } case JSType::JS_ASYNC_AWAIT_STATUS_FUNCTION: { CHECK_DUMP_FIELDS(JSFunction::SIZE, JSAsyncAwaitStatusFunction::SIZE, 1U) - JSHandle asyncAwaitFunc = factory->NewJSAsyncAwaitStatusFunction(); + JSHandle asyncAwaitFunc = factory->NewJSAsyncAwaitStatusFunction( + MethodIndex::BUILTINS_PROMISE_HANDLER_ASYNC_AWAIT_FULFILLED); DUMP_FOR_HANDLE(asyncAwaitFunc) break; } diff --git a/ecmascript/tests/native_pointer_test.cpp b/ecmascript/tests/native_pointer_test.cpp index 02b2d7e3..f7660649 100644 --- a/ecmascript/tests/native_pointer_test.cpp +++ b/ecmascript/tests/native_pointer_test.cpp @@ -66,7 +66,7 @@ HWTEST_F_L0(NativePointerTest, Print) JSHandle jsFunction = factory->NewJSFunction(env); EXPECT_TRUE(*jsFunction != nullptr); - JSMethod *target = thread->GetEcmaVM()->GetMethodForNativeFunction(nullptr); + JSMethod *target = factory->NewMethodForNativeFunction(nullptr); jsFunction->SetCallTarget(thread, target); // run cpp methed 'Print'