From 2f4afa40d80f1a7f38428aecf0d946d5dfdc94f1 Mon Sep 17 00:00:00 2001 From: raul Date: Fri, 31 Mar 2023 14:30:31 +0800 Subject: [PATCH] vm destroy notify Signed-off-by: raul Change-Id: Iea8c9608976e2816190afa1f9fd5f7edd7570132 --- frameworks/native/runtime/js_runtime.cpp | 7 +++++++ interfaces/inner_api/runtime/include/js_runtime.h | 1 + interfaces/inner_api/runtime/include/runtime.h | 3 +++ .../frameworks/js_environment/src/js_environment.cpp | 4 ++++ js_environment/interfaces/inner_api/js_environment.h | 8 ++++++++ 5 files changed, 23 insertions(+) diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index 06790af67fd..443c901be82 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -954,5 +954,12 @@ void JsRuntime::UpdateModuleNameAndAssetPath(const std::string& moduleName) panda::JSNApi::SetAssetPath(vm, path); panda::JSNApi::SetModuleName(vm, moduleName_); } + +void JsRuntime::RegisterVMDestroyCallback(const VMDestroyCallback &callback) +{ + CHECK_POINTER(jsEnv_); + HILOG_INFO("Register VMDestroyCallback success"); + jsEnv_->RegisterVMDestroyCallback(callback); +} } // namespace AbilityRuntime } // namespace OHOS diff --git a/interfaces/inner_api/runtime/include/js_runtime.h b/interfaces/inner_api/runtime/include/js_runtime.h index d57f06b4da1..f3b47b3ee5e 100644 --- a/interfaces/inner_api/runtime/include/js_runtime.h +++ b/interfaces/inner_api/runtime/include/js_runtime.h @@ -100,6 +100,7 @@ public: panda::ecmascript::EcmaVM* GetEcmaVm() const; void UpdateModuleNameAndAssetPath(const std::string& moduleName); + void RegisterVMDestroyCallback(const VMDestroyCallback &callback) override; private: void FinishPreload() override; diff --git a/interfaces/inner_api/runtime/include/runtime.h b/interfaces/inner_api/runtime/include/runtime.h index 889026305b7..ec02904b98e 100644 --- a/interfaces/inner_api/runtime/include/runtime.h +++ b/interfaces/inner_api/runtime/include/runtime.h @@ -34,6 +34,8 @@ class EventRunner; namespace AbilityRuntime { class Runtime { public: + using VMDestroyCallback = std::function; + enum class Language { JS = 0, }; @@ -77,6 +79,7 @@ public: virtual bool NotifyHotReloadPage() = 0; virtual bool UnLoadRepairPatch(const std::string& patchFile) = 0; virtual void UpdateExtensionType(int32_t extensionType) = 0; + virtual void RegisterVMDestroyCallback(const VMDestroyCallback &callback) {}; Runtime(const Runtime&) = delete; Runtime(Runtime&&) = delete; diff --git a/js_environment/frameworks/js_environment/src/js_environment.cpp b/js_environment/frameworks/js_environment/src/js_environment.cpp index 5fea783b7d5..4fde9c93cd4 100644 --- a/js_environment/frameworks/js_environment/src/js_environment.cpp +++ b/js_environment/frameworks/js_environment/src/js_environment.cpp @@ -38,6 +38,10 @@ JsEnvironment::~JsEnvironment() } if (vm_ != nullptr) { + if (vmDestroyCallback_) { + JSENV_LOG_I("VM destroy callback start"); + vmDestroyCallback_(); + } panda::JSNApi::DestroyJSVM(vm_); vm_ = nullptr; } diff --git a/js_environment/interfaces/inner_api/js_environment.h b/js_environment/interfaces/inner_api/js_environment.h index e22a81e31fa..8f359600ed7 100644 --- a/js_environment/interfaces/inner_api/js_environment.h +++ b/js_environment/interfaces/inner_api/js_environment.h @@ -26,6 +26,8 @@ namespace JsEnv { class JsEnvironmentImpl; class JsEnvironment final { public: + using VMDestroyCallback = std::function; + JsEnvironment() {} explicit JsEnvironment(std::unique_ptr impl); ~JsEnvironment(); @@ -59,10 +61,16 @@ public: void RemoveTask(const std::string& name); bool LoadScript(const std::string& path, std::vector* buffer = nullptr, bool isBundle = false); + + void RegisterVMDestroyCallback(const VMDestroyCallback &callback) + { + vmDestroyCallback_ = callback; + } private: std::unique_ptr impl_ = nullptr; NativeEngine* engine_ = nullptr; panda::ecmascript::EcmaVM* vm_ = nullptr; + VMDestroyCallback vmDestroyCallback_; }; } // namespace JsEnv } // namespace OHOS -- Gitee