Skip to content

Commit 71ee15d

Browse files
AndreasMadsenaddaleax
authored andcommitted
async_hooks: make AsyncResource match emitInit
AsyncResource previously called emitInitNative. Since AsyncResource is just an abstraction on top of the emitEventScript functions, it should call emitInitScript instead. PR-URL: #14152 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 1aac2c0 commit 71ee15d

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

lib/async_hooks.js

+9-18
Original file line numberDiff line numberDiff line change
@@ -207,28 +207,16 @@ function triggerAsyncId() {
207207
// Embedder API //
208208

209209
class AsyncResource {
210-
constructor(type, triggerAsyncId) {
211-
this[async_id_symbol] = ++async_uid_fields[kAsyncUidCntr];
212-
// Read and reset the current kInitTriggerId so that when the constructor
213-
// finishes the kInitTriggerId field is always 0.
214-
if (triggerAsyncId === undefined) {
215-
triggerAsyncId = initTriggerId();
216-
// If a triggerAsyncId was passed, any kInitTriggerId still must be null'd.
217-
} else {
218-
async_uid_fields[kInitTriggerId] = 0;
219-
}
220-
this[trigger_id_symbol] = triggerAsyncId;
221-
222-
if (typeof type !== 'string' || type.length <= 0)
223-
throw new TypeError('type must be a string with length > 0');
210+
constructor(type, triggerAsyncId = initTriggerId()) {
211+
// Unlike emitInitScript, AsyncResource doesn't supports null as the
212+
// triggerAsyncId.
224213
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < 0)
225214
throw new RangeError('triggerAsyncId must be an unsigned integer');
226215

227-
// Return immediately if there's nothing to do.
228-
if (async_hook_fields[kInit] === 0)
229-
return;
216+
this[async_id_symbol] = ++async_uid_fields[kAsyncUidCntr];
217+
this[trigger_id_symbol] = triggerAsyncId;
230218

231-
emitInitNative(this[async_id_symbol], type, triggerAsyncId, this);
219+
emitInitScript(this[async_id_symbol], type, this[trigger_id_symbol], this);
232220
}
233221

234222
emitBefore() {
@@ -323,6 +311,9 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) {
323311
// manually means that the embedder must have used initTriggerId().
324312
if (triggerAsyncId === null) {
325313
triggerAsyncId = initTriggerId();
314+
} else {
315+
// If a triggerAsyncId was passed, any kInitTriggerId still must be null'd.
316+
async_uid_fields[kInitTriggerId] = 0;
326317
}
327318

328319
// TODO(trevnorris): I'd prefer allowing these checks to not exist, or only

test/async-hooks/test-embedder.api.async-resource.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ assert.throws(() => new AsyncResource('invalid_trigger_id', null),
1818
/^RangeError: triggerAsyncId must be an unsigned integer$/);
1919

2020
assert.strictEqual(
21-
typeof new AsyncResource('default_trigger_id').triggerAsyncId(),
22-
'number'
21+
new AsyncResource('default_trigger_id').triggerAsyncId(),
22+
async_hooks.executionAsyncId()
2323
);
2424

2525
// create first custom event 'alcazares' with triggerAsyncId derived

test/parallel/test-async-wrap-asyncresource-constructor.js test/parallel/test-async-hooks-asyncresource-constructor.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ require('../common');
44
// This tests that AsyncResource throws an error if bad parameters are passed
55

66
const assert = require('assert');
7-
const AsyncResource = require('async_hooks').AsyncResource;
7+
const async_hooks = require('async_hooks');
8+
const { AsyncResource } = async_hooks;
9+
10+
// Setup init hook such parameters are validated
11+
async_hooks.createHook({
12+
init() {}
13+
}).enable();
814

915
assert.throws(() => {
1016
return new AsyncResource();

0 commit comments

Comments
 (0)