index.js 13 KB

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