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 byte order for big-endian platforms #4015

Merged
merged 3 commits into from
Jul 29, 2024

Conversation

magic-akari
Copy link
Contributor

@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
rustwasm/wasm-bindgen#4015 get mereged.
@magic-akari
Copy link
Contributor 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
Collaborator

@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
Contributor 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
Collaborator

daxpedda commented Jul 29, 2024

All these functions could be adapted:

fn expose_int16_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Int16", memory)
}
fn expose_uint16_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Uint16", memory)
}
fn expose_int32_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Int32", memory)
}
fn expose_uint32_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Uint32", memory)
}
fn expose_int64_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("BigInt64", memory)
}
fn expose_uint64_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("BigUint64", memory)
}
fn expose_f32_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Float32", memory)
}
fn expose_f64_memory(&mut self, memory: MemoryId) -> MemView {
self.memview("Float64", memory)
}

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
Collaborator

@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.

crates/cli-support/src/js/mod.rs Outdated Show resolved Hide resolved
crates/cli-support/src/js/mod.rs Outdated Show resolved Hide resolved
@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 rustwasm:main Jul 29, 2024
25 checks passed
kdy1 pushed a commit to swc-project/swc that referenced this pull request Aug 14, 2024
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