GetIntrinsic.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. 'use strict';
  2. /* globals
  3. Set,
  4. Map,
  5. WeakSet,
  6. WeakMap,
  7. Promise,
  8. Symbol,
  9. Proxy,
  10. Atomics,
  11. SharedArrayBuffer,
  12. ArrayBuffer,
  13. DataView,
  14. Uint8Array,
  15. Float32Array,
  16. Float64Array,
  17. Int8Array,
  18. Int16Array,
  19. Int32Array,
  20. Uint8ClampedArray,
  21. Uint16Array,
  22. Uint32Array,
  23. */
  24. var undefined; // eslint-disable-line no-shadow-restricted-names
  25. var ThrowTypeError = Object.getOwnPropertyDescriptor
  26. ? (function () { return Object.getOwnPropertyDescriptor(arguments, 'callee').get; }())
  27. : function () { throw new TypeError(); };
  28. var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol';
  29. var getProto = Object.getPrototypeOf || function (x) { return x.__proto__; }; // eslint-disable-line no-proto
  30. var generator; // = function * () {};
  31. var generatorFunction = generator ? getProto(generator) : undefined;
  32. var asyncFn; // async function() {};
  33. var asyncFunction = asyncFn ? asyncFn.constructor : undefined;
  34. var asyncGen; // async function * () {};
  35. var asyncGenFunction = asyncGen ? getProto(asyncGen) : undefined;
  36. var asyncGenIterator = asyncGen ? asyncGen() : undefined;
  37. var TypedArray = typeof Uint8Array === 'undefined' ? undefined : getProto(Uint8Array);
  38. var INTRINSICS = {
  39. '$ %Array%': Array,
  40. '$ %ArrayBuffer%': typeof ArrayBuffer === 'undefined' ? undefined : ArrayBuffer,
  41. '$ %ArrayBufferPrototype%': typeof ArrayBuffer === 'undefined' ? undefined : ArrayBuffer.prototype,
  42. '$ %ArrayIteratorPrototype%': hasSymbols ? getProto([][Symbol.iterator]()) : undefined,
  43. '$ %ArrayPrototype%': Array.prototype,
  44. '$ %ArrayProto_entries%': Array.prototype.entries,
  45. '$ %ArrayProto_forEach%': Array.prototype.forEach,
  46. '$ %ArrayProto_keys%': Array.prototype.keys,
  47. '$ %ArrayProto_values%': Array.prototype.values,
  48. '$ %AsyncFromSyncIteratorPrototype%': undefined,
  49. '$ %AsyncFunction%': asyncFunction,
  50. '$ %AsyncFunctionPrototype%': asyncFunction ? asyncFunction.prototype : undefined,
  51. '$ %AsyncGenerator%': asyncGen ? getProto(asyncGenIterator) : undefined,
  52. '$ %AsyncGeneratorFunction%': asyncGenFunction,
  53. '$ %AsyncGeneratorPrototype%': asyncGenFunction ? asyncGenFunction.prototype : undefined,
  54. '$ %AsyncIteratorPrototype%': asyncGenIterator && hasSymbols && Symbol.asyncIterator ? asyncGenIterator[Symbol.asyncIterator]() : undefined,
  55. '$ %Atomics%': typeof Atomics === 'undefined' ? undefined : Atomics,
  56. '$ %Boolean%': Boolean,
  57. '$ %BooleanPrototype%': Boolean.prototype,
  58. '$ %DataView%': typeof DataView === 'undefined' ? undefined : DataView,
  59. '$ %DataViewPrototype%': typeof DataView === 'undefined' ? undefined : DataView.prototype,
  60. '$ %Date%': Date,
  61. '$ %DatePrototype%': Date.prototype,
  62. '$ %decodeURI%': decodeURI,
  63. '$ %decodeURIComponent%': decodeURIComponent,
  64. '$ %encodeURI%': encodeURI,
  65. '$ %encodeURIComponent%': encodeURIComponent,
  66. '$ %Error%': Error,
  67. '$ %ErrorPrototype%': Error.prototype,
  68. '$ %eval%': eval, // eslint-disable-line no-eval
  69. '$ %EvalError%': EvalError,
  70. '$ %EvalErrorPrototype%': EvalError.prototype,
  71. '$ %Float32Array%': typeof Float32Array === 'undefined' ? undefined : Float32Array,
  72. '$ %Float32ArrayPrototype%': typeof Float32Array === 'undefined' ? undefined : Float32Array.prototype,
  73. '$ %Float64Array%': typeof Float64Array === 'undefined' ? undefined : Float64Array,
  74. '$ %Float64ArrayPrototype%': typeof Float64Array === 'undefined' ? undefined : Float64Array.prototype,
  75. '$ %Function%': Function,
  76. '$ %FunctionPrototype%': Function.prototype,
  77. '$ %Generator%': generator ? getProto(generator()) : undefined,
  78. '$ %GeneratorFunction%': generatorFunction,
  79. '$ %GeneratorPrototype%': generatorFunction ? generatorFunction.prototype : undefined,
  80. '$ %Int8Array%': typeof Int8Array === 'undefined' ? undefined : Int8Array,
  81. '$ %Int8ArrayPrototype%': typeof Int8Array === 'undefined' ? undefined : Int8Array.prototype,
  82. '$ %Int16Array%': typeof Int16Array === 'undefined' ? undefined : Int16Array,
  83. '$ %Int16ArrayPrototype%': typeof Int16Array === 'undefined' ? undefined : Int8Array.prototype,
  84. '$ %Int32Array%': typeof Int32Array === 'undefined' ? undefined : Int32Array,
  85. '$ %Int32ArrayPrototype%': typeof Int32Array === 'undefined' ? undefined : Int32Array.prototype,
  86. '$ %isFinite%': isFinite,
  87. '$ %isNaN%': isNaN,
  88. '$ %IteratorPrototype%': hasSymbols ? getProto(getProto([][Symbol.iterator]())) : undefined,
  89. '$ %JSON%': JSON,
  90. '$ %JSONParse%': JSON.parse,
  91. '$ %Map%': typeof Map === 'undefined' ? undefined : Map,
  92. '$ %MapIteratorPrototype%': typeof Map === 'undefined' || !hasSymbols ? undefined : getProto(new Map()[Symbol.iterator]()),
  93. '$ %MapPrototype%': typeof Map === 'undefined' ? undefined : Map.prototype,
  94. '$ %Math%': Math,
  95. '$ %Number%': Number,
  96. '$ %NumberPrototype%': Number.prototype,
  97. '$ %Object%': Object,
  98. '$ %ObjectPrototype%': Object.prototype,
  99. '$ %ObjProto_toString%': Object.prototype.toString,
  100. '$ %ObjProto_valueOf%': Object.prototype.valueOf,
  101. '$ %parseFloat%': parseFloat,
  102. '$ %parseInt%': parseInt,
  103. '$ %Promise%': typeof Promise === 'undefined' ? undefined : Promise,
  104. '$ %PromisePrototype%': typeof Promise === 'undefined' ? undefined : Promise.prototype,
  105. '$ %PromiseProto_then%': typeof Promise === 'undefined' ? undefined : Promise.prototype.then,
  106. '$ %Promise_all%': typeof Promise === 'undefined' ? undefined : Promise.all,
  107. '$ %Promise_reject%': typeof Promise === 'undefined' ? undefined : Promise.reject,
  108. '$ %Promise_resolve%': typeof Promise === 'undefined' ? undefined : Promise.resolve,
  109. '$ %Proxy%': typeof Proxy === 'undefined' ? undefined : Proxy,
  110. '$ %RangeError%': RangeError,
  111. '$ %RangeErrorPrototype%': RangeError.prototype,
  112. '$ %ReferenceError%': ReferenceError,
  113. '$ %ReferenceErrorPrototype%': ReferenceError.prototype,
  114. '$ %Reflect%': typeof Reflect === 'undefined' ? undefined : Reflect,
  115. '$ %RegExp%': RegExp,
  116. '$ %RegExpPrototype%': RegExp.prototype,
  117. '$ %Set%': typeof Set === 'undefined' ? undefined : Set,
  118. '$ %SetIteratorPrototype%': typeof Set === 'undefined' || !hasSymbols ? undefined : getProto(new Set()[Symbol.iterator]()),
  119. '$ %SetPrototype%': typeof Set === 'undefined' ? undefined : Set.prototype,
  120. '$ %SharedArrayBuffer%': typeof SharedArrayBuffer === 'undefined' ? undefined : SharedArrayBuffer,
  121. '$ %SharedArrayBufferPrototype%': typeof SharedArrayBuffer === 'undefined' ? undefined : SharedArrayBuffer.prototype,
  122. '$ %String%': String,
  123. '$ %StringIteratorPrototype%': hasSymbols ? getProto(''[Symbol.iterator]()) : undefined,
  124. '$ %StringPrototype%': String.prototype,
  125. '$ %Symbol%': hasSymbols ? Symbol : undefined,
  126. '$ %SymbolPrototype%': hasSymbols ? Symbol.prototype : undefined,
  127. '$ %SyntaxError%': SyntaxError,
  128. '$ %SyntaxErrorPrototype%': SyntaxError.prototype,
  129. '$ %ThrowTypeError%': ThrowTypeError,
  130. '$ %TypedArray%': TypedArray,
  131. '$ %TypedArrayPrototype%': TypedArray ? TypedArray.prototype : undefined,
  132. '$ %TypeError%': TypeError,
  133. '$ %TypeErrorPrototype%': TypeError.prototype,
  134. '$ %Uint8Array%': typeof Uint8Array === 'undefined' ? undefined : Uint8Array,
  135. '$ %Uint8ArrayPrototype%': typeof Uint8Array === 'undefined' ? undefined : Uint8Array.prototype,
  136. '$ %Uint8ClampedArray%': typeof Uint8ClampedArray === 'undefined' ? undefined : Uint8ClampedArray,
  137. '$ %Uint8ClampedArrayPrototype%': typeof Uint8ClampedArray === 'undefined' ? undefined : Uint8ClampedArray.prototype,
  138. '$ %Uint16Array%': typeof Uint16Array === 'undefined' ? undefined : Uint16Array,
  139. '$ %Uint16ArrayPrototype%': typeof Uint16Array === 'undefined' ? undefined : Uint16Array.prototype,
  140. '$ %Uint32Array%': typeof Uint32Array === 'undefined' ? undefined : Uint32Array,
  141. '$ %Uint32ArrayPrototype%': typeof Uint32Array === 'undefined' ? undefined : Uint32Array.prototype,
  142. '$ %URIError%': URIError,
  143. '$ %URIErrorPrototype%': URIError.prototype,
  144. '$ %WeakMap%': typeof WeakMap === 'undefined' ? undefined : WeakMap,
  145. '$ %WeakMapPrototype%': typeof WeakMap === 'undefined' ? undefined : WeakMap.prototype,
  146. '$ %WeakSet%': typeof WeakSet === 'undefined' ? undefined : WeakSet,
  147. '$ %WeakSetPrototype%': typeof WeakSet === 'undefined' ? undefined : WeakSet.prototype
  148. };
  149. module.exports = function GetIntrinsic(name, allowMissing) {
  150. if (arguments.length > 1 && typeof allowMissing !== 'boolean') {
  151. throw new TypeError('"allowMissing" argument must be a boolean');
  152. }
  153. var key = '$ ' + name;
  154. if (!(key in INTRINSICS)) {
  155. throw new SyntaxError('intrinsic ' + name + ' does not exist!');
  156. }
  157. // istanbul ignore if // hopefully this is impossible to test :-)
  158. if (typeof INTRINSICS[key] === 'undefined' && !allowMissing) {
  159. throw new TypeError('intrinsic ' + name + ' exists, but is not available. Please file an issue!');
  160. }
  161. return INTRINSICS[key];
  162. };