Skip to content

Commit 122d2b5

Browse files
Qardcodebytere
authored andcommitted
domain: remove native domain code
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]>
1 parent ef05e15 commit 122d2b5

File tree

7 files changed

+14
-58
lines changed

7 files changed

+14
-58
lines changed

lib/domain.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const {
4242
ERR_UNHANDLED_ERROR
4343
} = require('internal/errors').codes;
4444
const { createHook } = require('async_hooks');
45+
const { useDomainTrampoline } = require('internal/async_hooks');
4546

4647
// TODO(addaleax): Use a non-internal solution for this.
4748
const kWeak = Symbol('kWeak');
@@ -145,7 +146,7 @@ function topLevelDomainCallback(cb, ...args) {
145146
// another one. The stack is each entered domain.
146147
let stack = [];
147148
exports._stack = stack;
148-
internalBinding('domain').enable(topLevelDomainCallback);
149+
useDomainTrampoline(topLevelDomainCallback);
149150

150151
function updateExceptionCapture() {
151152
if (stack.every((domain) => domain.listenerCount('error') === 0)) {

lib/internal/async_hooks.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,13 @@ const emitDestroyNative = emitHookFactory(destroy_symbol, 'emitDestroyNative');
106106
const emitPromiseResolveNative =
107107
emitHookFactory(promise_resolve_symbol, 'emitPromiseResolveNative');
108108

109-
function callbackTrampoline(asyncId, cb, domain_cb, ...args) {
110-
if (hasHooks(kBefore))
109+
let domain_cb;
110+
function useDomainTrampoline(fn) {
111+
domain_cb = fn;
112+
}
113+
114+
function callbackTrampoline(asyncId, cb, ...args) {
115+
if (asyncId && hasHooks(kBefore))
111116
emitBeforeNative(asyncId);
112117

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

121-
if (hasHooks(kAfter))
126+
if (asyncId && hasHooks(kAfter))
122127
emitAfterNative(asyncId);
123128

124129
return result;
@@ -564,6 +569,7 @@ module.exports = {
564569
emitAfter: emitAfterScript,
565570
emitDestroy: emitDestroyScript,
566571
registerDestroyHook,
572+
useDomainTrampoline,
567573
nativeHooks: {
568574
init: emitInitNative,
569575
before: emitBeforeNative,

node.gyp

-1
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,6 @@
582582
'src/node_contextify.cc',
583583
'src/node_credentials.cc',
584584
'src/node_dir.cc',
585-
'src/node_domain.cc',
586585
'src/node_env_var.cc',
587586
'src/node_errors.cc',
588587
'src/node_file.cc',

src/api/callback.cc

+3-16
Original file line numberDiff line numberDiff line change
@@ -173,29 +173,16 @@ MaybeLocal<Value> InternalMakeCallback(Environment* env,
173173
return MaybeLocal<Value>();
174174
}
175175

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

179-
if (asyncContext.async_id != 0 && hook_count != 0) {
180-
MaybeStackBuffer<Local<Value>, 16> args(3 + argc);
178+
if (hook_count != 0) {
179+
MaybeStackBuffer<Local<Value>, 16> args(2 + argc);
181180
args[0] = v8::Number::New(env->isolate(), asyncContext.async_id);
182181
args[1] = callback;
183-
if (domain_cb.IsEmpty()) {
184-
args[2] = Undefined(env->isolate());
185-
} else {
186-
args[2] = domain_cb;
187-
}
188182
for (int i = 0; i < argc; i++) {
189-
args[i + 3] = argv[i];
183+
args[i + 2] = argv[i];
190184
}
191185
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);
194-
args[0] = callback;
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]);
199186
} else {
200187
ret = callback->Call(env->context(), recv, argc, argv);
201188
}

src/env.h

-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,6 @@ constexpr size_t kFsStatsBufferLength =
433433
V(async_hooks_promise_resolve_function, v8::Function) \
434434
V(buffer_prototype_object, v8::Object) \
435435
V(crypto_key_object_constructor, v8::Function) \
436-
V(domain_callback, v8::Function) \
437436
V(domexception_function, v8::Function) \
438437
V(enhance_fatal_stack_after_inspector, v8::Function) \
439438
V(enhance_fatal_stack_before_inspector, v8::Function) \

src/node_binding.cc

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
V(config) \
4343
V(contextify) \
4444
V(credentials) \
45-
V(domain) \
4645
V(errors) \
4746
V(fs) \
4847
V(fs_dir) \

src/node_domain.cc

-35
This file was deleted.

0 commit comments

Comments
 (0)