index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. /* adapted from https://github.com/lodash/lodash/blob/4.17.15/dist/lodash.js#L6735-L6744 */
  191. var rePropName = /[^%.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|%$))/g;
  192. var reEscapeChar = /\\(\\)?/g; /** Used to match backslashes in property paths. */
  193. var stringToPath = function stringToPath(string) {
  194. var first = $strSlice(string, 0, 1);
  195. var last = $strSlice(string, -1);
  196. if (first === '%' && last !== '%') {
  197. throw new $SyntaxError('invalid intrinsic syntax, expected closing `%`');
  198. } else if (last === '%' && first !== '%') {
  199. throw new $SyntaxError('invalid intrinsic syntax, expected opening `%`');
  200. }
  201. var result = [];
  202. $replace(string, rePropName, function (match, number, quote, subString) {
  203. result[result.length] = quote ? $replace(subString, reEscapeChar, '$1') : number || match;
  204. });
  205. return result;
  206. };
  207. /* end adaptation */
  208. var getBaseIntrinsic = function getBaseIntrinsic(name, allowMissing) {
  209. var intrinsicName = name;
  210. var alias;
  211. if (hasOwn(LEGACY_ALIASES, intrinsicName)) {
  212. alias = LEGACY_ALIASES[intrinsicName];
  213. intrinsicName = '%' + alias[0] + '%';
  214. }
  215. if (hasOwn(INTRINSICS, intrinsicName)) {
  216. var value = INTRINSICS[intrinsicName];
  217. if (value === needsEval) {
  218. value = doEval(intrinsicName);
  219. }
  220. if (typeof value === 'undefined' && !allowMissing) {
  221. throw new $TypeError('intrinsic ' + name + ' exists, but is not available. Please file an issue!');
  222. }
  223. return {
  224. alias: alias,
  225. name: intrinsicName,
  226. value: value
  227. };
  228. }
  229. throw new $SyntaxError('intrinsic ' + name + ' does not exist!');
  230. };
  231. module.exports = function GetIntrinsic(name, allowMissing) {
  232. if (typeof name !== 'string' || name.length === 0) {
  233. throw new $TypeError('intrinsic name must be a non-empty string');
  234. }
  235. if (arguments.length > 1 && typeof allowMissing !== 'boolean') {
  236. throw new $TypeError('"allowMissing" argument must be a boolean');
  237. }
  238. var parts = stringToPath(name);
  239. var intrinsicBaseName = parts.length > 0 ? parts[0] : '';
  240. var intrinsic = getBaseIntrinsic('%' + intrinsicBaseName + '%', allowMissing);
  241. var intrinsicRealName = intrinsic.name;
  242. var value = intrinsic.value;
  243. var skipFurtherCaching = false;
  244. var alias = intrinsic.alias;
  245. if (alias) {
  246. intrinsicBaseName = alias[0];
  247. $spliceApply(parts, $concat([0, 1], alias));
  248. }
  249. for (var i = 1, isOwn = true; i < parts.length; i += 1) {
  250. var part = parts[i];
  251. var first = $strSlice(part, 0, 1);
  252. var last = $strSlice(part, -1);
  253. if (
  254. (
  255. (first === '"' || first === "'" || first === '`')
  256. || (last === '"' || last === "'" || last === '`')
  257. )
  258. && first !== last
  259. ) {
  260. throw new $SyntaxError('property names with quotes must have matching quotes');
  261. }
  262. if (part === 'constructor' || !isOwn) {
  263. skipFurtherCaching = true;
  264. }
  265. intrinsicBaseName += '.' + part;
  266. intrinsicRealName = '%' + intrinsicBaseName + '%';
  267. if (hasOwn(INTRINSICS, intrinsicRealName)) {
  268. value = INTRINSICS[intrinsicRealName];
  269. } else if (value != null) {
  270. if (!(part in value)) {
  271. if (!allowMissing) {
  272. throw new $TypeError('base intrinsic for ' + name + ' exists, but the property is not available.');
  273. }
  274. return void undefined;
  275. }
  276. if ($gOPD && (i + 1) >= parts.length) {
  277. var desc = $gOPD(value, part);
  278. isOwn = !!desc;
  279. // By convention, when a data property is converted to an accessor
  280. // property to emulate a data property that does not suffer from
  281. // the override mistake, that accessor's getter is marked with
  282. // an `originalValue` property. Here, when we detect this, we
  283. // uphold the illusion by pretending to see that original data
  284. // property, i.e., returning the value rather than the getter
  285. // itself.
  286. if (isOwn && 'get' in desc && !('originalValue' in desc.get)) {
  287. value = desc.get;
  288. } else {
  289. value = value[part];
  290. }
  291. } else {
  292. isOwn = hasOwn(value, part);
  293. value = value[part];
  294. }
  295. if (isOwn && !skipFurtherCaching) {
  296. INTRINSICS[intrinsicRealName] = value;
  297. }
  298. }
  299. }
  300. return value;
  301. };