diff options
author | Andreas Madsen <amwebdk@gmail.com> | 2017-11-22 20:41:00 +0300 |
---|---|---|
committer | Andreas Madsen <amwebdk@gmail.com> | 2017-12-19 20:04:52 +0300 |
commit | 3b8da4cbe8a7f36fcd8892c6676a55246ba8c3be (patch) | |
tree | 1afc84002d1716f4acd28126df214127e0262c3c /lib/internal/async_hooks.js | |
parent | 0784b0440c05464f79b857f7d8698fcc953d3fb3 (diff) |
async_hooks: use scope for defaultTriggerAsyncId
Previously the getter would mutate the kDefaultTriggerAsncId value. This
refactor changes the setter to bind the current kDefaultTriggerAsncId to
a scope, such that the getter doesn't have to mutate its own value.
PR-URL: https://github.com/nodejs/node/pull/17273
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/internal/async_hooks.js')
-rw-r--r-- | lib/internal/async_hooks.js | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index 2cd4137fd08..4bd5f3191b6 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -250,9 +250,6 @@ function newUid() { // constructor is complete. function getDefaultTriggerAsyncId() { var defaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId]; - // Reset value after it's been called so the next constructor doesn't - // inherit it by accident. - async_id_fields[kDefaultTriggerAsyncId] = -1; // If defaultTriggerAsyncId isn't set, use the executionAsyncId if (defaultTriggerAsyncId < 0) defaultTriggerAsyncId = async_id_fields[kExecutionAsyncId]; @@ -260,10 +257,20 @@ function getDefaultTriggerAsyncId() { } -function setDefaultTriggerAsyncId(triggerAsyncId) { +function defaultTriggerAsyncIdScope(triggerAsyncId, opaque, block) { // CHECK(Number.isSafeInteger(triggerAsyncId)) // CHECK(triggerAsyncId > 0) + const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId]; async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId; + + var ret; + try { + ret = Reflect.apply(block, null, opaque); + } finally { + async_id_fields[kDefaultTriggerAsyncId] = oldDefaultTriggerAsyncId; + } + + return ret; } @@ -285,10 +292,6 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) { // manually means that the embedder must have used getDefaultTriggerAsyncId(). if (triggerAsyncId === null) { triggerAsyncId = getDefaultTriggerAsyncId(); - } else { - // If a triggerAsyncId was passed, any kDefaultTriggerAsyncId still must be - // null'd. - async_id_fields[kDefaultTriggerAsyncId] = -1; } emitInitNative(asyncId, type, triggerAsyncId, resource); @@ -341,7 +344,7 @@ module.exports = { // Internal Embedder API newUid, getDefaultTriggerAsyncId, - setDefaultTriggerAsyncId, + defaultTriggerAsyncIdScope, emitInit: emitInitScript, emitBefore: emitBeforeScript, emitAfter: emitAfterScript, |