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.

378 lines
14 KiB

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