Skip to content

Commit 54aec1e

Browse files
committed
lib,src: make process global non-writable
1 parent 61ed56a commit 54aec1e

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

lib/internal/bootstrap_node.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77

88
'use strict';
99

10-
(function(process) {
10+
(function(global, process) {
11+
12+
function startup(global, process) {
13+
// Expose the global object as a property on itself
14+
// (Allows you to set stuff on `global` from anywhere in JavaScript.)
15+
global.global = global;
1116

12-
function startup() {
1317
const EventEmitter = NativeModule.require('events');
1418
process._eventsCount = 0;
1519

@@ -196,7 +200,12 @@
196200
enumerable: false,
197201
configurable: true
198202
});
199-
global.process = process;
203+
Object.defineProperty(global, 'process', {
204+
value: process,
205+
writable: false,
206+
enumerable: true,
207+
configurable: true
208+
});
200209
const util = NativeModule.require('util');
201210

202211
// Deprecate GLOBAL and root
@@ -532,5 +541,5 @@
532541
NativeModule._cache[this.id] = this;
533542
};
534543

535-
startup();
544+
startup(global, process);
536545
});

src/node.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,19 +3441,15 @@ void LoadEnvironment(Environment* env) {
34413441

34423442
env->SetMethod(env->process_object(), "_rawDebug", RawDebug);
34433443

3444-
// Expose the global object as a property on itself
3445-
// (Allows you to set stuff on `global` from anywhere in JavaScript.)
3446-
global->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "global"), global);
3447-
34483444
// Now we call 'f' with the 'process' variable that we've built up with
34493445
// all our bindings. Inside bootstrap_node.js and internal/process we'll
34503446
// take care of assigning things to their places.
34513447

34523448
// We start the process this way in order to be more modular. Developers
34533449
// who do not like how bootstrap_node.js sets up the module system but do
34543450
// like Node's I/O bindings may want to replace 'f' with their own function.
3455-
Local<Value> arg = env->process_object();
3456-
f->Call(Null(env->isolate()), 1, &arg);
3451+
Local<Value> argv[] = { global, env->process_object() };
3452+
f->Call(Null(env->isolate()), arraysize(argv), argv);
34573453
}
34583454

34593455
static void PrintHelp() {
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-disable no-global-assign */
2-
/* eslint-disable required-modules */
32
'use strict';
43

5-
process = null; // Should not bring down program.
4+
require('../common');
5+
const assert = require('assert');
6+
7+
assert.throws(() => process = null, /Cannot assign to read only property/);

0 commit comments

Comments
 (0)