From 4afe801f90b667b793a2f1877c6dec06d2b9a5ce Mon Sep 17 00:00:00 2001 From: Andreas Madsen Date: Sat, 9 Jan 2016 10:58:55 +0100 Subject: [PATCH] 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. PR-URL: https://github.com/nodejs/node/pull/4600 Reviewed-By: Trevor Norris Reviewed-By: Sakthipriyan Vairamani --- src/async-wrap.cc | 5 ++- test/parallel/test-async-wrap-uid.js | 57 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-async-wrap-uid.js diff --git a/src/async-wrap.cc b/src/async-wrap.cc index a7a9822238329a..11a696cc2329a3 100644 --- a/src/async-wrap.cc +++ b/src/async-wrap.cc @@ -179,6 +179,7 @@ Local AsyncWrap::MakeCallback(const Local cb, Local pre_fn = env()->async_hooks_pre_function(); Local post_fn = env()->async_hooks_post_function(); + Local uid = Integer::New(env()->isolate(), get_uid()); Local context = object(); Local process = env()->process_object(); Local domain; @@ -207,14 +208,14 @@ Local AsyncWrap::MakeCallback(const Local 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 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()) FatalError("node::AsyncWrap::MakeCallback", "post hook threw"); } diff --git a/test/parallel/test-async-wrap-uid.js b/test/parallel/test-async-wrap-uid.js new file mode 100644 index 00000000000000..ad2dd5027cfa1d --- /dev/null +++ b/test/parallel/test-async-wrap-uid.js @@ -0,0 +1,57 @@ +'use strict'; + +require('../common'); +const fs = require('fs'); +const assert = require('assert'); +const async_wrap = process.binding('async_wrap'); + +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 + }); + } +});