diff --git a/lib/domain.js b/lib/domain.js index f18823960cde02..febd7620f21500 100644 --- a/lib/domain.js +++ b/lib/domain.js @@ -42,6 +42,7 @@ const { ERR_UNHANDLED_ERROR } = require('internal/errors').codes; const { createHook } = require('async_hooks'); +const { useDomainTrampoline } = require('internal/async_hooks'); // TODO(addaleax): Use a non-internal solution for this. const kWeak = Symbol('kWeak'); @@ -145,7 +146,7 @@ function topLevelDomainCallback(cb, ...args) { // another one. The stack is each entered domain. let stack = []; exports._stack = stack; -internalBinding('domain').enable(topLevelDomainCallback); +useDomainTrampoline(topLevelDomainCallback); function updateExceptionCapture() { if (stack.every((domain) => domain.listenerCount('error') === 0)) { diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index da7b2745e53bbc..fc894ebe4d773f 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -1,15 +1,18 @@ 'use strict'; const { + ArrayPrototypeUnshift, Error, FunctionPrototypeBind, ObjectPrototypeHasOwnProperty, ObjectDefineProperty, Promise, + ReflectApply, Symbol, } = primordials; const async_wrap = internalBinding('async_wrap'); +const { setCallbackTrampoline } = async_wrap; /* async_hook_fields is a Uint32Array wrapping the uint32_t array of * Environment::AsyncHooks::fields_[]. Each index tracks the number of active * hooks for each type. @@ -103,6 +106,31 @@ const emitDestroyNative = emitHookFactory(destroy_symbol, 'emitDestroyNative'); const emitPromiseResolveNative = emitHookFactory(promise_resolve_symbol, 'emitPromiseResolveNative'); +let domain_cb; +function useDomainTrampoline(fn) { + domain_cb = fn; +} + +function callbackTrampoline(asyncId, cb, ...args) { + if (asyncId && hasHooks(kBefore)) + emitBeforeNative(asyncId); + + let result; + if (typeof domain_cb === 'function') { + ArrayPrototypeUnshift(args, cb); + result = ReflectApply(domain_cb, this, args); + } else { + result = ReflectApply(cb, this, args); + } + + if (asyncId && hasHooks(kAfter)) + emitAfterNative(asyncId); + + return result; +} + +setCallbackTrampoline(callbackTrampoline); + const topLevelResource = {}; function executionAsyncResource() { @@ -541,6 +569,7 @@ module.exports = { emitAfter: emitAfterScript, emitDestroy: emitDestroyScript, registerDestroyHook, + useDomainTrampoline, nativeHooks: { init: emitInitNative, before: emitBeforeNative, diff --git a/node.gyp b/node.gyp index 1ebb8c2cc978e6..4d14c9c314e373 100644 --- a/node.gyp +++ b/node.gyp @@ -583,7 +583,6 @@ 'src/node_contextify.cc', 'src/node_credentials.cc', 'src/node_dir.cc', - 'src/node_domain.cc', 'src/node_env_var.cc', 'src/node_errors.cc', 'src/node_file.cc', diff --git a/src/api/callback.cc b/src/api/callback.cc index a03a2587b4c796..9f52c25cf0d900 100644 --- a/src/api/callback.cc +++ b/src/api/callback.cc @@ -158,20 +158,33 @@ MaybeLocal InternalMakeCallback(Environment* env, CHECK(!argv[i].IsEmpty()); #endif - InternalCallbackScope scope(env, resource, asyncContext); + Local hook_cb = env->async_hooks_callback_trampoline(); + int flags = InternalCallbackScope::kNoFlags; + int hook_count = 0; + if (!hook_cb.IsEmpty()) { + flags = InternalCallbackScope::kSkipAsyncHooks; + AsyncHooks* async_hooks = env->async_hooks(); + hook_count = async_hooks->fields()[AsyncHooks::kBefore] + + async_hooks->fields()[AsyncHooks::kAfter]; + } + + InternalCallbackScope scope(env, resource, asyncContext, flags); if (scope.Failed()) { return MaybeLocal(); } - Local domain_cb = env->domain_callback(); MaybeLocal ret; - if (asyncContext.async_id != 0 || domain_cb.IsEmpty()) { - ret = callback->Call(env->context(), recv, argc, argv); + + if (hook_count != 0) { + MaybeStackBuffer, 16> args(2 + argc); + args[0] = v8::Number::New(env->isolate(), asyncContext.async_id); + args[1] = callback; + for (int i = 0; i < argc; i++) { + args[i + 2] = argv[i]; + } + ret = hook_cb->Call(env->context(), recv, args.length(), &args[0]); } else { - std::vector> args(1 + argc); - args[0] = callback; - std::copy(&argv[0], &argv[argc], args.begin() + 1); - ret = domain_cb->Call(env->context(), recv, args.size(), &args[0]); + ret = callback->Call(env->context(), recv, argc, argv); } if (ret.IsEmpty()) { diff --git a/src/async_wrap.cc b/src/async_wrap.cc index 13c2c3ca5a5ec3..6f7e7af0df86e4 100644 --- a/src/async_wrap.cc +++ b/src/async_wrap.cc @@ -552,6 +552,14 @@ void AsyncWrap::QueueDestroyAsyncId(const FunctionCallbackInfo& args) { args[0].As()->Value()); } +void AsyncWrap::SetCallbackTrampoline(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + + CHECK(args[0]->IsFunction()); + + env->set_async_hooks_callback_trampoline(args[0].As()); +} + Local AsyncWrap::GetConstructorTemplate(Environment* env) { Local tmpl = env->async_wrap_ctor_template(); if (tmpl.IsEmpty()) { @@ -574,6 +582,7 @@ void AsyncWrap::Initialize(Local target, HandleScope scope(isolate); env->SetMethod(target, "setupHooks", SetupHooks); + env->SetMethod(target, "setCallbackTrampoline", SetCallbackTrampoline); env->SetMethod(target, "pushAsyncContext", PushAsyncContext); env->SetMethod(target, "popAsyncContext", PopAsyncContext); env->SetMethod(target, "queueDestroyAsyncId", QueueDestroyAsyncId); diff --git a/src/async_wrap.h b/src/async_wrap.h index 6004801117ba5a..f592e47beb5b8c 100644 --- a/src/async_wrap.h +++ b/src/async_wrap.h @@ -140,6 +140,8 @@ class AsyncWrap : public BaseObject { static void GetProviderType(const v8::FunctionCallbackInfo& args); static void QueueDestroyAsyncId( const v8::FunctionCallbackInfo& args); + static void SetCallbackTrampoline( + const v8::FunctionCallbackInfo& args); static void EmitAsyncInit(Environment* env, v8::Local object, diff --git a/src/env.h b/src/env.h index 4b8670de46a8c2..8d58badf289c69 100644 --- a/src/env.h +++ b/src/env.h @@ -425,13 +425,13 @@ constexpr size_t kFsStatsBufferLength = #define ENVIRONMENT_STRONG_PERSISTENT_VALUES(V) \ V(async_hooks_after_function, v8::Function) \ V(async_hooks_before_function, v8::Function) \ + V(async_hooks_callback_trampoline, v8::Function) \ V(async_hooks_binding, v8::Object) \ V(async_hooks_destroy_function, v8::Function) \ V(async_hooks_init_function, v8::Function) \ V(async_hooks_promise_resolve_function, v8::Function) \ V(buffer_prototype_object, v8::Object) \ V(crypto_key_object_constructor, v8::Function) \ - V(domain_callback, v8::Function) \ V(domexception_function, v8::Function) \ V(enhance_fatal_stack_after_inspector, v8::Function) \ V(enhance_fatal_stack_before_inspector, v8::Function) \ diff --git a/src/node_binding.cc b/src/node_binding.cc index 719ae95770602a..212533982a1c7b 100644 --- a/src/node_binding.cc +++ b/src/node_binding.cc @@ -42,7 +42,6 @@ V(config) \ V(contextify) \ V(credentials) \ - V(domain) \ V(errors) \ V(fs) \ V(fs_dir) \ diff --git a/src/node_domain.cc b/src/node_domain.cc deleted file mode 100644 index 9075845442fd4d..00000000000000 --- a/src/node_domain.cc +++ /dev/null @@ -1,35 +0,0 @@ -#include "env-inl.h" -#include "v8.h" - -namespace node { -namespace domain { - -using v8::Context; -using v8::Function; -using v8::FunctionCallbackInfo; -using v8::Local; -using v8::Object; -using v8::Value; - - -void Enable(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - - CHECK(args[0]->IsFunction()); - - env->set_domain_callback(args[0].As()); -} - -void Initialize(Local target, - Local unused, - Local context, - void* priv) { - Environment* env = Environment::GetCurrent(context); - - env->SetMethod(target, "enable", Enable); -} - -} // namespace domain -} // namespace node - -NODE_MODULE_CONTEXT_AWARE_INTERNAL(domain, node::domain::Initialize)