es.symbol.constructor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var globalThis = require('../internals/global-this');
  4. var call = require('../internals/function-call');
  5. var uncurryThis = require('../internals/function-uncurry-this');
  6. var IS_PURE = require('../internals/is-pure');
  7. var DESCRIPTORS = require('../internals/descriptors');
  8. var NATIVE_SYMBOL = require('../internals/symbol-constructor-detection');
  9. var fails = require('../internals/fails');
  10. var hasOwn = require('../internals/has-own-property');
  11. var isPrototypeOf = require('../internals/object-is-prototype-of');
  12. var anObject = require('../internals/an-object');
  13. var toIndexedObject = require('../internals/to-indexed-object');
  14. var toPropertyKey = require('../internals/to-property-key');
  15. var $toString = require('../internals/to-string');
  16. var createPropertyDescriptor = require('../internals/create-property-descriptor');
  17. var nativeObjectCreate = require('../internals/object-create');
  18. var objectKeys = require('../internals/object-keys');
  19. var getOwnPropertyNamesModule = require('../internals/object-get-own-property-names');
  20. var getOwnPropertyNamesExternal = require('../internals/object-get-own-property-names-external');
  21. var getOwnPropertySymbolsModule = require('../internals/object-get-own-property-symbols');
  22. var getOwnPropertyDescriptorModule = require('../internals/object-get-own-property-descriptor');
  23. var definePropertyModule = require('../internals/object-define-property');
  24. var definePropertiesModule = require('../internals/object-define-properties');
  25. var propertyIsEnumerableModule = require('../internals/object-property-is-enumerable');
  26. var defineBuiltIn = require('../internals/define-built-in');
  27. var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
  28. var shared = require('../internals/shared');
  29. var sharedKey = require('../internals/shared-key');
  30. var hiddenKeys = require('../internals/hidden-keys');
  31. var uid = require('../internals/uid');
  32. var wellKnownSymbol = require('../internals/well-known-symbol');
  33. var wrappedWellKnownSymbolModule = require('../internals/well-known-symbol-wrapped');
  34. var defineWellKnownSymbol = require('../internals/well-known-symbol-define');
  35. var defineSymbolToPrimitive = require('../internals/symbol-define-to-primitive');
  36. var setToStringTag = require('../internals/set-to-string-tag');
  37. var InternalStateModule = require('../internals/internal-state');
  38. var $forEach = require('../internals/array-iteration').forEach;
  39. var HIDDEN = sharedKey('hidden');
  40. var SYMBOL = 'Symbol';
  41. var PROTOTYPE = 'prototype';
  42. var setInternalState = InternalStateModule.set;
  43. var getInternalState = InternalStateModule.getterFor(SYMBOL);
  44. var ObjectPrototype = Object[PROTOTYPE];
  45. var $Symbol = globalThis.Symbol;
  46. var SymbolPrototype = $Symbol && $Symbol[PROTOTYPE];
  47. var RangeError = globalThis.RangeError;
  48. var TypeError = globalThis.TypeError;
  49. var QObject = globalThis.QObject;
  50. var nativeGetOwnPropertyDescriptor = getOwnPropertyDescriptorModule.f;
  51. var nativeDefineProperty = definePropertyModule.f;
  52. var nativeGetOwnPropertyNames = getOwnPropertyNamesExternal.f;
  53. var nativePropertyIsEnumerable = propertyIsEnumerableModule.f;
  54. var push = uncurryThis([].push);
  55. var AllSymbols = shared('symbols');
  56. var ObjectPrototypeSymbols = shared('op-symbols');
  57. var WellKnownSymbolsStore = shared('wks');
  58. // Don't use setters in Qt Script, https://github.com/zloirock/core-js/issues/173
  59. var USE_SETTER = !QObject || !QObject[PROTOTYPE] || !QObject[PROTOTYPE].findChild;
  60. // fallback for old Android, https://code.google.com/p/v8/issues/detail?id=687
  61. var fallbackDefineProperty = function (O, P, Attributes) {
  62. var ObjectPrototypeDescriptor = nativeGetOwnPropertyDescriptor(ObjectPrototype, P);
  63. if (ObjectPrototypeDescriptor) delete ObjectPrototype[P];
  64. nativeDefineProperty(O, P, Attributes);
  65. if (ObjectPrototypeDescriptor && O !== ObjectPrototype) {
  66. nativeDefineProperty(ObjectPrototype, P, ObjectPrototypeDescriptor);
  67. }
  68. };
  69. var setSymbolDescriptor = DESCRIPTORS && fails(function () {
  70. return nativeObjectCreate(nativeDefineProperty({}, 'a', {
  71. get: function () { return nativeDefineProperty(this, 'a', { value: 7 }).a; }
  72. })).a !== 7;
  73. }) ? fallbackDefineProperty : nativeDefineProperty;
  74. var wrap = function (tag, description) {
  75. var symbol = AllSymbols[tag] = nativeObjectCreate(SymbolPrototype);
  76. setInternalState(symbol, {
  77. type: SYMBOL,
  78. tag: tag,
  79. description: description
  80. });
  81. if (!DESCRIPTORS) symbol.description = description;
  82. return symbol;
  83. };
  84. var $defineProperty = function defineProperty(O, P, Attributes) {
  85. if (O === ObjectPrototype) $defineProperty(ObjectPrototypeSymbols, P, Attributes);
  86. anObject(O);
  87. var key = toPropertyKey(P);
  88. anObject(Attributes);
  89. if (hasOwn(AllSymbols, key)) {
  90. if (!Attributes.enumerable) {
  91. if (!hasOwn(O, HIDDEN)) nativeDefineProperty(O, HIDDEN, createPropertyDescriptor(1, nativeObjectCreate(null)));
  92. O[HIDDEN][key] = true;
  93. } else {
  94. if (hasOwn(O, HIDDEN) && O[HIDDEN][key]) O[HIDDEN][key] = false;
  95. Attributes = nativeObjectCreate(Attributes, { enumerable: createPropertyDescriptor(0, false) });
  96. } return setSymbolDescriptor(O, key, Attributes);
  97. } return nativeDefineProperty(O, key, Attributes);
  98. };
  99. var $defineProperties = function defineProperties(O, Properties) {
  100. anObject(O);
  101. var properties = toIndexedObject(Properties);
  102. var keys = objectKeys(properties).concat($getOwnPropertySymbols(properties));
  103. $forEach(keys, function (key) {
  104. if (!DESCRIPTORS || call($propertyIsEnumerable, properties, key)) $defineProperty(O, key, properties[key]);
  105. });
  106. return O;
  107. };
  108. var $create = function create(O, Properties) {
  109. return Properties === undefined ? nativeObjectCreate(O) : $defineProperties(nativeObjectCreate(O), Properties);
  110. };
  111. var $propertyIsEnumerable = function propertyIsEnumerable(V) {
  112. var P = toPropertyKey(V);
  113. var enumerable = call(nativePropertyIsEnumerable, this, P);
  114. if (this === ObjectPrototype && hasOwn(AllSymbols, P) && !hasOwn(ObjectPrototypeSymbols, P)) return false;
  115. return enumerable || !hasOwn(this, P) || !hasOwn(AllSymbols, P) || hasOwn(this, HIDDEN) && this[HIDDEN][P]
  116. ? enumerable : true;
  117. };
  118. var $getOwnPropertyDescriptor = function getOwnPropertyDescriptor(O, P) {
  119. var it = toIndexedObject(O);
  120. var key = toPropertyKey(P);
  121. if (it === ObjectPrototype && hasOwn(AllSymbols, key) && !hasOwn(ObjectPrototypeSymbols, key)) return;
  122. var descriptor = nativeGetOwnPropertyDescriptor(it, key);
  123. if (descriptor && hasOwn(AllSymbols, key) && !(hasOwn(it, HIDDEN) && it[HIDDEN][key])) {
  124. descriptor.enumerable = true;
  125. }
  126. return descriptor;
  127. };
  128. var $getOwnPropertyNames = function getOwnPropertyNames(O) {
  129. var names = nativeGetOwnPropertyNames(toIndexedObject(O));
  130. var result = [];
  131. $forEach(names, function (key) {
  132. if (!hasOwn(AllSymbols, key) && !hasOwn(hiddenKeys, key)) push(result, key);
  133. });
  134. return result;
  135. };
  136. var $getOwnPropertySymbols = function (O) {
  137. var IS_OBJECT_PROTOTYPE = O === ObjectPrototype;
  138. var names = nativeGetOwnPropertyNames(IS_OBJECT_PROTOTYPE ? ObjectPrototypeSymbols : toIndexedObject(O));
  139. var result = [];
  140. $forEach(names, function (key) {
  141. if (hasOwn(AllSymbols, key) && (!IS_OBJECT_PROTOTYPE || hasOwn(ObjectPrototype, key))) {
  142. push(result, AllSymbols[key]);
  143. }
  144. });
  145. return result;
  146. };
  147. // `Symbol` constructor
  148. // https://tc39.es/ecma262/#sec-symbol-constructor
  149. if (!NATIVE_SYMBOL) {
  150. $Symbol = function Symbol() {
  151. if (isPrototypeOf(SymbolPrototype, this)) throw new TypeError('Symbol is not a constructor');
  152. var description = !arguments.length || arguments[0] === undefined ? undefined : $toString(arguments[0]);
  153. var tag = uid(description);
  154. var setter = function (value) {
  155. var $this = this === undefined ? globalThis : this;
  156. if ($this === ObjectPrototype) call(setter, ObjectPrototypeSymbols, value);
  157. if (hasOwn($this, HIDDEN) && hasOwn($this[HIDDEN], tag)) $this[HIDDEN][tag] = false;
  158. var descriptor = createPropertyDescriptor(1, value);
  159. try {
  160. setSymbolDescriptor($this, tag, descriptor);
  161. } catch (error) {
  162. if (!(error instanceof RangeError)) throw error;
  163. fallbackDefineProperty($this, tag, descriptor);
  164. }
  165. };
  166. if (DESCRIPTORS && USE_SETTER) setSymbolDescriptor(ObjectPrototype, tag, { configurable: true, set: setter });
  167. return wrap(tag, description);
  168. };
  169. SymbolPrototype = $Symbol[PROTOTYPE];
  170. defineBuiltIn(SymbolPrototype, 'toString', function toString() {
  171. return getInternalState(this).tag;
  172. });
  173. defineBuiltIn($Symbol, 'withoutSetter', function (description) {
  174. return wrap(uid(description), description);
  175. });
  176. propertyIsEnumerableModule.f = $propertyIsEnumerable;
  177. definePropertyModule.f = $defineProperty;
  178. definePropertiesModule.f = $defineProperties;
  179. getOwnPropertyDescriptorModule.f = $getOwnPropertyDescriptor;
  180. getOwnPropertyNamesModule.f = getOwnPropertyNamesExternal.f = $getOwnPropertyNames;
  181. getOwnPropertySymbolsModule.f = $getOwnPropertySymbols;
  182. wrappedWellKnownSymbolModule.f = function (name) {
  183. return wrap(wellKnownSymbol(name), name);
  184. };
  185. if (DESCRIPTORS) {
  186. // https://github.com/tc39/proposal-Symbol-description
  187. defineBuiltInAccessor(SymbolPrototype, 'description', {
  188. configurable: true,
  189. get: function description() {
  190. return getInternalState(this).description;
  191. }
  192. });
  193. if (!IS_PURE) {
  194. defineBuiltIn(ObjectPrototype, 'propertyIsEnumerable', $propertyIsEnumerable, { unsafe: true });
  195. }
  196. }
  197. }
  198. $({ global: true, constructor: true, wrap: true, forced: !NATIVE_SYMBOL, sham: !NATIVE_SYMBOL }, {
  199. Symbol: $Symbol
  200. });
  201. $forEach(objectKeys(WellKnownSymbolsStore), function (name) {
  202. defineWellKnownSymbol(name);
  203. });
  204. $({ target: SYMBOL, stat: true, forced: !NATIVE_SYMBOL }, {
  205. useSetter: function () { USE_SETTER = true; },
  206. useSimple: function () { USE_SETTER = false; }
  207. });
  208. $({ target: 'Object', stat: true, forced: !NATIVE_SYMBOL, sham: !DESCRIPTORS }, {
  209. // `Object.create` method
  210. // https://tc39.es/ecma262/#sec-object.create
  211. create: $create,
  212. // `Object.defineProperty` method
  213. // https://tc39.es/ecma262/#sec-object.defineproperty
  214. defineProperty: $defineProperty,
  215. // `Object.defineProperties` method
  216. // https://tc39.es/ecma262/#sec-object.defineproperties
  217. defineProperties: $defineProperties,
  218. // `Object.getOwnPropertyDescriptor` method
  219. // https://tc39.es/ecma262/#sec-object.getownpropertydescriptors
  220. getOwnPropertyDescriptor: $getOwnPropertyDescriptor
  221. });
  222. $({ target: 'Object', stat: true, forced: !NATIVE_SYMBOL }, {
  223. // `Object.getOwnPropertyNames` method
  224. // https://tc39.es/ecma262/#sec-object.getownpropertynames
  225. getOwnPropertyNames: $getOwnPropertyNames
  226. });
  227. // `Symbol.prototype[@@toPrimitive]` method
  228. // https://tc39.es/ecma262/#sec-symbol.prototype-@@toprimitive
  229. defineSymbolToPrimitive();
  230. // `Symbol.prototype[@@toStringTag]` property
  231. // https://tc39.es/ecma262/#sec-symbol.prototype-@@tostringtag
  232. setToStringTag($Symbol, SYMBOL);
  233. hiddenKeys[HIDDEN] = true;