Skip to content

Commit

Permalink
src: make global non-enumerable
Browse files Browse the repository at this point in the history
Per https://github.com/tc39/proposal-global, the global value `global`
should be configurable, writable, and non-enumerable. `node` has always
provided it as configurable, writable, and enumerable.

The plan is to make it non-enumerable, and barring strong evidence that
node can not ship `global` as non-enumerable, this will be how it lands
as stage 4 (in the spec).
  • Loading branch information
ljharb committed Dec 22, 2016
1 parent f44f509 commit 4744e3f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2983,6 +2983,13 @@ void StopProfilerIdleNotifier(const FunctionCallbackInfo<Value>& args) {
env->StopProfilerIdleNotifier();
}

#define NONENUMERABLE_PROPERTY(obj, str, var) \
do { \
obj->DefineOwnProperty(env->context(), \
OneByteString(env->isolate(), str), \
var, \
v8::DontEnum).FromJust(); \
} while (0)

#define READONLY_PROPERTY(obj, str, var) \
do { \
Expand Down Expand Up @@ -3450,9 +3457,9 @@ void LoadEnvironment(Environment* env) {

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

// Expose the global object as a property on itself
// Expose the global object as a non-enumerable property on itself
// (Allows you to set stuff on `global` from anywhere in JavaScript.)
global->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "global"), global);
NONENUMERABLE_PROPERTY(global, "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
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-global-descriptors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';
require('../common');

const assert = require('assert');

const {
value,
configurable,
enumerable,
writable
} = Object.getOwnPropertyDescriptor(global, 'global');

const actualGlobal = Function('return this')();
assert.strictEqual(value, actualGlobal, 'global should be global object');

assert.strictEqual(configurable, true, 'global should be configurable');
assert.strictEqual(enumerable, false, 'global should be non-enumerable');
assert.strictEqual(writable, true, 'global should be writable');

0 comments on commit 4744e3f

Please sign in to comment.