summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Madsen <amwebdk@gmail.com>2017-07-10 14:28:33 +0200
committerAndreas Madsen <amwebdk@gmail.com>2017-07-13 14:15:06 +0200
commit31417b68822abeced64eeb120f064d37bc133f7c (patch)
tree5b2ef6e73bd1a00a52a47dcd7ef3d9aca6d304c0
parent628485ea01bb1773fc8814c0d61d06fef0e12ace (diff)
downloadandroid-node-v8-31417b68822abeced64eeb120f064d37bc133f7c.tar.gz
android-node-v8-31417b68822abeced64eeb120f064d37bc133f7c.tar.bz2
android-node-v8-31417b68822abeced64eeb120f064d37bc133f7c.zip
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: https://github.com/nodejs/node/pull/14152 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
-rw-r--r--lib/async_hooks.js27
-rw-r--r--test/async-hooks/test-embedder.api.async-resource.js4
-rw-r--r--test/parallel/test-async-hooks-asyncresource-constructor.js (renamed from test/parallel/test-async-wrap-asyncresource-constructor.js)8
3 files changed, 18 insertions, 21 deletions
diff --git a/lib/async_hooks.js b/lib/async_hooks.js
index 7758c8d305..39fd319fe5 100644
--- a/lib/async_hooks.js
+++ b/lib/async_hooks.js
@@ -207,28 +207,16 @@ function triggerAsyncId() {
// Embedder API //
class AsyncResource {
- constructor(type, triggerAsyncId) {
- this[async_id_symbol] = ++async_uid_fields[kAsyncUidCntr];
- // Read and reset the current kInitTriggerId so that when the constructor
- // finishes the kInitTriggerId field is always 0.
- if (triggerAsyncId === undefined) {
- triggerAsyncId = initTriggerId();
- // If a triggerAsyncId was passed, any kInitTriggerId still must be null'd.
- } else {
- async_uid_fields[kInitTriggerId] = 0;
- }
- this[trigger_id_symbol] = triggerAsyncId;
-
- if (typeof type !== 'string' || type.length <= 0)
- throw new TypeError('type must be a string with length > 0');
+ constructor(type, triggerAsyncId = initTriggerId()) {
+ // Unlike emitInitScript, AsyncResource doesn't supports null as the
+ // triggerAsyncId.
if (!Number.isSafeInteger(triggerAsyncId) || triggerAsyncId < 0)
throw new RangeError('triggerAsyncId must be an unsigned integer');
- // Return immediately if there's nothing to do.
- if (async_hook_fields[kInit] === 0)
- return;
+ this[async_id_symbol] = ++async_uid_fields[kAsyncUidCntr];
+ this[trigger_id_symbol] = triggerAsyncId;
- emitInitNative(this[async_id_symbol], type, triggerAsyncId, this);
+ emitInitScript(this[async_id_symbol], type, this[trigger_id_symbol], this);
}
emitBefore() {
@@ -323,6 +311,9 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) {
// manually means that the embedder must have used initTriggerId().
if (triggerAsyncId === null) {
triggerAsyncId = initTriggerId();
+ } else {
+ // If a triggerAsyncId was passed, any kInitTriggerId still must be null'd.
+ async_uid_fields[kInitTriggerId] = 0;
}
// TODO(trevnorris): I'd prefer allowing these checks to not exist, or only
diff --git a/test/async-hooks/test-embedder.api.async-resource.js b/test/async-hooks/test-embedder.api.async-resource.js
index 6574db8fff..2aec828c73 100644
--- a/test/async-hooks/test-embedder.api.async-resource.js
+++ b/test/async-hooks/test-embedder.api.async-resource.js
@@ -18,8 +18,8 @@ assert.throws(() => new AsyncResource('invalid_trigger_id', null),
/^RangeError: triggerAsyncId must be an unsigned integer$/);
assert.strictEqual(
- typeof new AsyncResource('default_trigger_id').triggerAsyncId(),
- 'number'
+ new AsyncResource('default_trigger_id').triggerAsyncId(),
+ async_hooks.executionAsyncId()
);
// create first custom event 'alcazares' with triggerAsyncId derived
diff --git a/test/parallel/test-async-wrap-asyncresource-constructor.js b/test/parallel/test-async-hooks-asyncresource-constructor.js
index 2465f95907..2fb0bb14cc 100644
--- a/test/parallel/test-async-wrap-asyncresource-constructor.js
+++ b/test/parallel/test-async-hooks-asyncresource-constructor.js
@@ -4,7 +4,13 @@ require('../common');
// This tests that AsyncResource throws an error if bad parameters are passed
const assert = require('assert');
-const AsyncResource = require('async_hooks').AsyncResource;
+const async_hooks = require('async_hooks');
+const { AsyncResource } = async_hooks;
+
+// Setup init hook such parameters are validated
+async_hooks.createHook({
+ init() {}
+}).enable();
assert.throws(() => {
return new AsyncResource();