From 60115bedaded216e2cc559a4fe5966daac975a7d Mon Sep 17 00:00:00 2001 From: Bradley Maier Date: Mon, 8 Jul 2019 10:25:46 -0400 Subject: [PATCH 1/3] Defer to, and provide a shim for, globalThis --- src/core/has.ts | 2 ++ src/shim/global.ts | 3 +++ src/shim/globalThis.ts | 6 ++++++ tests/shim/unit/globalThis.ts | 12 ++++++++++++ 4 files changed, 23 insertions(+) create mode 100644 src/shim/globalThis.ts create mode 100644 tests/shim/unit/globalThis.ts diff --git a/src/core/has.ts b/src/core/has.ts index aeff457ad..b3b423982 100644 --- a/src/core/has.ts +++ b/src/core/has.ts @@ -476,3 +476,5 @@ add('dom-pointer-events', () => has('host-browser') && global.onpointerdown !== add('build-elide', false); add('test', false); + +add('global-this', () => typeof global.globalThis !== 'undefined'); diff --git a/src/shim/global.ts b/src/shim/global.ts index 3ac19ee1a..1731fd18f 100644 --- a/src/shim/global.ts +++ b/src/shim/global.ts @@ -2,6 +2,9 @@ const globalObject: any = (function(): any { // the only reliable means to get the global object is // `Function('return this')()` // However, this causes CSP violations in Chrome apps. + if (typeof globalThis !== 'undefined') { + return globalThis; + } if (typeof self !== 'undefined') { return self; } diff --git a/src/shim/globalThis.ts b/src/shim/globalThis.ts new file mode 100644 index 000000000..fe1a87a63 --- /dev/null +++ b/src/shim/globalThis.ts @@ -0,0 +1,6 @@ +import global from './global'; +import has from '../core/has'; + +if (!has('global-this')) { + global.globalThis = global; +} diff --git a/tests/shim/unit/globalThis.ts b/tests/shim/unit/globalThis.ts new file mode 100644 index 000000000..5abdf0e29 --- /dev/null +++ b/tests/shim/unit/globalThis.ts @@ -0,0 +1,12 @@ +import globalObj from '../../../src/shim/global'; +import '../../../src/shim/globalThis'; + +const { registerSuite } = intern.getInterface('object'); +const { assert } = intern.getPlugin('chai'); + +registerSuite('globalThis', { + 'ensures that the global object is available as `globalThis`'() { + // If tests are running under CSP, unsafe eval will be denied and this test will fail + assert.strictEqual(globalObj, globalThis); + } +}); From a9530169043d357fb5c2c5c4b8475db86dbfbc11 Mon Sep 17 00:00:00 2001 From: Bradley Maier Date: Mon, 8 Jul 2019 10:58:17 -0400 Subject: [PATCH 2/3] add test suite to tests --- tests/shim/unit/all.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/shim/unit/all.ts b/tests/shim/unit/all.ts index b40d8cb17..0bf91a4a2 100644 --- a/tests/shim/unit/all.ts +++ b/tests/shim/unit/all.ts @@ -7,6 +7,7 @@ import './util/wrapper'; import './AbortController'; import './array'; import './global'; +import './globalThis'; import './iterator'; import './main'; import './Map'; From bbf1d601e291d6f749fc3e4616daa99a032cb902 Mon Sep 17 00:00:00 2001 From: Bradley Maier Date: Mon, 8 Jul 2019 14:53:24 -0400 Subject: [PATCH 3/3] Remove innacurate comment --- tests/shim/unit/globalThis.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/shim/unit/globalThis.ts b/tests/shim/unit/globalThis.ts index 5abdf0e29..3dfd42500 100644 --- a/tests/shim/unit/globalThis.ts +++ b/tests/shim/unit/globalThis.ts @@ -6,7 +6,6 @@ const { assert } = intern.getPlugin('chai'); registerSuite('globalThis', { 'ensures that the global object is available as `globalThis`'() { - // If tests are running under CSP, unsafe eval will be denied and this test will fail assert.strictEqual(globalObj, globalThis); } });