forked from nodejs/nan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add async context versions of MakeCallback
This commit adds support for the async context accepting versions of node::MakeCallback. An async_context concept has been added as a wrapper around node::async_context, along with APIs for initializing and destroying async context, similar to how [N-API][] exposes this functionality. Ref: nodejs/node#13254 [N-API]: https://nodejs.org/dist/latest-v9.x/docs/api/n-api.html#n_api_custom_asynchronous_operations
- Loading branch information
Showing
6 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/********************************************************************* | ||
* NAN - Native Abstractions for Node.js | ||
* | ||
* Copyright (c) 2018 NAN contributors | ||
* | ||
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md> | ||
********************************************************************/ | ||
|
||
#include <nan.h> | ||
#include <unistd.h> | ||
|
||
using namespace Nan; // NOLINT(build/namespaces) | ||
|
||
class DelayRequest { | ||
public: | ||
DelayRequest(int milliseconds_, v8::Local<v8::Function> callback_) | ||
: milliseconds(milliseconds_) { | ||
callback.Reset(callback_); | ||
request.data = this; | ||
asyncContext = AsyncInit(MaybeLocal<v8::Object>(), "test.DelayRequest"); | ||
} | ||
~DelayRequest() { | ||
AsyncDestroy(asyncContext); | ||
callback.Reset(); | ||
} | ||
|
||
Persistent<v8::Function> callback; | ||
uv_work_t request; | ||
async_context asyncContext; | ||
int milliseconds; | ||
}; | ||
|
||
void Delay(uv_work_t* req) { | ||
DelayRequest *delay_request = static_cast<DelayRequest*>(req->data); | ||
sleep(delay_request->milliseconds / 1000); | ||
} | ||
|
||
void AfterDelay(uv_work_t* req, int status) { | ||
HandleScope scope; | ||
|
||
DelayRequest *delay_request = static_cast<DelayRequest*>(req->data); | ||
v8::Local<v8::Function> callback = New(delay_request->callback); | ||
v8::Local<v8::Value> argv[0] = {}; | ||
|
||
v8::Local<v8::Object> target = New<v8::Object>(); | ||
MakeCallback(target, callback, 0, argv, delay_request->asyncContext); | ||
|
||
delete delay_request; | ||
} | ||
|
||
NAN_METHOD(Delay) { | ||
int delay = To<int>(info[0]).FromJust(); | ||
v8::Local<v8::Function> cb = To<v8::Function>(info[1]).ToLocalChecked(); | ||
|
||
DelayRequest* delay_request = new DelayRequest(delay, cb); | ||
|
||
uv_queue_work(uv_default_loop(), &delay_request->request, Delay, AfterDelay); | ||
} | ||
|
||
NAN_MODULE_INIT(Init) { | ||
Set(target, New<v8::String>("delay").ToLocalChecked(), | ||
GetFunction(New<v8::FunctionTemplate>(Delay)).ToLocalChecked()); | ||
} | ||
|
||
NODE_MODULE(makecallbackcontext, Init) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/********************************************************************* | ||
* NAN - Native Abstractions for Node.js | ||
* | ||
* Copyright (c) 2018 NAN contributors | ||
* | ||
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md> | ||
********************************************************************/ | ||
|
||
try { | ||
require('async_hooks'); | ||
} catch (e) { | ||
process.exit(0); | ||
} | ||
|
||
const test = require('tap').test | ||
, testRoot = require('path').resolve(__dirname, '..') | ||
, delay = require('bindings')({ module_root: testRoot, bindings: 'makecallbackcontext' }).delay | ||
, asyncHooks = require('async_hooks'); | ||
|
||
test('makecallbackcontext', function (t) { | ||
t.plan(7); | ||
|
||
var resourceAsyncId; | ||
var originalExecutionAsyncId; | ||
var beforeCalled = false; | ||
var afterCalled = false; | ||
var destroyCalled = false; | ||
|
||
var hooks = asyncHooks.createHook({ | ||
init: function(asyncId, type, triggerAsyncId, resource) { | ||
if (type === 'test.DelayRequest') { | ||
resourceAsyncId = asyncId; | ||
} | ||
}, | ||
before: function(asyncId) { | ||
if (asyncId === resourceAsyncId) { | ||
beforeCalled = true; | ||
} | ||
}, | ||
after: function(asyncId) { | ||
if (asyncId === resourceAsyncId) { | ||
afterCalled = true; | ||
} | ||
}, | ||
destroy: function(asyncId) { | ||
if (asyncId === resourceAsyncId) { | ||
destroyCalled = true; | ||
} | ||
} | ||
|
||
}); | ||
hooks.enable(); | ||
|
||
originalExecutionAsyncId = asyncHooks.executionAsyncId(); | ||
delay(1000, function() { | ||
t.equal(asyncHooks.executionAsyncId(), resourceAsyncId, | ||
'callback should have the correct execution context'); | ||
t.equal(asyncHooks.triggerAsyncId(), originalExecutionAsyncId, | ||
'callback should have the correct trigger context'); | ||
t.ok(beforeCalled, 'before should have been called'); | ||
t.notOk(afterCalled, 'after should not have been called yet'); | ||
setTimeout(function() { | ||
t.ok(afterCalled, 'after should have been called'); | ||
t.ok(destroyCalled, 'destroy should have been called'); | ||
t.equal(asyncHooks.triggerAsyncId(), resourceAsyncId, | ||
'setTimeout should have been triggered by the async resource'); | ||
hooks.disable(); | ||
}, 1); | ||
}); | ||
}); |