-
-
Notifications
You must be signed in to change notification settings - Fork 689
add js.lib.BigInt #8731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add js.lib.BigInt #8731
Conversation
|
This is out of scope of this PR, but if it get merged
|
std/js/lib/BigInt.hx
Outdated
|
|
||
| @see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt> | ||
| **/ | ||
| abstract BigInt(_BigInt) from _BigInt to _BigInt { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be @:coreType instead of an underlying extern class because BigInt is a primitive type:
BigInt is slated to become the first new primitive type added to JavaScript since Symbol in ES2015.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And then all bodies of @:op methods should be removed leaving declarations only. E.g.
@:op(-A) static inline function neg(a:BigInt):BigInt;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to use @:coretype, but it didn't work.
http://try-haxe.mrcdk.com/#091Cf
Haxe converts a++ to a = a + 1. But BigInt + Int doesn't work...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In JS, var a = 1n; a++; works fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Increment/decrement could still have bodies even with @:coreType
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Umm..., increment works fine with untyped __js__, but it doesn't work with Syntax.code().
http://try-haxe.mrcdk.com/#6BB44
http://try-haxe.mrcdk.com/#6B8e6
Is it correct? Should I use untyped __js__?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm... Yeah, looks like it's the only option for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Simn any idea how to make @:op(A++) function():BigInt; not to inline into BigInt(1)++?
std/js/lib/BigInt.hx
Outdated
| return Syntax.code("({0} ^ {1})", a, b); | ||
| } | ||
|
|
||
| @:op(A << B) static inline function lshift(a:BigInt, b:BigInt):BigInt { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure bit shift ops require a BigInt as the right hand argument?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. In V8(Chrome and Node.js),
> 1n << 1
Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions
But Firefox accepts int.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I find a mistake of sample code... (It is already fixed.)
It works fine.
> 1n << 1n
2n
It doesn't work.
> 1n << 1
Thrown:
TypeError: Cannot mix BigInt and other types, use explicit conversions
|
@terurou, what do you think about using (same as #8633 (comment)) |
|
@haxiomic I tried This is not good... I don't need var a = new BigInt(1);
a.toLocaleString();BigInt.prototype.toLocaleString.call(BigInt(1),null,null); |
|
@terurou thanks for testing, I see what's happening. I guess the null checks are the best approach then Edit: more discussion #8633 (comment) |
|
What's the status here? |
|
This PR still needs some work. |
|
At this point, I suppose it's best to close this particular PR. It would require someone to look into this more, and I don't see that happening. |
BigIntis supported by Chrome, Firefox and Node.js.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
Node.js v12 was added some BigInt API.
https://nodejs.org/dist/latest-v12.x/docs/api/buffer.html#buffer_buf_readbigint64be_offset