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.

GetIntrinsic.js 9.8KB

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