-
Notifications
You must be signed in to change notification settings - Fork 47.7k
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
[compiler] Add support for BigIntLiteral #32305
base: main
Are you sure you want to change the base?
Conversation
result = {kind: 'Primitive', value: lhs + rhs, loc: value.loc}; | ||
} else if (typeof lhs === 'string' && typeof rhs === 'string') { | ||
result = {kind: 'Primitive', value: lhs + rhs, loc: value.loc}; | ||
} else if (typeof lhs === 'bigint' && typeof rhs === 'bigint') { | ||
result = {kind: 'Primitive', value: lhs + rhs, loc: value.loc}; |
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.
This code appears to be somewhat redundant. For example, multiple if/else-if conditions could be combined, and the condition typeof lhs === 'number' && typeof rhs === 'number'
could be moved to an outer layer. However, I'm not sure why this wasn't done before—perhaps it was intentional—so I've chosen to keep the original implementation.
case '-': { | ||
if (typeof lhs === 'number' && typeof rhs === 'number') { | ||
result = {kind: 'Primitive', value: lhs - rhs, loc: value.loc}; | ||
} else if (typeof lhs === 'bigint' && typeof rhs === 'bigint') { | ||
result = {kind: 'Primitive', value: lhs - rhs, loc: value.loc}; |
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 did not apply this code to every operation. Specifically, I excluded operators such as *
, **
, and <<
because they might compute extremely large values, which could significantly increase the size of the compiled code.
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.
++
++ |
Summary
The compiler currently doesn't support BigInt. As a primitive type in JS, I believe it can be implemented using existing Primitive kind.
However, in the AST obtained via Babel, the value of BigIntLiteral is also of string type. Therefore, a new field is needed to distinguish it from a actual string. I've temporarily named it isBigInt, as the name is quite intuitive.I realized that I can convert the value corresponding to BigIntLiteral to the bigint type directly, which would eliminate the need for a new field to identify it.
How did you test this change?
Added a test. I tried modifying the existing test cases, but the results looked messy, so I added new test cases instead.