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

Add self #1118

Merged
merged 3 commits into from Oct 23, 2022
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,7 @@
- Don't await non-objects returned from functions passed to `AsyncIterator` helpers, [proposal-iterator-helpers/239](https://github.com/tc39/proposal-iterator-helpers/pull/239)
- `{ Iterator, AsyncIterator }.prototype.flatMap` supports returning both - iterables and iterators, [proposal-iterator-helpers/233](https://github.com/tc39/proposal-iterator-helpers/pull/233)
- Early exit on broken `.next` in missed cases of `{ Iterator, AsyncIterator }.from`, [proposal-iterator-helpers/232](https://github.com/tc39/proposal-iterator-helpers/pull/232)
- Added `self` polyfill as a part of [The Minimum Common Web Platform API](https://common-min-api.proposal.wintercg.org/), [specification](https://html.spec.whatwg.org/multipage/window-object.html#dom-self), [#1118](https://github.com/zloirock/core-js/issues/1118)
- Added `inverse` option to `core-js-compat`, [#1119](https://github.com/zloirock/core-js/issues/1119)
- Added `format` option to `core-js-builder`, [#1120](https://github.com/zloirock/core-js/issues/1120)
- Added NodeJS 19.0 compat data
Expand Down
17 changes: 16 additions & 1 deletion README.md
Expand Up @@ -159,6 +159,7 @@ queueMicrotask(() => console.log('called as microtask'));
- [Pre-stage 0 proposals](#pre-stage-0-proposals)
- [`Reflect` metadata](#reflect-metadata)
- [Web standards](#web-standards)
- [`self`](#self)
- [`structuredClone`](#structuredclone)
- [Base64 utility methods](#base64-utility-methods)
- [`setTimeout` and `setInterval`](#settimeout-and-setinterval)
Expand Down Expand Up @@ -1831,7 +1832,7 @@ JSON.stringify({ '𠮷': ['\uDF06\uD834'] }); // => '{"𠮷":["\\udf06\\ud834"]}
#### ECMAScript: globalThis[⬆](#index)
Module [`es.global-this`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/es.global-this.js).
```js
let globalThis: Object;
let globalThis: GlobalThisValue;
```
[*CommonJS entry points:*](#commonjs-api)
```js
Expand Down Expand Up @@ -3021,6 +3022,20 @@ Reflect.getOwnMetadata('foo', object); // => 'bar'
```

### Web standards[⬆](#index)
#### `self`[⬆](#index)
[Spec](https://html.spec.whatwg.org/multipage/window-object.html#dom-self), module [`web.self`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.self.js)
```js
getter self: GlobalThisValue;
```
[*CommonJS entry points:*](#commonjs-api)
```js
core-js(-pure)/stable|actual|full/self
```
[*Examples*](https://tinyurl.com/27nghouh):
```js
self.Array === Array; // => true
```

#### `structuredClone`[⬆](#index)
[Spec](https://html.spec.whatwg.org/multipage/structured-data.html#dom-structuredclone), module [`web.structured-clone`](https://github.com/zloirock/core-js/blob/master/packages/core-js/modules/web.structured-clone.js)
```js
Expand Down
9 changes: 9 additions & 0 deletions packages/core-js-compat/src/data.mjs
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions packages/core-js-compat/src/modules-by-versions.mjs
Expand Up @@ -160,5 +160,6 @@ export default {
3.26: [
'esnext.string.is-well-formed',
'esnext.string.to-well-formed',
'web.self',
],
};
8 changes: 8 additions & 0 deletions 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
});
3 changes: 3 additions & 0 deletions packages/core-js/actual/self.js
@@ -0,0 +1,3 @@
var parent = require('../stable/self');

module.exports = parent;
3 changes: 3 additions & 0 deletions packages/core-js/full/self.js
@@ -0,0 +1,3 @@
var parent = require('../actual/self');

module.exports = parent;
41 changes: 41 additions & 0 deletions 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 */ }
4 changes: 4 additions & 0 deletions packages/core-js/stable/self.js
@@ -0,0 +1,4 @@
require('../modules/web.self');
var path = require('../internals/path');

module.exports = path.self;
1 change: 1 addition & 0 deletions packages/core-js/web/index.js
Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions tests/commonjs.mjs
Expand Up @@ -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');
Expand Down
7 changes: 7 additions & 0 deletions tests/compat/tests.js
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions 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');
});
14 changes: 14 additions & 0 deletions 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');
}
});