diff --git a/frameworks/native/appkit/app/main_thread.cpp b/frameworks/native/appkit/app/main_thread.cpp index db5924fbbc..1458da1193 100644 --- a/frameworks/native/appkit/app/main_thread.cpp +++ b/frameworks/native/appkit/app/main_thread.cpp @@ -2178,7 +2178,15 @@ void MainThread::InitUncatchableTask(JsEnv::UncatchableTask &uncatchableTask, co ProcessExit(info); ErrorObject appExecErrorObj = { errorObject.name, errorObject.message, errorObject.stack}; - auto mainEnv = (static_cast(*appThread->application_->GetRuntime())).GetNapiEnv(); + napi_env mainEnv = nullptr; + auto &runtime = appThread->application_->GetRuntime(); + if (runtime->GetLanguage() == AbilityRuntime::Runtime::Language::ETS) { + auto& etsRuntime = static_cast(*runtime); + auto& jsRuntime = static_cast(*etsRuntime.GetJsRuntime()); + mainEnv = jsRuntime.GetNapiEnv(); + } else { + mainEnv = (static_cast(*runtime)).GetNapiEnv(); + } ApplicationDataManager::ExceptionParams params = {env, mainEnv, exception, summary, isUncatchable}; if (ApplicationDataManager::NotifyUncaughtException(params, appExecErrorObj)) { return; @@ -4460,11 +4468,11 @@ void MainThread::RegisterHybridException(const std::unique_ptr(*jsRuntime)).RegisterUncaughtExceptionHandler( - uncaughtExceptionInfo); + uncaughtExceptionInfo, true); JsEnv::UncatchableTask uncatchableTask; InitUncatchableTask(uncatchableTask, uncatchableTaskInfo, true); (static_cast(*jsRuntime)).RegisterUncatchableExceptionHandler( - uncatchableTask); + uncatchableTask, true); } } } diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index 3112d579b1..41ea51ca92 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -1585,18 +1585,19 @@ void JsRuntime::UpdateModuleNameAndAssetPath(const std::string& moduleName) panda::JSNApi::SetModuleName(vm, moduleName_); } -void JsRuntime::RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo) +void JsRuntime::RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo, + bool isStatic) { HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); CHECK_POINTER(jsEnv_); - jsEnv_->RegisterUncaughtExceptionHandler(uncaughtExceptionInfo); + jsEnv_->RegisterUncaughtExceptionHandler(uncaughtExceptionInfo, isStatic); } -void JsRuntime::RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask) +void JsRuntime::RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask, bool isStatic) { HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); CHECK_POINTER(jsEnv_); - jsEnv_->RegisterUncatchableExceptionHandler(uncatchableTask); + jsEnv_->RegisterUncatchableExceptionHandler(uncatchableTask, isStatic); } void JsRuntime::RegisterQuickFixQueryFunc(const std::map& moduleAndPath) diff --git a/interfaces/inner_api/runtime/include/js_runtime.h b/interfaces/inner_api/runtime/include/js_runtime.h index eb8c5af402..159ee30f83 100644 --- a/interfaces/inner_api/runtime/include/js_runtime.h +++ b/interfaces/inner_api/runtime/include/js_runtime.h @@ -127,8 +127,9 @@ public: bool LoadRepairPatch(const std::string& hqfFile, const std::string& hapPath) override; bool UnLoadRepairPatch(const std::string& hqfFile) override; bool NotifyHotReloadPage() override; - void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo); - void RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask); + void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo, + bool isStatic = false); + void RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask, bool isStatic = false); 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, const std::string& srcEntrance = ""); diff --git a/js_environment/frameworks/js_environment/src/js_environment.cpp b/js_environment/frameworks/js_environment/src/js_environment.cpp index 2ee6522893..ded4e885fe 100644 --- a/js_environment/frameworks/js_environment/src/js_environment.cpp +++ b/js_environment/frameworks/js_environment/src/js_environment.cpp @@ -153,7 +153,8 @@ void JsEnvironment::InitSourceMap(const std::shared_ptrRegisterSourceMapTranslateCallback(translateUrlBySourceMapFunc); } -void JsEnvironment::RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo) +void JsEnvironment::RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo, + bool isStatic) { if (engine_ == nullptr) { TAG_LOGE(AAFwkTag::JSENV, "Invalid Native Engine"); @@ -161,10 +162,10 @@ void JsEnvironment::RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExcept } engine_->RegisterNapiUncaughtExceptionHandler(NapiUncaughtExceptionCallback(uncaughtExceptionInfo.uncaughtTask, - sourceMapOperator_, reinterpret_cast(engine_))); + sourceMapOperator_, reinterpret_cast(engine_), isStatic)); } -void JsEnvironment::RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask) +void JsEnvironment::RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask, bool isStatic) { if (uncatchableTask == nullptr) { return; @@ -172,12 +173,12 @@ void JsEnvironment::RegisterUncatchableExceptionHandler(const JsEnv::Uncatchable std::weak_ptr weakThis = shared_from_this(); panda::JSNApi::RegisterUncatchableErrorHandler(const_cast(engine_->GetEcmaVm()), - [weakThis, uncatchableTask] (auto& trycatch) { + [weakThis, uncatchableTask, isStatic] (auto& trycatch) { auto sharedThis = weakThis.lock(); if (sharedThis) { void* env = trycatch.GetEnv(); NapiUncaughtExceptionCallback napiUncaughtExceptionCallback(uncatchableTask, - sharedThis->sourceMapOperator_, reinterpret_cast(env)); + sharedThis->sourceMapOperator_, reinterpret_cast(env), isStatic); napiUncaughtExceptionCallback(trycatch); } else { TAG_LOGE(AAFwkTag::JSENV, "JsEnvironment has been destructed."); diff --git a/js_environment/frameworks/js_environment/src/uncaught_exception_callback.cpp b/js_environment/frameworks/js_environment/src/uncaught_exception_callback.cpp index 21fd5b6f48..c6dad33bc5 100644 --- a/js_environment/frameworks/js_environment/src/uncaught_exception_callback.cpp +++ b/js_environment/frameworks/js_environment/src/uncaught_exception_callback.cpp @@ -85,7 +85,10 @@ void NapiUncaughtExceptionCallback::operator()(panda::TryCatch& trycatch) void NapiUncaughtExceptionCallback::CallbackTask(napi_value& obj) { - HandleAndLogIfNotJsError(obj); + // Static objects cannot be dynamically serialized. + if (!isStatic_) { + HandleAndLogIfNotJsError(obj); + } std::string errorMsg = GetNativeStrFromJsTaggedObj(obj, "message"); AppendExtraInfo(errorMsg); std::string errorName = GetNativeStrFromJsTaggedObj(obj, "name"); diff --git a/js_environment/interfaces/inner_api/js_environment.h b/js_environment/interfaces/inner_api/js_environment.h index 33f4c3c536..7116773ffc 100644 --- a/js_environment/interfaces/inner_api/js_environment.h +++ b/js_environment/interfaces/inner_api/js_environment.h @@ -74,9 +74,11 @@ public: void RemoveTask(const std::string& name); - void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo); + void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo, + bool isStatic = false); - void RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask); + void RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask, + bool isStatic = false); bool LoadScript(const std::string& path, std::vector* buffer = nullptr, bool isBundle = false); diff --git a/js_environment/interfaces/inner_api/uncaught_exception_callback.h b/js_environment/interfaces/inner_api/uncaught_exception_callback.h index 399bd54e0b..8bd3731cfd 100644 --- a/js_environment/interfaces/inner_api/uncaught_exception_callback.h +++ b/js_environment/interfaces/inner_api/uncaught_exception_callback.h @@ -42,8 +42,8 @@ public: NapiUncaughtExceptionCallback( std::function uncaughtTask, - std::shared_ptr sourceMapOperator, napi_env env) - : uncaughtTask_(uncaughtTask), sourceMapOperator_(sourceMapOperator), env_(env) + std::shared_ptr sourceMapOperator, napi_env env, bool isStatic = false) + : uncaughtTask_(uncaughtTask), sourceMapOperator_(sourceMapOperator), env_(env), isStatic_(isStatic) {} ~NapiUncaughtExceptionCallback() = default; @@ -71,6 +71,7 @@ private: napi_value exception)> uncaughtTask_; std::shared_ptr sourceMapOperator_ = nullptr; napi_env env_ = nullptr; + bool isStatic_; void AppendStackTrace(const std::string& errorStack, std::string& summary);