From c26f2bd7ed071d836e76127a9068a7feea5b1b51 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 13 Sep 2018 02:24:43 -0500 Subject: [PATCH] src: add `globalThis` Per https://github.com/tc39/proposal-global, the global value `globalThis` should be configurable, writable, and non-enumerable. --- lib/internal/per_context.js | 10 ++++++++++ test/parallel/test-global-descriptors.js | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 test/parallel/test-global-descriptors.js diff --git a/lib/internal/per_context.js b/lib/internal/per_context.js index f07e8822296b03..d4982344cb1478 100644 --- a/lib/internal/per_context.js +++ b/lib/internal/per_context.js @@ -3,6 +3,16 @@ // node::NewContext calls this script (function(global) { + // https://github.com/tc39/proposal-global + // https://github.com/nodejs/node/pull/22835 + // TODO(devsnek,ljharb) remove when V8 71 lands + Object.defineProperty(global, 'globalThis', { + value: global, + writable: true, + enumerable: false, + configurable: true, + }); + // https://github.com/nodejs/node/issues/14909 if (global.Intl) delete global.Intl.v8BreakIterator; diff --git a/test/parallel/test-global-descriptors.js b/test/parallel/test-global-descriptors.js new file mode 100644 index 00000000000000..ab56d949f16c50 --- /dev/null +++ b/test/parallel/test-global-descriptors.js @@ -0,0 +1,18 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); + +const actualGlobal = Function('return this')(); + +const { + value, + configurable, + enumerable, + writable +} = Object.getOwnPropertyDescriptor(actualGlobal, 'globalThis'); + +assert.strictEqual(value, actualGlobal, 'globalThis should be global object'); +assert.strictEqual(configurable, true, 'globalThis should be configurable'); +assert.strictEqual(enumerable, false, 'globalThis should be non-enumerable'); +assert.strictEqual(writable, true, 'globalThis should be writable');