From f2c49f6c1965ca9b4f13cd685752b4c30210198d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=B0=B8=E5=87=AF?= Date: Mon, 23 Jun 2025 09:48:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=92=E4=BB=B6=E6=A1=86=E6=9E=B6=E5=A3=B0?= =?UTF-8?q?=E6=98=8E=E5=BC=8F=E6=8E=A7=E4=BB=B6=E4=BD=BF=E7=94=A8=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 刘永凯 Change-Id: I320e6058a52c68ed78deed1628a99c93317642b2 --- .../engine/jsi/jsi_declarative_engine.cpp | 36 +++++++++++++++++-- .../engine/jsi/jsi_view_register_impl.cpp | 6 ++++ .../engine/jsi/modules/jsi_module_manager.cpp | 20 +++++++++++ .../engine/jsi/modules/jsi_module_manager.h | 2 ++ .../js_frontend/engine/jsi/ark_js_runtime.cpp | 11 ++---- .../js_frontend/engine/jsi/ark_js_runtime.h | 1 - 6 files changed, 63 insertions(+), 13 deletions(-) diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.cpp b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.cpp index 22c3f702ab1..65c84646e60 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.cpp +++ b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.cpp @@ -190,6 +190,28 @@ shared_ptr RequireNativeModule(const shared_ptr& runtime, co return runtime->NewNull(); } +shared_ptr RequireNativeModuleForCustomRuntime(const shared_ptr& runtime, + const shared_ptr& thisObj, const std::vector>& argv, int32_t argc) +{ + std::string moduleName = argv[0]->ToString(runtime); + + // has already init module object + shared_ptr global = runtime->GetGlobal(); + shared_ptr moduleObject = global->GetProperty(runtime, moduleName); + if (moduleObject != nullptr && moduleObject->IsObject(runtime)) { + return moduleObject; + } + + // init module object first time + shared_ptr newObject = runtime->NewObject(); + if (ModuleManager::GetInstance()->InitModuleForCustomRuntime(runtime, newObject, moduleName)) { + global->SetProperty(runtime, moduleName, newObject); + return newObject; + } + + return runtime->NewNull(); +} + inline bool PreloadJsEnums(const shared_ptr& runtime) { std::string str("arkui_binary_jsEnumStyle_abc_loadFile"); @@ -321,9 +343,13 @@ inline bool PreloadExports(const shared_ptr& runtime, const shared_pt return global->SetProperty(runtime, "exports", exportsUtilObj); } -inline bool PreloadRequireNative(const shared_ptr& runtime, const shared_ptr& global) +inline bool PreloadRequireNative( + const shared_ptr& runtime, const shared_ptr& global, bool isCustomEnvSupported = false) { - return global->SetProperty(runtime, "requireNativeModule", runtime->NewFunction(RequireNativeModule)); + auto func = isCustomEnvSupported + ? runtime->NewFunction(RequireNativeModuleForCustomRuntime) + : runtime->NewFunction(RequireNativeModule); + return global->SetProperty(runtime, "requireNativeModule", func); } /** @@ -810,7 +836,7 @@ void JsiDeclarativeEngineInstance::PreloadAceModuleForCustomRuntime(void* runtim JsRegisterViews(JSNApi::GetGlobalObject(vm, nativeArkEngine->GetContext()), runtime, true); // preload console - shared_ptr global = arkRuntime->GetGlobal(nativeArkEngine); + shared_ptr global = arkRuntime->GetGlobal(); OHOS::JsSysModule::Console::InitConsoleModule(static_cast(runtime)); OHOS::JsSysModule::Timer::RegisterTime(static_cast(runtime)); JsiSyscapModule::GetInstance()->InitSyscapModule(arkRuntime, global); @@ -825,6 +851,10 @@ void JsiDeclarativeEngineInstance::PreloadAceModuleForCustomRuntime(void* runtim // preload perfutil PreloadPerfutil(arkRuntime, global); + // preload exports and requireNative + PreloadExports(arkRuntime, global); + PreloadRequireNative(arkRuntime, global, true); + // preload js enums LOGI("preload js enums in PreloadAceModuleForCustomRuntime"); bool jsEnumStyleResult = PreloadJsEnums(arkRuntime); diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_view_register_impl.cpp b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_view_register_impl.cpp index e46e91f5ab3..ff54acf5965 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/jsi_view_register_impl.cpp +++ b/frameworks/bridge/declarative_frontend/engine/jsi/jsi_view_register_impl.cpp @@ -551,6 +551,12 @@ static const std::unordered_set unsupportedTargetsInCustomEnv = { "UIExtensionProxy", "FormLink", "AbilityController", + "Navigation", + "Navigator", + "NavRouter", + "NavDestination", + "XComponent", + "XComponentController", }; static const std::unordered_map> bindFuncs = { diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_module_manager.cpp b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_module_manager.cpp index 53c626688f8..e1ff7653ab3 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_module_manager.cpp +++ b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_module_manager.cpp @@ -60,4 +60,24 @@ bool ModuleManager::InitModule( } } +bool ModuleManager::InitModuleForCustomRuntime( + const shared_ptr& runtime, shared_ptr& thisObj, const std::string& moduleName) +{ + static const std::unordered_map& runtime, shared_ptr& thisObj)> + MODULE_LIST = { + { "ohos.curves", [](const shared_ptr& runtime, + shared_ptr& thisObj) { InitCurvesModule(runtime, thisObj); } }, + { "ohos.matrix4", [](const shared_ptr& runtime, + shared_ptr& thisObj) { InitMatrix4Module(runtime, thisObj); } }, + }; + auto iter = MODULE_LIST.find(moduleName); + if (iter != MODULE_LIST.end()) { + iter->second(runtime, thisObj); + return true; + } else { + return false; + } +} + } // namespace OHOS::Ace::Framework \ No newline at end of file diff --git a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_module_manager.h b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_module_manager.h index 5e27ff88395..aff628b2a52 100644 --- a/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_module_manager.h +++ b/frameworks/bridge/declarative_frontend/engine/jsi/modules/jsi_module_manager.h @@ -34,6 +34,8 @@ public: static ModuleManager* GetInstance(); bool InitModule(const shared_ptr& runtime, shared_ptr& thisObj, const std::string& moduleName); + bool InitModuleForCustomRuntime( + const shared_ptr& runtime, shared_ptr& thisObj, const std::string& moduleName); private: ACE_DISALLOW_COPY_AND_MOVE(ModuleManager); diff --git a/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.cpp b/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.cpp index 599520a7561..228a12ac12d 100644 --- a/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.cpp +++ b/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.cpp @@ -256,15 +256,8 @@ bool ArkJSRuntime::ExecuteJsBinForAOT(const std::string& fileName, shared_ptr ArkJSRuntime::GetGlobal() { LocalScope scope(vm_); - return std::make_shared(shared_from_this(), JSNApi::GetGlobalObject(vm_)); -} - -shared_ptr ArkJSRuntime::GetGlobal(ArkNativeEngine* nativeArkEngine) -{ - LocalScope scope(vm_); - CHECK_NULL_RETURN(nativeArkEngine, nullptr); - return std::make_shared( - shared_from_this(), JSNApi::GetGlobalObject(vm_, nativeArkEngine->GetContext())); + auto context = panda::JSNApi::GetCurrentContext(vm_); + return std::make_shared(shared_from_this(), JSNApi::GetGlobalObject(vm_, context)); } void ArkJSRuntime::RunGC() diff --git a/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h b/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h index bc798d2b952..59ed48517fb 100644 --- a/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h +++ b/frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h @@ -77,7 +77,6 @@ public: bool ExecuteJsBinForAOT(const std::string& fileName, const std::function& errorCallback = nullptr) override; shared_ptr GetGlobal() override; - shared_ptr GetGlobal(ArkNativeEngine* nativeArkEngine); void RunGC() override; void RunFullGC() override; -- Gitee