diff --git a/packages/core-js-compat/src/data.mjs b/packages/core-js-compat/src/data.mjs index 74b033ac7216..c25e9997e598 100644 --- a/packages/core-js-compat/src/data.mjs +++ b/packages/core-js-compat/src/data.mjs @@ -2520,6 +2520,15 @@ export const data = { node: '12.0', // '11.0', safari: '12.1', }, + 'web.self': { + chrome: '86', + // https://github.com/denoland/deno/issues/15765 + // deno: false, + // fails in early Chrome-based Edge + // edge: '12', + firefox: '31', + safari: '10', + }, 'web.set-immediate': { bun: '0.1.7', ie: '10', diff --git a/packages/core-js-compat/src/modules-by-versions.mjs b/packages/core-js-compat/src/modules-by-versions.mjs index a522a4a4de2d..b205bb46c72b 100644 --- a/packages/core-js-compat/src/modules-by-versions.mjs +++ b/packages/core-js-compat/src/modules-by-versions.mjs @@ -160,5 +160,6 @@ export default { 3.26: [ 'esnext.string.is-well-formed', 'esnext.string.to-well-formed', + 'web.self', ], }; diff --git a/packages/core-js-pure/override/modules/web.self.js b/packages/core-js-pure/override/modules/web.self.js new file mode 100644 index 000000000000..ebb1d0ecb769 --- /dev/null +++ b/packages/core-js-pure/override/modules/web.self.js @@ -0,0 +1,8 @@ +var $ = require('../internals/export'); +var global = require('../internals/global'); + +// `self` getter +// https://html.spec.whatwg.org/multipage/window-object.html#dom-self +$({ global: true, forced: global.self !== global }, { + self: global +}); diff --git a/packages/core-js/actual/self.js b/packages/core-js/actual/self.js new file mode 100644 index 000000000000..a95c76aa3d42 --- /dev/null +++ b/packages/core-js/actual/self.js @@ -0,0 +1,3 @@ +var parent = require('../stable/self'); + +module.exports = parent; diff --git a/packages/core-js/full/self.js b/packages/core-js/full/self.js new file mode 100644 index 000000000000..b889db5abe31 --- /dev/null +++ b/packages/core-js/full/self.js @@ -0,0 +1,3 @@ +var parent = require('../actual/self'); + +module.exports = parent; diff --git a/packages/core-js/modules/web.self.js b/packages/core-js/modules/web.self.js new file mode 100644 index 000000000000..a89518e9bd72 --- /dev/null +++ b/packages/core-js/modules/web.self.js @@ -0,0 +1,41 @@ +'use strict'; +var $ = require('../internals/export'); +var global = require('../internals/global'); +var defineBuiltInAccessor = require('../internals/define-built-in-accessor'); +var DESCRIPTORS = require('../internals/descriptors'); + +var $TypeError = TypeError; +// eslint-disable-next-line es/no-object-defineproperty -- safe +var defineProperty = Object.defineProperty; +var INCORRECT_VALUE = global.self !== global; + +// `self` getter +// https://html.spec.whatwg.org/multipage/window-object.html#dom-self +try { + if (DESCRIPTORS) { + // eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe + var descriptor = Object.getOwnPropertyDescriptor(global, 'self'); + // some engines have `self`, but with incorrect descriptor + // https://github.com/denoland/deno/issues/15765 + if (INCORRECT_VALUE || !descriptor || !descriptor.get || !descriptor.enumerable) { + defineBuiltInAccessor(global, 'self', { + get: function self() { + return global; + }, + set: function self(value) { + if (this !== global) throw $TypeError('Illegal invocation'); + defineProperty(global, 'self', { + value: value, + writable: true, + configurable: true, + enumerable: true + }); + }, + configurable: true, + enumerable: true + }); + } + } else $({ global: true, simple: true, forced: INCORRECT_VALUE }, { + self: global + }); +} catch (error) { /* empty */ } diff --git a/packages/core-js/stable/self.js b/packages/core-js/stable/self.js new file mode 100644 index 000000000000..d40f0e337da3 --- /dev/null +++ b/packages/core-js/stable/self.js @@ -0,0 +1,4 @@ +require('../modules/web.self'); +var path = require('../internals/path'); + +module.exports = path.self; diff --git a/packages/core-js/web/index.js b/packages/core-js/web/index.js index 92031d808df3..78cf6b984a9e 100644 --- a/packages/core-js/web/index.js +++ b/packages/core-js/web/index.js @@ -7,6 +7,7 @@ require('../modules/web.dom-exception.stack'); require('../modules/web.dom-exception.to-string-tag'); require('../modules/web.immediate'); require('../modules/web.queue-microtask'); +require('../modules/web.self'); require('../modules/web.structured-clone'); require('../modules/web.timers'); require('../modules/web.url'); diff --git a/tests/commonjs.mjs b/tests/commonjs.mjs index 3a6d99ec2ae6..6163e5318cb7 100644 --- a/tests/commonjs.mjs +++ b/tests/commonjs.mjs @@ -584,6 +584,7 @@ for (PATH of ['core-js-pure', 'core-js']) { ok(typeof load(NS, 'dom-collections').iterator == 'function'); ok(typeof load(NS, 'dom-collections/for-each') == 'function'); ok(typeof load(NS, 'dom-collections/iterator') == 'function'); + ok(load(NS, 'self').Math === Math); ok(typeof load(NS, 'set-timeout') == 'function'); ok(typeof load(NS, 'set-interval') == 'function'); ok(typeof load(NS, 'set-immediate') == 'function'); diff --git a/tests/compat/tests.js b/tests/compat/tests.js index 8af96b031716..03b5c0fb59c3 100644 --- a/tests/compat/tests.js +++ b/tests/compat/tests.js @@ -1853,6 +1853,13 @@ GLOBAL.tests = { 'web.queue-microtask': function () { return Object.getOwnPropertyDescriptor(GLOBAL, 'queueMicrotask').value; }, + 'web.self': function () { + // eslint-disable-next-line no-restricted-globals -- safe + if (self !== GLOBAL) return false; + if (!DESCRIPTORS_SUPPORT) return true; + var descriptor = Object.getOwnPropertyDescriptor(GLOBAL, 'self'); + return descriptor.get && descriptor.enumerable; + }, 'web.set-immediate': IMMEDIATE, 'web.set-interval': TIMERS, 'web.set-timeout': TIMERS, diff --git a/tests/pure/web.self.js b/tests/pure/web.self.js new file mode 100644 index 000000000000..a14ca62ca528 --- /dev/null +++ b/tests/pure/web.self.js @@ -0,0 +1,6 @@ +import self from 'core-js-pure/stable/self'; + +QUnit.test('self', assert => { + assert.same(self, Object(self), 'is object'); + assert.same(self.Math, Math, 'contains globals'); +}); diff --git a/tests/tests/web.self.js b/tests/tests/web.self.js new file mode 100644 index 000000000000..4b6991dc153d --- /dev/null +++ b/tests/tests/web.self.js @@ -0,0 +1,14 @@ +/* eslint-disable no-restricted-globals -- safe */ +import { DESCRIPTORS } from '../helpers/constants'; + +QUnit.test('self', assert => { + assert.same(self, Object(self), 'is object'); + assert.same(self.Math, Math, 'contains globals'); + if (DESCRIPTORS) { + const descriptor = Object.getOwnPropertyDescriptor(self, 'self'); + // can't be properly defined (non-configurable) in some ancient engines like PhantomJS + // assert.isFunction(descriptor.get, 'a getter'); + // assert.true(descriptor.configurable, 'configurable'); + assert.true(descriptor.enumerable, 'enumerable'); + } +});