GetIntrinsic.js 11 KB

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