diff --git a/android/sdk/src/main/jni/src/bridge/java2js.cc b/android/sdk/src/main/jni/src/bridge/java2js.cc index f0a6e4626..d3264a9e1 100644 --- a/android/sdk/src/main/jni/src/bridge/java2js.cc +++ b/android/sdk/src/main/jni/src/bridge/java2js.cc @@ -120,7 +120,7 @@ void CallFunction(JNIEnv* j_env, } std::shared_ptr action = context->CreateString(action_name); - std::shared_ptr params = nullptr; + std::shared_ptr params; if (runtime->IsEnableV8Serialization()) { v8::Isolate* isolate = std::static_pointer_cast( runtime->GetEngine()->GetVM()) @@ -129,7 +129,7 @@ void CallFunction(JNIEnv* j_env, v8::Local ctx = std::static_pointer_cast( runtime->GetScope()->GetContext()) ->context_persistent_.Get(isolate); - + hippy::napi::V8TryCatch try_catch(true, context); v8::ValueDeserializer deserializer( isolate, reinterpret_cast(buffer_data_.c_str()), buffer_data_.length()); @@ -139,8 +139,13 @@ void CallFunction(JNIEnv* j_env, params = std::make_shared( isolate, ret.ToLocalChecked()); } else { - jstring j_msg = - JniUtils::StrViewToJString(j_env, u"deserializer error"); + jstring j_msg; + if (try_catch.HasCaught()) { + unicode_string_view msg = try_catch.GetExceptionMsg(); + j_msg = JniUtils::StrViewToJString(j_env, msg); + } else { + j_msg = JniUtils::StrViewToJString(j_env, u"deserializer error"); + } CallJavaMethod( cb_->GetObj(), hippy::bridge::CALLFUNCTION_CB_STATE::DESERIALIZER_FAILED, j_msg); @@ -155,6 +160,9 @@ void CallFunction(JNIEnv* j_env, << ", buf_str = " << buf_str; params = context->CreateObject(buf_str); } + if (!params) { + params = context->CreateNull(); + } std::shared_ptr argv[] = {action, params}; context->CallFunction(runtime->GetBridgeFunc(), 2, argv); diff --git a/core/src/modules/contextify_module.cc b/core/src/modules/contextify_module.cc index 98ad1736b..a028f74d3 100644 --- a/core/src/modules/contextify_module.cc +++ b/core/src/modules/contextify_module.cc @@ -153,7 +153,7 @@ void ContextifyModule::LoadUntrustedContent(const CallbackInfo& info) { } std::shared_ptr ctx = scope->GetContext(); - std::shared_ptr error; + std::shared_ptr error = nullptr; if (!move_code.empty()) { auto last_dir_str_obj = ctx->GetGlobalStrVar("__HIPPYCURDIR__"); TDF_BASE_DLOG(INFO) << "__HIPPYCURDIR__ cur_dir = " << cur_dir; @@ -172,8 +172,6 @@ void ContextifyModule::LoadUntrustedContent(const CallbackInfo& info) { error = try_catch->Exception(); TDF_BASE_DLOG(ERROR) << "RequestUntrustedContent error = " << try_catch->GetExceptionMsg(); - } else { - error = ctx->CreateNull(); } } else { unicode_string_view err_msg = uri + " not found"; @@ -183,6 +181,9 @@ void ContextifyModule::LoadUntrustedContent(const CallbackInfo& info) { std::shared_ptr function = weak_function.lock(); if (function) { TDF_BASE_DLOG(INFO) << "run js cb"; + if (!error) { + error = ctx->CreateNull(); + } std::shared_ptr argv[] = {error}; ctx->CallFunction(function, 1, argv); RemoveCBFunc(uri); diff --git a/core/src/napi/v8/js_native_api_v8.cc b/core/src/napi/v8/js_native_api_v8.cc index 289865ca2..d92bd99e1 100644 --- a/core/src/napi/v8/js_native_api_v8.cc +++ b/core/src/napi/v8/js_native_api_v8.cc @@ -875,7 +875,7 @@ std::shared_ptr V8Ctx::CallFunction( TDF_BASE_DLOG(INFO) << "V8Ctx CallFunction begin"; if (!function) { - TDF_BASE_DLOG(ERROR) << "function is nullptr"; + TDF_BASE_LOG(ERROR) << "function is nullptr"; return nullptr; } @@ -883,7 +883,7 @@ std::shared_ptr V8Ctx::CallFunction( v8::Local context = context_persistent_.Get(isolate_); v8::Context::Scope contextScope(context); if (context.IsEmpty() || context->Global().IsEmpty()) { - TDF_BASE_DLOG(ERROR) << "CallFunction context error"; + TDF_BASE_LOG(ERROR) << "CallFunction context error"; return nullptr; } @@ -893,7 +893,7 @@ std::shared_ptr V8Ctx::CallFunction( v8::Local handle_value = v8::Local::New(isolate_, global_value); if (!handle_value->IsFunction()) { - TDF_BASE_DLOG(WARNING) << "CallFunction handle_value is not a function"; + TDF_BASE_LOG(WARNING) << "CallFunction handle_value is not a function"; return nullptr; } @@ -906,13 +906,15 @@ std::shared_ptr V8Ctx::CallFunction( const v8::Global& global_value = argument->global_value_; args[i] = v8::Local::New(isolate_, global_value); } else { - args[i] = v8::Null(isolate_); + TDF_BASE_LOG(WARNING) << "CallFunction argument error, i = " << i; + return nullptr; } } - TDF_BASE_DLOG(INFO) << "CallFunction call fn"; + TDF_BASE_DLOG(INFO) << "v8 CallFunction call begin"; v8::MaybeLocal maybe_result = v8_fn->Call( context, context->Global(), static_cast(argument_count), args); + TDF_BASE_DLOG(INFO) << "v8 CallFunction call end"; if (maybe_result.IsEmpty()) { TDF_BASE_DLOG(INFO) << "maybe_result is empty"; @@ -943,7 +945,7 @@ std::shared_ptr V8Ctx::CreateBoolean(bool b) { std::shared_ptr V8Ctx::CreateString( const unicode_string_view& str_view) { - if (StringViewUtils::IsEmpty(str_view)) { + if (str_view.encoding() == unicode_string_view::Encoding::Unkown) { return nullptr; } v8::HandleScope isolate_scope(isolate_); @@ -1092,6 +1094,7 @@ std::shared_ptr V8Ctx::ToJsValueWrapper( std::shared_ptr V8Ctx::CreateCtxValue( std::shared_ptr wrapper) { + TDF_BASE_DCHECK(wrapper); if (wrapper->IsUndefined()) { return CreateUndefined(); } else if (wrapper->IsNull()) { @@ -1132,6 +1135,7 @@ std::shared_ptr V8Ctx::CreateCtxValue( std::static_pointer_cast( CreateCtxValue(std::make_shared(obj_value))); const v8::Global& global_value = ctx_value->global_value_; + TDF_BASE_DCHECK(!global_value.IsEmpty()); v8::Local handle_value = v8::Local::New(isolate_, global_value); v8_obj->Set(context, key, handle_value).ToChecked(); @@ -1180,9 +1184,6 @@ std::shared_ptr V8Ctx::CreateObject(const unicode_string_view& json) { std::shared_ptr V8Ctx::CreateArray( size_t count, std::shared_ptr value[]) { - if (!count) { - return nullptr; - } v8::HandleScope handle_scope(isolate_); v8::Local array = v8::Array::New(isolate_, count); v8::Local context = context_persistent_.Get(isolate_); @@ -1195,10 +1196,11 @@ std::shared_ptr V8Ctx::CreateArray( const v8::Global& global_value = ctx_value->global_value_; handle_value = v8::Local::New(isolate_, global_value); } else { - handle_value = v8::Null(isolate_); + TDF_BASE_LOG(ERROR) << "array item error"; + return nullptr; } if (!array->Set(context, i, handle_value).FromMaybe(false)) { - TDF_BASE_DLOG(INFO) << "set array item failed"; + TDF_BASE_LOG(ERROR) << "set array item failed"; return nullptr; } } @@ -1213,7 +1215,7 @@ std::shared_ptr V8Ctx::CreateJsError(const unicode_string_view& msg) { v8::Local error = v8::Exception::Error(CreateV8String(msg)); if (error.IsEmpty()) { - TDF_BASE_DLOG(INFO) << "error is empty"; + TDF_BASE_LOG(INFO) << "error is empty"; return nullptr; } return std::make_shared(isolate_, error);