fix: check the result when calling js function (#17443)

* fix: check the result when calling js function

* test: should not crash when callback returns nothing
This commit is contained in:
Cheng Zhao 2019-03-21 00:27:06 +09:00 committed by Shelley Vohr
parent 1571461bae
commit 671533f7d2
5 changed files with 26 additions and 18 deletions

View file

@ -55,11 +55,12 @@ struct V8FunctionInvoker<v8::Local<v8::Value>(ArgTypes...)> {
v8::Local<v8::Context> context = holder->CreationContext();
v8::Context::Scope context_scope(context);
std::vector<v8::Local<v8::Value>> args{ConvertToV8(isolate, raw)...};
v8::Local<v8::Value> ret(holder
->Call(context, holder, args.size(),
args.empty() ? nullptr : &args.front())
.ToLocalChecked());
return handle_scope.Escape(ret);
v8::MaybeLocal<v8::Value> ret = holder->Call(
context, holder, args.size(), args.empty() ? nullptr : &args.front());
if (ret.IsEmpty())
return v8::Undefined(isolate);
else
return handle_scope.Escape(ret.ToLocalChecked());
}
};
@ -81,7 +82,7 @@ struct V8FunctionInvoker<void(ArgTypes...)> {
holder
->Call(context, holder, args.size(),
args.empty() ? nullptr : &args.front())
.ToLocalChecked();
.IsEmpty();
}
};

View file

@ -334,11 +334,10 @@ std::unique_ptr<base::Value> V8ValueConverter::FromV8ValueImpl(
v8::NewStringType::kNormal)
.ToLocalChecked());
if (toISOString->IsFunction()) {
v8::Local<v8::Value> result = toISOString.As<v8::Function>()
->Call(context, val, 0, nullptr)
.ToLocalChecked();
v8::MaybeLocal<v8::Value> result =
toISOString.As<v8::Function>()->Call(context, val, 0, nullptr);
if (!result.IsEmpty()) {
v8::String::Utf8Value utf8(isolate, result);
v8::String::Utf8Value utf8(isolate, result.ToLocalChecked());
return std::make_unique<base::Value>(std::string(*utf8, utf8.length()));
}
}