Skip to content

Commit 13d465b

Browse files
AndreasMadsenMyles Borins
authored and
Myles Borins
committed
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. Ref: #7048 PR-URL: #4600 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent 68f391b commit 13d465b

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

src/async-wrap.cc

+3-2
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ Local<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
179179

180180
Local<Function> pre_fn = env()->async_hooks_pre_function();
181181
Local<Function> post_fn = env()->async_hooks_post_function();
182+
Local<Value> uid = Integer::New(env()->isolate(), get_uid());
182183
Local<Object> context = object();
183184
Local<Object> process = env()->process_object();
184185
Local<Object> domain;
@@ -207,14 +208,14 @@ Local<Value> AsyncWrap::MakeCallback(const Local<Function> cb,
207208
}
208209

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

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

216217
if (ran_init_callback() && !post_fn.IsEmpty()) {
217-
if (post_fn->Call(context, 0, nullptr).IsEmpty())
218+
if (post_fn->Call(context, 1, &uid).IsEmpty())
218219
FatalError("node::AsyncWrap::MakeCallback", "post hook threw");
219220
}
220221

test/parallel/test-async-wrap-uid.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
require('../common');
4+
const fs = require('fs');
5+
const assert = require('assert');
6+
const async_wrap = process.binding('async_wrap');
7+
8+
const storage = new Map();
9+
async_wrap.setupHooks(init, pre, post, destroy);
10+
async_wrap.enable();
11+
12+
function init(provider, uid) {
13+
storage.set(uid, {
14+
init: true,
15+
pre: false,
16+
post: false,
17+
destroy: false
18+
});
19+
}
20+
21+
function pre(uid) {
22+
storage.get(uid).pre = true;
23+
}
24+
25+
function post(uid) {
26+
storage.get(uid).post = true;
27+
}
28+
29+
function destroy(uid) {
30+
storage.get(uid).destroy = true;
31+
}
32+
33+
fs.access(__filename, function(err) {
34+
assert.ifError(err);
35+
});
36+
37+
fs.access(__filename, function(err) {
38+
assert.ifError(err);
39+
});
40+
41+
async_wrap.disable();
42+
43+
process.once('exit', function() {
44+
assert.strictEqual(storage.size, 2);
45+
46+
for (const item of storage) {
47+
const uid = item[0];
48+
const value = item[1];
49+
assert.strictEqual(typeof uid, 'number');
50+
assert.deepStrictEqual(value, {
51+
init: true,
52+
pre: true,
53+
post: true,
54+
destroy: true
55+
});
56+
}
57+
});

0 commit comments

Comments
 (0)