1
0

index.js 14 KB

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