Skip to content

Commit

Permalink
lib,src: make process global non-writable
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Dec 11, 2016
1 parent 61ed56a commit 54aec1e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
17 changes: 13 additions & 4 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@

'use strict';

(function(process) {
(function(global, process) {

function startup(global, process) {
// Expose the global object as a property on itself
// (Allows you to set stuff on `global` from anywhere in JavaScript.)
global.global = global;

function startup() {
const EventEmitter = NativeModule.require('events');
process._eventsCount = 0;

Expand Down Expand Up @@ -196,7 +200,12 @@
enumerable: false,
configurable: true
});
global.process = process;
Object.defineProperty(global, 'process', {
value: process,
writable: false,
enumerable: true,
configurable: true
});
const util = NativeModule.require('util');

// Deprecate GLOBAL and root
Expand Down Expand Up @@ -532,5 +541,5 @@
NativeModule._cache[this.id] = this;
};

startup();
startup(global, process);
});
8 changes: 2 additions & 6 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3441,19 +3441,15 @@ void LoadEnvironment(Environment* env) {

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

// Expose the global object as a property on itself
// (Allows you to set stuff on `global` from anywhere in JavaScript.)
global->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "global"), global);

// Now we call 'f' with the 'process' variable that we've built up with
// all our bindings. Inside bootstrap_node.js and internal/process we'll
// take care of assigning things to their places.

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

static void PrintHelp() {
Expand Down
6 changes: 4 additions & 2 deletions test/parallel/test-process-clobber.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* eslint-disable no-global-assign */
/* eslint-disable required-modules */
'use strict';

process = null; // Should not bring down program.
require('../common');
const assert = require('assert');

assert.throws(() => process = null, /Cannot assign to read only property/);

0 comments on commit 54aec1e

Please sign in to comment.