diff --git a/src/node.cc b/src/node.cc index 5a2b4843bed284..c6cf07e1df7f8d 100644 --- a/src/node.cc +++ b/src/node.cc @@ -2983,6 +2983,13 @@ void StopProfilerIdleNotifier(const FunctionCallbackInfo& 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 { \ @@ -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 diff --git a/test/parallel/test-global-descriptors.js b/test/parallel/test-global-descriptors.js new file mode 100644 index 00000000000000..9816c24daa5d3f --- /dev/null +++ b/test/parallel/test-global-descriptors.js @@ -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');