Skip to content

Instantly share code, notes, and snippets.

@peterhuene
Created June 14, 2022 21:40
Show Gist options
  • Save peterhuene/9c1e501f82412c5a39db72443caaebc0 to your computer and use it in GitHub Desktop.
Save peterhuene/9c1e501f82412c5a39db72443caaebc0 to your computer and use it in GitHub Desktop.
Extern component binding crates
/// Declares the export of the interface for the given type.
#[macro_export]
macro_rules! export(($t:ident) => {
#[export_name = "backend-0.1.0#fetch"]
unsafe extern "C" fn __wit_bindgen_backend_fetch(arg0: i32, arg1: i32, ) -> i32{
#[allow(unused_imports)]
use wit_bindgen_rust;
use backend::*;
let len0 = arg1 as usize;
let result = <$t as Backend>::fetch(String::from_utf8(Vec::from_raw_parts(arg0 as *mut _, len0, len0)).unwrap());
let ptr1 = BACKEND_RET_AREA.0.as_mut_ptr() as i32;
let vec2 = (result).into_boxed_slice();
let ptr2 = vec2.as_ptr() as i32;
let len2 = vec2.len() as i32;
core::mem::forget(vec2);
*((ptr1 + 4) as *mut i32) = len2;
*((ptr1 + 0) as *mut i32) = ptr2;
ptr1
}
#[repr(align(4))]
struct RetArea([u8; 8]);
static mut BACKEND_RET_AREA: RetArea = RetArea([0; 8]);
});
pub trait Backend {
/// Fetch the content bytes of the given URL.
fn fetch(url: String) -> Vec<u8>;
}
/// Fetch the content bytes of the given URL.
fetch: func(url: string) -> list<u8>
/// Get a value from the cache.
pub fn get(key: &str) -> Option<Vec<u8>> {
unsafe {
let vec0 = key;
let ptr0 = vec0.as_ptr() as i32;
let len0 = vec0.len() as i32;
let ptr1 = CACHE_RET_AREA.0.as_mut_ptr() as i32;
#[link(wasm_import_module = "cache-0.1.0")]
extern "C" {
#[cfg_attr(target_arch = "wasm32", link_name = "get")]
#[cfg_attr(not(target_arch = "wasm32"), link_name = "cache-0.1.0_get")]
fn wit_import(_: i32, _: i32, _: i32);
}
wit_import(ptr0, len0, ptr1);
match i32::from(*((ptr1 + 0) as *const u8)) {
0 => None,
1 => Some({
let len2 = *((ptr1 + 8) as *const i32) as usize;
Vec::from_raw_parts(*((ptr1 + 4) as *const i32) as *mut _, len2, len2)
}),
_ => panic!("invalid enum discriminant"),
}
}
}
/// Put a value into the cache.
pub fn put(key: &str, value: &[u8]) -> () {
unsafe {
let vec0 = key;
let ptr0 = vec0.as_ptr() as i32;
let len0 = vec0.len() as i32;
let vec1 = value;
let ptr1 = vec1.as_ptr() as i32;
let len1 = vec1.len() as i32;
#[link(wasm_import_module = "cache-0.1.0")]
extern "C" {
#[cfg_attr(target_arch = "wasm32", link_name = "put")]
#[cfg_attr(not(target_arch = "wasm32"), link_name = "cache-0.1.0_put")]
fn wit_import(_: i32, _: i32, _: i32, _: i32);
}
wit_import(ptr0, len0, ptr1, len1);
()
}
}
#[repr(align(4))]
struct RetArea([u8; 12]);
static mut CACHE_RET_AREA: RetArea = RetArea([0; 12]);
/// Get a value from the cache.
get: func(key: string) -> option<list<u8>>
/// Put a value into the cache.
put: func(key: string, value: list<u8>)
# ...
[package.metadata.component.dependencies]
cache = { version = "0.1.0", path = "cache.wit" }
origin = { version = "0.1.0", path = "backend.wit" }
backend = { version = "0.1.0", path = "backend.wit", export = true }
# ...
/// Fetch the content bytes of the given URL.
pub fn fetch(url: &str) -> Vec<u8> {
unsafe {
let vec0 = url;
let ptr0 = vec0.as_ptr() as i32;
let len0 = vec0.len() as i32;
let ptr1 = ORIGIN_RET_AREA.0.as_mut_ptr() as i32;
#[link(wasm_import_module = "backend-0.1.0")]
extern "C" {
#[cfg_attr(target_arch = "wasm32", link_name = "fetch")]
#[cfg_attr(not(target_arch = "wasm32"), link_name = "backend-0.1.0_fetch")]
fn wit_import(_: i32, _: i32, _: i32);
}
wit_import(ptr0, len0, ptr1);
let len2 = *((ptr1 + 4) as *const i32) as usize;
Vec::from_raw_parts(*((ptr1 + 0) as *const i32) as *mut _, len2, len2)
}
}
#[repr(align(4))]
struct RetArea([u8; 8]);
static mut ORIGIN_RET_AREA: RetArea = RetArea([0; 8]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment