Skip to content

Commit

Permalink
feat(wasm): add setup_allocator macro for optimize the binary a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexcited committed Dec 28, 2024
1 parent 0d27e3d commit bd9ac78
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@
wasm = { git = "https://github.com/LiterateInk/Utilities" }
wasm-bindgen = "0.2" # required for the `wasm_bindgen::prelude::wasm_bindgen` macro
js-sys = "0.3" # required for the `js_sys::Function` type
wee_alloc = "0.4" # required when using `setup_allocator!()`
```

## Usage

### `setup_allocator`

```rust
#[cfg(target_arch = "wasm32")]
wasm::setup_allocator!();
```

Will use the `wee_alloc` crate to set up a global allocator for the `wasm32` target.

### `api_method`

```rust
#[cfg_attr(target_arch = "wasm32", wasm::api_method)]
pub async fn fetch_github(something: String) -> String {
Expand Down
12 changes: 12 additions & 0 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,15 @@ pub fn api_method(_args: TokenStream, input: TokenStream) -> TokenStream {

TokenStream::from(output)
}

#[proc_macro]
pub fn setup_allocator(_input: TokenStream) -> TokenStream {
let expanded = quote! {
extern crate wee_alloc;

#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
};

TokenStream::from(expanded)
}

0 comments on commit bd9ac78

Please sign in to comment.