index.js 12 KB

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