Skip to content

Commit

Permalink
fix(core): fix uncaughtException bug
Browse files Browse the repository at this point in the history
  • Loading branch information
churchill-zhang authored and zoomchan-cxj committed Apr 3, 2023
1 parent 51bb33a commit 00a7338
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
24 changes: 13 additions & 11 deletions android/sdk/src/main/jni/src/bridge/entry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ constexpr char kCurDir[] = "__HIPPYCURDIR__";
std::vector<intptr_t> external_references{};

void HandleUncaughtJsError(v8::Local<v8::Message> message,
v8::Local<v8::Value> error) {
v8::Local<v8::Value> data) {
TDF_BASE_DLOG(INFO) << "HandleUncaughtJsError begin";

if (error.IsEmpty()) {
TDF_BASE_DLOG(ERROR) << "HandleUncaughtJsError error is empty";
if (message.IsEmpty()) {
TDF_BASE_DLOG(ERROR) << "HandleUncaughtJsError message is empty";
return;
}

Expand All @@ -97,17 +97,19 @@ void HandleUncaughtJsError(v8::Local<v8::Message> message,
if (!context) {
return;
}
std::shared_ptr<hippy::napi::V8Ctx> ctx = std::static_pointer_cast<hippy::napi::V8Ctx>(context);
auto ctx = std::static_pointer_cast<hippy::napi::V8Ctx>(context);
auto description = ctx->GetMsgDesc(message);
auto stack = ctx->GetStackInfo(message);

TDF_BASE_LOG(ERROR) << "HandleUncaughtJsError, runtime_id = "
<< runtime->GetId()
<< ", desc = "
<< ctx->GetMsgDesc(message)
<< ", stack = " << ctx->GetStackInfo(message);
ExceptionHandler::ReportJsException(runtime, ctx->GetMsgDesc(message),
ctx->GetStackInfo(message));
ctx->HandleUncaughtException(
std::make_shared<hippy::napi::V8CtxValue>(isolate, error));

<< description
<< ", stack = "
<< stack;
ExceptionHandler::ReportJsException(runtime, description, stack);
auto error = ctx->CreateError(message);
ctx->HandleUncaughtException(error);
TDF_BASE_DLOG(INFO) << "HandleUncaughtJsError end";
}

Expand Down
3 changes: 2 additions & 1 deletion core/include/core/napi/v8/v8_ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ class V8Ctx : public Ctx {

std::string GetSerializationBuffer(const std::shared_ptr<CtxValue>& value, std::string& reused_buffer);
unicode_string_view ToStringView(v8::Local<v8::String> str) const;
unicode_string_view GetMsgDesc(v8::Local<v8::Message> message);
unicode_string_view GetMsgDesc(v8::Local<v8::Message> message) const;
unicode_string_view GetStackInfo(v8::Local<v8::Message> message) const;
unicode_string_view GetStackTrace(v8::Local<v8::StackTrace> trace) const;
std::shared_ptr<CtxValue> CreateError(v8::Local<v8::Message> message) const;
v8::Local<v8::String> CreateV8String(const unicode_string_view& string) const;
void SetAlignedPointerInEmbedderData(int index, intptr_t address);

Expand Down
7 changes: 6 additions & 1 deletion core/src/napi/v8/v8_ctx.cc
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class ExternalStringResourceImpl : public v8::String::ExternalStringResource {
size_t length_;
};

unicode_string_view V8Ctx::GetMsgDesc(v8::Local<v8::Message> message) {
unicode_string_view V8Ctx::GetMsgDesc(v8::Local<v8::Message> message) const {
if (message.IsEmpty()) {
return "";
}
Expand Down Expand Up @@ -262,6 +262,11 @@ unicode_string_view V8Ctx::GetStackInfo(v8::Local<v8::Message> message) const {
return GetStackTrace(trace);
}

std::shared_ptr<CtxValue> V8Ctx::CreateError(v8::Local<v8::Message> message) const {
auto string = message->Get();
return std::make_shared<V8CtxValue>(isolate_, string);
}

void V8Ctx::SetExternalData(void* address) {
SetAlignedPointerInEmbedderData(kScopeWrapperIndex, reinterpret_cast<intptr_t>(address));
}
Expand Down

0 comments on commit 00a7338

Please sign in to comment.