diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index 0ebf062e1768e39242df2267628c0a46eadd1424..2a6407bc9357e385c38d7009ef2837de44c43276 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -141,7 +141,7 @@ JsRuntime::JsRuntime() JsRuntime::~JsRuntime() { - HILOG_DEBUG("JsRuntime destructor."); + HILOG_INFO("JsRuntime destructor."); Deinitialize(); StopDebugMode(); } @@ -181,7 +181,6 @@ void JsRuntime::StartDebugMode(bool needBreakPoint) HILOG_INFO("Already in debug mode"); return; } - CHECK_POINTER(jsEnv_); // Set instance id to tid after the first instance. if (JsRuntime::hasInstance.exchange(true, std::memory_order_relaxed)) { instanceId_ = static_cast(gettid()); @@ -415,7 +414,7 @@ bool JsRuntime::LoadScript(const std::string& path, std::vector* buffer return jsEnv_->LoadScript(path, buffer, isBundle); } -bool JsRuntime::LoadScript(const std::string& path, uint8_t *buffer, size_t len, bool isBundle) +bool JsRuntime::LoadScript(const std::string& path, uint8_t* buffer, size_t len, bool isBundle) { HILOG_DEBUG("function called."); CHECK_POINTER_AND_RETURN(jsEnv_, false); @@ -425,9 +424,9 @@ bool JsRuntime::LoadScript(const std::string& path, uint8_t *buffer, size_t len, std::unique_ptr JsRuntime::LoadSystemModuleByEngine(NativeEngine* engine, const std::string& moduleName, NativeValue* const* argv, size_t argc) { - HILOG_DEBUG("JsRuntime::LoadSystemModule(%{public}s)", moduleName.c_str()); + HILOG_DEBUG("Load system module %{public}s.", moduleName.c_str()); if (engine == nullptr) { - HILOG_INFO("JsRuntime::LoadSystemModule: invalid engine."); + HILOG_INFO("Invalid engine."); return std::unique_ptr(); } @@ -457,6 +456,41 @@ void JsRuntime::FinishPreload() panda::JSNApi::PreFork(vm); } +void JsRuntime::PostPreload(const Options& options) +{ + auto vm = GetEcmaVm(); + CHECK_POINTER(vm); + auto nativeEngine = GetNativeEnginePointer(); + CHECK_POINTER(nativeEngine); + panda::RuntimeOption postOption; + postOption.SetBundleName(options.bundleName); + if (!options.arkNativeFilePath.empty()) { + std::string sandBoxAnFilePath = SANDBOX_ARK_CACHE_PATH + options.arkNativeFilePath; + postOption.SetAnDir(sandBoxAnFilePath); + } + bool profileEnabled = OHOS::system::GetBoolParameter("ark.profile", false); + postOption.SetEnableProfile(profileEnabled); + panda::JSNApi::PostFork(vm, postOption); + nativeEngine->ReinitUVLoop(); + panda::JSNApi::SetLoop(vm, nativeEngine->GetUVLoop()); +} + +void JsRuntime::LoadAotFile(const Options& options) +{ + auto vm = GetEcmaVm(); + CHECK_POINTER(vm); + if (options.hapPath.empty()) { + return; + } + + bool newCreate = false; + std::string loadPath = ExtractorUtil::GetLoadFilePath(options.hapPath); + std::shared_ptr extractor = ExtractorUtil::GetExtractor(loadPath, newCreate, true); + if (extractor != nullptr && newCreate) { + panda::JSNApi::LoadAotFile(vm, options.moduleName); + } +} + bool JsRuntime::Initialize(const Options& options) { HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); @@ -483,17 +517,7 @@ bool JsRuntime::Initialize(const Options& options) CHECK_POINTER_AND_RETURN(vm, false); if (preloaded_) { - panda::RuntimeOption postOption; - postOption.SetBundleName(options.bundleName); - if (!options.arkNativeFilePath.empty()) { - std::string sandBoxAnFilePath = SANDBOX_ARK_CACHE_PATH + options.arkNativeFilePath; - postOption.SetAnDir(sandBoxAnFilePath); - } - bool profileEnabled = OHOS::system::GetBoolParameter("ark.profile", false); - postOption.SetEnableProfile(profileEnabled); - panda::JSNApi::PostFork(vm, postOption); - nativeEngine->ReinitUVLoop(); - panda::JSNApi::SetLoop(vm, nativeEngine->GetUVLoop()); + PostPreload(options); } NativeObject* globalObj = ConvertNativeValueTo(nativeEngine->GetGlobal()); @@ -524,19 +548,7 @@ bool JsRuntime::Initialize(const Options& options) bundleName_ = options.bundleName; codePath_ = options.codePath; ReInitJsEnvImpl(options); - - if (!options.hapPath.empty()) { - bool newCreate = false; - std::string loadPath = ExtractorUtil::GetLoadFilePath(options.hapPath); - std::shared_ptr extractor = ExtractorUtil::GetExtractor(loadPath, newCreate, true); - if (!extractor) { - HILOG_ERROR("Get extractor failed. hapPath[%{private}s]", options.hapPath.c_str()); - return false; - } - if (newCreate) { - panda::JSNApi::LoadAotFile(vm, options.moduleName); - } - } + LoadAotFile(options); panda::JSNApi::SetBundle(vm, options.isBundle); panda::JSNApi::SetBundleName(vm, options.bundleName); @@ -751,7 +763,7 @@ std::unique_ptr JsRuntime::LoadModule(const std::string& module const std::string& hapPath, bool esmodule, bool useCommonChunk) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); - HILOG_DEBUG("JsRuntime::LoadModule(%{public}s, %{private}s, %{private}s, %{public}s)", + HILOG_DEBUG("Load module(%{public}s, %{private}s, %{private}s, %{public}s)", moduleName.c_str(), modulePath.c_str(), hapPath.c_str(), esmodule ? "true" : "false"); auto nativeEngine = GetNativeEnginePointer(); CHECK_POINTER_AND_RETURN(nativeEngine, std::unique_ptr()); @@ -823,8 +835,6 @@ std::unique_ptr JsRuntime::LoadSystemModule( bool JsRuntime::RunScript(const std::string& srcPath, const std::string& hapPath, bool useCommonChunk) { HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); - auto nativeEngine = GetNativeEnginePointer(); - CHECK_POINTER_AND_RETURN(nativeEngine, false); auto vm = GetEcmaVm(); CHECK_POINTER_AND_RETURN(vm, false); @@ -1031,7 +1041,7 @@ void JsRuntime::UpdateModuleNameAndAssetPath(const std::string& moduleName) panda::JSNApi::SetModuleName(vm, moduleName_); } -void JsRuntime::RegisterUncaughtExceptionHandler(JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo) +void JsRuntime::RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo) { CHECK_POINTER(jsEnv_); jsEnv_->RegisterUncaughtExceptionHandler(uncaughtExceptionInfo); @@ -1040,9 +1050,8 @@ void JsRuntime::RegisterUncaughtExceptionHandler(JsEnv::UncaughtExceptionInfo un void JsRuntime::RegisterQuickFixQueryFunc(const std::map& moduleAndPath) { auto vm = GetEcmaVm(); - if (vm != nullptr) { - panda::JSNApi::RegisterQuickFixQueryFunc(vm, JsQuickfixCallback(moduleAndPath)); - } + CHECK_POINTER(vm); + panda::JSNApi::RegisterQuickFixQueryFunc(vm, JsQuickfixCallback(moduleAndPath)); } bool JsRuntime::ReadSourceMapData(const std::string& hapPath, const std::string& sourceMapPath, std::string& content) @@ -1175,5 +1184,5 @@ void JsRuntime::SetModuleLoadChecker(const std::shared_ptrSetModuleLoadChecker(moduleCheckerDelegate); } -} // namespace AbilityRuntime -} // namespace OHOS +} // namespace AbilityRuntime +} // namespace OHOS diff --git a/frameworks/native/runtime/ohos_js_environment_impl.cpp b/frameworks/native/runtime/ohos_js_environment_impl.cpp index 350c72831ab6b5ee2f5c44b9e128499225177631..5a707b80c2b89250143949238c3f2e2700264df0 100644 --- a/frameworks/native/runtime/ohos_js_environment_impl.cpp +++ b/frameworks/native/runtime/ohos_js_environment_impl.cpp @@ -86,6 +86,7 @@ void OHOSJsEnvironmentImpl::InitConsoleModule(NativeEngine* engine) bool OHOSJsEnvironmentImpl::InitLoop(NativeEngine* engine) { HILOG_DEBUG("called"); + CHECK_POINTER_AND_RETURN(engine, false); auto uvLoop = engine->GetUVLoop(); auto fd = uvLoop != nullptr ? uv_backend_fd(uvLoop) : -1; if (fd < 0) { @@ -104,6 +105,7 @@ bool OHOSJsEnvironmentImpl::InitLoop(NativeEngine* engine) void OHOSJsEnvironmentImpl::DeInitLoop(NativeEngine* engine) { + CHECK_POINTER(engine); auto uvLoop = engine->GetUVLoop(); auto fd = uvLoop != nullptr ? uv_backend_fd(uvLoop) : -1; if (fd >= 0 && eventHandler_ != nullptr) { @@ -112,16 +114,17 @@ void OHOSJsEnvironmentImpl::DeInitLoop(NativeEngine* engine) RemoveTask(TIMER_TASK); } -void OHOSJsEnvironmentImpl::InitWorkerModule(NativeEngine& engine, std::shared_ptr workerInfo) +void OHOSJsEnvironmentImpl::InitWorkerModule(NativeEngine* engine, std::shared_ptr workerInfo) { HILOG_DEBUG("called"); - engine.SetInitWorkerFunc(InitWorkerFunc); - engine.SetOffWorkerFunc(OffWorkerFunc); - engine.SetGetAssetFunc(AssetHelper(workerInfo)); + CHECK_POINTER(engine); + engine->SetInitWorkerFunc(InitWorkerFunc); + engine->SetOffWorkerFunc(OffWorkerFunc); + engine->SetGetAssetFunc(AssetHelper(workerInfo)); - engine.SetGetContainerScopeIdFunc(GetContainerId); - engine.SetInitContainerScopeFunc(UpdateContainerScope); - engine.SetFinishContainerScopeFunc(RestoreContainerScope); + engine->SetGetContainerScopeIdFunc(GetContainerId); + engine->SetInitContainerScopeFunc(UpdateContainerScope); + engine->SetFinishContainerScopeFunc(RestoreContainerScope); } void OHOSJsEnvironmentImpl::InitSyscapModule() diff --git a/frameworks/native/runtime/ohos_js_environment_impl.h b/frameworks/native/runtime/ohos_js_environment_impl.h index 6d4799cee06f8b7ec697f5af8a4a6a449febec20..7f113058fdcf4e84f40c3785b1f716325ff9b2d2 100644 --- a/frameworks/native/runtime/ohos_js_environment_impl.h +++ b/frameworks/native/runtime/ohos_js_environment_impl.h @@ -40,7 +40,7 @@ public: void DeInitLoop(NativeEngine* engine) override; - void InitWorkerModule(NativeEngine& engine, std::shared_ptr workerInfo) override; + void InitWorkerModule(NativeEngine* engine, std::shared_ptr workerInfo) override; void InitSyscapModule() override; diff --git a/interfaces/inner_api/runtime/include/js_runtime.h b/interfaces/inner_api/runtime/include/js_runtime.h index 4d2be4764cd93944698a2c11688e6f639b3cc5d6..d3b91f8168bc5f4907b29b9fb209a75448ab962a 100644 --- a/interfaces/inner_api/runtime/include/js_runtime.h +++ b/interfaces/inner_api/runtime/include/js_runtime.h @@ -97,11 +97,11 @@ public: bool LoadRepairPatch(const std::string& hqfFile, const std::string& hapPath) override; bool UnLoadRepairPatch(const std::string& hqfFile) override; bool NotifyHotReloadPage() override; - void RegisterUncaughtExceptionHandler(JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo); + void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo); bool LoadScript(const std::string& path, std::vector* buffer = nullptr, bool isBundle = false); + bool LoadScript(const std::string& path, uint8_t* buffer, size_t len, bool isBundle); bool StartDebugger(bool needBreakPoint, uint32_t instanceId); void StopDebugger(); - bool LoadScript(const std::string& path, uint8_t *buffer, size_t len, bool isBundle); NativeEngine* GetNativeEnginePointer() const; panda::ecmascript::EcmaVM* GetEcmaVm() const; @@ -154,8 +154,9 @@ private: void InitTimerModule(); void InitWorkerModule(const Options& options); void ReInitJsEnvImpl(const Options& options); + void PostPreload(const Options& options); + void LoadAotFile(const Options& options); }; -} // namespace AbilityRuntime -} // namespace OHOS - -#endif // OHOS_ABILITY_RUNTIME_JS_RUNTIME_H +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_JS_RUNTIME_H diff --git a/js_environment/frameworks/js_environment/src/js_environment.cpp b/js_environment/frameworks/js_environment/src/js_environment.cpp index e2feb469c6b00492718e3e3d88a9afb49616550d..89ba50a49763a0839eb0077e0e77a3d1a07a43f9 100644 --- a/js_environment/frameworks/js_environment/src/js_environment.cpp +++ b/js_environment/frameworks/js_environment/src/js_environment.cpp @@ -80,8 +80,13 @@ void JsEnvironment::InitTimerModule() void JsEnvironment::InitWorkerModule(std::shared_ptr workerInfo) { - if (impl_ != nullptr && engine_ != nullptr) { - impl_->InitWorkerModule(*engine_, workerInfo); + if (engine_ == nullptr) { + JSENV_LOG_E("Invalid native engine."); + return; + } + + if (impl_ != nullptr) { + impl_->InitWorkerModule(engine_, workerInfo); } } @@ -120,17 +125,19 @@ void JsEnvironment::InitSourceMap(const std::shared_ptrTranslateBySourceMap(rawStack); }; engine_->RegisterTranslateBySourceMap(translateBySourceMapFunc); + auto translateUrlBySourceMapFunc = [&](std::string& url, int& line, int& column) { return sourceMapOperator_->TranslateUrlPositionBySourceMap(url, line, column); }; engine_->RegisterSourceMapTranslateCallback(translateUrlBySourceMapFunc); } -void JsEnvironment::RegisterUncaughtExceptionHandler(JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo) +void JsEnvironment::RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo) { if (engine_ == nullptr) { JSENV_LOG_E("Invalid Native Engine."); @@ -157,26 +164,31 @@ bool JsEnvironment::LoadScript(const std::string& path, std::vector* bu bool JsEnvironment::StartDebugger(const char* libraryPath, bool needBreakPoint, uint32_t instanceId) { - if (vm_ != nullptr) { - panda::JSNApi::DebugOption debugOption = {libraryPath, needBreakPoint}; - auto debuggerPostTask = [weak = weak_from_this()](std::function&& task) { - auto jsEnv = weak.lock(); - if (jsEnv == nullptr) { - JSENV_LOG_E("JsEnv is invalid."); - return; - } - jsEnv->PostTask(task); - }; - return panda::JSNApi::StartDebugger(vm_, debugOption, instanceId, debuggerPostTask); + if (vm_ == nullptr) { + JSENV_LOG_E("Invalid vm."); + return false; } - return false; + + panda::JSNApi::DebugOption debugOption = {libraryPath, needBreakPoint}; + auto debuggerPostTask = [weak = weak_from_this()](std::function&& task) { + auto jsEnv = weak.lock(); + if (jsEnv == nullptr) { + JSENV_LOG_E("JsEnv is invalid."); + return; + } + jsEnv->PostTask(task); + }; + return panda::JSNApi::StartDebugger(vm_, debugOption, instanceId, debuggerPostTask); } void JsEnvironment::StopDebugger() { - if (vm_ != nullptr) { - (void)panda::JSNApi::StopDebugger(vm_); + if (vm_ == nullptr) { + JSENV_LOG_E("Invalid vm."); + return; } + + (void)panda::JSNApi::StopDebugger(vm_); } void JsEnvironment::InitConsoleModule() @@ -216,9 +228,14 @@ void JsEnvironment::DeInitLoop() } } -bool JsEnvironment::LoadScript(const std::string& path, uint8_t *buffer, size_t len, bool isBundle) +bool JsEnvironment::LoadScript(const std::string& path, uint8_t* buffer, size_t len, bool isBundle) { - return engine_->RunScriptBuffer(path.c_str(), buffer, len, isBundle); + if (engine_ == nullptr) { + JSENV_LOG_E("Invalid Native Engine."); + return false; + } + + return engine_->RunScriptBuffer(path, buffer, len, isBundle); } void JsEnvironment::StartProfiler(const char* libraryPath, uint32_t instanceId, PROFILERTYPE profiler, @@ -228,6 +245,7 @@ void JsEnvironment::StartProfiler(const char* libraryPath, uint32_t instanceId, JSENV_LOG_E("Invalid vm."); return; } + auto debuggerPostTask = [weak = weak_from_this()](std::function&& task) { auto jsEnv = weak.lock(); if (jsEnv == nullptr) { @@ -254,9 +272,10 @@ void JsEnvironment::ReInitJsEnvImpl(std::unique_ptr impl) void JsEnvironment::SetModuleLoadChecker(const std::shared_ptr& moduleCheckerDelegate) { if (engine_ == nullptr) { - JSENV_LOG_E("SetModuleLoadChecker failed, engine_ is null"); + JSENV_LOG_E("Invalid native engine."); return; } + engine_->SetModuleLoadChecker(moduleCheckerDelegate); } } // namespace JsEnv diff --git a/js_environment/interfaces/inner_api/js_environment.h b/js_environment/interfaces/inner_api/js_environment.h index 4978cb975357e9b8dc4ab5fc6bcc105146bfe979..d0605f3ecdb7ec7c2ed920ce3beca00619d348af 100644 --- a/js_environment/interfaces/inner_api/js_environment.h +++ b/js_environment/interfaces/inner_api/js_environment.h @@ -64,7 +64,8 @@ public: void RemoveTask(const std::string& name); - void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo); + void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo); + bool LoadScript(const std::string& path, std::vector* buffer = nullptr, bool isBundle = false); bool StartDebugger(const char* libraryPath, bool needBreakPoint, uint32_t instanceId); @@ -77,7 +78,7 @@ public: void DeInitLoop(); - bool LoadScript(const std::string& path, uint8_t *buffer, size_t len, bool isBundle); + bool LoadScript(const std::string& path, uint8_t* buffer, size_t len, bool isBundle); void StartProfiler(const char* libraryPath, uint32_t instanceId, PROFILERTYPE profiler, int32_t interval); diff --git a/js_environment/interfaces/inner_api/js_environment_impl.h b/js_environment/interfaces/inner_api/js_environment_impl.h index d63c9f3dca4f26d20210588157399ea3cc022292..8b9e91133400ae1d0b4aeacc0ed9469eb83a5e63 100644 --- a/js_environment/interfaces/inner_api/js_environment_impl.h +++ b/js_environment/interfaces/inner_api/js_environment_impl.h @@ -33,6 +33,7 @@ struct WorkerInfo { std::string hapPath; bool isStageModel = true; }; + class JsEnvironmentImpl { public: JsEnvironmentImpl() {} @@ -46,13 +47,13 @@ public: virtual void InitTimerModule(NativeEngine* engine) = 0; - virtual void InitConsoleModule(NativeEngine *engine) = 0; + virtual void InitConsoleModule(NativeEngine* engine) = 0; - virtual bool InitLoop(NativeEngine *engine) = 0; + virtual bool InitLoop(NativeEngine* engine) = 0; - virtual void DeInitLoop(NativeEngine *engine) = 0; + virtual void DeInitLoop(NativeEngine* engine) = 0; - virtual void InitWorkerModule(NativeEngine& engine, std::shared_ptr workerInfo) = 0; + virtual void InitWorkerModule(NativeEngine* engine, std::shared_ptr workerInfo) = 0; virtual void InitSyscapModule() = 0; }; diff --git a/test/unittest/runtime_test/ohos_js_environment_test.cpp b/test/unittest/runtime_test/ohos_js_environment_test.cpp index f96b1a67e26585807221456295eda1906af9e3bc..21d3a40602cb4ba2d229a237819fcd2a8c4b789b 100644 --- a/test/unittest/runtime_test/ohos_js_environment_test.cpp +++ b/test/unittest/runtime_test/ohos_js_environment_test.cpp @@ -129,8 +129,9 @@ HWTEST_F(OHOSJsEnvironmentTest, InitWorkerModule_0100, TestSize.Level0) auto jsEnvImpl = std::make_shared(); ASSERT_NE(jsEnvImpl, nullptr); AbilityRuntime::Runtime::Options options; - auto runtime = AbilityRuntime::Runtime::Create(options); - auto& jsEngine = (static_cast(*runtime)).GetNativeEngine(); + auto runtime = AbilityRuntime::JsRuntime::Create(options); + ASSERT_NE(runtime, nullptr); + auto jsEngine = runtime->GetNativeEnginePointer(); std::shared_ptr workerInfo = std::make_shared(); jsEnvImpl->InitWorkerModule(jsEngine, workerInfo); }