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