feat(core): refactor ctx value

This commit is contained in:
pollyzhang
2021-06-23 15:59:19 +08:00
committed by churchill-zhang
parent 8ed5e22ada
commit eb745be9ba
3 changed files with 30 additions and 19 deletions
+12 -4
View File
@@ -120,7 +120,7 @@ void CallFunction(JNIEnv* j_env,
}
std::shared_ptr<CtxValue> action = context->CreateString(action_name);
std::shared_ptr<CtxValue> params = nullptr;
std::shared_ptr<CtxValue> params;
if (runtime->IsEnableV8Serialization()) {
v8::Isolate* isolate = std::static_pointer_cast<hippy::napi::V8VM>(
runtime->GetEngine()->GetVM())
@@ -129,7 +129,7 @@ void CallFunction(JNIEnv* j_env,
v8::Local<v8::Context> ctx = std::static_pointer_cast<hippy::napi::V8Ctx>(
runtime->GetScope()->GetContext())
->context_persistent_.Get(isolate);
hippy::napi::V8TryCatch try_catch(true, context);
v8::ValueDeserializer deserializer(
isolate, reinterpret_cast<const uint8_t*>(buffer_data_.c_str()),
buffer_data_.length());
@@ -139,8 +139,13 @@ void CallFunction(JNIEnv* j_env,
params = std::make_shared<hippy::napi::V8CtxValue>(
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<CtxValue> argv[] = {action, params};
context->CallFunction(runtime->GetBridgeFunc(), 2, argv);
+4 -3
View File
@@ -153,7 +153,7 @@ void ContextifyModule::LoadUntrustedContent(const CallbackInfo& info) {
}
std::shared_ptr<Ctx> ctx = scope->GetContext();
std::shared_ptr<CtxValue> error;
std::shared_ptr<CtxValue> 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<CtxValue> function = weak_function.lock();
if (function) {
TDF_BASE_DLOG(INFO) << "run js cb";
if (!error) {
error = ctx->CreateNull();
}
std::shared_ptr<CtxValue> argv[] = {error};
ctx->CallFunction(function, 1, argv);
RemoveCBFunc(uri);
+14 -12
View File
@@ -875,7 +875,7 @@ std::shared_ptr<CtxValue> 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<CtxValue> V8Ctx::CallFunction(
v8::Local<v8::Context> 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<CtxValue> V8Ctx::CallFunction(
v8::Local<v8::Value> handle_value =
v8::Local<v8::Value>::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<CtxValue> V8Ctx::CallFunction(
const v8::Global<v8::Value>& global_value = argument->global_value_;
args[i] = v8::Local<v8::Value>::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<v8::Value> maybe_result = v8_fn->Call(
context, context->Global(), static_cast<int>(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<CtxValue> V8Ctx::CreateBoolean(bool b) {
std::shared_ptr<CtxValue> 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<JSValueWrapper> V8Ctx::ToJsValueWrapper(
std::shared_ptr<CtxValue> V8Ctx::CreateCtxValue(
std::shared_ptr<JSValueWrapper> wrapper) {
TDF_BASE_DCHECK(wrapper);
if (wrapper->IsUndefined()) {
return CreateUndefined();
} else if (wrapper->IsNull()) {
@@ -1132,6 +1135,7 @@ std::shared_ptr<CtxValue> V8Ctx::CreateCtxValue(
std::static_pointer_cast<V8CtxValue>(
CreateCtxValue(std::make_shared<JSValueWrapper>(obj_value)));
const v8::Global<v8::Value>& global_value = ctx_value->global_value_;
TDF_BASE_DCHECK(!global_value.IsEmpty());
v8::Local<v8::Value> handle_value =
v8::Local<v8::Value>::New(isolate_, global_value);
v8_obj->Set(context, key, handle_value).ToChecked();
@@ -1180,9 +1184,6 @@ std::shared_ptr<CtxValue> V8Ctx::CreateObject(const unicode_string_view& json) {
std::shared_ptr<CtxValue> V8Ctx::CreateArray(
size_t count,
std::shared_ptr<CtxValue> value[]) {
if (!count) {
return nullptr;
}
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Array> array = v8::Array::New(isolate_, count);
v8::Local<v8::Context> context = context_persistent_.Get(isolate_);
@@ -1195,10 +1196,11 @@ std::shared_ptr<CtxValue> V8Ctx::CreateArray(
const v8::Global<v8::Value>& global_value = ctx_value->global_value_;
handle_value = v8::Local<v8::Value>::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<CtxValue> V8Ctx::CreateJsError(const unicode_string_view& msg) {
v8::Local<v8::Value> 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<V8CtxValue>(isolate_, error);