From c9ddbc1e235173916f75dc958f1f148bbb5b4f54 Mon Sep 17 00:00:00 2001 From: wwwcg Date: Tue, 10 Sep 2024 16:44:05 +0800 Subject: [PATCH] fix(ios): resolve jsc_try_catch invalid issue (#4021) --- .../js/include/driver/napi/jsc/jsc_try_catch.h | 1 - driver/js/src/napi/jsc/jsc_try_catch.cc | 16 +++++++++------- framework/ios/base/bridge/HippyBridge.mm | 10 +++++++--- modules/ios/base/HippyAssert.m | 1 + 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/driver/js/include/driver/napi/jsc/jsc_try_catch.h b/driver/js/include/driver/napi/jsc/jsc_try_catch.h index b4ece21d608..77c270596c2 100644 --- a/driver/js/include/driver/napi/jsc/jsc_try_catch.h +++ b/driver/js/include/driver/napi/jsc/jsc_try_catch.h @@ -46,7 +46,6 @@ class JSCTryCatch : public TryCatch { virtual footstone::string_view GetExceptionMessage(); private: - std::shared_ptr exception_; bool is_verbose_; bool is_rethrow_; }; diff --git a/driver/js/src/napi/jsc/jsc_try_catch.cc b/driver/js/src/napi/jsc/jsc_try_catch.cc index f0b8465d1de..a85052b2dc8 100644 --- a/driver/js/src/napi/jsc/jsc_try_catch.cc +++ b/driver/js/src/napi/jsc/jsc_try_catch.cc @@ -21,7 +21,6 @@ */ #include "driver/napi/jsc/jsc_try_catch.h" - #include "driver/napi/jsc/jsc_ctx.h" #include "driver/napi/jsc/jsc_ctx_value.h" @@ -45,7 +44,6 @@ JSCTryCatch::~JSCTryCatch() { if (HasCaught()) { if (is_rethrow_ || is_verbose_) { std::shared_ptr ctx = std::static_pointer_cast(ctx_); - ctx->SetException(exception_); if (is_rethrow_) { ctx->SetExceptionHandled(false); } else { @@ -61,21 +59,24 @@ void JSCTryCatch::ReThrow() { bool JSCTryCatch::HasCaught() { if (enable_) { - return !!exception_; + std::shared_ptr ctx = std::static_pointer_cast(ctx_); + return !!ctx->GetException(); } return false; } bool JSCTryCatch::CanContinue() { if (enable_) { - return !exception_; + std::shared_ptr ctx = std::static_pointer_cast(ctx_); + return !ctx->GetException(); } return true; } bool JSCTryCatch::HasTerminated() { if (enable_) { - return !!exception_; + std::shared_ptr ctx = std::static_pointer_cast(ctx_); + return !!ctx->GetException(); } return false; } @@ -89,13 +90,14 @@ void JSCTryCatch::SetVerbose(bool is_verbose) { } std::shared_ptr JSCTryCatch::Exception() { - return exception_; + std::shared_ptr ctx = std::static_pointer_cast(ctx_); + return ctx->GetException(); } string_view JSCTryCatch::GetExceptionMessage() { if (enable_) { std::shared_ptr ctx = std::static_pointer_cast(ctx_); - return ctx->GetExceptionMessage(exception_); + return ctx->GetExceptionMessage(ctx->GetException()); } return ""; } diff --git a/framework/ios/base/bridge/HippyBridge.mm b/framework/ios/base/bridge/HippyBridge.mm index fcf3d33117b..a2d738277ba 100644 --- a/framework/ios/base/bridge/HippyBridge.mm +++ b/framework/ios/base/bridge/HippyBridge.mm @@ -710,15 +710,19 @@ - (void)executeJSCode:(NSData *)script return; } HippyAssert(self.javaScriptExecutor, @"js executor must not be null"); - __weak HippyBridge *weakSelf = self; + __weak __typeof(self)weakSelf = self; [self.javaScriptExecutor executeApplicationScript:script sourceURL:sourceURL onComplete:^(id result ,NSError *error) { - HippyBridge *strongSelf = weakSelf; + __strong __typeof(weakSelf)strongSelf = weakSelf; if (!strongSelf || ![strongSelf isValid]) { completion(result, error); return; } if (error) { - [strongSelf stopLoadingWithError:error scriptSourceURL:sourceURL]; + HippyLogError(@"ExecuteApplicationScript Error! %@", error.description); + HippyExecuteOnMainQueue(^{ + __strong __typeof(weakSelf)strongSelf = weakSelf; + [strongSelf stopLoadingWithError:error scriptSourceURL:sourceURL]; + }); } completion(result, error); }]; diff --git a/modules/ios/base/HippyAssert.m b/modules/ios/base/HippyAssert.m index b9dbc775df0..39b9139bcf5 100644 --- a/modules/ios/base/HippyAssert.m +++ b/modules/ios/base/HippyAssert.m @@ -96,6 +96,7 @@ void HippyFatal(NSError *error) { } [NSException raise:name format:@"%@", message]; } @catch (NSException *e) { + // no op } #endif //#ifdef DEBUG }