Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: make global non-enumerable #10405

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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');