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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Stephen Belanger 2020-06-09 14:50:03 -07:00 committed by Anna Henningsen
parent 59e6230a30
commit 646e5a4717
No known key found for this signature in database
GPG Key ID: A94130F0BFC8EBE9
7 changed files with 14 additions and 58 deletions

View File

@ -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)) {

View File

@ -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;
@ -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;
@ -564,6 +569,7 @@ module.exports = {
emitAfter: emitAfterScript,
emitDestroy: emitDestroyScript,
registerDestroyHook,
useDomainTrampoline,
nativeHooks: {
init: emitInitNative,
before: emitBeforeNative,

View File

@ -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',

View File

@ -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);
}

View File

@ -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) \

View File

@ -48,7 +48,6 @@
V(config) \
V(contextify) \
V(credentials) \
V(domain) \
V(errors) \
V(fs) \
V(fs_dir) \

View File

@ -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<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK(args[0]->IsFunction());
env->set_domain_callback(args[0].As<Function>());
}
void Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> 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)