feat: add __network/html-crawl-parse, __wasm/wasmtime-serde-demo
This commit is contained in:
16
__wasm/wasmtime-serde-demo/crates/host_macro/Cargo.toml
Normal file
16
__wasm/wasmtime-serde-demo/crates/host_macro/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "wasmtime_serde_host_macro"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "Unlicense OR MIT"
|
||||
authors = ["Heráclito <heraclitoqsaldanha@gmail.com>"]
|
||||
description = "Simple library for serializing complex types to the wasmtime runtime using serde"
|
||||
repository = "https://github.com/Heraclito-Q-Saldanha/wasmtime_serde"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
syn = {version = "2.0.18", features = ["full"] }
|
||||
proc-macro2 = "1.0.59"
|
||||
quote = "1.0.28"
|
||||
70
__wasm/wasmtime-serde-demo/crates/host_macro/src/lib.rs
Normal file
70
__wasm/wasmtime-serde-demo/crates/host_macro/src/lib.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
//! Simple library for serializing complex types to the wasmtime runtime using serde
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::{parse_macro_input, ItemFn};
|
||||
|
||||
struct FnHost {
|
||||
functions: Vec<syn::Ident>,
|
||||
}
|
||||
|
||||
impl syn::parse::Parse for FnHost {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::parse::Result<Self> {
|
||||
let mut functions = vec![];
|
||||
let mut end = false;
|
||||
while let Ok(f) = input.parse::<syn::Ident>() {
|
||||
if end {
|
||||
panic!("comma")
|
||||
}
|
||||
functions.push(f);
|
||||
end = input.parse::<syn::Token![,]>().is_err();
|
||||
}
|
||||
Ok(Self { functions })
|
||||
}
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn host_funcs(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as FnHost);
|
||||
let mut list = quote!();
|
||||
let len = input.functions.len();
|
||||
for name in input.functions {
|
||||
let name = format_ident!("_wasm_host_{}", name);
|
||||
let str_name: proc_macro2::TokenStream = format!(r#""{name}""#).parse().unwrap();
|
||||
list = quote!(#list (#str_name, #name),);
|
||||
}
|
||||
quote!({
|
||||
const HOST_FUNC:[(&str, fn(&[u8]) -> Vec<u8>);#len] = [#list];
|
||||
&HOST_FUNC
|
||||
})
|
||||
.into()
|
||||
}
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn export_fn(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
let data = parse_macro_input!(item as ItemFn);
|
||||
let name = &data.sig.ident;
|
||||
let extern_name = format_ident!("_wasm_host_{}", name);
|
||||
let gen = {
|
||||
let mut argument_types = quote!();
|
||||
let mut call = quote!();
|
||||
for (i, arg) in data.sig.inputs.iter().enumerate() {
|
||||
let i = syn::Index::from(i);
|
||||
call = quote!(#call message.#i,);
|
||||
if let syn::FnArg::Typed(t) = arg {
|
||||
let ty = &t.ty;
|
||||
argument_types = quote!(#argument_types #ty,);
|
||||
} else {
|
||||
panic!();
|
||||
}
|
||||
}
|
||||
argument_types = quote! { (#argument_types) };
|
||||
quote! {
|
||||
fn #extern_name(value: &[u8]) -> Vec<u8> {
|
||||
let message:#argument_types = wasmtime_serde_host::deserialize(value).unwrap();
|
||||
wasmtime_serde_host::serialize(&#name(#call)).unwrap()
|
||||
}
|
||||
}
|
||||
};
|
||||
quote!(#gen #data).into()
|
||||
}
|
||||
Reference in New Issue
Block a user