Skip to content

Commit

Permalink
Merge pull request #3145 from wasmerio/capi-wasi-overwrite-stdin-3
Browse files Browse the repository at this point in the history
C-API: add functions to overwrite stdin / stdout / stderr handlers
  • Loading branch information
syrusakbary authored Oct 17, 2022
2 parents 03e8911 + 05d74ea commit e1e08f4
Show file tree
Hide file tree
Showing 16 changed files with 1,268 additions and 196 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions examples/wasi_pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use std::io::{Read, Write};
use wasmer::{Instance, Module, Store};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_wasi::{Pipe, WasiState};
use wasmer_wasi::{WasiBidirectionalSharedPipePair, WasiState};

fn main() -> Result<(), Box<dyn std::error::Error>> {
let wasm_path = concat!(
Expand All @@ -36,8 +36,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("Creating `WasiEnv`...");
// First, we create the `WasiEnv` with the stdio pipes
let mut input = Pipe::new();
let mut output = Pipe::new();
let mut input = WasiBidirectionalSharedPipePair::new().with_blocking(false);
let mut output = WasiBidirectionalSharedPipePair::new().with_blocking(false);
let wasi_env = WasiState::new("hello")
.stdin(Box::new(input.clone()))
.stdout(Box::new(output.clone()))
Expand Down
46 changes: 20 additions & 26 deletions lib/api/tests/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,28 +210,25 @@ fn memory_grow() -> Result<(), String> {
fn function_new() -> Result<(), String> {
let mut store = Store::default();
let function = Function::new_typed(&mut store, || {});
assert_eq!(
function.ty(&mut store).clone(),
FunctionType::new(vec![], vec![])
);
assert_eq!(function.ty(&mut store), FunctionType::new(vec![], vec![]));
let function = Function::new_typed(&mut store, |_a: i32| {});
assert_eq!(
function.ty(&mut store).clone(),
function.ty(&mut store),
FunctionType::new(vec![Type::I32], vec![])
);
let function = Function::new_typed(&mut store, |_a: i32, _b: i64, _c: f32, _d: f64| {});
assert_eq!(
function.ty(&mut store).clone(),
function.ty(&mut store),
FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![])
);
let function = Function::new_typed(&mut store, || -> i32 { 1 });
assert_eq!(
function.ty(&mut store).clone(),
function.ty(&mut store),
FunctionType::new(vec![], vec![Type::I32])
);
let function = Function::new_typed(&mut store, || -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) });
assert_eq!(
function.ty(&mut store).clone(),
function.ty(&mut store),
FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64])
);
Ok(())
Expand All @@ -246,14 +243,11 @@ fn function_new_env() -> Result<(), String> {
let my_env = MyEnv {};
let env = FunctionEnv::new(&mut store, my_env);
let function = Function::new_typed_with_env(&mut store, &env, |_env: FunctionEnvMut<MyEnv>| {});
assert_eq!(
function.ty(&mut store).clone(),
FunctionType::new(vec![], vec![])
);
assert_eq!(function.ty(&mut store), FunctionType::new(vec![], vec![]));
let function =
Function::new_typed_with_env(&mut store, &env, |_env: FunctionEnvMut<MyEnv>, _a: i32| {});
assert_eq!(
function.ty(&mut store).clone(),
function.ty(&mut store),
FunctionType::new(vec![Type::I32], vec![])
);
let function = Function::new_typed_with_env(
Expand All @@ -262,13 +256,13 @@ fn function_new_env() -> Result<(), String> {
|_env: FunctionEnvMut<MyEnv>, _a: i32, _b: i64, _c: f32, _d: f64| {},
);
assert_eq!(
function.ty(&mut store).clone(),
function.ty(&mut store),
FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![])
);
let function =
Function::new_typed_with_env(&mut store, &env, |_env: FunctionEnvMut<MyEnv>| -> i32 { 1 });
assert_eq!(
function.ty(&mut store).clone(),
function.ty(&mut store),
FunctionType::new(vec![], vec![Type::I32])
);
let function = Function::new_typed_with_env(
Expand All @@ -277,7 +271,7 @@ fn function_new_env() -> Result<(), String> {
|_env: FunctionEnvMut<MyEnv>| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) },
);
assert_eq!(
function.ty(&mut store).clone(),
function.ty(&mut store),
FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64])
);
Ok(())
Expand All @@ -294,35 +288,35 @@ fn function_new_dynamic() -> Result<(), String> {
&function_type,
|_values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&mut store).clone(), function_type);
assert_eq!(function.ty(&mut store), function_type);
let function_type = FunctionType::new(vec![Type::I32], vec![]);
let function = Function::new(
&mut store,
&function_type,
|_values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&mut store).clone(), function_type);
assert_eq!(function.ty(&mut store), function_type);
let function_type = FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]);
let function = Function::new(
&mut store,
&function_type,
|_values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&mut store).clone(), function_type);
assert_eq!(function.ty(&mut store), function_type);
let function_type = FunctionType::new(vec![], vec![Type::I32]);
let function = Function::new(
&mut store,
&function_type,
|_values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&mut store).clone(), function_type);
assert_eq!(function.ty(&mut store), function_type);
let function_type = FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]);
let function = Function::new(
&mut store,
&function_type,
|_values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&mut store).clone(), function_type);
assert_eq!(function.ty(&mut store), function_type);

