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