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