diff --git a/ets_environment/frameworks/ets_environment/src/ets_environment.cpp b/ets_environment/frameworks/ets_environment/src/ets_environment.cpp index b0a69b1d7858a85c33380dba3e509bec99976a6f..0d07b7c4a9974760c8e82aa0c1649e9a529833ef 100644 --- a/ets_environment/frameworks/ets_environment/src/ets_environment.cpp +++ b/ets_environment/frameworks/ets_environment/src/ets_environment.cpp @@ -67,6 +67,7 @@ const char ETS_SYS_NSNAME[] = "ets_system"; constexpr const char* CLASSNAME_STRING = "Lstd/core/String;"; constexpr const char* CLASSNAME_LINKER = "Lstd/core/AbcRuntimeLinker;"; +constexpr const char* CLASSNAME_COROUTINE = "std.core.Coroutine"; } // namespace ETSRuntimeAPI ETSEnvironment::lazyApis_ {}; @@ -528,8 +529,11 @@ void ETSEnvironment::FinishPreload() { void ETSEnvironment::PostFork(void *napiEnv, const std::string &aotPath, const std::vector &appInnerHspPathList, - const std::vector &commonHspBundleInfos) + const std::vector &commonHspBundleInfos, + const std::shared_ptr &eventRunner) { + InitEventHandler(eventRunner); + std::vector options; std::string aotPathString = ""; if (!aotPath.empty()) { @@ -549,6 +553,7 @@ void ETSEnvironment::PostFork(void *napiEnv, const std::string &aotPath, ark::ets::ETSAni::Postfork(env, options); appInnerHspPathList_ = appInnerHspPathList; commonHspBundleInfos_ = commonHspBundleInfos; + PostCoroutineScheduleTask(); } void ETSEnvironment::PreloadSystemClass(const char *className) @@ -602,8 +607,10 @@ ETSEnvFuncs *ETSEnvironment::RegisterFuncs() ETSEnvironment::GetInstance()->FinishPreload(); }, .PostFork = [](void *napiEnv, const std::string &aotPath, const std::vector &appInnerHspPathList, - const std::vector &commonHspBundleInfos) { - ETSEnvironment::GetInstance()->PostFork(napiEnv, aotPath, appInnerHspPathList, commonHspBundleInfos); + const std::vector &commonHspBundleInfos, + const std::shared_ptr &eventRunner) { + ETSEnvironment::GetInstance()->PostFork( + napiEnv, aotPath, appInnerHspPathList, commonHspBundleInfos, eventRunner); }, .PreloadSystemClass = [](const char *className) { ETSEnvironment::GetInstance()->PreloadSystemClass(className); @@ -696,7 +703,7 @@ int32_t ETSEnvironment::ParseHdcRegisterOption(std::string& option) void ETSEnvironment::InitEventHandler(const std::shared_ptr &eventRunner) { TAG_LOGD(AAFwkTag::ETSRUNTIME, "InitEventHandler called"); - if (eventRunner != nullptr) { + if (eventRunner != nullptr && eventHandler_ == nullptr) { eventHandler_ = std::make_shared(eventRunner); } } @@ -872,6 +879,35 @@ ani_object ETSEnvironment::CreateRuntimeLinker( return object; } + +static void ScheduleCoroutine(ani_env *aniEnv) +{ + ani_class cls = nullptr; + if (aniEnv->FindClass(CLASSNAME_COROUTINE, &cls) != ANI_OK) { + TAG_LOGE(AAFwkTag::ETSRUNTIME, "FindClass std.core.Coroutine Failed"); + return; + } + ani_static_method schedule {}; + if (aniEnv->Class_FindStaticMethod(cls, "Schedule", ":", &schedule) != ANI_OK) { + TAG_LOGE(AAFwkTag::ETSRUNTIME, "Class_FindStaticMethod Schedule failed"); + return; + } + if (aniEnv->Class_CallStaticMethod_Void(cls, schedule) != ANI_OK) { + TAG_LOGE(AAFwkTag::ETSRUNTIME, "Class_CallStaticMethod_Void Schedule failed"); + return; + } +} + +void ETSEnvironment::PostCoroutineScheduleTask() +{ + auto scheduleTask = []() { + auto &etsEnv = GetInstance(); + ScheduleCoroutine(etsEnv->GetAniEnv()); + etsEnv->PostCoroutineScheduleTask(); + }; + PostTask(scheduleTask, "ScheduleCoroutine", 1); +} + } // namespace EtsEnv } // namespace OHOS diff --git a/ets_environment/interfaces/inner_api/ets_environment.h b/ets_environment/interfaces/inner_api/ets_environment.h index 2e9a19aea07ed8c825d04da7808054b8b9344233..e8155c75829f5eccb9f6a8f35919135821c25eb8 100644 --- a/ets_environment/interfaces/inner_api/ets_environment.h +++ b/ets_environment/interfaces/inner_api/ets_environment.h @@ -61,7 +61,8 @@ public: void *&obj, void *&ref); void FinishPreload(); void PostFork(void *napiEnv, const std::string &aotPath, const std::vector& appInnerHspPathList, - const std::vector &commonHspBundleInfos); + const std::vector &commonHspBundleInfos, + const std::shared_ptr &eventRunner); void PreloadSystemClass(const char *className); void RemoveInstance(uint32_t instanceId); @@ -99,6 +100,7 @@ private: bool LoadAbcLinker(ani_env *env, const std::string &modulePath, ani_class &abcCls, ani_object &abcObj); void InitEventHandler(const std::shared_ptr &eventRunner); int32_t ParseHdcRegisterOption(std::string& option); + void PostCoroutineScheduleTask(); static ETSRuntimeAPI lazyApis_; VMEntry vmEntry_; ETSUncaughtExceptionInfo uncaughtExceptionInfo_; diff --git a/ets_environment/interfaces/inner_api/ets_interface.h b/ets_environment/interfaces/inner_api/ets_interface.h index 935a46ef708412bcd415f773598ca741dc5bb7f9..23c967c83948bd769c6b774339a20a9775149d8e 100644 --- a/ets_environment/interfaces/inner_api/ets_interface.h +++ b/ets_environment/interfaces/inner_api/ets_interface.h @@ -50,7 +50,8 @@ struct ETSEnvFuncs { void (*FinishPreload)() = nullptr; void (*PostFork)(void *napiEnv, const std::string &aotPath, const std::vector &appInnerHspPathList, - const std::vector &commonHspBundleInfos) = nullptr; + const std::vector &commonHspBundleInfos, + const std::shared_ptr &eventRunner) = nullptr; void (*PreloadSystemClass)(const char *className) = nullptr; void (*RemoveInstance)(uint32_t instanceId) = nullptr; void (*StopDebugMode)(void *jsVm) = nullptr; diff --git a/frameworks/native/runtime/ets_runtime.cpp b/frameworks/native/runtime/ets_runtime.cpp index 98ed7221404c4a20b535adcaf28fc1695f16e051..4ecc92277061bb14983ee588a7039029bd5e48b6 100644 --- a/frameworks/native/runtime/ets_runtime.cpp +++ b/frameworks/native/runtime/ets_runtime.cpp @@ -279,7 +279,7 @@ void ETSRuntime::PostFork(const Options &options, std::unique_ptr &js } napi_env napiEnv = static_cast(jsRuntime_.get())->GetNapiEnv(); g_etsEnvFuncs->PostFork(reinterpret_cast(napiEnv), aotFilePath, options.appInnerHspPathList, - options.commonHspBundleInfos); + options.commonHspBundleInfos, options.eventRunner); } std::unique_ptr ETSRuntime::Create(const Options &options, std::unique_ptr &jsRuntime)