duktape
duktape copied to clipboard
Add support for BigInts
BigInts are useful to represent 64-bit values and also for some algorithms where 53-bit precision is not enough. There's a stage 3 proposal, implemented by e.g. NodeJS, here: https://github.com/tc39/proposal-bigint. http://thecodebarbarian.com/an-overview-of-bigint-in-node-js.html.
The changes required are:
- An internal non-object type to represent bigints (heap allocated naturally, like strings)
- Possibly a new type tag for bigints
- C API support, at least for converting between non-bigints
There's already a bigint implementation needed by number conversion code. Ideally that implementation would be replaced by internal bigints, optimizing footprint so that without bigints the additional footprint would be minimal for number conversion.
Will BigInt save you from this behavior?
function test() {
var a = 5527939700884757;
var b = 8944394323791464;
var c = a + b;
// should be 14472334024676221
// but print 14472334024676220
console.log(c);
}
test();
As you probably know, that behavior happens because ECMAScript number type is IEEE double which all arithmetic is based on. With bigints it would be:
function test() {
var a = 5527939700884757n;
var b = 8944394323791464n;
var c = a + b;
console.log(c);
}
test(); // prints 14472334024676221n
It won't save you from that behavior without code changes however, as one needs to use bigints explicitly.
BigInt will be useful. Especially for applications like games.
I would like to add my vote for this feature with priority - it's pretty much the only modern language feature which can't be polyfilled or easily transpiled out.
Almost all languages support 64-bit support now. In addition, 64-bit has a lot of applications in today's world.
It would be super cool if Duktape provides BigInt or internal some flag in duk_config.h
which could help us work just with int64 'number' without precision loss (deferring from IEEE standard).
Just curious, is there any plan on adding above support?
Also, related to https://github.com/svaarala/duktape/issues/196
@svaarala Could you give us any hints as to when the above would be on your to-do list
Extremely important.