Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

inottn
Copy link
Contributor

@inottn inottn commented Feb 4, 2025

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.

Comment on lines 346 to +350
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};
Copy link
Contributor Author

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.

Comment on lines 354 to +358
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};
Copy link
Contributor Author

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.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

@inottn inottn marked this pull request as ready for review February 5, 2025 12:16
@Receptua
Copy link

Receptua commented Feb 6, 2025

++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants