Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
async_wrap: add uid to all asyncWrap hooks
By doing this users can use a Map object for storing information
instead of modifying the handle object.
  • Loading branch information
AndreasMadsen committed Feb 18, 2016
commit 7b5e8126049fa3fec9fb951ef7f2062d417c0e81
5 changes: 3 additions & 2 deletions src/async-wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ Local<Value> AsyncWrap::MakeCallback(const Local<Function> cb,

Local<Function> pre_fn = env()->async_hooks_pre_function();
Local<Function> post_fn = env()->async_hooks_post_function();
Local<Value> uid = Integer::New(env()->isolate(), get_uid());
Local<Object> context = object();
Local<Object> process = env()->process_object();
Local<Object> domain;
Expand Down Expand Up @@ -207,14 +208,14 @@ Local<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
}

if (ran_init_callback() && !pre_fn.IsEmpty()) {
if (pre_fn->Call(context, 0, nullptr).IsEmpty())
if (pre_fn->Call(context, 1, &uid).IsEmpty())
FatalError("node::AsyncWrap::MakeCallback", "pre hook threw");
}

Local<Value> ret = cb->Call(context, argc, argv);

if (ran_init_callback() && !post_fn.IsEmpty()) {
if (post_fn->Call(context, 0, nullptr).IsEmpty())
if (post_fn->Call(context, 1, &uid).IsEmpty())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the meaning of the number 1 here? Also, will the uid be enough during pre or post calls?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It specifies the length of the arguments array (&uid) that follows.

The general idea is that the user will collect all the required information in the init hook and map it to a Map object using the uid. Thus the uid should be all that the user needs in the pre, post and destroy hook.

FatalError("node::AsyncWrap::MakeCallback", "post hook threw");
}

Expand Down
57 changes: 57 additions & 0 deletions test/parallel/test-async-wrap-uid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

require('../common');
const fs = require('fs');
const assert = require('assert');
const async_wrap = process.binding('async_wrap');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In JavaScript code we follow camelCase style.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


const storage = new Map();
async_wrap.setupHooks(init, pre, post, destroy);
async_wrap.enable();

function init(provider, uid) {
storage.set(uid, {
init: true,
pre: false,
post: false,
destroy: false
});
}

function pre(uid) {
storage.get(uid).pre = true;
}

function post(uid) {
storage.get(uid).post = true;
}

function destroy(uid) {
storage.get(uid).destroy = true;
}

fs.access(__filename, function(err) {
assert.ifError(err);
});

fs.access(__filename, function(err) {
assert.ifError(err);
});

async_wrap.disable();

process.once('exit', function() {
assert.strictEqual(storage.size, 2);

for (const item of storage) {
const uid = item[0];
const value = item[1];
assert.strictEqual(typeof uid, 'number');
assert.deepStrictEqual(value, {
init: true,
pre: true,
post: true,
destroy: true
});
}
});