0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-28 22:46:31 +01:00

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: https://github.com/nodejs/node/pull/25189
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Joyee Cheung 2018-12-23 09:27:35 +08:00
parent 00944c7cc2
commit 8a60be4f0f
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
4 changed files with 37 additions and 42 deletions

View File

@ -14,8 +14,7 @@
// This file is compiled as if it's wrapped in a function with arguments // This file is compiled as if it's wrapped in a function with arguments
// passed by node::LoadEnvironment() // passed by node::LoadEnvironment()
/* global process, loaderExports, triggerFatalException */ /* global process, loaderExports, isMainThread */
/* global isMainThread */
const { internalBinding, NativeModule } = loaderExports; const { internalBinding, NativeModule } = loaderExports;
@ -605,12 +604,12 @@ function setupGlobalEncoding() {
function setupQueueMicrotask() { function setupQueueMicrotask() {
Object.defineProperty(global, 'queueMicrotask', { Object.defineProperty(global, 'queueMicrotask', {
get: () => { get() {
process.emitWarning('queueMicrotask() is experimental.', process.emitWarning('queueMicrotask() is experimental.',
'ExperimentalWarning'); 'ExperimentalWarning');
const { setupQueueMicrotask } = const { queueMicrotask } =
NativeModule.require('internal/queue_microtask'); NativeModule.require('internal/queue_microtask');
const queueMicrotask = setupQueueMicrotask(triggerFatalException);
Object.defineProperty(global, 'queueMicrotask', { Object.defineProperty(global, 'queueMicrotask', {
value: queueMicrotask, value: queueMicrotask,
writable: true, writable: true,
@ -619,7 +618,7 @@ function setupQueueMicrotask() {
}); });
return queueMicrotask; return queueMicrotask;
}, },
set: (v) => { set(v) {
Object.defineProperty(global, 'queueMicrotask', { Object.defineProperty(global, 'queueMicrotask', {
value: v, value: v,
writable: true, writable: true,

View File

@ -3,37 +3,36 @@
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes; const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
const { AsyncResource } = require('async_hooks'); const { AsyncResource } = require('async_hooks');
const { getDefaultTriggerAsyncId } = require('internal/async_hooks'); const { getDefaultTriggerAsyncId } = require('internal/async_hooks');
const { enqueueMicrotask } = internalBinding('util'); const {
enqueueMicrotask,
triggerFatalException
} = internalBinding('util');
const setupQueueMicrotask = (triggerFatalException) => { function queueMicrotask(callback) {
const queueMicrotask = (callback) => { if (typeof callback !== 'function') {
if (typeof callback !== 'function') { throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback);
throw new ERR_INVALID_ARG_TYPE('callback', 'function', callback); }
}
const asyncResource = new AsyncResource('Microtask', { const asyncResource = new AsyncResource('Microtask', {
triggerAsyncId: getDefaultTriggerAsyncId(), triggerAsyncId: getDefaultTriggerAsyncId(),
requireManualDestroy: true, requireManualDestroy: true,
});
enqueueMicrotask(() => {
asyncResource.runInAsyncScope(() => {
try {
callback();
} catch (error) {
// TODO(devsnek) remove this if
// https://bugs.chromium.org/p/v8/issues/detail?id=8326
// is resolved such that V8 triggers the fatal exception
// handler for microtasks
triggerFatalException(error);
} finally {
asyncResource.emitDestroy();
}
}); });
});
}
enqueueMicrotask(() => { module.exports = { queueMicrotask };
asyncResource.runInAsyncScope(() => {
try {
callback();
} catch (error) {
// TODO(devsnek) remove this if
// https://bugs.chromium.org/p/v8/issues/detail?id=8326
// is resolved such that V8 triggers the fatal exception
// handler for microtasks
triggerFatalException(error);
} finally {
asyncResource.emitDestroy();
}
});
});
};
return queueMicrotask;
};
module.exports = { setupQueueMicrotask };

View File

@ -1200,18 +1200,14 @@ void LoadEnvironment(Environment* env) {
return; return;
} }
// process, bootstrappers, loaderExports, triggerFatalException // process, loaderExports, isMainThread
std::vector<Local<String>> node_params = { std::vector<Local<String>> node_params = {
env->process_string(), env->process_string(),
FIXED_ONE_BYTE_STRING(isolate, "loaderExports"), FIXED_ONE_BYTE_STRING(isolate, "loaderExports"),
FIXED_ONE_BYTE_STRING(isolate, "triggerFatalException"),
FIXED_ONE_BYTE_STRING(isolate, "isMainThread")}; FIXED_ONE_BYTE_STRING(isolate, "isMainThread")};
std::vector<Local<Value>> node_args = { std::vector<Local<Value>> node_args = {
process, process,
loader_exports.ToLocalChecked(), loader_exports.ToLocalChecked(),
env->NewFunctionTemplate(FatalException)
->GetFunction(context)
.ToLocalChecked(),
Boolean::New(isolate, env->is_main_thread())}; Boolean::New(isolate, env->is_main_thread())};
if (ExecuteBootstrapper( if (ExecuteBootstrapper(

View File

@ -1,4 +1,5 @@
#include "node_internals.h" #include "node_internals.h"
#include "node_errors.h"
#include "node_watchdog.h" #include "node_watchdog.h"
namespace node { namespace node {
@ -221,7 +222,7 @@ void Initialize(Local<Object> target,
WatchdogHasPendingSigint); WatchdogHasPendingSigint);
env->SetMethod(target, "enqueueMicrotask", EnqueueMicrotask); env->SetMethod(target, "enqueueMicrotask", EnqueueMicrotask);
env->SetMethod(target, "triggerFatalException", FatalException);
Local<Object> constants = Object::New(env->isolate()); Local<Object> constants = Object::New(env->isolate());
NODE_DEFINE_CONSTANT(constants, ALL_PROPERTIES); NODE_DEFINE_CONSTANT(constants, ALL_PROPERTIES);
NODE_DEFINE_CONSTANT(constants, ONLY_WRITABLE); NODE_DEFINE_CONSTANT(constants, ONLY_WRITABLE);