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 <[email protected]>
Reviewed-By: Vladimir de Turckheim <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Andrey Pechkurov <[email protected]>
Reviewed-By: Gerhard Stöbich <[email protected]>
  • Loading branch information
Qard authored and codebytere committed Jul 14, 2020
1 parent e7b7a9d commit 13d1cae
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.
const 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 @@ -103,8 +103,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 @@ -115,7 +120,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 @@ -498,6 +503,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 @@ -571,7 +571,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 @@ -441,7 +441,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 @@ -42,7 +42,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 13d1cae

Please sign in to comment.