1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

feat(cli): give access to process global everywhere (#25291)

This commit is contained in:
Luca Casonato 2024-09-04 11:04:06 +02:00 committed by GitHub
parent 4c3b17b547
commit b333dccee8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 17 additions and 40 deletions

View file

@ -54,7 +54,6 @@ const fn str_to_utf16<const N: usize>(s: &str) -> [u16; N] {
// - clearTimeout (both, but different implementation) // - clearTimeout (both, but different implementation)
// - global (node only) // - global (node only)
// - performance (both, but different implementation) // - performance (both, but different implementation)
// - process (node only)
// - setImmediate (node only) // - setImmediate (node only)
// - setInterval (both, but different implementation) // - setInterval (both, but different implementation)
// - setTimeout (both, but different implementation) // - setTimeout (both, but different implementation)
@ -62,7 +61,7 @@ const fn str_to_utf16<const N: usize>(s: &str) -> [u16; N] {
// UTF-16 encodings of the managed globals. THIS LIST MUST BE SORTED. // UTF-16 encodings of the managed globals. THIS LIST MUST BE SORTED.
#[rustfmt::skip] #[rustfmt::skip]
const MANAGED_GLOBALS: [&[u16]; 13] = [ const MANAGED_GLOBALS: [&[u16]; 12] = [
&str_to_utf16::<6>("Buffer"), &str_to_utf16::<6>("Buffer"),
&str_to_utf16::<17>("WorkerGlobalScope"), &str_to_utf16::<17>("WorkerGlobalScope"),
&str_to_utf16::<14>("clearImmediate"), &str_to_utf16::<14>("clearImmediate"),
@ -70,7 +69,6 @@ const MANAGED_GLOBALS: [&[u16]; 13] = [
&str_to_utf16::<12>("clearTimeout"), &str_to_utf16::<12>("clearTimeout"),
&str_to_utf16::<6>("global"), &str_to_utf16::<6>("global"),
&str_to_utf16::<11>("performance"), &str_to_utf16::<11>("performance"),
&str_to_utf16::<7>("process"),
&str_to_utf16::<4>("self"), &str_to_utf16::<4>("self"),
&str_to_utf16::<12>("setImmediate"), &str_to_utf16::<12>("setImmediate"),
&str_to_utf16::<11>("setInterval"), &str_to_utf16::<11>("setInterval"),

View file

@ -940,12 +940,11 @@ Module.prototype.require = function (id) {
// The module wrapper looks slightly different to Node. Instead of using one // The module wrapper looks slightly different to Node. Instead of using one
// wrapper function, we use two. The first one exists to performance optimize // wrapper function, we use two. The first one exists to performance optimize
// access to magic node globals, like `Buffer` or `process`. The second one // access to magic node globals, like `Buffer`. The second one is the actual
// is the actual wrapper function we run the users code in. // wrapper function we run the users code in. The only observable difference is
// The only observable difference is that in Deno `arguments.callee` is not // that in Deno `arguments.callee` is not null.
// null.
Module.wrapper = [ Module.wrapper = [
"(function (exports, require, module, __filename, __dirname, Buffer, clearImmediate, clearInterval, clearTimeout, global, process, setImmediate, setInterval, setTimeout, performance) { (function (exports, require, module, __filename, __dirname) {", "(function (exports, require, module, __filename, __dirname, Buffer, clearImmediate, clearInterval, clearTimeout, global, setImmediate, setInterval, setTimeout, performance) { (function (exports, require, module, __filename, __dirname) {",
"\n}).call(this, exports, require, module, __filename, __dirname); })", "\n}).call(this, exports, require, module, __filename, __dirname); })",
]; ];
Module.wrap = function (script) { Module.wrap = function (script) {
@ -1030,7 +1029,6 @@ Module.prototype._compile = function (content, filename, format) {
clearInterval, clearInterval,
clearTimeout, clearTimeout,
global, global,
process,
setImmediate, setImmediate,
setInterval, setInterval,
setTimeout, setTimeout,
@ -1049,7 +1047,6 @@ Module.prototype._compile = function (content, filename, format) {
clearInterval, clearInterval,
clearTimeout, clearTimeout,
global, global,
process,
setImmediate, setImmediate,
setInterval, setInterval,
setTimeout, setTimeout,

View file

@ -46,7 +46,6 @@ import {
} from "ext:deno_node/internal/validators.mjs"; } from "ext:deno_node/internal/validators.mjs";
import { spliceOne } from "ext:deno_node/_utils.ts"; import { spliceOne } from "ext:deno_node/_utils.ts";
import { nextTick } from "ext:deno_node/_process/process.ts"; import { nextTick } from "ext:deno_node/_process/process.ts";
import { nodeGlobals } from "ext:deno_node/00_globals.js";
const kCapture = Symbol("kCapture"); const kCapture = Symbol("kCapture");
const kErrorMonitor = Symbol("events.errorMonitor"); const kErrorMonitor = Symbol("events.errorMonitor");
@ -55,6 +54,11 @@ const kMaxEventTargetListenersWarned = Symbol(
"events.maxEventTargetListenersWarned", "events.maxEventTargetListenersWarned",
); );
let process;
export function setProcess(p) {
process = p;
}
/** /**
* Creates a new `EventEmitter` instance. * Creates a new `EventEmitter` instance.
* @param {{ captureRejections?: boolean; }} [opts] * @param {{ captureRejections?: boolean; }} [opts]
@ -469,7 +473,7 @@ function _addListener(target, type, listener, prepend) {
w.emitter = target; w.emitter = target;
w.type = type; w.type = type;
w.count = existing.length; w.count = existing.length;
nodeGlobals.process.emitWarning(w); process.emitWarning(w);
} }
} }

View file

@ -69,6 +69,7 @@ import * as constants from "ext:deno_node/internal_binding/constants.ts";
import * as uv from "ext:deno_node/internal_binding/uv.ts"; import * as uv from "ext:deno_node/internal_binding/uv.ts";
import type { BindingName } from "ext:deno_node/internal_binding/mod.ts"; import type { BindingName } from "ext:deno_node/internal_binding/mod.ts";
import { buildAllowedFlags } from "ext:deno_node/internal/process/per_thread.mjs"; import { buildAllowedFlags } from "ext:deno_node/internal/process/per_thread.mjs";
import { setProcess } from "ext:deno_node/_events.mjs";
const notImplementedEvents = [ const notImplementedEvents = [
"multipleResolves", "multipleResolves",
@ -960,4 +961,6 @@ internals.__bootstrapNodeProcess = function (
} }
}; };
setProcess(process);
export default process; export default process;

View file

@ -31,6 +31,7 @@ import * as webidl from "ext:deno_webidl/00_webidl.js";
import { DOMException } from "ext:deno_web/01_dom_exception.js"; import { DOMException } from "ext:deno_web/01_dom_exception.js";
import * as abortSignal from "ext:deno_web/03_abort_signal.js"; import * as abortSignal from "ext:deno_web/03_abort_signal.js";
import * as imageData from "ext:deno_web/16_image_data.js"; import * as imageData from "ext:deno_web/16_image_data.js";
import process from "node:process";
import { loadWebGPU } from "ext:deno_webgpu/00_init.js"; import { loadWebGPU } from "ext:deno_webgpu/00_init.js";
import * as webgpuSurface from "ext:deno_webgpu/02_surface.js"; import * as webgpuSurface from "ext:deno_webgpu/02_surface.js";
import { unstableIds } from "ext:runtime/90_deno_ns.js"; import { unstableIds } from "ext:runtime/90_deno_ns.js";
@ -137,6 +138,7 @@ const windowOrWorkerGlobalScope = {
fetch: core.propWritable(fetch.fetch), fetch: core.propWritable(fetch.fetch),
EventSource: core.propWritable(eventSource.EventSource), EventSource: core.propWritable(eventSource.EventSource),
performance: core.propWritable(performance.performance), performance: core.propWritable(performance.performance),
process: core.propWritable(process),
reportError: core.propWritable(event.reportError), reportError: core.propWritable(event.reportError),
setInterval: core.propWritable(timers.setInterval), setInterval: core.propWritable(timers.setInterval),
setTimeout: core.propWritable(timers.setTimeout), setTimeout: core.propWritable(timers.setTimeout),

View file

@ -1,5 +1,4 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import process from "node:process";
import { Buffer } from "node:buffer"; import { Buffer } from "node:buffer";
import { import {
clearImmediate, clearImmediate,
@ -10,15 +9,12 @@ import {
setTimeout, setTimeout,
} from "node:timers"; } from "node:timers";
import { performance } from "node:perf_hooks"; import { performance } from "node:perf_hooks";
import console from "node:console";
globalThis.Buffer = Buffer; globalThis.Buffer = Buffer;
globalThis.clearImmediate = clearImmediate; globalThis.clearImmediate = clearImmediate;
globalThis.clearInterval = clearInterval; globalThis.clearInterval = clearInterval;
globalThis.clearTimeout = clearTimeout; globalThis.clearTimeout = clearTimeout;
globalThis.console = console;
globalThis.global = globalThis; globalThis.global = globalThis;
globalThis.performance = performance; globalThis.performance = performance;
globalThis.process = process;
globalThis.setImmediate = setImmediate; globalThis.setImmediate = setImmediate;
globalThis.setInterval = setInterval; globalThis.setInterval = setInterval;
globalThis.setTimeout = setTimeout; globalThis.setTimeout = setTimeout;

View file

@ -15,7 +15,6 @@ type _TestHasProcessGlobal = AssertTrue<
export function deleteSetTimeout(): void; export function deleteSetTimeout(): void;
export function getSetTimeout(): typeof setTimeout; export function getSetTimeout(): typeof setTimeout;
export function checkProcessGlobal(): void;
export function checkWindowGlobal(): void; export function checkWindowGlobal(): void;
export function checkSelfGlobal(): void; export function checkSelfGlobal(): void;

View file

@ -10,11 +10,6 @@ exports.getSetTimeout = function () {
return globalThis.setTimeout; return globalThis.setTimeout;
}; };
exports.checkProcessGlobal = function () {
console.log("process" in globalThis);
console.log(Object.getOwnPropertyDescriptor(globalThis, "process") !== undefined);
};
exports.checkWindowGlobal = function () { exports.checkWindowGlobal = function () {
console.log("window" in globalThis); console.log("window" in globalThis);
console.log(Object.getOwnPropertyDescriptor(globalThis, "window") !== undefined); console.log(Object.getOwnPropertyDescriptor(globalThis, "window") !== undefined);

View file

@ -3,8 +3,7 @@
"steps": [ "steps": [
{ {
"args": "run main.ts", "args": "run main.ts",
"output": "error.out", "output": ""
"exitCode": 1
}, },
{ {
"args": "lint main.ts", "args": "lint main.ts",

View file

@ -1,4 +0,0 @@
error: Uncaught (in promise) ReferenceError: process is not defined
const _foo = process.env.FOO;
^
at [WILDCARD]main.ts:3:14

View file

@ -15,10 +15,6 @@ setTimeout 2 function
setTimeout 3 function setTimeout 3 function
setTimeout 4 function setTimeout 4 function
setTimeout 5 undefined setTimeout 5 undefined
process 1 false
process 2 false
true
true
window 1 false window 1 false
window 2 false window 2 false
false false

View file

@ -30,14 +30,6 @@ globals.deleteSetTimeout();
console.log("setTimeout 4", typeof globalThis.setTimeout); console.log("setTimeout 4", typeof globalThis.setTimeout);
console.log("setTimeout 5", typeof globals.getSetTimeout()); console.log("setTimeout 5", typeof globals.getSetTimeout());
// In Deno, the process global is not defined, but in Node it is.
console.log("process 1", "process" in globalThis);
console.log(
"process 2",
Object.getOwnPropertyDescriptor(globalThis, "process") !== undefined,
);
globals.checkProcessGlobal();
// In Deno 2 and Node.js, the window global is not defined. // In Deno 2 and Node.js, the window global is not defined.
console.log("window 1", "window" in globalThis); console.log("window 1", "window" in globalThis);
console.log( console.log(