選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.js 13KB

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