Skip to content

Conversation

@yurydelendik
Copy link
Contributor

@yurydelendik yurydelendik commented Sep 23, 2019

The problem: the have multiple ways to express signature metadata needed by wasmtime attached to a Rust function. There are several ways to define WASI interface in wasi-common and other project. This patch will try to describe and combine all macros that are needed to connect a native Rust function with its wasmtime cranelift_codegen::ir::Signature. (If wrapper/proxy exists, a native function arguments are expected to be converted automatically to corresponding cranelift_codegen::ir::Type)

TODOs:

  • convert WASI signature/syscall as example

@yurydelendik
Copy link
Contributor Author

yurydelendik commented Sep 23, 2019

Docs:

#[wasmtime_method(module(func1_desc))]
fn func1(a: u8) -> u8 { a + 1 }

will generate/create a function with wasmtime-compatible ABI that looks like, and converts parameters to wasm-friendly types:

extern "C" fn func1(vmctx: *mut VMContext, arg1: u32) -> u32 {
  let c = |a:u8| -> u8 { a + 1 };
  c(arg1 as u8) as u32
}
mod func1_desc { ... }

The func1_desc will have signature() -> ir::Signature and Wrapper class to map function signature to the wasmtime_runtime export.

TODO:

  • add macros to hide the Wrapper implementation details

Also,

#[wasmtime_trait(module(trait1_desc))]
trait Trait1 {
  fn func2(a: u8) -> u8;
}

produces:

trait Trait1 { .. }
mod trait1_desc {
  fn get_self(vmctx: *mut VMContext) -> dyn Trait1 { ... }
  extern "C" fn func2(vmctx: *mut VMContext, arg1: u32) -> u32 {
     ...
     get_self(vmctx).func2(arg1 as u8) as u32
  }
}

And similar to function the Wrapper struct will be generated to map InstanceHandle exports to trait.

TODO:

  • macro to wrap implementation details on how to wrap InstanceHandle
  • more docs and examples (please find "wasmtime-api/examples/hello2.rs" for now and use cargo expand)

@yurydelendik yurydelendik force-pushed the bindings branch 4 times, most recently from ff231a9 to 20d66de Compare October 1, 2019 14:20
@yurydelendik
Copy link
Contributor Author

yurydelendik commented Oct 10, 2019

hello2.rs and hello3.rs expanded https://gist.github.com/yurydelendik/2c1bd0fa76642712203f9ece3680a104

TODO:

@yurydelendik
Copy link
Contributor Author

* Why function wrapper has InstanceHandle?

The vmctx field is needed when context() is provided #[wasmtime_method(...)]. The context object is connected with instance (e.g. to get global_exports) rather than CL methods vmctx.

@yurydelendik
Copy link
Contributor Author

added hello4.rs with annotated impl (the gist above has expanded code)

@steveej
Copy link
Contributor

steveej commented Nov 26, 2019

I filed a PR to your PR branch 😄 It adds basic support for a visibility modifier: yurydelendik#1

First, this fixes a typo where the hardcoded `Inherited` visiblity
wasn't properly set due to a variable name mixup.

Second, this adds support for a visibility attribute to the `wrap_impl`
macro.

I tested the _pub_ modifier, only manually, all others are completely
untested.
@yurydelendik
Copy link
Contributor Author

The zero shims PR addresses the same issue. WasmTy provides flexibility to access callers context. Closing this PR as a duplicate.

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

Successfully merging this pull request may close these issues.

2 participants