Skip to content

Commit

Permalink
domain: remove native domain code
Browse files Browse the repository at this point in the history
With the async_hooks callback trampoline, domains no longer need any
native code. With this, domains can exist in pure JavaScript.

PR-URL: #33801
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
  • Loading branch information
Qard authored and addaleax committed Jun 19, 2020
1 parent 59e6230 commit 646e5a4
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 58 deletions.
3 changes: 2 additions & 1 deletion lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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)) {
Expand Down
12 changes: 9 additions & 3 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,13 @@ const emitDestroyNative = emitHookFactory(destroy_symbol, 'emitDestroyNative');
const emitPromiseResolveNative =
emitHookFactory(promise_resolve_symbol, 'emitPromiseResolveNative');

function callbackTrampoline(asyncId, cb, domain_cb, ...args) {
if (hasHooks(kBefore))
let domain_cb;
function useDomainTrampoline(fn) {
domain_cb = fn;
}

function callbackTrampoline(asyncId, cb, ...args) {
if (asyncId && hasHooks(kBefore))
emitBeforeNative(asyncId);

let result;
Expand All @@ -118,7 +123,7 @@ function callbackTrampoline(asyncId, cb, domain_cb, ...args) {
result = ReflectApply(cb, this, args);
}

if (hasHooks(kAfter))
if (asyncId && hasHooks(kAfter))
emitAfterNative(asyncId);

return result;
Expand Down Expand Up @@ -564,6 +569,7 @@ module.exports = {
emitAfter: emitAfterScript,
emitDestroy: emitDestroyScript,
registerDestroyHook,
useDomainTrampoline,
nativeHooks: {
init: emitInitNative,
before: emitBeforeNative,
Expand Down
1 change: 0 additions & 1 deletion node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,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',
Expand Down
19 changes: 3 additions & 16 deletions src/api/callback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,29 +173,16 @@ MaybeLocal<Value> InternalMakeCallback(Environment* env,
return MaybeLocal<Value>();
}

Local<Function> domain_cb = env->domain_callback();
MaybeLocal<Value> ret;

if (asyncContext.async_id != 0 && hook_count != 0) {
MaybeStackBuffer<Local<Value>, 16> args(3 + argc);
if (hook_count != 0) {
MaybeStackBuffer<Local<Value>, 16> args(2 + argc);
args[0] = v8::Number::New(env->isolate(), asyncContext.async_id);
args[1] = callback;
if (domain_cb.IsEmpty()) {
args[2] = Undefined(env->isolate());
} else {
args[2] = domain_cb;
}
for (int i = 0; i < argc; i++) {
args[i + 3] = argv[i];
args[i + 2] = argv[i];
}
ret = hook_cb->Call(env->context(), recv, args.length(), &args[0]);
} else if (asyncContext.async_id == 0 && !domain_cb.IsEmpty()) {
MaybeStackBuffer<Local<Value>, 16> args(1 + argc);
args[0] = callback;
for (int i = 0; i < argc; i++) {
args[i + 1] = argv[i];
}
ret = domain_cb->Call(env->context(), recv, args.length(), &args[0]);
} else {
ret = callback->Call(env->context(), recv, argc, argv);
}
Expand Down
1 change: 0 additions & 1 deletion src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ constexpr size_t kFsStatsBufferLength =
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) \
Expand Down
1 change: 0 additions & 1 deletion src/node_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
V(config) \
V(contextify) \
V(credentials) \
V(domain) \
V(errors) \
V(fs) \
V(fs_dir) \
Expand Down
35 changes: 0 additions & 35 deletions src/node_domain.cc

This file was deleted.

0 comments on commit 646e5a4

Please sign in to comment.