Skip to content

Commit

Permalink
fix: check the result when calling js function (backport: 5-0-x) (#17481
Browse files Browse the repository at this point in the history
)

* fix: check the result when calling js function

* test: should not crash when callback returns nothing
  • Loading branch information
trop[bot] authored and MarshallOfSound committed Mar 20, 2019
1 parent d909345 commit 821b88e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
13 changes: 7 additions & 6 deletions atom/common/native_mate_converters/callback.h
Expand Up @@ -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());
}
};

Expand All @@ -81,7 +82,7 @@ struct V8FunctionInvoker<void(ArgTypes...)> {
holder
->Call(context, holder, args.size(),
args.empty() ? nullptr : &args.front())
.ToLocalChecked();
.IsEmpty();
}
};

Expand Down
7 changes: 3 additions & 4 deletions atom/common/native_mate_converters/v8_value_converter.cc
Expand Up @@ -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()));
}
}
Expand Down
2 changes: 1 addition & 1 deletion atom/renderer/api/atom_api_spell_check_client.cc
Expand Up @@ -215,7 +215,7 @@ void SpellCheckClient::SpellCheckWords(
v8::Local<v8::Value> args[] = {mate::ConvertToV8(isolate_, words),
templ->GetFunction(context).ToLocalChecked()};
// Call javascript with the words and the callback function
scope.spell_check_->Call(context, scope.provider_, 2, args).ToLocalChecked();
scope.spell_check_->Call(context, scope.provider_, 2, args).IsEmpty();
}

// Returns whether or not the given string is a contraction.
Expand Down
3 changes: 1 addition & 2 deletions native_mate/native_mate/wrappable.cc
Expand Up @@ -39,8 +39,7 @@ void WrappableBase::InitWith(v8::Isolate* isolate,
// Call object._init if we have one.
v8::Local<v8::Function> init;
if (Dictionary(isolate, wrapper).Get("_init", &init))
init->Call(isolate->GetCurrentContext(), wrapper, 0, nullptr)
.ToLocalChecked();
init->Call(isolate->GetCurrentContext(), wrapper, 0, nullptr).IsEmpty();

AfterInit(isolate);
}
Expand Down
19 changes: 14 additions & 5 deletions spec/api-browser-window-spec.js
Expand Up @@ -2442,17 +2442,26 @@ describe('BrowserWindow module', () => {

describe('beginFrameSubscription method', () => {
before(function () {
// This test is too slow, only test it on CI.
if (!isCI) {
this.skip()
}

// FIXME These specs crash on Linux when run in a docker container
if (isCI && process.platform === 'linux') {
this.skip()
}
})

it('does not crash when callback returns nothing', (done) => {
w.loadFile(path.join(fixtures, 'api', 'frame-subscriber.html'))
w.webContents.on('dom-ready', () => {
w.webContents.beginFrameSubscription(function (data) {
// Pending endFrameSubscription to next tick can reliably reproduce
// a crash which happens when nothing is returned in the callback.
setTimeout(() => {
w.webContents.endFrameSubscription()
done()
})
})
})
})

it('subscribes to frame updates', (done) => {
let called = false
w.loadFile(path.join(fixtures, 'api', 'frame-subscriber.html'))
Expand Down

0 comments on commit 821b88e

Please sign in to comment.