Skip to content

Commit

Permalink
add one more workaround of possible error with Symbol polyfill on g…
Browse files Browse the repository at this point in the history
…lobal object

#1289 (comment)
  • Loading branch information
zloirock committed Oct 18, 2023
1 parent 74582eb commit 9a76ac2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Changelog
##### Unreleased
- Added one more workaround of possible error with `Symbol` polyfill on global object, [#1289](https://github.com/zloirock/core-js/issues/1289#issuecomment-1768411444)
- Directly specified `type: commonjs` in `package.json` of all packages to avoid potential breakage in future Node versions, see [this issue](https://github.com/nodejs/TSC/issues/1445)
- Some minor internal fixes and optimizations
- Compat data improvements:
Expand Down
9 changes: 5 additions & 4 deletions packages/core-js/modules/es.symbol.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,15 @@ if (!NATIVE_SYMBOL) {
var description = !arguments.length || arguments[0] === undefined ? undefined : $toString(arguments[0]);
var tag = uid(description);
var setter = function (value) {
if (this === ObjectPrototype) call(setter, ObjectPrototypeSymbols, value);
if (hasOwn(this, HIDDEN) && hasOwn(this[HIDDEN], tag)) this[HIDDEN][tag] = false;
var $this = this === undefined ? global : this;
if ($this === ObjectPrototype) call(setter, ObjectPrototypeSymbols, value);
if (hasOwn($this, HIDDEN) && hasOwn($this[HIDDEN], tag)) $this[HIDDEN][tag] = false;
var descriptor = createPropertyDescriptor(1, value);
try {
setSymbolDescriptor(this, tag, descriptor);
setSymbolDescriptor($this, tag, descriptor);
} catch (error) {
if (!(error instanceof RangeError)) throw error;
fallbackDefineProperty(this, tag, descriptor);
fallbackDefineProperty($this, tag, descriptor);
}
};
if (DESCRIPTORS && USE_SETTER) setSymbolDescriptor(ObjectPrototype, tag, { configurable: true, set: setter });
Expand Down
7 changes: 7 additions & 0 deletions tests/unit-global/es.symbol.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ QUnit.test('Symbol', assert => {
}
});

QUnit.test('Symbol as global key', assert => {
const TEXT = 'test global symbol key';
const symbol = Symbol(TEXT);
GLOBAL[symbol] = TEXT;
assert.same(GLOBAL[symbol], TEXT, TEXT);
});

QUnit.test('Well-known Symbols', assert => {
const wks = [
'hasInstance',
Expand Down
9 changes: 8 additions & 1 deletion tests/unit-pure/es.symbol.constructor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DESCRIPTORS } from '../helpers/constants.js';
import { DESCRIPTORS, GLOBAL } from '../helpers/constants.js';

import create from 'core-js-pure/es/object/create';
import defineProperty from 'core-js-pure/es/object/define-property';
Expand Down Expand Up @@ -31,6 +31,13 @@ QUnit.test('Symbol', assert => {
}
});

QUnit.test('Symbol as global key', assert => {
const TEXT = 'test global symbol key';
const symbol = Symbol(TEXT);
GLOBAL[symbol] = TEXT;
assert.same(GLOBAL[symbol], TEXT, TEXT);
});

QUnit.test('Well-known Symbols', assert => {
const wks = [
'hasInstance',
Expand Down

0 comments on commit 9a76ac2

Please sign in to comment.