es.symbol.constructor.js 10 KB

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