Skip to content

Fix byte order for big-endian platforms#4015

Merged
daxpedda merged 3 commits intowasm-bindgen:mainfrom
magic-akari:fix/endianness
Jul 29, 2024
Merged

Fix byte order for big-endian platforms#4015
daxpedda merged 3 commits intowasm-bindgen:mainfrom
magic-akari:fix/endianness

Conversation

@magic-akari
Copy link
Copy Markdown
Member

@magic-akari magic-akari commented Jul 17, 2024

Note

WebAssembly memory is always in little-endian format, regardless of the platform it's run on. Therefore, for portability, you should read and write multi-byte values in JavaScript using DataView.

https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Memory

Related issues:

@magic-akari magic-akari marked this pull request as draft July 17, 2024 09:37
@magic-akari magic-akari force-pushed the fix/endianness branch 4 times, most recently from 1c10293 to 85877e3 Compare July 17, 2024 11:39
@magic-akari magic-akari marked this pull request as ready for review July 17, 2024 11:40
@magic-akari magic-akari marked this pull request as draft July 17, 2024 11:43
@magic-akari magic-akari force-pushed the fix/endianness branch 2 times, most recently from d15a159 to 0b99b3e Compare July 17, 2024 12:05
@magic-akari magic-akari marked this pull request as ready for review July 17, 2024 12:15
kdy1 pushed a commit to swc-project/swc that referenced this pull request Jul 18, 2024
> [!NOTE]  
> We should remove this if
wasm-bindgen/wasm-bindgen#4015 get mereged.
@magic-akari
Copy link
Copy Markdown
Member Author

Could you review this pull request? @daxpedda

@magic-akari magic-akari changed the title Fix byte order for big-endian platform Fix byte order for big-endian platforms Jul 23, 2024
Copy link
Copy Markdown
Member

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

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

I'm a bit confused why you chose not to re-use expose_*_memory().
The only place it still seems to be used is for importing arrays, which you actually haven't covered here.

I will have more time tomorrow to look into this properly.

Could you review this pull request? @daxpedda

I apologize for the delay, my backlog from my vacation was quite big and I'm just getting to it.

@magic-akari
Copy link
Copy Markdown
Member Author

I'm a bit confused why you chose not to re-use expose_*_memory(). The only place it still seems to be used is for importing arrays, which you actually haven't covered here.

Could you please specify which part you're referring to? Marking it on the relevant code would be helpful.

@daxpedda
Copy link
Copy Markdown
Member

daxpedda commented Jul 29, 2024

All these functions could be adapted:
https://github.com/rustwasm/wasm-bindgen/blob/7f3c21b9e24b90502e858ba91d7f3aeab0ce7dbe/crates/cli-support/src/js/mod.rs#L1782-L1812

The corresponding memview() as well.
What this PR does is only switch pointer handling and vector exports to the DataView API, is there any reason not to just switch everything? AFAICS vector importing is missing.
It would require much less code changes and the logic could be simplified.

Copy link
Copy Markdown
Member

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

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

My earlier assumptions were wrong, when getting arrays out of Wasm to JS land, we can't actually use DataView, this is a responsibility the user will have to take when extracting data from DataView.

Comment thread crates/cli-support/src/js/mod.rs Outdated
Comment thread crates/cli-support/src/js/mod.rs Outdated
@daxpedda daxpedda force-pushed the fix/endianness branch 6 times, most recently from 5f93ead to 925fc07 Compare July 29, 2024 07:45
@daxpedda daxpedda merged commit a2761f6 into wasm-bindgen:main Jul 29, 2024
kdy1 pushed a commit to swc-project/swc that referenced this pull request Aug 14, 2024
@magic-akari magic-akari deleted the fix/endianness branch December 21, 2025 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants