async_hooks: minor cleanup and improvements · nodejs/node@ca7c4f4 (original) (raw)
`@@ -75,6 +75,8 @@ const { kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
`
75
75
` kCheck, kExecutionAsyncId, kAsyncIdCounter, kTriggerAsyncId,
`
76
76
` kDefaultTriggerAsyncId, kStackLength } = async_wrap.constants;
`
77
77
``
``
78
`+
const FunctionBind = Function.call.bind(Function.prototype.bind);
`
``
79
+
78
80
`// Used in AsyncHook and AsyncResource.
`
79
81
`const async_id_symbol = Symbol('asyncId');
`
80
82
`const trigger_async_id_symbol = Symbol('triggerAsyncId');
`
`@@ -148,38 +150,37 @@ function emitInitNative(asyncId, type, triggerAsyncId, resource) {
`
148
150
`}
`
149
151
`}
`
150
152
``
151
``
-
152
``
`-
function emitHookFactory(symbol, name) {
`
153
``
`-
// Called from native. The asyncId stack handling is taken care of there
`
154
``
`-
// before this is called.
`
155
``
`-
// eslint-disable-next-line func-style
`
156
``
`-
const fn = function(asyncId) {
`
157
``
`-
active_hooks.call_depth += 1;
`
158
``
`-
// Use a single try/catch for all hook to avoid setting up one per
`
159
``
`-
// iteration.
`
160
``
`-
try {
`
161
``
`-
for (var i = 0; i < active_hooks.array.length; i++) {
`
162
``
`-
if (typeof active_hooks.array[i][symbol] === 'function') {
`
163
``
`-
active_hooks.array[i]symbol;
`
164
``
`-
}
`
``
153
`+
// Called from native. The asyncId stack handling is taken care of there
`
``
154
`+
// before this is called.
`
``
155
`+
function emitHook(symbol, asyncId) {
`
``
156
`+
active_hooks.call_depth += 1;
`
``
157
`+
// Use a single try/catch for all hook to avoid setting up one per
`
``
158
`+
// iteration.
`
``
159
`+
try {
`
``
160
`+
for (var i = 0; i < active_hooks.array.length; i++) {
`
``
161
`+
if (typeof active_hooks.array[i][symbol] === 'function') {
`
``
162
`+
active_hooks.array[i]symbol;
`
165
163
`}
`
166
``
`-
} catch (e) {
`
167
``
`-
fatalError(e);
`
168
``
`-
} finally {
`
169
``
`-
active_hooks.call_depth -= 1;
`
170
164
`}
`
``
165
`+
} catch (e) {
`
``
166
`+
fatalError(e);
`
``
167
`+
} finally {
`
``
168
`+
active_hooks.call_depth -= 1;
`
``
169
`+
}
`
171
170
``
172
``
`-
// Hooks can only be restored if there have been no recursive hook calls.
`
173
``
`-
// Also the active hooks do not need to be restored if enable()/disable()
`
174
``
`-
// weren't called during hook execution, in which case
`
175
``
`-
// active_hooks.tmp_array will be null.
`
176
``
`-
if (active_hooks.call_depth === 0 && active_hooks.tmp_array !== null) {
`
177
``
`-
restoreActiveHooks();
`
178
``
`-
}
`
179
``
`-
};
`
``
171
`+
// Hooks can only be restored if there have been no recursive hook calls.
`
``
172
`+
// Also the active hooks do not need to be restored if enable()/disable()
`
``
173
`+
// weren't called during hook execution, in which case
`
``
174
`+
// active_hooks.tmp_array will be null.
`
``
175
`+
if (active_hooks.call_depth === 0 && active_hooks.tmp_array !== null) {
`
``
176
`+
restoreActiveHooks();
`
``
177
`+
}
`
``
178
`+
}
`
``
179
+
``
180
`+
function emitHookFactory(symbol, name) {
`
``
181
`+
const fn = FunctionBind(emitHook, undefined, symbol);
`
180
182
``
181
``
`-
// Set the name property of the anonymous function as it looks good in the
`
182
``
`-
// stack trace.
`
``
183
`+
// Set the name property of the function as it looks good in the stack trace.
`
183
184
`Object.defineProperty(fn, 'name', {
`
184
185
`value: name
`
185
186
`});
`
`@@ -261,10 +262,10 @@ function getOrSetAsyncId(object) {
`
261
262
`// the user to safeguard this call and make sure it's zero'd out when the
`
262
263
`// constructor is complete.
`
263
264
`function getDefaultTriggerAsyncId() {
`
264
``
`-
let defaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
`
``
265
`+
const defaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
`
265
266
`// If defaultTriggerAsyncId isn't set, use the executionAsyncId
`
266
267
`if (defaultTriggerAsyncId < 0)
`
267
``
`-
defaultTriggerAsyncId = async_id_fields[kExecutionAsyncId];
`
``
268
`+
return async_id_fields[kExecutionAsyncId];
`
268
269
`return defaultTriggerAsyncId;
`
269
270
`}
`
270
271
``
`@@ -393,8 +394,8 @@ function pushAsyncIds(asyncId, triggerAsyncId) {
`
393
394
``
394
395
`// This is the equivalent of the native pop_async_ids() call.
`
395
396
`function popAsyncIds(asyncId) {
`
396
``
`-
if (async_hook_fields[kStackLength] === 0) return false;
`
397
397
`const stackLength = async_hook_fields[kStackLength];
`
``
398
`+
if (stackLength === 0) return false;
`
398
399
``
399
400
`if (async_hook_fields[kCheck] > 0 &&
`
400
401
`async_id_fields[kExecutionAsyncId] !== asyncId) {
`