pit_rust_guest/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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
use std::iter::once;
use pit_core::{Arg, Interface, ResTy, Sig};
use proc_macro2::TokenStream;
use quasiquote::quasiquote;
use quote::{format_ident, quote};
use sha3::Digest;
use std::io::Write;
pub struct Opts {
pub root: TokenStream,
pub salt: Vec<u8>,
pub tpit: bool,
}
pub fn render(opts: &Opts, i: &Interface) -> TokenStream {
let root = &opts.root;
let id = i.rid_str();
let mut ha = sha3::Sha3_256::default();
write!(ha, "~{}", id);
ha.write(&opts.salt);
let ha = hex::encode(ha.finalize());
let id2 = format_ident!("R{}", i.rid_str());
let methods = i.methods.iter().map(|(a, b)| {
quasiquote! {
fn #{format_ident!("{a}")}#{render_sig(opts,root,i,b,"e! {&mut self},false)};
}
});
let xref = if opts.tpit {
quote! {}
} else {
quasiquote!(
#[#root::externref::externref(crate = #{quote! {#root::externref}.to_string()})]
)
};
let res = if opts.tpit {
quote! {
#root::tpit_rt::Tpit
}
} else {
quote! {
#root::externref::Resource
}
};
let rx = if opts.tpit {
quote! {
u32
}
} else {
quote! {
&mut #res<Box<dyn #id2>>
}
};
let t = if opts.tpit { "t" } else { "" };
let impl_dyns = i.methods.iter().map(|(a, b)| {
quasiquote! {
fn #{format_ident!("{a}")}#{render_sig(opts,root,i,b,"e! {&mut self},false)}{
#xref
#[link(wasm_import_module = #{format!("{t}pit/{}",i.rid_str())})]
extern "C"{
#[link_name = #a]
fn go #{render_sig(opts,root, i,b, "e! {this: #rx},true)};
}
return unsafe{go(#{
if opts.tpit{
quote!{self.ptr()}
}else{
quote!{self}
}
},#{
let params = b.params.iter().enumerate().map(|(a,b)|{
let mut c = format_ident!("p{a}");
let mut c = quote!{
#c
};
if let Arg::Resource { ty, nullable, take, ann } = b{
if opts.tpit{
if !*take{
c = quote!{
#root::tpit_rt::Tpit::summon(&mut #c)
}
}
}
}
c
});
quote! {
#(#params),*
}
})};
}
}
});
let chains2 = i.methods.iter().map(|(a,b)|quasiquote! {
#xref
#[export_name = #{format!("{t}pit/{id}/~{ha}/{a}")}]
extern "C" fn #{format_ident!("{a}")}#{render_sig(opts,root,i,b,"e! {id: u32},true)}{
return unsafe{&mut *(TABLE.all.get())}.get_mut(&id).unwrap().#{format_ident!("{a}")}(#{
let params = b.params.iter().enumerate().map(|(a,b)|{
let mut c = format_ident!("p{a}");
let mut c = quote!{
#c
};
if let Arg::Resource { ty, nullable, take, ann } = b{
if opts.tpit{
if !*take{
c = quote!{
#c.ptr()
}
}
}
}
c
});
quote! {
#(#params),*
}
});
}
});
let sb = i.to_string();
let sc = sb
.as_bytes()
.iter()
.cloned()
.chain(once(0u8))
.collect::<Vec<_>>();
quasiquote! {
pub trait #id2{
#(#methods)*
}
const _: () = {
#[link_section = ".pit-types"]
static SECTION_CONTENT: [u8; #{sc.len()}] = #{
quote!{
[#(#sc),*]
}
};
fn alloc<T>(m: &mut ::std::collections::BTreeMap<u32,T>, x: T) -> u32{
let mut u = 0;
while m.contains_key(&u){
u += 1;
};
m.insert(u,x);
return u;
}
#[derive(Default)]
struct TableCell{
all: std::cell::UnsafeCell<::std::collections::BTreeMap<u32,Box<dyn #id2>>>
};
unsafe impl Send for TableCell{}
unsafe impl Sync for TableCell{}
static TABLE: ::std::sync::LazyLock<TableCell> = ::std::sync::LazyLock::new(||TableCell::default());
impl #id2 for #res<Box<dyn #id2>>{
#(#impl_dyns)*
}
#xref
#[export_name = #{format!("{t}pit/{id}/~{ha}.drop")}]
extern "C" fn _drop(a: u32){
unsafe{
(&mut *(TABLE.all.get())).remove(&a)
};
}
#(#chains2)*
impl From<Box<dyn #id2>> for #res<Box<dyn #id2>>{
fn from(a: Box<dyn #id2>) -> Self{
#xref
#[link(wasm_import_module = #{format!("pit/{}",i.rid_str())})]
extern "C"{
#[link_name = #{format!("~{ha}")}]
fn _push(a: u32) -> #res<Box<dyn #id2>>;
}
return unsafe{
_push(alloc(&mut *(TABLE.all.get()),a))
}
}
}
};
// mod #internal{
// use super::#id2;
// pub fn push(a: Box<dyn #id2>) -> #root::externref::Resource<Box<dyn #id2>>{
// return unsafe{
// _push(Box::into_raw(Box::new(a)))
// }
// }
// }
}
}
pub fn render_sig(
opts: &Opts,
root: &TokenStream,
base: &Interface,
s: &Sig,
self_: &TokenStream,
ffi: bool,
) -> TokenStream {
let params = s
.params
.iter()
.map(|a| render_ty(opts, root, base, a, ffi))
.enumerate()
.map(|(a, b)| quasiquote!(#{format_ident!("p{a}")} : #b));
let params = once(self_).cloned().chain(params);
let rets = s.rets.iter().map(|a| render_ty(opts, root, base, a, ffi));
quote! {
(#(#params),*) -> (#(#rets),*)
}
}
pub fn render_ty(
opts: &Opts,
root: &TokenStream,
base: &Interface,
p: &Arg,
ffi: bool,
) -> TokenStream {
match p {
Arg::I32 => quote! {
u32
},
Arg::I64 => quote! {
u64
},
Arg::F32 => quote! {
f32
},
Arg::F64 => quote! {
f64
},
Arg::Resource {
ty,
nullable,
take,
ann,
} => {
if !opts.tpit {
let ty = match ty {
ResTy::Of(a) => quasiquote! {
#root::externref::Resource<Box<dyn #{format_ident!("R{}",hex::encode(a))}>>
},
ResTy::None => quote! {
#root::externref::Resource<()>
},
ResTy::This => quasiquote! {
#root::externref::Resource<Box<dyn #{format_ident!("R{}",base.rid_str())}>>
},
};
let ty = if *nullable {
quote! {Option<#ty>}
} else {
ty
};
let ty = if *take {
ty
} else {
quote! {&mut #ty}
};
ty
} else {
let ty = match ty {
ResTy::Of(a) => quasiquote! {
#root::tpit_rt::Tpit<Box<dyn #{format_ident!("R{}",hex::encode(a))}>>
},
ResTy::None => quote! {
#root::tpit_rt::Tpit<()>
},
ResTy::This => quasiquote! {
#root::tpit_rt::Tpit<Box<dyn #{format_ident!("R{}",base.rid_str())}>>
},
};
let mut ty = if *take {
ty
} else {
quote! {&mut #ty}
};
if *take && ffi {
ty = quote! {u32}
}
ty
}
}
// Arg::Func(_) => todo!()
}
}