fl_wasm_rs_macros/
lib.rs

1use proc_macro::TokenStream;
2use proc_macro_error::proc_macro_error;
3use quote::quote;
4
5#[proc_macro_error]
6#[proc_macro_derive(FLFunction)]
7pub fn derive_function(_input: TokenStream) -> TokenStream {
8    quote!(
9        // version of the runtime api
10        pub const VERSION: i32 = 1;
11
12        #[no_mangle]
13        pub extern "C" fn __runtime_version() -> i32 {
14            VERSION
15        }
16
17        #[no_mangle]
18        #[cfg(target_arch = "wasm32")]
19        pub extern "C" fn __invoke(req_len: i32) -> i32 {
20            // get input data
21            let input_data = get_input_data(req_len);
22
23            // parse input data
24            let json = serde_json::from_slice(&input_data);
25
26            if json.is_err() {
27                insert_error("input JSON not well-formatted");
28                return 1;
29            }
30            // unwrap is safe, since we checked for errors above
31            let json = json.unwrap();
32
33            // call the user-defined function
34            let res = fl_main(json);
35
36            // handle the result
37            match res {
38                Ok(data) => {
39                    let resp = data.to_string();
40                    insert_response(&resp);
41                    0
42                }
43                Err(e) => {
44                    let errmsg = e.to_string();
45                    insert_error(&errmsg);
46                    1
47                }
48            }
49        }
50    )
51    .into()
52}