@@ -158,20 +158,46 @@ MaybeLocal<Value> InternalMakeCallback(Environment* env,
158
158
CHECK (!argv[i].IsEmpty ());
159
159
#endif
160
160
161
- InternalCallbackScope scope (env, resource, asyncContext);
161
+ Local<Function> hook_cb = env->async_hooks_callback_trampoline ();
162
+ int flags = InternalCallbackScope::kNoFlags ;
163
+ int hook_count = 0 ;
164
+ if (!hook_cb.IsEmpty ()) {
165
+ flags = InternalCallbackScope::kSkipAsyncHooks ;
166
+ AsyncHooks* async_hooks = env->async_hooks ();
167
+ hook_count = async_hooks->fields ()[AsyncHooks::kBefore ] +
168
+ async_hooks->fields ()[AsyncHooks::kAfter ];
169
+ }
170
+
171
+ InternalCallbackScope scope (env, resource, asyncContext, flags);
162
172
if (scope.Failed ()) {
163
173
return MaybeLocal<Value>();
164
174
}
165
175
166
176
Local<Function> domain_cb = env->domain_callback ();
167
177
MaybeLocal<Value> ret;
168
- if (asyncContext.async_id != 0 || domain_cb.IsEmpty ()) {
169
- ret = callback->Call (env->context (), recv, argc, argv);
170
- } else {
171
- std::vector<Local<Value>> args (1 + argc);
178
+
179
+ if (asyncContext.async_id != 0 && hook_count != 0 ) {
180
+ MaybeStackBuffer<Local<Value>, 16 > args (3 + argc);
181
+ args[0 ] = v8::Number::New (env->isolate (), asyncContext.async_id );
182
+ args[1 ] = callback;
183
+ if (domain_cb.IsEmpty ()) {
184
+ args[2 ] = Undefined (env->isolate ());
185
+ } else {
186
+ args[2 ] = domain_cb;
187
+ }
188
+ for (int i = 0 ; i < argc; i++) {
189
+ args[i + 3 ] = argv[i];
190
+ }
191
+ ret = hook_cb->Call (env->context (), recv, args.length (), &args[0 ]);
192
+ } else if (asyncContext.async_id == 0 && !domain_cb.IsEmpty ()) {
193
+ MaybeStackBuffer<Local<Value>, 16 > args (1 + argc);
172
194
args[0 ] = callback;
173
- std::copy (&argv[0 ], &argv[argc], args.begin () + 1 );
174
- ret = domain_cb->Call (env->context (), recv, args.size (), &args[0 ]);
195
+ for (int i = 0 ; i < argc; i++) {
196
+ args[i + 1 ] = argv[i];
197
+ }
198
+ ret = domain_cb->Call (env->context (), recv, args.length (), &args[0 ]);
199
+ } else {
200
+ ret = callback->Call (env->context (), recv, argc, argv);
175
201
}
176
202
177
203
if (ret.IsEmpty ()) {
0 commit comments