-
Notifications
You must be signed in to change notification settings - Fork 50.4k
[compiler] Add support for BigIntLiteral #32305
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -346,12 +346,16 @@ function evaluateInstruction( | |
| 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}; | ||
| } | ||
| break; | ||
| } | ||
| 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}; | ||
|
Comment on lines
354
to
+358
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ++ |
||
| } | ||
| break; | ||
| } | ||
|
|
@@ -364,24 +368,32 @@ function evaluateInstruction( | |
| 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}; | ||
| } | ||
| break; | ||
| } | ||
| 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}; | ||
| } | ||
| break; | ||
| } | ||
| 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}; | ||
| } | ||
| break; | ||
| } | ||
| 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}; | ||
| } | ||
| break; | ||
| } | ||
|
|
@@ -394,6 +406,8 @@ function evaluateInstruction( | |
| 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}; | ||
| } | ||
| break; | ||
| } | ||
|
|
@@ -410,6 +424,8 @@ function evaluateInstruction( | |
| 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}; | ||
| } | ||
| break; | ||
| } | ||
|
|
@@ -422,24 +438,32 @@ function evaluateInstruction( | |
| 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}; | ||
| } | ||
| break; | ||
| } | ||
| 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}; | ||
| } | ||
| break; | ||
| } | ||
| 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}; | ||
| } | ||
| break; | ||
| } | ||
| 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}; | ||
| } | ||
| break; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| function Test(props) { | ||
| if (props.num % 2n === 0n) { | ||
| return <>even</>; | ||
| } | ||
|
|
||
| return <>odd</>; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: Test, | ||
| params: [{num: 1n}], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ## Code | ||
|
|
||
| ```javascript | ||
| import { c as _c } from "react/compiler-runtime"; | ||
| function Test(props) { | ||
| const $ = _c(2); | ||
| if (props.num % 2n === 0n) { | ||
| let t0; | ||
| if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
| t0 = <>even</>; | ||
| $[0] = t0; | ||
| } else { | ||
| t0 = $[0]; | ||
| } | ||
| return t0; | ||
| } | ||
| let t0; | ||
| if ($[1] === Symbol.for("react.memo_cache_sentinel")) { | ||
| t0 = <>odd</>; | ||
| $[1] = t0; | ||
| } else { | ||
| t0 = $[1]; | ||
| } | ||
| return t0; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: Test, | ||
| params: [{ num: 1n }], | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ### Eval output | ||
| (kind: ok) odd |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| function Test(props) { | ||
| if (props.num % 2n === 0n) { | ||
| return <>even</>; | ||
| } | ||
|
|
||
| return <>odd</>; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: Test, | ||
| params: [{num: 1n}], | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| import {Stringify} from 'shared-runtime'; | ||
|
|
||
| function foo() { | ||
| return ( | ||
| <Stringify | ||
| value={[ | ||
| 12n | 0n, | ||
| 12n & 0n, | ||
| 12n ^ 0n, | ||
| 12n | 3n, | ||
| 12n & 5n, | ||
| 12n ^ 7n, | ||
| 12n >> 0n, | ||
| 12n >> 1n, | ||
| 4n % 2n, | ||
| ]} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: foo, | ||
| params: [], | ||
| isComponent: false, | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ## Code | ||
|
|
||
| ```javascript | ||
| import { c as _c } from "react/compiler-runtime"; | ||
| import { Stringify } from "shared-runtime"; | ||
|
|
||
| function foo() { | ||
| const $ = _c(1); | ||
| let t0; | ||
| if ($[0] === Symbol.for("react.memo_cache_sentinel")) { | ||
| t0 = <Stringify value={[12n, 0n, 12n, 15n, 4n, 11n, 12n, 6n, 0n]} />; | ||
| $[0] = t0; | ||
| } else { | ||
| t0 = $[0]; | ||
| } | ||
| return t0; | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: foo, | ||
| params: [], | ||
| isComponent: false, | ||
| }; | ||
|
|
||
| ``` | ||
|
|
||
| ### Eval output | ||
| (kind: exception) Do not know how to serialize a BigInt |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import {Stringify} from 'shared-runtime'; | ||
|
|
||
| function foo() { | ||
| return ( | ||
| <Stringify | ||
| value={[ | ||
| 12n | 0n, | ||
| 12n & 0n, | ||
| 12n ^ 0n, | ||
| 12n | 3n, | ||
| 12n & 5n, | ||
| 12n ^ 7n, | ||
| 12n >> 0n, | ||
| 12n >> 1n, | ||
| 4n % 2n, | ||
| ]} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export const FIXTURE_ENTRYPOINT = { | ||
| fn: foo, | ||
| params: [], | ||
| isComponent: false, | ||
| }; |
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.