1 line
150 KiB
Plaintext
Raw Normal View History

{"version":3,"file":"jsbi.mjs","sources":["../tsc-out/jsbi.mjs"],"sourcesContent":["// Copyright 2018 Google Inc.\n//\n// Licensed under the Apache License, Version 2.0 (the “License”);\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n// <https://apache.org/licenses/LICENSE-2.0>.\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an “AS IS” BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\nclass JSBI extends Array {\n constructor(length, sign) {\n super(length);\n this.sign = sign;\n // Explicitly set the prototype as per\n // https://github.com/Microsoft/TypeScript-wiki/blob/main/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work\n Object.setPrototypeOf(this, JSBI.prototype);\n if (length > JSBI.__kMaxLength) {\n throw new RangeError('Maximum BigInt size exceeded');\n }\n }\n static BigInt(arg) {\n if (typeof arg === 'number') {\n if (arg === 0)\n return JSBI.__zero();\n if (JSBI.__isOneDigitInt(arg)) {\n if (arg < 0) {\n return JSBI.__oneDigit(-arg, true);\n }\n return JSBI.__oneDigit(arg, false);\n }\n if (!Number.isFinite(arg) || Math.floor(arg) !== arg) {\n throw new RangeError('The number ' + arg + ' cannot be converted to ' +\n 'BigInt because it is not an integer');\n }\n return JSBI.__fromDouble(arg);\n }\n else if (typeof arg === 'string') {\n const result = JSBI.__fromString(arg);\n if (result === null) {\n throw new SyntaxError('Cannot convert ' + arg + ' to a BigInt');\n }\n return result;\n }\n else if (typeof arg === 'boolean') {\n if (arg === true) {\n return JSBI.__oneDigit(1, false);\n }\n return JSBI.__zero();\n }\n else if (typeof arg === 'object') {\n if (arg.constructor === JSBI)\n return arg;\n const primitive = JSBI.__toPrimitive(arg);\n return JSBI.BigInt(primitive);\n }\n throw new TypeError('Cannot convert ' + arg + ' to a BigInt');\n }\n toDebugString() {\n const result = ['BigInt['];\n for (const digit of this) {\n result.push((digit ? (digit >>> 0).toString(16) : digit) + ', ');\n }\n result.push(']');\n return result.join('');\n }\n toString(radix = 10) {\n if (radix < 2 || radix > 36) {\n throw new RangeError('toString() radix argument must be between 2 and 36');\n }\n if (this.length === 0)\n return '0';\n if ((radix & (radix - 1)) === 0) {\n return JSBI.__toStringBasePowerOfTwo(this, radix);\n }\n return JSBI.__toStringGeneric(this, radix, false);\n }\n valueOf() {\n throw new Error('Convert JSBI instances to native numbers using `toNumber`.');\n }\n // Equivalent of \"Number(my_bigint)\" in the native implementation.\n // TODO: add more tests\n static toNumber(x) {\n const xLength = x.length;\n if (xLength === 0)\n return 0;\n if (xLength === 1) {\n const value = x.__unsignedDigit(0);\n return x.sign ? -value : value;\n }\n const xMsd = x.__digit(xLength - 1);\n const msdLeadingZeros = JSBI.__clz30(xMsd);\n const xBitLength = xLength * 30 - msdLeadingZeros;\n if (xBitLength > 1024)\n return x.sign ? -Infinity : Infinity;\n let exponent = xBitLength - 1;\n let currentDigit = xMsd;\n let digitIndex = xLength - 1;\n const shift = ms