GetIntrinsic.js 8.2 KB

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