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

Move allocation functions into javy lib #461

Merged
merged 5 commits into from
Sep 6, 2023
Merged

Conversation

jeffcharles
Copy link
Collaborator

This does two things:

  1. Moves the realloc and free implementations from the core crate into the javy crate under the publicly exported alloc module
  2. Adds a cargo feature named export_alloc_fns which, when enabled, will export the realloc and free functions from the produced Wasm module

This will allow users of the Javy crate to share memory allocation and free functions instead of potentially having to write their own. I opted to put the function exports behind a cargo feature because someone using the Javy crate may want to expose their own allocation and free functions instead with their allocation logic and wouldn't want someone calling our functions or if they want to export them under a different name.

@@ -69,6 +71,7 @@ pub fn generate(js: &JS, exports: Vec<Export>) -> Result<Vec<u8>> {

// We no longer need these exports so remove them.
module.exports.delete(realloc_export);
module.exports.delete(free_export);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm doing this for consistency with us hiding the realloc function

Copy link
Member

@saulecabrera saulecabrera left a comment

Choose a reason for hiding this comment

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

Generally looks good to me. There are a couple of minor fixes that we could add before landing it.

@@ -40,6 +40,7 @@ pub use config::Config;
pub use quickjs_wasm_rs as quickjs;
pub use runtime::Runtime;

pub mod alloc;
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we instead add [cfg(feature = "export_alloc_fns")] here and remove the cfg directive from each of the function definitions?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

One small reason is if they don't enable the export_alloc_fns feature, they can still add exported functions in their own crate to call canonical_abi_realloc and canonical_abi_free but export them with a name of their own choosing instead of with the export name we chose.

crates/javy/src/alloc.rs Outdated Show resolved Hide resolved
Copy link
Member

@saulecabrera saulecabrera left a comment

Choose a reason for hiding this comment

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

Discussed the cfg feature offline. I think that a comment will help with some of the confusion here, it feels a bit odd that Rust/Cargo requires a feature/export_name in order to keep those symbols.

@jeffcharles jeffcharles merged commit 5c0ac2d into main Sep 6, 2023
14 checks passed
@jeffcharles jeffcharles deleted the jc.move-alloc-fns-to-lib branch September 6, 2023 14:28
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