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

fix(es/minifier): Fix panic in bitwise logic and incorrect values #9258

Merged
merged 10 commits into from
Jul 16, 2024

Conversation

levi-nz
Copy link
Contributor

@levi-nz levi-nz commented Jul 16, 2024

Description:

This PR fixes all issues listed in #9256 as well as the following:

1.7976931348623157e+308 << 1.7976931348623157e+308 - swc currently does not transform this and leaves it as-is

Anything involving low floating-point numbers, like 5e-324, such as 5e-324 >> 5e-324, 5e-324 >> 0 etc - swc currently panics; https://play.swc.rs/?version=1.6.7&code=H4sIAAAAAAAAA0vTME3VNTYyUbCzUzDQBACkm%2Ft7DgAAAA%3D%3D&config=H4sIAAAAAAAAA32UO3LjMAyG%2B5zCozrFjostcoDtcgYOTYIyvXxoCNCxJuO7L0TJj40hdRI%2B%2FAAJgPh%2B2%2B26E5ruY%2FfNn%2Fwz6IJQ7v9swTGRvrClAxM1muIH6t5v9IQTcjogNNN1Jh3p0gM1Fe5%2F7feLogs5I9wUiy365N34nNPkOBRAfLKxlUPWCInwf%2F3CSv6aAJX6bD%2FkHECnDaI0Kp8IeihSYJND0AOCOusiRJlOqovHLKWYYCWwaih5EHmynnxOnPOVWtBWmWxBQL6AIX8GSca5WJaQryfcp2ELh9r3rc8%2F1HDWoWoScsKltYRPK0Q9Zo%2BkXE1SCWe4UoMZLsX9qfROFaBa0qvulH1a6clfAK5A0IhJR5DiNg%2FH87SmdptKnxyPLI0C5%2FmWbpmg56Iq751Q2akyUMhL3Sxgq4GpskY6zoJXyofeggLneFaE0PjlyRylpDQOkJ0AuL%2FaSVM1A3V%2FhSt8ehAb%2BA%2FfkuQBWzyipuM6xTEecthIEIGO2W44cCsor%2BPCW%2BIyrPOaLPBogBVdKjbwugT4AVBWoe3Ll9ng58ERVR%2Fy4bEmFofrfQ9HnfrHe59X8dvi0MVsa4PLkp%2F6O6%2Fm393D6baF7wfvPH7elC3p9R%2BoYzQdMAYAAA%3D%3D

This PR also fixes shiftCount being incorrect and incorrect conversion between f64 and i32/u32. A new API in swc_ecma_utils has been added, to_js_int32 and to_js_uint32 that allows external callers to convert f64s to i32 and u32 so they can perform bitwise operations themselves if they're trying to replicate JavaScript behaviour.

I also believe the updated bit shifting logic should be accessible externally via swc_ecma_utils, but unsure about this.

Related issue:

@levi-nz levi-nz requested a review from a team as a code owner July 16, 2024 02:03
@kdy1 kdy1 added this to the Planned milestone Jul 16, 2024
kdy1
kdy1 previously approved these changes Jul 16, 2024
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Thank you!


swc-bump:

  • swc_ecma_utils --breaking

@kdy1
Copy link
Member

kdy1 commented Jul 16, 2024

CI failed. Can you take a look?

@kdy1 kdy1 enabled auto-merge (squash) July 16, 2024 02:49
@kdy1 kdy1 changed the title fix(es/minifier): Fix panicking bitwise logic and incorrect values fix(es/minifier): Fix panic in bitwise logic and incorrect values Jul 16, 2024
auto-merge was automatically disabled July 16, 2024 02:49

Head branch was pushed to by a user without write access

kdy1
kdy1 previously approved these changes Jul 16, 2024
@kdy1 kdy1 enabled auto-merge (squash) July 16, 2024 02:49
@levi-nz
Copy link
Contributor Author

levi-nz commented Jul 16, 2024

Not sure if that was the problem or if something else was wrong. Will take a look once it's done.

Copy link
Collaborator

@swc-bot swc-bot left a comment

Choose a reason for hiding this comment

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

Automated review comment generated by auto-rebase script

auto-merge was automatically disabled July 16, 2024 02:59

Head branch was pushed to by a user without write access

Copy link
Member

@magic-akari magic-akari left a comment

Choose a reason for hiding this comment

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

block berfore #9251 get merged

@levi-nz
Copy link
Contributor Author

levi-nz commented Jul 16, 2024

block berfore #9251 get merged

lgtm, will update once this is merged

Copy link

codspeed-hq bot commented Jul 16, 2024

CodSpeed Performance Report

Merging #9258 will not alter performance

Comparing levi-nz:issue-9256 (d2554e6) with main (06bb533)

Summary

✅ 178 untouched benchmarks

@levi-nz levi-nz requested a review from a team as a code owner July 16, 2024 05:03
@levi-nz
Copy link
Contributor Author

levi-nz commented Jul 16, 2024

Will rebase properly shortly. Not sure how this is always so troublesome on my end lol

@levi-nz
Copy link
Contributor Author

levi-nz commented Jul 16, 2024

Not sure if there's a better way to implement as_int32 and as_uint32, the current implementations are correct, test_fold_bit_shifts doesn't fold everything with the impl in #9251 as it doesn't overflow correctly in as_uint32

@magic-akari
Copy link
Member

Some implementations are available for reference:

In my opinion, leveraging the Rust standard library is optimal. Should there be any discrepancies from ECMAScript, we address those particular instances. The worst case would be to develop everything from scratch.

@levi-nz
Copy link
Contributor Author

levi-nz commented Jul 16, 2024

Some implementations are available for reference:

In my opinion, leveraging the Rust standard library is optimal. Should there be any discrepancies from ECMAScript, we address those particular instances. The worst case would be to develop everything from scratch.

I don't think Rust has a std function that behaves the same as the ECMAScript functions. I've seen the functions in V8 engine src before (like the ones you sent), the current impl follows ECMAScript spec. I guess we could implement one of those instead just to be sure. The only issue with trunc() from what I saw is that negative values don't overflow. e.g. assert_eq!(JsNumber::from(-8.0).as_uint32(), 4294967288); is required for expressions like -8 >>> 0 otherwise they'll produce an incorrect value

@@ -49,7 +45,8 @@ impl JsNumber {
return 0;
}

self.0.trunc() as u32
// pow(2, 32) = 4294967296
self.0.trunc().rem_euclid(4294967296.0) as u32
Copy link
Member

Choose a reason for hiding this comment

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

I've updated the implementation, and all the tests have passed.
Take a look to see if we need to add more test cases. @levi-nz

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lgtm, maybe we could see if v8 or another project has some test cases? https://github.com/v8/v8/blob/main/src/numbers/conversions.h

I think the current test cases are (probably) fine, as long as we're testing for overflowing, the rest should work fine

@kdy1 kdy1 merged commit baeb9e2 into swc-project:main Jul 16, 2024
157 checks passed
@kdy1 kdy1 modified the milestones: Planned, v1.7.0 Jul 17, 2024
@swc-project swc-project locked as resolved and limited conversation to collaborators Aug 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

minifier produces incorrect constant value on large floating-point value
4 participants