reactr
reactr copied to clipboard
Playing around with witx-bindgen
I've been reading over the witx-bindgen repo and decided to give it a try to see how to generate host imports. Still need to test it out with their macro that lets you import this, but I'm still pretty happy with the initial output. Rust encodes enums starting at 0, so this should work out of the box!
enum method {
Get,
Head,
Options,
Post,
Put,
Patch,
Delete,
}
type url = string
type body = list<u8>
fetch_url: function(method: method, url: url, body: body, ident: s32)
witx-bindgen rust-wasm -i input.witx --out-dir ./out
mod imports {
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Method {
Get,
Head,
Options,
Post,
Put,
Patch,
Delete,
}
impl std::fmt::Debug for Method {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Method::Get => f.debug_tuple("Method::Get").finish(),
Method::Post => f.debug_tuple("Method::Post").finish(),
Method::Patch => f.debug_tuple("Method::Patch").finish(),
Method::Delete => f.debug_tuple("Method::Delete").finish(),
}
}
}
pub type Url<'a> = &'a str;
pub type Body<'a> = &'a [u8];
pub fn fetch_url(method: Method, url: Url<'_>, body: Body<'_>, ident: i32) {
unsafe {
let vec0 = url;
let ptr0 = vec0.as_ptr() as i32;
let len0 = vec0.len() as i32;
let vec1 = body;
let ptr1 = vec1.as_ptr() as i32;
let len1 = vec1.len() as i32;
#[link(wasm_import_module = "imports")]
extern "C" {
#[cfg_attr(target_arch = "wasm32", link_name = "fetch_url")]
#[cfg_attr(not(target_arch = "wasm32"), link_name = "imports_fetch_url")]
fn witx_import(_: i32, _: i32, _: i32, _: i32, _: i32, _: i32);
}
witx_import(
method as i32,
ptr0,
len0,
ptr1,
len1,
witx_bindgen_rust::rt::as_i32(ident),
);
}
}
}
Yeah!! @ospencer is working hard on supporting this in multiple languages 🙂