Skip to content

Commit

Permalink
Add scope.Escape() to Call() (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
Flarna authored and kkoopa committed Oct 22, 2018
1 parent 3736dd1 commit 2e5ed4f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
6 changes: 3 additions & 3 deletions nan.h
Expand Up @@ -1658,9 +1658,9 @@ class Callback {
v8::EscapableHandleScope scope(isolate);
# if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
AsyncResource async("nan:Callback:Call");
return Call_(isolate, isolate->GetCurrentContext()->Global(), argc, argv,
&async)
.FromMaybe(v8::Local<v8::Value>());
return scope.Escape(Call_(isolate, isolate->GetCurrentContext()->Global(),
argc, argv, &async)
.FromMaybe(v8::Local<v8::Value>()));
# else
return scope.Escape(
Call_(isolate, isolate->GetCurrentContext()->Global(), argc, argv));
Expand Down
14 changes: 14 additions & 0 deletions test/cpp/nancallback.cpp
Expand Up @@ -57,6 +57,16 @@ NAN_METHOD(ResetSet) {
info.GetReturnValue().Set(callback.IsEmpty());
}

NAN_METHOD(CallRetval) {
Callback callback(To<v8::Function>(info[0]).ToLocalChecked());
v8::Local<v8::Value> result = callback.Call(0, NULL);
if (result->IsNumber()) {
info.GetReturnValue().Set(Nan::True());
} else {
info.GetReturnValue().Set(Nan::False());
}
}

NAN_MODULE_INIT(Init) {
Set(target
, New<v8::String>("globalContext").ToLocalChecked()
Expand Down Expand Up @@ -90,6 +100,10 @@ NAN_MODULE_INIT(Init) {
, New<v8::String>("resetSet").ToLocalChecked()
, New<v8::FunctionTemplate>(ResetSet)->GetFunction()
);
Set(target
, New<v8::String>("callRetval").ToLocalChecked()
, New<v8::FunctionTemplate>(CallRetval)->GetFunction()
);
}

NODE_MODULE(nancallback, Init)
4 changes: 3 additions & 1 deletion test/js/nancallback-test.js
Expand Up @@ -12,7 +12,7 @@ const test = require('tap').test
, round = Math.round;

test('nancallback', function (t) {
t.plan(17)
t.plan(19)

var persistent = bindings;
t.type(persistent.globalContext, 'function');
Expand All @@ -23,13 +23,15 @@ test('nancallback', function (t) {
t.type(persistent.callAsFunction, 'function');
t.type(persistent.resetUnset, 'function');
t.type(persistent.resetSet, 'function');
t.type(persistent.callRetval, 'function');
persistent.globalContext(function () { t.ok(true); });
persistent.specificContext(function () { t.ok(true); });
persistent.customReceiver(function () { t.equal(this, process); }, process);
persistent.callDirect(function () { t.ok(true); });
persistent.callAsFunction(function () { t.ok(true); });
t.ok(persistent.resetUnset());
t.ok(persistent.resetSet(function () {}));
t.ok(persistent.callRetval(function () { return 1; }));

var round2 = Math.round
, x = function(param) { return param + 1; }
Expand Down

0 comments on commit 2e5ed4f

Please sign in to comment.