diff --git a/BUILD.gn b/BUILD.gn index 433afa933eae399df772364cb7820ea9998a0e63..4fe7f57108088ba3840a7c07619c2f18ddce6650 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -52,9 +52,12 @@ ohos_executable("appspawn") { external_deps = [ "ability_base:want", "ability_runtime:app_manager", + "ability_runtime:runtime", "eventhandler:libeventhandler", "hiviewdfx_hilog_native:libhilog", + "init:libbegetutil", "ipc:ipc_core", + "napi:ace_napi", "utils_base:utils", ] diff --git a/adapter/appspawn_ace.cpp b/adapter/appspawn_ace.cpp index debbbd4bd85b52b3f47545e4316e949a33b65d4f..c6e794cc687c4a1aeb854cd6f000f5d2b87595eb 100644 --- a/adapter/appspawn_ace.cpp +++ b/adapter/appspawn_ace.cpp @@ -15,6 +15,10 @@ #include "appspawn_adapter.h" +#include "js_runtime.h" +#include "parameters.h" +#include "runtime.h" + #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/app/main_thread.h" void LoadExtendLib(AppSpawnContent *content) @@ -24,14 +28,43 @@ void LoadExtendLib(AppSpawnContent *content) #else const char *acelibdir("/system/lib/libace.z.so"); #endif - APPSPAWN_LOGI("MainThread::LoadAbilityLibrary. Start calling dlopen acelibdir."); + APPSPAWN_LOGI("LoadExtendLib: Start calling dlopen acelibdir."); #ifndef APPSPAWN_TEST void *AceAbilityLib = NULL; AceAbilityLib = dlopen(acelibdir, RTLD_NOW | RTLD_GLOBAL); APPSPAWN_CHECK(AceAbilityLib != NULL, return, "Fail to dlopen %s, [%s]", acelibdir, dlerror()); #endif - APPSPAWN_LOGI("Success to dlopen %s", acelibdir); - APPSPAWN_LOGI("MainThread::LoadAbilityLibrary. End calling dlopen"); + APPSPAWN_LOGI("LoadExtendLib: Success to dlopen %s", acelibdir); + APPSPAWN_LOGI("LoadExtendLib: End calling dlopen"); + + bool preload = OHOS::system::GetBoolParameter("const.appspawn.preload", false); + if (!preload) { + APPSPAWN_LOGI("LoadExtendLib: Do not preload JS VM"); + return; + } + + APPSPAWN_LOGI("LoadExtendLib: Start preload JS VM"); + + OHOS::AbilityRuntime::Runtime::Options options; + options.lang = OHOS::AbilityRuntime::Runtime::Language::JS; + options.loadAce = true; + options.preload = true; + + auto runtime = OHOS::AbilityRuntime::Runtime::Create(options); + if (!runtime) { + APPSPAWN_LOGE("LoadExtendLib: Failed to create runtime"); + return; + } + + // Preload napi module + runtime->PreloadSystemModule("application.Ability"); + runtime->PreloadSystemModule("application.Context"); + runtime->PreloadSystemModule("application.AbilityContext"); + + // Save preloaded runtime + OHOS::AbilityRuntime::Runtime::SavePreloaded(std::move(runtime)); + + APPSPAWN_LOGI("LoadExtendLib: End preload JS VM"); } void RunChildProcessor(AppSpawnContent *content, AppSpawnClient *client) diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 13e89322a4b318485b8c64b4848f8c73986b639a..84621c9c5379a220d6f5aafc810d63e1a74d6c70 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -89,13 +89,14 @@ ohos_unittest("AppSpawn_ut") { external_deps = [ "ability_base:want", "ability_runtime:app_manager", + "ability_runtime:runtime", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "eventhandler:libeventhandler", "hiviewdfx_hilog_native:libhilog", - "hiviewdfx_hilog_native:libhilog", "init:libbegetutil", "ipc:ipc_core", + "napi:ace_napi", "utils_base:utils", ] }