diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index 06790af67fda907007dcd559f4e5e18e523089a6..443c901be8233f7290e573cf490f154da06681fd 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 d57f06b4da12be8848206acb691ed71097a53482..f3b47b3ee5e5e2e62398adac5d24ecd6d7fd5e96 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 889026305b78fd7af51548d8ea1003e73208b60a..ec02904b98eea61329a8b69f6d90f2bb04b735b3 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 5fea783b7d56df609d3df0b0fd76a36d9832160b..4fde9c93cd43db115b9066f9ff894959dbea2f47 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 e22a81e31fa77f0777c836c93c9bed7f316f9ae7..8f359600ed78ef1c1b84662946124c650148733b 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