Skip to content

Commit 3fc7225

Browse files
committed
fix(ios): add exception handle process for jsc (#3976)
* fix(ios): add exception handle process for jsc * chore(ios): remove unnecessary exception check log
1 parent 773a21f commit 3fc7225

File tree

5 files changed

+67
-9
lines changed

5 files changed

+67
-9
lines changed

driver/js/include/driver/scope.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,24 @@ class Scope : public std::enable_shared_from_this<Scope> {
166166
inline std::any GetTurbo() { return turbo_; }
167167
inline void SetTurbo(std::any turbo) { turbo_ = turbo; }
168168
inline std::weak_ptr<Engine> GetEngine() { return engine_; }
169+
inline std::unique_ptr<RegisterMap>& GetRegisterMap() { return extra_function_map_; }
170+
171+
inline bool RegisterExtraCallback(const std::string& key, RegisterFunction func) {
172+
if (!func) {
173+
return false;
174+
}
175+
(*extra_function_map_)[key] = std::move(func);
176+
return true;
177+
}
178+
179+
inline bool GetExtraCallback(const std::string& key, RegisterFunction& outFunc) const {
180+
auto it = extra_function_map_->find(key);
181+
if (it != extra_function_map_->end()) {
182+
outFunc = it->second;
183+
return true;
184+
}
185+
return false;
186+
}
169187

170188
inline std::any GetClassTemplate(const string_view& name) {
171189
auto engine = engine_.lock();
@@ -466,6 +484,7 @@ class Scope : public std::enable_shared_from_this<Scope> {
466484
std::any bridge_;
467485
std::any turbo_;
468486
std::string name_;
487+
std::unique_ptr<RegisterMap> extra_function_map_; // store some callback functions
469488
uint32_t call_ui_function_callback_id_;
470489
std::unordered_map<uint32_t, std::shared_ptr<CtxValue>> call_ui_function_callback_holder_;
471490
std::unordered_map<uint32_t, std::unordered_map<std::string, std::unordered_map<uint64_t, std::shared_ptr<CtxValue>>>>

driver/js/src/modules/timer_module.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ std::shared_ptr<hippy::napi::CtxValue> TimerModule::Start(
216216
}
217217
std::shared_ptr<hippy::napi::Ctx> context = scope->GetContext();
218218
context->CallFunction(function, context->GetGlobalObject(), 0, nullptr);
219+
220+
#if defined(JS_JSC)
221+
// exception check for jsc
222+
RegisterFunction func;
223+
if (scope->GetExtraCallback(kAsyncTaskEndKey, func)) {
224+
func(nullptr);
225+
}
226+
#endif /* defined(JS_JSC) */
227+
219228
if (!repeat) {
220229
timer_map->erase(task_id);
221230
}

driver/js/src/napi/jsc/jsc_ctx.cc

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,6 @@ std::shared_ptr<CtxValue> JSCCtx::DefineClass(const string_view& name,
359359
JSObjectCallAsFunction(context_, define_property, object, 3, values, &exception);
360360
if (exception) {
361361
SetException(std::make_shared<JSCCtxValue>(context_, exception));
362-
FOOTSTONE_LOG(ERROR) << GetExceptionMessage(exception_);
363362
return nullptr;
364363
}
365364
}
@@ -895,23 +894,20 @@ std::shared_ptr<CtxValue> JSCCtx::CallFunction(const std::shared_ptr<CtxValue>&
895894
auto function_object = JSValueToObject(context_, function_value->value_, &exception);
896895
if (exception) {
897896
SetException(std::make_shared<JSCCtxValue>(context_, exception));
898-
FOOTSTONE_LOG(ERROR) << GetExceptionMessage(exception_);
899897
return nullptr;
900898
}
901899

902900
auto receiver_value = std::static_pointer_cast<JSCCtxValue>(receiver);
903901
auto receiver_object = JSValueToObject(context_, receiver_value->value_, &exception);
904902
if (exception) {
905903
SetException(std::make_shared<JSCCtxValue>(context_, exception));
906-
FOOTSTONE_LOG(ERROR) << GetExceptionMessage(exception_);
907904
return nullptr;
908905
}
909906

910907
if (argc <= 0) {
911908
auto ret_value_ref = JSObjectCallAsFunction(context_, function_object, receiver_object, 0, nullptr, &exception);
912909
if (exception) {
913910
SetException(std::make_shared<JSCCtxValue>(context_, exception));
914-
FOOTSTONE_LOG(ERROR) << GetExceptionMessage(exception_);
915911
return nullptr;
916912
}
917913
return std::make_shared<JSCCtxValue>(context_, ret_value_ref);
@@ -926,7 +922,6 @@ std::shared_ptr<CtxValue> JSCCtx::CallFunction(const std::shared_ptr<CtxValue>&
926922
auto ret_value_ref = JSObjectCallAsFunction(context_, function_object, receiver_object, argc, values, &exception);
927923
if (exception) {
928924
SetException(std::make_shared<JSCCtxValue>(context_, exception));
929-
FOOTSTONE_LOG(ERROR) << GetExceptionMessage(exception_);
930925
return nullptr;
931926
}
932927

@@ -939,7 +934,7 @@ std::shared_ptr<CtxValue> JSCCtx::CallFunction(const std::shared_ptr<CtxValue>&
939934

940935
string_view JSCCtx::GetExceptionMessage(const std::shared_ptr<CtxValue>& exception) {
941936
if (!exception) {
942-
return string_view();
937+
return string_view("");
943938
}
944939

945940
std::shared_ptr<CtxValue> msg_obj = CopyNamedProperty(exception, string_view(kMessageStr, ARRAY_SIZE(kMessageStr) - 1));
@@ -1142,7 +1137,6 @@ std::shared_ptr<CtxValue> JSCCtx::RunScript(const string_view& data,
11421137

11431138
if (exception) {
11441139
SetException(std::make_shared<JSCCtxValue>(context_, exception));
1145-
FOOTSTONE_LOG(ERROR) << GetExceptionMessage(exception_);
11461140
return nullptr;
11471141
}
11481142

driver/js/src/scope.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Scope::Scope(std::weak_ptr<Engine> engine,
126126
context_(nullptr),
127127
name_(std::move(name)),
128128
call_ui_function_callback_id_(0),
129+
extra_function_map_(std::make_unique<RegisterMap>()),
129130
performance_(std::make_shared<Performance>()) {}
130131

131132
Scope::~Scope() {

framework/ios/base/executors/HippyJSExecutor.mm

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
constexpr char kGlobalKey[] = "global";
7676
constexpr char kHippyKey[] = "Hippy";
7777
static NSString * const kHippyNativeGlobalKey = @"__HIPPYNATIVEGLOBAL__";
78-
78+
static const char * kHippyExceptionEventName = "uncaughtException";
7979

8080

8181
@interface HippyJSExecutor () {
@@ -102,8 +102,18 @@ - (void)setup {
102102
const char *pName = [self.enginekey UTF8String] ?: "";
103103
auto scope = engine->GetEngine()->CreateScope(pName);
104104

105+
__weak __typeof(self)weakSelf = self;
106+
hippy::base::RegisterFunction taskEndCB = [weakSelf](void *) {
107+
@autoreleasepool {
108+
HippyJSExecutor *strongSelf = weakSelf;
109+
if (strongSelf) {
110+
handleJsExcepiton(strongSelf->_pScope);
111+
}
112+
}
113+
};
114+
scope->RegisterExtraCallback(hippy::kAsyncTaskEndKey, taskEndCB);
115+
105116
dispatch_semaphore_t scopeSemaphore = dispatch_semaphore_create(0);
106-
__weak HippyJSExecutor *weakSelf = self;
107117
footstone::TimePoint startPoint = footstone::TimePoint::SystemNow();
108118
engine->GetEngine()->GetJsTaskRunner()->PostTask([weakSelf, scopeSemaphore, startPoint](){
109119
@autoreleasepool {
@@ -727,4 +737,29 @@ - (NSString *)completeWSURLWithBridge:(HippyBridge *)bridge {
727737
return [devInfo assembleFullWSURLWithClientId:clientId contextName:bridge.contextName];
728738
}
729739

740+
741+
#pragma mark - Exception Handle
742+
743+
static void handleJsExcepiton(std::shared_ptr<hippy::Scope> scope) {
744+
if (!scope) {
745+
return;
746+
}
747+
std::shared_ptr<hippy::napi::JSCCtx> context = std::static_pointer_cast<hippy::napi::JSCCtx>(scope->GetContext());
748+
std::shared_ptr<hippy::napi::JSCCtxValue> exception = std::static_pointer_cast<hippy::napi::JSCCtxValue>(context->GetException());
749+
if (exception) {
750+
// if native does not handled, rethrow to js
751+
if (!context->IsExceptionHandled()) {
752+
hippy::vm::VM::HandleException(context, kHippyExceptionEventName, exception);
753+
}
754+
string_view exceptionStrView = context->GetExceptionMessage(exception);
755+
auto errU8Str = StringViewUtils::ConvertEncoding(exceptionStrView, string_view::Encoding::Utf8).utf8_value();
756+
std::string errStr = StringViewUtils::ToStdString(errU8Str);
757+
NSError *error = HippyErrorWithMessage([NSString stringWithUTF8String:errStr.c_str()]);
758+
HippyFatal(error);
759+
context->SetException(nullptr);
760+
context->SetExceptionHandled(true);
761+
}
762+
}
763+
764+
730765
@end

0 commit comments

Comments
 (0)