Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Playing around with witx-bindgen #175

Open
willemneal opened this issue Nov 9, 2021 · 1 comment
Open

Playing around with witx-bindgen #175

willemneal opened this issue Nov 9, 2021 · 1 comment

Comments

@willemneal
Copy link
Contributor

willemneal commented Nov 9, 2021

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),
			);
		}
	}
}
@cohix
Copy link
Contributor

cohix commented Nov 11, 2021

Yeah!! @ospencer is working hard on supporting this in multiple languages 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants