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

Add host env via trait with proc macro #1739

Merged
merged 47 commits into from
Dec 1, 2020
Merged
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
3c7dfe2
Add hacked together vertical slice of host env updating idea
Oct 20, 2020
ed12021
Clean up code a bit
Oct 20, 2020
103ea03
Rename trait, get tests running
Oct 20, 2020
011ec22
Merge branch 'master' into feature/host-env-prototype
MarkMcCaskey Oct 20, 2020
b78447f
Fix up merge, use new union
Oct 20, 2020
d64908d
Fix package tests
Oct 21, 2020
46204d6
Fix up more tests
Oct 21, 2020
1b71887
Remove VMContext port to union, split into #1753
Oct 22, 2020
f3b7f42
Remove union logic from deprecated crate too
Oct 22, 2020
c5b94c5
Add first draft of `WasmerEnv` derive macro
Oct 22, 2020
a7abee6
Add lazy abstraction, improve macro, use macro internally
Oct 23, 2020
cfa87b2
Add misc changes, update from feedback
Oct 26, 2020
5b67b2f
Merge branch 'master' into feature/host-env-prototype
MarkMcCaskey Oct 27, 2020
1dd6d8d
Add modified attribute to get `NativeFunc` working
Oct 28, 2020
8233001
Add support for generics and lifetimes for WasmerEnv derive
Oct 28, 2020
8b87526
Create single interface to get exports from Instance.exports
Oct 28, 2020
be3ab95
Add error handling to host init logic
Oct 29, 2020
83008a7
Fix up error handling code, get tests passing
Oct 29, 2020
78f958b
Clean up misc code
Oct 29, 2020
a25288a
Improve error messages on `WasmerEnv` macro, clean up code, add tests
Oct 29, 2020
52191a0
Clean up and fix test
Oct 29, 2020
1c9fc1f
Fix up doc tests in api
Oct 29, 2020
2690e5e
Merge branch 'master' into feature/host-env-prototype
MarkMcCaskey Nov 16, 2020
a19705a
Remove free method, call finalizers in C API
Nov 17, 2020
7570710
Add test initializing and calling host function manually
Nov 17, 2020
655ac09
Fix up helpers generated by WasmerEnv; add `unchecked` variant
Nov 17, 2020
619afb5
Add support for tuple structs and unit structs in derive(WasmerEnv)
Nov 17, 2020
38b296e
Move `WasmerEnv` into its own mod, implement it for stdlib types
Nov 17, 2020
2b0464a
Port wasmer-emscripten to use `WasmerEnv`
Nov 18, 2020
6aab77e
Add default impl for `WasmerEnv::finish` remove macro
Nov 18, 2020
3580f16
Rename `WasmerEnv::finish` to `WasmerEnv::init_with_instance`
Nov 18, 2020
7131caa
Implement `WasmerEnv` for `dyn Any`
Nov 18, 2020
15e88a1
Clean up error message in proc macro, improve docs on WasmerEnv
Nov 18, 2020
2065556
Merge branch 'master' into feature/host-env-prototype
MarkMcCaskey Nov 18, 2020
85169f6
Add misc clean ups and corrections
Nov 18, 2020
0e03544
Merge branch 'feature/host-env-prototype' of github.com:wasmerio/wasm…
Nov 18, 2020
37d0c36
Clean up code, reduce memory usage, improve correctness
Nov 18, 2020
de6cb9c
Merge branch 'master' into feature/host-env-prototype
Nov 20, 2020
7013163
WIP attempt to make an Export wrapper type
Nov 25, 2020
40eec3f
Address feedback: misc clean ups
Nov 25, 2020
8669e92
Move `EngineExport` to engine
Nov 26, 2020
9e0cfce
Rename vm::Export to vm::VMExport
Nov 30, 2020
03410c1
Rename `EngineExport` to `Export`
Nov 30, 2020
2c9ca4a
Add engine::export
Nov 30, 2020
d0c44c6
Merge pull request #1842 from wasmerio/host-env-prototype-export-change
MarkMcCaskey Nov 30, 2020
4ef3286
Merge branch 'master' into feature/host-env-prototype
Dec 1, 2020
e960220
Update changelog with info about `WasmerEnv`
Dec 1, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename trait, get tests running
  • Loading branch information
Mark McCaskey committed Oct 20, 2020
commit 103ea035361c64c174e56ee1e77aebf606ce13d4
3 changes: 0 additions & 3 deletions examples/wasi.rs
Original file line number Diff line number Diff line change
@@ -51,9 +51,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let import_object = wasi_env.import_object(&module)?;
let instance = Instance::new(&module, &import_object)?;

// WASI requires to explicitly set the memory for the `WasiEnv`
wasi_env.set_memory(instance.exports.get_memory("memory")?.clone());

println!("Call WASI `_start` function...");
// And we just call the `_start` function!
let start = instance.exports.get_function("_start")?;
4 changes: 2 additions & 2 deletions lib/api/src/externals/function.rs
Original file line number Diff line number Diff line change
@@ -126,7 +126,7 @@ impl Function {
pub fn new_with_env<F, Env>(store: &Store, ty: &FunctionType, env: Env, func: F) -> Self
where
F: Fn(&mut Env, &[Val]) -> Result<Vec<Val>, RuntimeError> + 'static,
Env: Sized + crate::WasmerPostInstantiate + 'static,
Env: Sized + crate::WasmerEnv + 'static,
{
let dynamic_ctx = VMDynamicFunctionContext::from_context(VMDynamicFunctionWithEnv {
env: RefCell::new(env),
@@ -230,7 +230,7 @@ impl Function {
F: HostFunction<Args, Rets, WithEnv, Env>,
Args: WasmTypeList,
Rets: WasmTypeList,
Env: Sized + crate::WasmerPostInstantiate + 'static,
Env: Sized + crate::WasmerEnv + 'static,
{
let function = inner::Function::<Args, Rets>::new(func);
let address = function.address();
2 changes: 1 addition & 1 deletion lib/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -124,7 +124,7 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
// TODO: rename everything, all names are throw-away names

/// Prototype trait for finishing envs.
pub trait WasmerPostInstantiate {
pub trait WasmerEnv {
/// The function that Wasmer will call on your type to let it finish
/// instantiating.
fn finish(&mut self, instance: &Instance);
2 changes: 1 addition & 1 deletion lib/wasi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ pub struct WasiEnv {
memory: *mut Memory,
}

impl wasmer::WasmerPostInstantiate for WasiEnv {
impl wasmer::WasmerEnv for WasiEnv {
fn finish(&mut self, instance: &wasmer::Instance) {
dbg!("in Wasi::Finish");
let memory = instance.exports.get_memory("memory").unwrap();
42 changes: 36 additions & 6 deletions tests/compilers/imports.rs
Original file line number Diff line number Diff line change
@@ -86,7 +86,22 @@ fn dynamic_function_with_env() -> Result<()> {
let store = get_store(false);
let module = get_module(&store)?;

let env: Arc<AtomicUsize> = Arc::new(AtomicUsize::new(0));
#[derive(Clone)]
struct Env(Arc<AtomicUsize>);

impl std::ops::Deref for Env {
type Target = Arc<AtomicUsize>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl WasmerEnv for Env {
fn finish(&mut self, _instance: &Instance) {}
fn free(&mut self) {}
}

let env: Env = Env(Arc::new(AtomicUsize::new(0)));
Instance::new(
&module,
&imports! {
@@ -203,25 +218,40 @@ fn static_function_with_env() -> Result<()> {
let store = get_store(false);
let module = get_module(&store)?;

let env: Arc<AtomicUsize> = Arc::new(AtomicUsize::new(0));
#[derive(Clone)]
struct Env(Arc<AtomicUsize>);

impl std::ops::Deref for Env {
type Target = Arc<AtomicUsize>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl WasmerEnv for Env {
fn finish(&mut self, _instance: &Instance) {}
fn free(&mut self) {}
}

let env: Env = Env(Arc::new(AtomicUsize::new(0)));
Instance::new(
&module,
&imports! {
"host" => {
"0" => Function::new_native_with_env(&store, env.clone(), |env: &mut Arc<AtomicUsize>| {
"0" => Function::new_native_with_env(&store, env.clone(), |env: &mut Env| {
assert_eq!(env.fetch_add(1, SeqCst), 0);
}),
"1" => Function::new_native_with_env(&store, env.clone(), |env: &mut Arc<AtomicUsize>, x: i32| -> i32 {
"1" => Function::new_native_with_env(&store, env.clone(), |env: &mut Env, x: i32| -> i32 {
assert_eq!(x, 0);
assert_eq!(env.fetch_add(1, SeqCst), 1);
1
}),
"2" => Function::new_native_with_env(&store, env.clone(), |env: &mut Arc<AtomicUsize>, x: i32, y: i64| {
"2" => Function::new_native_with_env(&store, env.clone(), |env: &mut Env, x: i32, y: i64| {
assert_eq!(x, 2);
assert_eq!(y, 3);
assert_eq!(env.fetch_add(1, SeqCst), 2);
}),
"3" => Function::new_native_with_env(&store, env.clone(), |env: &mut Arc<AtomicUsize>, a: i32, b: i64, c: i32, d: f32, e: f64| {
"3" => Function::new_native_with_env(&store, env.clone(), |env: &mut Env, a: i32, b: i64, c: i32, d: f32, e: f64| {
assert_eq!(a, 100);
assert_eq!(b, 200);
assert_eq!(c, 300);
24 changes: 24 additions & 0 deletions tests/compilers/native_functions.rs
Original file line number Diff line number Diff line change
@@ -108,6 +108,18 @@ fn static_host_function_with_env() -> anyhow::Result<()> {
#[derive(Clone)]
struct Env(Rc<RefCell<i32>>);

impl std::ops::Deref for Env {
type Target = Rc<RefCell<i32>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl WasmerEnv for Env {
fn finish(&mut self, _instance: &Instance) {}
fn free(&mut self) {}
}

// Native static host function that returns a tuple.
{
let env = Env(Rc::new(RefCell::new(100)));
@@ -175,6 +187,18 @@ fn dynamic_host_function_with_env() -> anyhow::Result<()> {
#[derive(Clone)]
struct Env(Rc<RefCell<i32>>);

impl std::ops::Deref for Env {
type Target = Rc<RefCell<i32>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}

impl WasmerEnv for Env {
fn finish(&mut self, _instance: &Instance) {}
fn free(&mut self) {}
}

let env = Env(Rc::new(RefCell::new(100)));
let f = Function::new_with_env(
&store,