Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
52 lines (45 loc) · 1.4 KB
/
Copy pathlib.rs
File metadata and controls
52 lines (45 loc) · 1.4 KB
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
use proc_macro::TokenStream;
use proc_macro_error::proc_macro_error;
use quote::quote;
#[proc_macro_error]
#[proc_macro_derive(FLFunction)]
pub fn derive_function(_input: TokenStream) -> TokenStream {
quote!(
// version of the runtime api
pub const VERSION: i32 = 1;
#[no_mangle]
pub extern "C" fn __runtime_version() -> i32 {
VERSION
}
#[no_mangle]
#[cfg(target_arch = "wasm32")]
pub extern "C" fn __invoke(req_len: i32) -> i32 {
// get input data
let input_data = get_input_data(req_len);
// parse input data
let json = serde_json::from_slice(&input_data);
if json.is_err() {
insert_error("input JSON not well-formatted");
return 1;
}
// unwrap is safe, since we checked for errors above
let json = json.unwrap();
// call the user-defined function
let res = fl_main(json);
// handle the result
match res {
Ok(data) => {
let resp = data.to_string();
insert_response(&resp);
0
}
Err(e) => {
let errmsg = e.to_string();
insert_error(&errmsg);
1
}
}
}
)
.into()
}