Skip to content

Commit 16fff80

Browse files
committed
[js] Throw when unboxing an bigint that doesn't fit in 64bits
We need to think if we throw if it doesn't fit in 32bits
1 parent 943f7f7 commit 16fff80

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,15 @@ function getBI(obj) {
12931293
return obj.$$getBignum();
12941294
}
12951295

1296+
function getIntFromBI(bignum) {
1297+
const bits = bignum.bitLength();
1298+
if (bits >= 64) {
1299+
throw new NQPException(`Cannot unbox ${bits} bit wide bigint into native integer`);
1300+
} else {
1301+
return bignum.toNumber() | 0;
1302+
}
1303+
}
1304+
12961305
class P6bigint extends REPR {
12971306
setupSTable(STable) {
12981307
STable.addInternalMethods(class {
@@ -1301,7 +1310,7 @@ class P6bigint extends REPR {
13011310
}
13021311

13031312
$$getInt() {
1304-
return this.value.toNumber() | 0;
1313+
return getIntFromBI(this.value);
13051314
}
13061315

13071316
$$setBignum(value) {
@@ -1313,7 +1322,7 @@ class P6bigint extends REPR {
13131322
}
13141323

13151324
$$decont_i(ctx) {
1316-
return this.value.toNumber() | 0;
1325+
return getIntFromBI(this.value);
13171326
}
13181327
});
13191328
}
@@ -1377,11 +1386,11 @@ class P6bigint extends REPR {
13771386
}
13781387

13791388
$$getInt() {
1380-
return this[name].toNumber() | 0;
1389+
return getIntFromBI(this[name]);
13811390
}
13821391

13831392
$$decont_i(ctx) {
1384-
return this[name].toNumber() | 0;
1393+
return getIntFromBI(this[name]);
13851394
}
13861395

13871396
$$getBignum() {

0 commit comments

Comments
 (0)