index.js 13 KB

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