Skip to content

Commit

Permalink
Use immutable env in the Wasm C API
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Nov 20, 2020
1 parent a95c445 commit 52ef800
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions lib/c-api/src/wasm_c_api/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,42 +100,41 @@ pub unsafe extern "C" fn wasm_func_new_with_env(

let func_sig = &function_type.inner().function_type;
let num_rets = func_sig.results().len();
let inner_callback =
move |env: &mut *mut c_void, args: &[Val]| -> Result<Vec<Val>, RuntimeError> {
let processed_args: wasm_val_vec_t = args
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<wasm_val_t>, _>>()
.expect("Argument conversion failed")
.into();

let mut results: wasm_val_vec_t = vec![
wasm_val_t {
kind: wasm_valkind_enum::WASM_I64 as _,
of: wasm_val_inner { int64_t: 0 },
};
num_rets
]
let inner_callback = move |env: &*mut c_void, args: &[Val]| -> Result<Vec<Val>, RuntimeError> {
let processed_args: wasm_val_vec_t = args
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<wasm_val_t>, _>>()
.expect("Argument conversion failed")
.into();

let trap = callback(*env, &processed_args, &mut results);
let mut results: wasm_val_vec_t = vec![
wasm_val_t {
kind: wasm_valkind_enum::WASM_I64 as _,
of: wasm_val_inner { int64_t: 0 },
};
num_rets
]
.into();

if !trap.is_null() {
let trap: Box<wasm_trap_t> = Box::from_raw(trap);
let trap = callback(*env, &processed_args, &mut results);

return Err(trap.inner);
}
if !trap.is_null() {
let trap: Box<wasm_trap_t> = Box::from_raw(trap);

let processed_results = results
.into_slice()
.expect("Failed to convert `results` into a slice")
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<Val>, _>>()
.expect("Result conversion failed");
return Err(trap.inner);
}

Ok(processed_results)
};
let processed_results = results
.into_slice()
.expect("Failed to convert `results` into a slice")
.into_iter()
.map(TryInto::try_into)
.collect::<Result<Vec<Val>, _>>()
.expect("Result conversion failed");

Ok(processed_results)
};

let function = Function::new_with_env(&store.inner, &func_sig, env, inner_callback);

Expand Down

0 comments on commit 52ef800

Please sign in to comment.