-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathlib.rs
226 lines (197 loc) · 7.12 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
use proc_macro::TokenStream;
use digest::Digest;
use quote::{format_ident, quote};
use syn::{parse_macro_input, parse_quote, FnArg, Ident, ItemFn, ReturnType, Signature};
#[proc_macro_attribute]
pub fn wasm_split(args: TokenStream, input: TokenStream) -> TokenStream {
let module_ident = parse_macro_input!(args as Ident);
let item_fn = parse_macro_input!(input as ItemFn);
if item_fn.sig.asyncness.is_none() {
panic!("wasm_split functions must be async. Use a LazyLoader with synchronous functions instead.");
}
let LoaderNames {
split_loader_ident,
impl_import_ident,
impl_export_ident,
load_module_ident,
..
} = LoaderNames::new(item_fn.sig.ident.clone(), module_ident.to_string());
let mut desugard_async_sig = item_fn.sig.clone();
desugard_async_sig.asyncness = None;
desugard_async_sig.output = match &desugard_async_sig.output {
ReturnType::Default => {
parse_quote! { -> std::pin::Pin<Box<dyn std::future::Future<Output = ()>>> }
}
ReturnType::Type(_, ty) => {
parse_quote! { -> std::pin::Pin<Box<dyn std::future::Future<Output = #ty>>> }
}
};
let import_sig = Signature {
ident: impl_import_ident.clone(),
..desugard_async_sig.clone()
};
let export_sig = Signature {
ident: impl_export_ident.clone(),
..desugard_async_sig.clone()
};
let default_item = item_fn.clone();
let mut wrapper_sig = item_fn.sig;
wrapper_sig.asyncness = Some(Default::default());
let mut args = Vec::new();
for (i, param) in wrapper_sig.inputs.iter_mut().enumerate() {
match param {
syn::FnArg::Receiver(_) => args.push(format_ident!("self")),
syn::FnArg::Typed(pat_type) => {
let param_ident = format_ident!("__wasm_split_arg_{i}");
args.push(param_ident.clone());
pat_type.pat = Box::new(syn::Pat::Ident(syn::PatIdent {
attrs: vec![],
by_ref: None,
mutability: None,
ident: param_ident,
subpat: None,
}));
}
}
}
let attrs = &item_fn.attrs;
let stmts = &item_fn.block.stmts;
quote! {
#[cfg(target_arch = "wasm32")]
#wrapper_sig {
#(#attrs)*
#[allow(improper_ctypes_definitions)]
#[no_mangle]
pub extern "C" #export_sig {
Box::pin(async move { #(#stmts)* })
}
#[link(wasm_import_module = "./__wasm_split.js")]
extern "C" {
#[no_mangle]
fn #load_module_ident (
callback: unsafe extern "C" fn(*const ::std::ffi::c_void, bool),
data: *const ::std::ffi::c_void
);
#[allow(improper_ctypes)]
#[no_mangle]
#import_sig;
}
thread_local! {
static #split_loader_ident: wasm_split::LazySplitLoader = unsafe {
wasm_split::LazySplitLoader::new(#load_module_ident)
};
}
// Initiate the download by calling the load_module_ident function which will kick-off the loader
if !wasm_split::LazySplitLoader::ensure_loaded(&#split_loader_ident).await {
panic!("Failed to load wasm-split module");
}
unsafe { #impl_import_ident( #(#args),* ) }.await
}
#[cfg(not(target_arch = "wasm32"))]
#default_item
}
.into()
}
/// Create a lazy loader for a given function. Meant to be used in statics. Designed for libraries to
/// integrate with.
///
/// ```rust, ignore
/// fn SomeFunction(args: Args) -> Ret {}
///
/// static LOADER: wasm_split::LazyLoader<Args, Ret> = lazy_loader!(SomeFunction);
///
/// LOADER.load().await.call(args)
/// ```
#[proc_macro]
pub fn lazy_loader(input: TokenStream) -> TokenStream {
// We can only accept idents/paths that will be the source function
let sig = parse_macro_input!(input as Signature);
let params = sig.inputs.clone();
let outputs = sig.output.clone();
let Some(FnArg::Typed(arg)) = params.first().cloned() else {
panic!(
"Lazy Loader must define a single input argument to satisfy the LazyLoader signature"
)
};
let arg_ty = arg.ty.clone();
let LoaderNames {
name,
split_loader_ident,
impl_import_ident,
impl_export_ident,
load_module_ident,
..
} = LoaderNames::new(
sig.ident.clone(),
sig.abi
.as_ref()
.and_then(|abi| abi.name.as_ref().map(|f| f.value()))
.expect("abi to be module name")
.to_string(),
);
quote! {
{
#[cfg(target_arch = "wasm32")]
{
#[link(wasm_import_module = "./__wasm_split.js")]
extern "C" {
// The function we'll use to initiate the download of the module
#[no_mangle]
fn #load_module_ident(
callback: unsafe extern "C" fn(*const ::std::ffi::c_void, bool),
data: *const ::std::ffi::c_void,
);
#[allow(improper_ctypes)]
#[no_mangle]
fn #impl_import_ident(arg: #arg_ty) #outputs;
}
#[allow(improper_ctypes_definitions)]
#[no_mangle]
pub extern "C" fn #impl_export_ident(arg: #arg_ty) #outputs {
#name(arg)
}
thread_local! {
static #split_loader_ident: wasm_split::LazySplitLoader = unsafe {
wasm_split::LazySplitLoader::new(#load_module_ident)
};
};
unsafe {
wasm_split::LazyLoader::new(#impl_import_ident, &#split_loader_ident)
}
}
#[cfg(not(target_arch = "wasm32"))]
{
wasm_split::LazyLoader::preloaded(#name)
}
}
}
.into()
}
struct LoaderNames {
name: Ident,
split_loader_ident: Ident,
impl_import_ident: Ident,
impl_export_ident: Ident,
load_module_ident: Ident,
}
impl LoaderNames {
fn new(name: Ident, module: String) -> Self {
let unique_identifier = base16::encode_lower(
&sha2::Sha256::digest(format!("{name} {span:?}", name = name, span = name.span()))
[..16],
);
Self {
split_loader_ident: format_ident!("__wasm_split_loader_{module}"),
impl_export_ident: format_ident!(
"__wasm_split_00___{module}___00_export_{unique_identifier}_{name}"
),
impl_import_ident: format_ident!(
"__wasm_split_00___{module}___00_import_{unique_identifier}_{name}"
),
load_module_ident: format_ident!(
"__wasm_split_load_{module}_{unique_identifier}_{name}"
),
name,
}
}
}