Skip to content

Commit

Permalink
WASM support
Browse files Browse the repository at this point in the history
  • Loading branch information
mmastrac committed Feb 12, 2025
1 parent 81b9471 commit d0cfb13
Show file tree
Hide file tree
Showing 3 changed files with 384 additions and 1 deletion.
37 changes: 37 additions & 0 deletions codegen/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ declare_macros!(

macro_rules! ctor_entry {
(meta=[$($meta:meta)?], macro_path=$($macro_path:ident)::+, features=$features:tt, imeta=$(#[$fnmeta:meta])*, vis=[$($vis:tt)*], item=fn $ident:ident() $block:block) => {
#[cfg(target_family="wasm")]
$(#[$fnmeta])*
#[allow(unused)]
#[wasm_bindgen(start)]
$($vis)* fn $ident() {
$block
}

#[cfg(not(target_family="wasm"))]
$(#[$fnmeta])*
#[allow(unused)]
$($vis)* fn $ident() {
Expand Down Expand Up @@ -90,6 +99,15 @@ declare_macros!(
}
};
(meta=[$($meta:meta)?], macro_path=$($macro_path:ident)::+, features=$features:tt, imeta=$(#[$fnmeta:meta])*, vis=[$($vis:tt)*], item=unsafe fn $ident:ident() $block:block) => {
#[cfg(target_family="wasm")]
$(#[$fnmeta])*
#[allow(unused)]
#[wasm_bindgen(start)]
$($vis)* unsafe fn $ident() {
$block
}

#[cfg(not(target_family="wasm"))]
$(#[$fnmeta])*
#[allow(unused)]
$($vis)* unsafe fn $ident() {
Expand Down Expand Up @@ -144,6 +162,25 @@ declare_macros!(
}
}

#[cfg(target_family="wasm")]
#[doc(hidden)]
#[allow(non_upper_case_globals, non_snake_case)]
mod $ident {
#[derive(Default)]
#[allow(non_camel_case_types)]
pub struct Static<T> {
pub _storage: ::std::cell::UnsafeCell<::std::option::Option<T>>
}

unsafe impl <T> std::marker::Sync for Static<T> {}

#[wasm_bindgen(start)]
fn init() {
super::$ident.init_once();
}
}

#[cfg(not(target_family="wasm"))]
#[doc(hidden)]
#[allow(non_upper_case_globals, non_snake_case)]
mod $ident {
Expand Down
4 changes: 3 additions & 1 deletion codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ mod tests {
fn test_up_to_date() {
let contents = generate_code().expect("Failed to generate code");
assert_eq!(
std::fs::read_to_string("../ctor/src/gen.rs").unwrap().replace("\r\n", "\n"),
std::fs::read_to_string("../ctor/src/gen.rs")
.unwrap()
.replace("\r\n", "\n"),
contents
);
}
Expand Down
Loading

0 comments on commit d0cfb13

Please sign in to comment.