Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
src: make global non-enumerable
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
commit 4744e3f0145fd08e80ef61ac82333fe34eabd424
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);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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');