index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. 'use strict';
  2. var undefined;
  3. var $SyntaxError = SyntaxError;
  4. var $Function = Function;
  5. var $TypeError = TypeError;
  6. // eslint-disable-next-line consistent-return
  7. var getEvalledConstructor = function (expressionSyntax) {
  8. try {
  9. return $Function('"use strict"; return (' + expressionSyntax + ').constructor;')();
  10. } catch (e) {}
  11. };
  12. var $gOPD = Object.getOwnPropertyDescriptor;
  13. if ($gOPD) {
  14. try {
  15. $gOPD({}, '');
  16. } catch (e) {
  17. $gOPD = null; // this is IE 8, which has a broken gOPD
  18. }
  19. }
  20. var throwTypeError = function () {
  21. throw new $TypeError();
  22. };
  23. var ThrowTypeError = $gOPD
  24. ? (function () {
  25. try {
  26. // eslint-disable-next-line no-unused-expressions, no-caller, no-restricted-properties
  27. arguments.callee; // IE 8 does not throw here
  28. return throwTypeError;
  29. } catch (calleeThrows) {
  30. try {
  31. // IE 8 throws on Object.getOwnPropertyDescriptor(arguments, '')
  32. return $gOPD(arguments, 'callee').get;
  33. } catch (gOPDthrows) {
  34. return throwTypeError;
  35. }
  36. }
  37. }())
  38. : throwTypeError;
  39. var hasSymbols = require('has-symbols')();
  40. var getProto = Object.getPrototypeOf || function (x) { return x.__proto__; }; // eslint-disable-line no-proto
  41. var needsEval = {};
  42. var TypedArray = typeof Uint8Array === 'undefined' ? undefined : getProto(Uint8Array);
  43. var INTRINSICS = {
  44. '%AggregateError%': typeof AggregateError === 'undefined' ? undefined : AggregateError,
  45. '%Array%': Array,
  46. '%ArrayBuffer%': typeof ArrayBuffer === 'undefined' ? undefined : ArrayBuffer,
  47. '%ArrayIteratorPrototype%': hasSymbols ? getProto([][Symbol.iterator]()) : undefined,
  48. '%AsyncFromSyncIteratorPrototype%': undefined,
  49. '%AsyncFunction%': needsEval,
  50. '%AsyncGenerator%': needsEval,
  51. '%AsyncGeneratorFunction%': needsEval,
  52. '%AsyncIteratorPrototype%': needsEval,
  53. '%Atomics%': typeof Atomics === 'undefined' ? undefined : Atomics,
  54. '%BigInt%': typeof BigInt === 'undefined' ? undefined : BigInt,
  55. '%Boolean%': Boolean,
  56. '%DataView%': typeof DataView === 'undefined' ? undefined : DataView,
  57. '%Date%': Date,
  58. '%decodeURI%': decodeURI,
  59. '%decodeURIComponent%': decodeURIComponent,
  60. '%encodeURI%': encodeURI,
  61. '%encodeURIComponent%': encodeURIComponent,
  62. '%Error%': Error,
  63. '%eval%': eval, // eslint-disable-line no-eval
  64. '%EvalError%': EvalError,
  65. '%Float32Array%': typeof Float32Array === 'undefined' ? undefined : Float32Array,
  66. '%Float64Array%': typeof Float64Array === 'undefined' ? undefined : Float64Array,
  67. '%FinalizationRegistry%': typeof FinalizationRegistry === 'undefined' ? undefined : FinalizationRegistry,
  68. '%Function%': $Function,
  69. '%GeneratorFunction%': needsEval,
  70. '%Int8Array%': typeof Int8Array === 'undefined' ? undefined : Int8Array,
  71. '%Int16Array%': typeof Int16Array === 'undefined' ? undefined : Int16Array,
  72. '%Int32Array%': typeof Int32Array === 'undefined' ? undefined : Int32Array,
  73. '%isFinite%': isFinite,
  74. '%isNaN%': isNaN,
  75. '%IteratorPrototype%': hasSymbols ? getProto(getProto([][Symbol.iterator]())) : undefined,
  76. '%JSON%': typeof JSON === 'object' ? JSON : undefined,
  77. '%Map%': typeof Map === 'undefined' ? undefined : Map,
  78. '%MapIteratorPrototype%': typeof Map === 'undefined' || !hasSymbols ? undefined : getProto(new Map()[Symbol.iterator]()),
  79. '%Math%': Math,
  80. '%Number%': Number,
  81. '%Object%': Object,
  82. '%parseFloat%': parseFloat,
  83. '%parseInt%': parseInt,
  84. '%Promise%': typeof Promise === 'undefined' ? undefined : Promise,
  85. '%Proxy%': typeof Proxy === 'undefined' ? undefined : Proxy,
  86. '%RangeError%': RangeError,
  87. '%ReferenceError%': ReferenceError,
  88. '%Reflect%': typeof Reflect === 'undefined' ? undefined : Reflect,
  89. '%RegExp%': RegExp,
  90. '%Set%': typeof Set === 'undefined' ? undefined : Set,
  91. '%SetIteratorPrototype%': typeof Set === 'undefined' || !hasSymbols ? undefined : getProto(new Set()[Symbol.iterator]()),
  92. '%SharedArrayBuffer%': typeof SharedArrayBuffer === 'undefined' ? undefined : SharedArrayBuffer,
  93. '%String%': String,
  94. '%StringIteratorPrototype%': hasSymbols ? getProto(''[Symbol.iterator]()) : undefined,
  95. '%Symbol%': hasSymbols ? Symbol : undefined,
  96. '%SyntaxError%': $SyntaxError,
  97. '%ThrowTypeError%': ThrowTypeError,
  98. '%TypedArray%': TypedArray,
  99. '%TypeError%': $TypeError,
  100. '%Uint8Array%': typeof Uint8Array === 'undefined' ? undefined : Uint8Array,
  101. '%Uint8ClampedArray%': typeof Uint8ClampedArray === 'undefined' ? undefined : Uint8ClampedArray,
  102. '%Uint16Array%': typeof Uint16Array === 'undefined' ? undefined : Uint16Array,
  103. '%Uint32Array%': typeof Uint32Array === 'undefined' ? undefined : Uint32Array,
  104. '%URIError%': URIError,
  105. '%WeakMap%': typeof WeakMap === 'undefined' ? undefined : WeakMap,
  106. '%WeakRef%': typeof WeakRef === 'undefined' ? undefined : WeakRef,
  107. '%WeakSet%': typeof WeakSet === 'undefined' ? undefined : WeakSet
  108. };
  109. var doEval = function doEval(name) {
  110. var value;
  111. if (name === '%AsyncFunction%') {
  112. value = getEvalledConstructor('async function () {}');
  113. } else if (name === '%GeneratorFunction%') {
  114. value = getEvalledConstructor('function* () {}');
  115. } else if (name === '%AsyncGeneratorFunction%') {
  116. value = getEvalledConstructor('async function* () {}');
  117. } else if (name === '%AsyncGenerator%') {
  118. var fn = doEval('%AsyncGeneratorFunction%');
  119. if (fn) {
  120. value = fn.prototype;
  121. }
  122. } else if (name === '%AsyncIteratorPrototype%') {
  123. var gen = doEval('%AsyncGenerator%');
  124. if (gen) {
  125. value = getProto(gen.prototype);
  126. }
  127. }
  128. INTRINSICS[name] = value;
  129. return value;
  130. };
  131. var LEGACY_ALIASES = {
  132. '%ArrayBufferPrototype%': ['ArrayBuffer', 'prototype'],
  133. '%ArrayPrototype%': ['Array', 'prototype'],
  134. '%ArrayProto_entries%': ['Array', 'prototype', 'entries'],
  135. '%ArrayProto_forEach%': ['Array', 'prototype', 'forEach'],
  136. '%ArrayProto_keys%': ['Array', 'prototype', 'keys'],
  137. '%ArrayProto_values%': ['Array', 'prototype', 'values'],
  138. '%AsyncFunctionPrototype%': ['AsyncFunction', 'prototype'],
  139. '%AsyncGenerator%': ['AsyncGeneratorFunction', 'prototype'],
  140. '%AsyncGeneratorPrototype%': ['AsyncGeneratorFunction', 'prototype', 'prototype'],
  141. '%BooleanPrototype%': ['Boolean', 'prototype'],
  142. '%DataViewPrototype%': ['DataView', 'prototype'],
  143. '%DatePrototype%': ['Date', 'prototype'],
  144. '%ErrorPrototype%': ['Error', 'prototype'],
  145. '%EvalErrorPrototype%': ['EvalError', 'prototype'],
  146. '%Float32ArrayPrototype%': ['Float32Array', 'prototype'],
  147. '%Float64ArrayPrototype%': ['Float64Array', 'prototype'],
  148. '%FunctionPrototype%': ['Function', 'prototype'],
  149. '%Generator%': ['GeneratorFunction', 'prototype'],
  150. '%GeneratorPrototype%': ['GeneratorFunction', 'prototype', 'prototype'],
  151. '%Int8ArrayPrototype%': ['Int8Array', 'prototype'],
  152. '%Int16ArrayPrototype%': ['Int16Array', 'prototype'],
  153. '%Int32ArrayPrototype%': ['Int32Array', 'prototype'],
  154. '%JSONParse%': ['JSON', 'parse'],
  155. '%JSONStringify%': ['JSON', 'stringify'],
  156. '%MapPrototype%': ['Map', 'prototype'],
  157. '%NumberPrototype%': ['Number', 'prototype'],
  158. '%ObjectPrototype%': ['Object', 'prototype'],
  159. '%ObjProto_toString%': ['Object', 'prototype', 'toString'],
  160. '%ObjProto_valueOf%': ['Object', 'prototype', 'valueOf'],
  161. '%PromisePrototype%': ['Promise', 'prototype'],
  162. '%PromiseProto_then%': ['Promise', 'prototype', 'then'],
  163. '%Promise_all%': ['Promise', 'all'],
  164. '%Promise_reject%': ['Promise', 'reject'],
  165. '%Promise_resolve%': ['Promise', 'resolve'],
  166. '%RangeErrorPrototype%': ['RangeError', 'prototype'],
  167. '%ReferenceErrorPrototype%': ['ReferenceError', 'prototype'],
  168. '%RegExpPrototype%': ['RegExp', 'prototype'],
  169. '%SetPrototype%': ['Set', 'prototype'],
  170. '%SharedArrayBufferPrototype%': ['SharedArrayBuffer', 'prototype'],
  171. '%StringPrototype%': ['String', 'prototype'],
  172. '%SymbolPrototype%': ['Symbol', 'prototype'],
  173. '%SyntaxErrorPrototype%': ['SyntaxError', 'prototype'],
  174. '%TypedArrayPrototype%': ['TypedArray', 'prototype'],
  175. '%TypeErrorPrototype%': ['TypeError', 'prototype'],
  176. '%Uint8ArrayPrototype%': ['Uint8Array', 'prototype'],
  177. '%Uint8ClampedArrayPrototype%': ['Uint8ClampedArray', 'prototype'],
  178. '%Uint16ArrayPrototype%': ['Uint16Array', 'prototype'],
  179. '%Uint32ArrayPrototype%': ['Uint32Array', 'prototype'],
  180. '%URIErrorPrototype%': ['URIError', 'prototype'],
  181. '%WeakMapPrototype%': ['WeakMap', 'prototype'],
  182. '%WeakSetPrototype%': ['WeakSet', 'prototype']
  183. };
  184. var bind = require('function-bind');
  185. var hasOwn = require('has');
  186. var $concat = bind.call(Function.call, Array.prototype.concat);
  187. var $spliceApply = bind.call(Function.apply, Array.prototype.splice);
  188. var $replace = bind.call(Function.call, String.prototype.replace);
  189. var $strSlice = bind.call(Function.call, String.prototype.slice);
  190. var $exec = bind.call(Function.call, RegExp.prototype.exec);
  191. /* adapted from https://github.com/lodash/lodash/blob/4.17.15/dist/lodash.js#L6735-L6744 */
  192. var rePropName = /[^%.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|%$))/g;
  193. var reEscapeChar = /\\(\\)?/g; /** Used to match backslashes in property paths. */
  194. var stringToPath = function stringToPath(string) {
  195. var first = $strSlice(string, 0, 1);
  196. var last = $strSlice(string, -1);
  197. if (first === '%' && last !== '%') {
  198. throw new $SyntaxError('invalid intrinsic syntax, expected closing `%`');
  199. } else if (last === '%' && first !== '%') {
  200. throw new $SyntaxError('invalid intrinsic syntax, expected opening `%`');
  201. }
  202. var result = [];
  203. $replace(string, rePropName, function (match, number, quote, subString) {
  204. result[result.length] = quote ? $replace(subString, reEscapeChar, '$1') : number || match;
  205. });
  206. return result;
  207. };
  208. /* end adaptation */
  209. var getBaseIntrinsic = function getBaseIntrinsic(name, allowMissing) {
  210. var intrinsicName = name;
  211. var alias;
  212. if (hasOwn(LEGACY_ALIASES, intrinsicName)) {
  213. alias = LEGACY_ALIASES[intrinsicName];
  214. intrinsicName = '%' + alias[0] + '%';
  215. }
  216. if (hasOwn(INTRINSICS, intrinsicName)) {
  217. var value = INTRINSICS[intrinsicName];
  218. if (value === needsEval) {
  219. value = doEval(intrinsicName);
  220. }
  221. if (typeof value === 'undefined' && !allowMissing) {
  222. throw new $TypeError('intrinsic ' + name + ' exists, but is not available. Please file an issue!');
  223. }
  224. return {
  225. alias: alias,
  226. name: intrinsicName,
  227. value: value
  228. };
  229. }
  230. throw new $SyntaxError('intrinsic ' + name + ' does not exist!');
  231. };
  232. module.exports = function GetIntrinsic(name, allowMissing) {
  233. if (typeof name !== 'string' || name.length === 0) {
  234. throw new $TypeError('intrinsic name must be a non-empty string');
  235. }
  236. if (arguments.length > 1 && typeof allowMissing !== 'boolean') {
  237. throw new $TypeError('"allowMissing" argument must be a boolean');
  238. }
  239. if ($exec(/^%?[^%]*%?$/g, name) === null) {
  240. throw new $SyntaxError('`%` may not be present anywhere but at the beginning and end of the intrinsic name');
  241. }
  242. var parts = stringToPath(name);
  243. var intrinsicBaseName = parts.length > 0 ? parts[0] : '';
  244. var intrinsic = getBaseIntrinsic('%' + intrinsicBaseName + '%', allowMissing);
  245. var intrinsicRealName = intrinsic.name;
  246. var value = intrinsic.value;
  247. var skipFurtherCaching = false;
  248. var alias = intrinsic.alias;
  249. if (alias) {
  250. intrinsicBaseName = alias[0];
  251. $spliceApply(parts, $concat([0, 1], alias));
  252. }
  253. for (var i = 1, isOwn = true; i < parts.length; i += 1) {
  254. var part = parts[i];
  255. var first = $strSlice(part, 0, 1);
  256. var last = $strSlice(part, -1);
  257. if (
  258. (
  259. (first === '"' || first === "'" || first === '`')
  260. || (last === '"' || last === "'" || last === '`')
  261. )
  262. && first !== last
  263. ) {
  264. throw new $SyntaxError('property names with quotes must have matching quotes');
  265. }
  266. if (part === 'constructor' || !isOwn) {
  267. skipFurtherCaching = true;
  268. }
  269. intrinsicBaseName += '.' + part;
  270. intrinsicRealName = '%' + intrinsicBaseName + '%';
  271. if (hasOwn(INTRINSICS, intrinsicRealName)) {
  272. value = INTRINSICS[intrinsicRealName];
  273. } else if (value != null) {
  274. if (!(part in value)) {
  275. if (!allowMissing) {
  276. throw new $TypeError('base intrinsic for ' + name + ' exists, but the property is not available.');
  277. }
  278. return void undefined;
  279. }
  280. if ($gOPD && (i + 1) >= parts.length) {
  281. var desc = $gOPD(value, part);
  282. isOwn = !!desc;
  283. // By convention, when a data property is converted to an accessor
  284. // property to emulate a data property that does not suffer from
  285. // the override mistake, that accessor's getter is marked with
  286. // an `originalValue` property. Here, when we detect this, we
  287. // uphold the illusion by pretending to see that original data
  288. // property, i.e., returning the value rather than the getter
  289. // itself.
  290. if (isOwn && 'get' in desc && !('originalValue' in desc.get)) {
  291. value = desc.get;
  292. } else {
  293. value = value[part];
  294. }
  295. } else {
  296. isOwn = hasOwn(value, part);
  297. value = value[part];
  298. }
  299. if (isOwn && !skipFurtherCaching) {
  300. INTRINSICS[intrinsicRealName] = value;
  301. }
  302. }
  303. }
  304. return value;
  305. };