@@ -75,6 +75,8 @@ const { kInit, kBefore, kAfter, kDestroy, kTotals, kPromiseResolve,
7575 kCheck, kExecutionAsyncId, kAsyncIdCounter, kTriggerAsyncId,
7676 kDefaultTriggerAsyncId, kStackLength } = async_wrap . constants ;
7777
78+ const FunctionBind = Function . call . bind ( Function . prototype . bind ) ;
79+
7880// Used in AsyncHook and AsyncResource.
7981const async_id_symbol = Symbol ( 'asyncId' ) ;
8082const trigger_async_id_symbol = Symbol ( 'triggerAsyncId' ) ;
@@ -148,38 +150,37 @@ function emitInitNative(asyncId, type, triggerAsyncId, resource) {
148150 }
149151}
150152
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 ] ( asyncId ) ;
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 ] ( asyncId ) ;
165163 }
166- } catch ( e ) {
167- fatalError ( e ) ;
168- } finally {
169- active_hooks . call_depth -= 1 ;
170164 }
165+ } catch ( e ) {
166+ fatalError ( e ) ;
167+ } finally {
168+ active_hooks . call_depth -= 1 ;
169+ }
171170
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 ) ;
180182
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.
183184 Object . defineProperty ( fn , 'name' , {
184185 value : name
185186 } ) ;
@@ -261,10 +262,10 @@ function getOrSetAsyncId(object) {
261262// the user to safeguard this call and make sure it's zero'd out when the
262263// constructor is complete.
263264function getDefaultTriggerAsyncId ( ) {
264- let defaultTriggerAsyncId = async_id_fields [ kDefaultTriggerAsyncId ] ;
265+ const defaultTriggerAsyncId = async_id_fields [ kDefaultTriggerAsyncId ] ;
265266 // If defaultTriggerAsyncId isn't set, use the executionAsyncId
266267 if ( defaultTriggerAsyncId < 0 )
267- defaultTriggerAsyncId = async_id_fields [ kExecutionAsyncId ] ;
268+ return async_id_fields [ kExecutionAsyncId ] ;
268269 return defaultTriggerAsyncId ;
269270}
270271
@@ -393,8 +394,8 @@ function pushAsyncIds(asyncId, triggerAsyncId) {
393394
394395// This is the equivalent of the native pop_async_ids() call.
395396function popAsyncIds ( asyncId ) {
396- if ( async_hook_fields [ kStackLength ] === 0 ) return false ;
397397 const stackLength = async_hook_fields [ kStackLength ] ;
398+ if ( stackLength === 0 ) return false ;
398399
399400 if ( async_hook_fields [ kCheck ] > 0 &&
400401 async_id_fields [ kExecutionAsyncId ] !== asyncId ) {
0 commit comments