// Using array signature
let function_type = ([Type::V128], [Type::I32, Type::F32, Type::F64]);
Expand Down Expand Up @@ -356,39 +350,39 @@ fn function_new_dynamic_env() -> Result<(), String> {
&function_type,
|_env: FunctionEnvMut<MyEnv>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&mut store).clone(), function_type);
assert_eq!(function.ty(&mut store), function_type);
let function_type = FunctionType::new(vec![Type::I32], vec![]);
let function = Function::new_with_env(
&mut store,
&env,
&function_type,
|_env: FunctionEnvMut<MyEnv>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&mut store).clone(), function_type);
assert_eq!(function.ty(&mut store), function_type);
let function_type = FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]);
let function = Function::new_with_env(
&mut store,
&env,
&function_type,
|_env: FunctionEnvMut<MyEnv>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&mut store).clone(), function_type);
assert_eq!(function.ty(&mut store), function_type);
let function_type = FunctionType::new(vec![], vec![Type::I32]);
let function = Function::new_with_env(
&mut store,
&env,
&function_type,
|_env: FunctionEnvMut<MyEnv>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&mut store).clone(), function_type);
assert_eq!(function.ty(&mut store), function_type);
let function_type = FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]);
let function = Function::new_with_env(
&mut store,
&env,
&function_type,
|_env: FunctionEnvMut<MyEnv>, _values: &[Value]| unimplemented!(),
);
assert_eq!(function.ty(&mut store).clone(), function_type);
assert_eq!(function.ty(&mut store), function_type);

// Using array signature
let function_type = ([Type::V128], [Type::I32, Type::F32, Type::F64]);
Expand Down
10 changes: 5 additions & 5 deletions lib/api/tests/reference_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ pub mod reference_types {
let global: &Global = instance.exports.get_global("global")?;
{
let er = ExternRef::new(&mut store, 3usize);
global.set(&mut store, Value::ExternRef(Some(er.clone())))?;
global.set(&mut store, Value::ExternRef(Some(er)))?;
}
let get_from_global: TypedFunction<(), Option<ExternRef>> = instance
.exports
Expand Down Expand Up @@ -398,7 +398,7 @@ pub mod reference_types {

let er = ExternRef::new(&mut store, 3usize);

let result = pass_extern_ref.call(&mut store, Some(er.clone()));
let result = pass_extern_ref.call(&mut store, Some(er));
assert!(result.is_err());

Ok(())
Expand Down Expand Up @@ -442,7 +442,7 @@ pub mod reference_types {
let result = grow_table_with_ref.call(&mut store, Some(er1.clone()), 10_000)?;
assert_eq!(result, -1);

let result = grow_table_with_ref.call(&mut store, Some(er1.clone()), 8)?;
let result = grow_table_with_ref.call(&mut store, Some(er1), 8)?;
assert_eq!(result, 2);

for i in 2..10 {
Expand All @@ -454,15 +454,15 @@ pub mod reference_types {
}

{
fill_table_with_ref.call(&mut store, Some(er2.clone()), 0, 2)?;
fill_table_with_ref.call(&mut store, Some(er2), 0, 2)?;
}

{
table2.set(&mut store, 0, Value::ExternRef(Some(er3.clone())))?;
table2.set(&mut store, 1, Value::ExternRef(Some(er3.clone())))?;
table2.set(&mut store, 2, Value::ExternRef(Some(er3.clone())))?;
table2.set(&mut store, 3, Value::ExternRef(Some(er3.clone())))?;
table2.set(&mut store, 4, Value::ExternRef(Some(er3.clone())))?;
table2.set(&mut store, 4, Value::ExternRef(Some(er3)))?;
}

{
Expand Down
Loading

0 comments on commit e1e08f4

Please sign in to comment.