Skip to content

Commit

Permalink
Change load-component sig
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <[email protected]>
  • Loading branch information
rylev committed Mar 5, 2024
1 parent c9f9006 commit a2a15b9
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 65 additions & 8 deletions example/dynamic-guest/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ pub mod component {
}
}
}
#[derive(Clone)]
pub enum LoadError{
InvalidBytes(wit_bindgen::rt::string::String),
}
impl ::core::fmt::Debug for LoadError {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
match self {
LoadError::InvalidBytes(e) => {
f.debug_tuple("LoadError::InvalidBytes").field(e).finish()
}
}
}
}
impl ::core::fmt::Display for LoadError {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
write!(f, "{:?}", self)
}
}

impl std::error::Error for LoadError {}
impl Engine {
#[allow(unused_unsafe, clippy::all)]
pub fn new() -> Self{
Expand All @@ -167,26 +187,63 @@ pub mod component {
}
impl Engine {
#[allow(unused_unsafe, clippy::all)]
pub fn load_component(&self,path: &str,) -> Component{
pub fn load_component(&self,bytes: &[u8],) -> Result<Component,LoadError>{

#[allow(unused_imports)]
use wit_bindgen::rt::{alloc, vec::Vec, string::String};
unsafe {
let vec0 = path;

#[repr(align(4))]
struct RetArea([u8; 16]);
let mut ret_area = ::core::mem::MaybeUninit::<RetArea>::uninit();
let vec0 = bytes;
let ptr0 = vec0.as_ptr() as i32;
let len0 = vec0.len() as i32;

let ptr1 = ret_area.as_mut_ptr() as i32;
#[cfg(target_arch = "wasm32")]
#[link(wasm_import_module = "component:dyna/dynamic-component")]
extern "C" {
#[link_name = "[method]engine.load-component"]
fn wit_import(_: i32, _: i32, _: i32, ) -> i32;
fn wit_import(_: i32, _: i32, _: i32, _: i32, );
}

#[cfg(not(target_arch = "wasm32"))]
fn wit_import(_: i32, _: i32, _: i32, ) -> i32{ unreachable!() }
let ret = wit_import((self).handle() as i32, ptr0, len0);
Component::from_handle(ret as u32)
fn wit_import(_: i32, _: i32, _: i32, _: i32, ){ unreachable!() }
wit_import((self).handle() as i32, ptr0, len0, ptr1);
let l2 = i32::from(*((ptr1 + 0) as *const u8));
match l2 {
0 => {
let e = {
let l3 = *((ptr1 + 4) as *const i32);

Component::from_handle(l3 as u32)
};
Ok(e)
}
1 => {
let e = {
let l4 = i32::from(*((ptr1 + 4) as *const u8));
let v8 = match l4 {
n => {
debug_assert_eq!(n, 0, "invalid enum discriminant");
let e8 = {
let l5 = *((ptr1 + 8) as *const i32);
let l6 = *((ptr1 + 12) as *const i32);
let len7 = l6 as usize;
let bytes7 = Vec::from_raw_parts(l5 as *mut _, len7, len7);

wit_bindgen::rt::string_lift(bytes7)
};
LoadError::InvalidBytes(e8)
}
};

v8
};
Err(e)
}
_ => wit_bindgen::rt::invalid_enum_discriminant(),
}
}
}
}
Expand Down Expand Up @@ -289,7 +346,7 @@ pub mod component {
#[cfg(target_arch = "wasm32")]
#[link_section = "component-type:guest"]
#[doc(hidden)]
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 401] = [0, 97, 115, 109, 13, 0, 1, 0, 0, 25, 22, 119, 105, 116, 45, 99, 111, 109, 112, 111, 110, 101, 110, 116, 45, 101, 110, 99, 111, 100, 105, 110, 103, 4, 0, 7, 150, 2, 1, 65, 2, 1, 65, 4, 1, 66, 15, 4, 0, 6, 101, 110, 103, 105, 110, 101, 3, 1, 4, 0, 9, 99, 111, 109, 112, 111, 110, 101, 110, 116, 3, 1, 1, 113, 1, 3, 115, 116, 114, 1, 115, 0, 4, 0, 3, 118, 97, 108, 3, 0, 2, 1, 105, 0, 1, 64, 0, 0, 4, 4, 0, 19, 91, 99, 111, 110, 115, 116, 114, 117, 99, 116, 111, 114, 93, 101, 110, 103, 105, 110, 101, 1, 5, 1, 104, 0, 1, 105, 1, 1, 64, 2, 4, 115, 101, 108, 102, 6, 4, 112, 97, 116, 104, 115, 0, 7, 4, 0, 29, 91, 109, 101, 116, 104, 111, 100, 93, 101, 110, 103, 105, 110, 101, 46, 108, 111, 97, 100, 45, 99, 111, 109, 112, 111, 110, 101, 110, 116, 1, 8, 1, 104, 1, 1, 112, 3, 1, 64, 3, 4, 115, 101, 108, 102, 9, 4, 110, 97, 109, 101, 115, 6, 112, 97, 114, 97, 109, 115, 10, 0, 10, 4, 0, 22, 91, 109, 101, 116, 104, 111, 100, 93, 99, 111, 109, 112, 111, 110, 101, 110, 116, 46, 99, 97, 108, 108, 1, 11, 3, 1, 32, 99, 111, 109, 112, 111, 110, 101, 110, 116, 58, 100, 121, 110, 97, 47, 100, 121, 110, 97, 109, 105, 99, 45, 99, 111, 109, 112, 111, 110, 101, 110, 116, 5, 0, 1, 64, 0, 1, 0, 4, 0, 5, 104, 101, 108, 108, 111, 1, 1, 4, 1, 21, 99, 111, 109, 112, 111, 110, 101, 110, 116, 58, 103, 117, 101, 115, 116, 47, 103, 117, 101, 115, 116, 4, 0, 11, 11, 1, 0, 5, 103, 117, 101, 115, 116, 3, 0, 0, 0, 70, 9, 112, 114, 111, 100, 117, 99, 101, 114, 115, 1, 12, 112, 114, 111, 99, 101, 115, 115, 101, 100, 45, 98, 121, 2, 13, 119, 105, 116, 45, 99, 111, 109, 112, 111, 110, 101, 110, 116, 6, 48, 46, 50, 49, 46, 48, 16, 119, 105, 116, 45, 98, 105, 110, 100, 103, 101, 110, 45, 114, 117, 115, 116, 6, 48, 46, 49, 56, 46, 48];
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 447] = [0, 97, 115, 109, 13, 0, 1, 0, 0, 25, 22, 119, 105, 116, 45, 99, 111, 109, 112, 111, 110, 101, 110, 116, 45, 101, 110, 99, 111, 100, 105, 110, 103, 4, 0, 7, 196, 2, 1, 65, 2, 1, 65, 4, 1, 66, 19, 4, 0, 6, 101, 110, 103, 105, 110, 101, 3, 1, 4, 0, 9, 99, 111, 109, 112, 111, 110, 101, 110, 116, 3, 1, 1, 113, 1, 3, 115, 116, 114, 1, 115, 0, 4, 0, 3, 118, 97, 108, 3, 0, 2, 1, 113, 1, 13, 105, 110, 118, 97, 108, 105, 100, 45, 98, 121, 116, 101, 115, 1, 115, 0, 4, 0, 10, 108, 111, 97, 100, 45, 101, 114, 114, 111, 114, 3, 0, 4, 1, 105, 0, 1, 64, 0, 0, 6, 4, 0, 19, 91, 99, 111, 110, 115, 116, 114, 117, 99, 116, 111, 114, 93, 101, 110, 103, 105, 110, 101, 1, 7, 1, 104, 0, 1, 112, 125, 1, 105, 1, 1, 106, 1, 10, 1, 5, 1, 64, 2, 4, 115, 101, 108, 102, 8, 5, 98, 121, 116, 101, 115, 9, 0, 11, 4, 0, 29, 91, 109, 101, 116, 104, 111, 100, 93, 101, 110, 103, 105, 110, 101, 46, 108, 111, 97, 100, 45, 99, 111, 109, 112, 111, 110, 101, 110, 116, 1, 12, 1, 104, 1, 1, 112, 3, 1, 64, 3, 4, 115, 101, 108, 102, 13, 4, 110, 97, 109, 101, 115, 6, 112, 97, 114, 97, 109, 115, 14, 0, 14, 4, 0, 22, 91, 109, 101, 116, 104, 111, 100, 93, 99, 111, 109, 112, 111, 110, 101, 110, 116, 46, 99, 97, 108, 108, 1, 15, 3, 1, 32, 99, 111, 109, 112, 111, 110, 101, 110, 116, 58, 100, 121, 110, 97, 47, 100, 121, 110, 97, 109, 105, 99, 45, 99, 111, 109, 112, 111, 110, 101, 110, 116, 5, 0, 1, 64, 0, 1, 0, 4, 0, 5, 104, 101, 108, 108, 111, 1, 1, 4, 1, 21, 99, 111, 109, 112, 111, 110, 101, 110, 116, 58, 103, 117, 101, 115, 116, 47, 103, 117, 101, 115, 116, 4, 0, 11, 11, 1, 0, 5, 103, 117, 101, 115, 116, 3, 0, 0, 0, 70, 9, 112, 114, 111, 100, 117, 99, 101, 114, 115, 1, 12, 112, 114, 111, 99, 101, 115, 115, 101, 100, 45, 98, 121, 2, 13, 119, 105, 116, 45, 99, 111, 109, 112, 111, 110, 101, 110, 116, 6, 48, 46, 50, 49, 46, 48, 16, 119, 105, 116, 45, 98, 105, 110, 100, 103, 101, 110, 45, 114, 117, 115, 116, 6, 48, 46, 49, 56, 46, 48];

#[inline(never)]
#[doc(hidden)]
Expand Down
3 changes: 2 additions & 1 deletion example/dynamic-guest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ pub struct Component;

impl Guest for Component {
fn hello() {
let component_bytes = std::fs::read("example/static_guest.wasm").unwrap();
let engine = Engine::new();
let component = engine.load_component("example/static_guest.wasm");
let component = engine.load_component(&component_bytes).unwrap();
let val = component.call("hello-world", &[]);
println!("Hello from the guest: {:?}", val);
}
Expand Down
1 change: 1 addition & 0 deletions example/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"

[dependencies]
cap-std = "2.0"
tokio = { version = "1.0", features = ["full"] }
wasmtime = "18.0.0"
wasmtime-wasi = "18.0.0"
Expand Down
4 changes: 4 additions & 0 deletions example/host/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ struct State {

impl State {
fn new() -> Self {
let dir = cap_std::fs::Dir::open_ambient_dir(".", cap_std::ambient_authority()).unwrap();
let dir_perms = wasmtime_wasi::preview2::DirPerms::all();
let file_perms = wasmtime_wasi::preview2::FilePerms::all();
Self {
table: wasmtime::component::ResourceTable::new(),
ctx: wasmtime_wasi::preview2::WasiCtxBuilder::new()
.preopened_dir(dir, dir_perms, file_perms, "/")
.inherit_stdio()
.build(),
}
Expand Down
14 changes: 9 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,27 @@ where
fn load_component(
&mut self,
self_: wasmtime::component::Resource<component::dyna::dynamic_component::Engine>,
path: String,
bytes: Vec<u8>,
) -> wasmtime::Result<
wasmtime::component::Resource<component::dyna::dynamic_component::Component>,
Result<
wasmtime::component::Resource<component::dyna::dynamic_component::Component>,
component::dyna::dynamic_component::LoadError,
>,
> {
println!("Loading component from path: {}", path);
let self_ = wasmtime::component::Resource::new_borrow(self_.rep());
let engine = self.table().get(&self_).unwrap();
let mut store = wasmtime::Store::new(engine, ());
let component = wasmtime::component::Component::from_file(engine, path).unwrap();
let component = wasmtime::component::Component::new(engine, bytes).map_err(|e| {
component::dyna::dynamic_component::LoadError::InvalidBytes(e.to_string())
})?;
let linker = wasmtime::component::Linker::new(engine);
let instance = linker.instantiate(&mut store, &component).unwrap();
let component_state = ComponentState { instance, store };
let resource = self
.table()
.push(component_state)
.map_err(|e| anyhow::anyhow!("{e}"))?;
Ok(wasmtime::component::Resource::new_own(resource.rep()))
Ok(Ok(wasmtime::component::Resource::new_own(resource.rep())))
}

fn drop(
Expand Down
6 changes: 5 additions & 1 deletion wit/world.wit
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ world dynamic-dispatch {
interface dynamic-component {
resource engine {
constructor();
load-component: func(path: string) -> component;
load-component: func(bytes: list<u8>) -> result<component, load-error>;
}

resource component {
Expand All @@ -17,4 +17,8 @@ interface dynamic-component {
variant val {
str(string)
}

variant load-error {
invalid-bytes(string)
}
}

0 comments on commit a2a15b9

Please sign in to comment.