Skip to content

Commit 06f1eed

Browse files
committed
[js] Standardize the unboxing of too big bigints error
The size is likely misleading and should be changed on both this and moar backends
1 parent 9b5bc18 commit 06f1eed

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/vm/js/nqp-runtime/bignum.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,17 @@ op.radix_I = function(currentHLL, radix, str, zpos, flags, type) {
304304
function bitSize(n) {
305305
let bits = 0;
306306

307+
if (n < 0) n = -n;
308+
307309
while (n) {
308310
n = n >> 1n;
309311
bits++;
310312
}
311313
return bits;
312314
}
313315

316+
exports.bitSize = bitSize;
317+
314318
function randomWithSameBitSize(n) {
315319
let got = 0n;
316320
let needed = bitSize(n);

src/vm/js/nqp-runtime/reprs.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const EDGE_CODEPOINT_I_LL = 15;
4646
const reprs = {};
4747
const reprById = [];
4848

49+
const bignum = require('./bignum.js');
50+
4951
function basicTypeObjectFor(HOW) {
5052
const st = new sixmodel.STable(this, HOW);
5153
this._STable = st;
@@ -1597,12 +1599,12 @@ function getBI(obj) {
15971599
}
15981600

15991601

1600-
function getIntFromBI(bignum) {
1601-
if (bignum < -(2n**64n) || 2n**64n <= bignum) {
1602+
function getIntFromBI(n) {
1603+
if (n < -(2n**63n) || 2n**63n <= n) {
16021604
// TODO - put exact number of bits into exception
1603-
throw new NQPException(`Cannot unbox too big bigint into native integer`);
1605+
throw new NQPException(`Cannot unbox ${bignum.bitSize(n)} bit wide bigint into native integer`);
16041606
} else {
1605-
return Number(bignum) | 0;
1607+
return Number(n) | 0;
16061608
}
16071609
}
16081610

0 commit comments

Comments
 (0)