Generate read & write implementation using macros#1
Merged
CharlieTap merged 2 commits intoCharlieTap:mainfrom Nov 13, 2025
Merged
Generate read & write implementation using macros#1CharlieTap merged 2 commits intoCharlieTap:mainfrom
CharlieTap merged 2 commits intoCharlieTap:mainfrom
Conversation
f9b2a41 to
4a56f3f
Compare
JackThomson2
commented
Nov 4, 2025
src/macros.rs
Outdated
| // Safety we assume the params passed are correct | ||
| unsafe { | ||
| let pointer = self.memory.as_ptr().add(address as usize).cast::<$address_type>(); | ||
| std::ptr::read_unaligned(pointer) as $read_type |
Collaborator
Author
There was a problem hiding this comment.
I see we lose the from le bytes which I guess may be a problem for the wasm spec.
Maybe we can generate something like:
let pointer = self.memory.as_ptr().add(address as usize).cast::<[u8; 2]>();
u16::from_le_bytes(std::ptr::read_unaligned(pointer)) as i32Move the creation of the methods into macros to reduce the amount of duplicate code required. Update the method used for reads and writes, which generates more efficient assembly completely bypassing the bounds checks. Signed-off-by: Jack Thomson <jackathomson@outlook.com>
A macro to reduce the amount of duplication again by calling both variants of read and write on all the types. Signed-off-by: Jack Thomson <jackabt@amazon.com>
4a56f3f to
6c08989
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Update the read and write functions to be generated by macros reducing the amount of duplicate code and consolidate the implementations.
This also updates the method used for writing different sizes to the data which generate more efficient assembly 1
Notes
This adds
pasteas a new dependency to cleanly generate the function names but it is considered unmaintained 2 however, this seems to be as the author considers the crate "complete"I ran integration tests in package, but haven't run with chasm.
It does add unsafe code and we lose length checks which we may need to add back in