Skip to content

Commit f32e6a8

Browse files
joyeecheungBridgeAR
authored andcommitted
process: make internal/queue_microtask.js more self-contained
- Instead of passing triggerFatalException through node.js, simply put it on `internalBinding('util')` which has to be loaded anyway. - Expose the implementation of `queueMicrotask` directly instead of through an unnecessary factory function. PR-URL: #25189 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 2e33ad1 commit f32e6a8

File tree

4 files changed

+36
-41
lines changed

4 files changed

+36
-41
lines changed

lib/internal/bootstrap/node.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
// This file is compiled as if it's wrapped in a function with arguments
1616
// passed by node::LoadEnvironment()
17-
/* global process, loaderExports, triggerFatalException */
18-
/* global isMainThread */
17+
/* global process, loaderExports, isMainThread */
1918

2019
const { internalBinding, NativeModule } = loaderExports;
2120

@@ -619,12 +618,12 @@ function setupGlobalEncoding() {
619618

620619
function setupQueueMicrotask() {
621620
Object.defineProperty(global, 'queueMicrotask', {
622-
get: () => {
621+
get() {
623622
process.emitWarning('queueMicrotask() is experimental.',
624623
'ExperimentalWarning');
625-
const { setupQueueMicrotask } =
624+
const { queueMicrotask } =
626625
NativeModule.require('internal/queue_microtask');
627-
const queueMicrotask = setupQueueMicrotask(triggerFatalException);
626+
628627
Object.defineProperty(global, 'queueMicrotask', {
629628
value: queueMicrotask,
630629
writable: true,
@@ -633,7 +632,7 @@ function setupQueueMicrotask() {
633632
});
634633
return queueMicrotask;
635634
},
636-
set: (v) => {
635+
set(v) {
637636
Object.defineProperty(global, 'queueMicrotask', {
638637
value: v,
639638
writable: true,

lib/internal/queue_microtask.js

+28-29
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,36 @@
33
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
44
const { AsyncResource } = require('async_hooks');
55
const { getDefaultTriggerAsyncId } = require('internal/async_hooks');
6-
const { enqueueMicrotask } = internalBinding('util');
6+
const {
7+
enqueueMicrotask,
8+
triggerFatalException
9+
} = internalBinding('util');
710

8-
const setupQueueMicrotask = (triggerFatalException) => {
9-
const queueMicrotask = (callback) => {
10-
if (typeof callback !== 'function') {
11-
throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback);
12-
}
11+
function queueMicrotask(callback) {
12+
if (typeof callback !== 'function') {
13+
throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback);
14+
}
1315

14-
const asyncResource = new AsyncResource('Microtask', {
15-
triggerAsyncId: getDefaultTriggerAsyncId(),
16-
requireManualDestroy: true,
17-
});
16+
const asyncResource = new AsyncResource('Microtask', {
17+
triggerAsyncId: getDefaultTriggerAsyncId(),
18+
requireManualDestroy: true,
19+
});
1820

19-
enqueueMicrotask(() => {
20-
asyncResource.runInAsyncScope(() => {
21-
try {
22-
callback();
23-
} catch (error) {
24-
// TODO(devsnek) remove this if
25-
// https://bugs.chromium.org/p/v8/issues/detail?id=8326
26-
// is resolved such that V8 triggers the fatal exception
27-
// handler for microtasks
28-
triggerFatalException(error);
29-
} finally {
30-
asyncResource.emitDestroy();
31-
}
32-
});
21+
enqueueMicrotask(() => {
22+
asyncResource.runInAsyncScope(() => {
23+
try {
24+
callback();
25+
} catch (error) {
26+
// TODO(devsnek) remove this if
27+
// https://bugs.chromium.org/p/v8/issues/detail?id=8326
28+
// is resolved such that V8 triggers the fatal exception
29+
// handler for microtasks
30+
triggerFatalException(error);
31+
} finally {
32+
asyncResource.emitDestroy();
33+
}
3334
});
34-
};
35-
36-
return queueMicrotask;
37-
};
35+
});
36+
}
3837

39-
module.exports = { setupQueueMicrotask };
38+
module.exports = { queueMicrotask };

src/node.cc

+1-5
Original file line numberDiff line numberDiff line change
@@ -1163,18 +1163,14 @@ void LoadEnvironment(Environment* env) {
11631163
return;
11641164
}
11651165

1166-
// process, bootstrappers, loaderExports, triggerFatalException
1166+
// process, loaderExports, isMainThread
11671167
std::vector<Local<String>> node_params = {
11681168
env->process_string(),
11691169
FIXED_ONE_BYTE_STRING(isolate, "loaderExports"),
1170-
FIXED_ONE_BYTE_STRING(isolate, "triggerFatalException"),
11711170
FIXED_ONE_BYTE_STRING(isolate, "isMainThread")};
11721171
std::vector<Local<Value>> node_args = {
11731172
process,
11741173
loader_exports.ToLocalChecked(),
1175-
env->NewFunctionTemplate(FatalException)
1176-
->GetFunction(context)
1177-
.ToLocalChecked(),
11781174
Boolean::New(isolate, env->is_main_thread())};
11791175

11801176
if (ExecuteBootstrapper(

src/node_util.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "node_internals.h"
2+
#include "node_errors.h"
23
#include "node_watchdog.h"
34

45
namespace node {
@@ -221,7 +222,7 @@ void Initialize(Local<Object> target,
221222
WatchdogHasPendingSigint);
222223

223224
env->SetMethod(target, "enqueueMicrotask", EnqueueMicrotask);
224-
225+
env->SetMethod(target, "triggerFatalException", FatalException);
225226
Local<Object> constants = Object::New(env->isolate());
226227
NODE_DEFINE_CONSTANT(constants, ALL_PROPERTIES);
227228
NODE_DEFINE_CONSTANT(constants, ONLY_WRITABLE);

0 commit comments

Comments
 (0)