You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
9.6 KiB

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