Skip to content

Commit

Permalink
Pushed a bit more changes into C-API
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Jul 14, 2022
1 parent 24210ab commit 4eff8bd
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 22 deletions.
14 changes: 7 additions & 7 deletions lib/c-api/src/wasm_c_api/externals/function.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::super::function_env::wasmer_funcenv_ref_mut_t;
use super::super::store::wasm_store_t;
use super::super::trap::wasm_trap_t;
use super::super::types::{wasm_functype_t, wasm_valkind_enum};
Expand Down Expand Up @@ -26,7 +25,7 @@ impl wasm_func_t {

#[allow(non_camel_case_types)]
pub type wasm_func_callback_t = unsafe extern "C" fn(
context: &mut wasmer_funcenv_ref_mut_t,
context: &mut (),
args: &wasm_val_vec_t,
results: &mut wasm_val_vec_t,
) -> Option<Box<wasm_trap_t>>;
Expand All @@ -39,7 +38,8 @@ pub unsafe extern "C" fn wasm_func_new(
) -> Option<Box<wasm_func_t>> {
let function_type = function_type?;
let callback = callback?;
let mut store = store?.store.store_mut();
let store = store?;
let mut store_mut = store.store.store_mut();

let func_sig = &function_type.inner().function_type;
let num_rets = func_sig.results().len();
Expand All @@ -61,7 +61,7 @@ pub unsafe extern "C" fn wasm_func_new(
]
.into();

let trap = callback(ctx.data_mut().data, &processed_args, &mut results);
let trap = callback(ctx.data_mut(), &processed_args, &mut results);

if let Some(trap) = trap {
return Err(trap.inner);
Expand All @@ -77,13 +77,13 @@ pub unsafe extern "C" fn wasm_func_new(
Ok(processed_results)
};
let function = Function::new(
&mut store,
&FunctionEnv::new(&mut store, ()),
&mut store_mut,
&FunctionEnv::new(&mut store_mut, ()),
func_sig,
inner_callback,
);
Some(Box::new(wasm_func_t {
extern_: wasm_extern_t::new(store.clone(), function.into()),
extern_: wasm_extern_t::new(store.store.clone(), function.into()),
}))
}

Expand Down
11 changes: 6 additions & 5 deletions lib/c-api/src/wasm_c_api/externals/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ pub unsafe extern "C" fn wasm_global_new(
val: Option<&wasm_val_t>,
) -> Option<Box<wasm_global_t>> {
let global_type = global_type?;
let mut store = store?.store.store_mut();
let store = store?;
let mut store_mut = store.store.store_mut();
let val = val?;

let global_type = &global_type.inner().global_type;
let wasm_val = val.try_into().ok()?;
let global = if global_type.mutability.is_mutable() {
Global::new_mut(&mut store, wasm_val)
Global::new_mut(&mut store_mut, wasm_val)
} else {
Global::new(&mut store, wasm_val)
Global::new(&mut store_mut, wasm_val)
};
Some(Box::new(wasm_global_t {
extern_: wasm_extern_t::new(store, global.into()),
extern_: wasm_extern_t::new(store.store.clone(), global.into()),
}))
}

Expand Down Expand Up @@ -73,7 +74,7 @@ pub unsafe extern "C" fn wasm_global_set(global: &mut wasm_global_t, val: &wasm_
c_try!(global
.extern_
.global()
.set(&mut global.extern_.store.store_mut(), value));
.set(&mut global.extern_.store.store_mut(), value); otherwise ());
}

#[no_mangle]
Expand Down
3 changes: 2 additions & 1 deletion lib/c-api/src/wasm_c_api/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ pub unsafe extern "C" fn wasm_memory_new(
memory_type: Option<&wasm_memorytype_t>,
) -> Option<Box<wasm_memory_t>> {
let memory_type = memory_type?;
let mut store_mut = store?.store.store_mut();
let store = store?;
let mut store_mut = store.store.store_mut();
let memory_type = memory_type.inner().memory_type;
let memory = c_try!(Memory::new(&mut store_mut, memory_type));
Some(Box::new(wasm_memory_t {
Expand Down
2 changes: 1 addition & 1 deletion lib/c-api/src/wasm_c_api/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub unsafe extern "C" fn wasm_instance_exports(
let mut extern_vec: Vec<Option<Box<wasm_extern_t>>> = instance
.exports
.iter()
.map(|(_name, r#extern)| Some(Box::new(r#extern.clone().into())))
.map(|(_name, r#extern)| Some(Box::new(wasm_extern_t::new(original_instance.store.clone(), #extern.clone().into()))))
.collect();
out.set_buffer(extern_vec);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/c-api/src/wasm_c_api/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub unsafe extern "C" fn wasm_module_validate(

Module::validate(&mut store, bytes.as_slice())
.map(|_| true)
.unwrap_or(false);
.unwrap_or(false)
}

/// Returns an array of the exported types in the module.
Expand Down Expand Up @@ -474,7 +474,7 @@ pub unsafe extern "C" fn wasm_module_deserialize(
/// See [`wasm_module_deserialize`].
#[no_mangle]
pub unsafe extern "C" fn wasm_module_serialize(module: &wasm_module_t, out: &mut wasm_byte_vec_t) {
let byte_vec = c_try!(module.inner.serialize());
let byte_vec = c_try!(module.inner.serialize(); otherwise ());
out.set_buffer(byte_vec);
}

Expand Down
9 changes: 5 additions & 4 deletions lib/c-api/src/wasm_c_api/unstable/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,27 +161,28 @@ fn wasi_get_unordered_imports_inner(
wasi_env: Option<&wasi_env_t>,
imports: &mut wasmer_named_extern_vec_t,
) -> Option<()> {
let mut store = store?.store.store_mut();
let store = store?;
let mut store_mut = store.store.store_mut();
let module = module?;
let _wasi_env = wasi_env?;

let version = c_try!(get_wasi_version(&module.inner, false)
.ok_or("could not detect a WASI version on the given module"));

let import_object = generate_import_object_from_env(&mut store, version);
let import_object = generate_import_object_from_env(&mut store_mut, version);

imports.set_buffer(
import_object
.into_iter()
.map(|((module, name), extern_)| {
let module = module.into();
let name = name.into();
let extern_inner = Extern::from_vm_extern(&mut store, extern_.to_vm_extern());
let extern_inner = Extern::from_vm_extern(&mut store_mut, extern_.to_vm_extern());

Some(Box::new(wasmer_named_extern_t {
module,
name,
r#extern: Box::new(extern_inner.into()),
r#extern: Box::new(wasm_extern_t::new(store.store.clone(), extern_inner.clone())),
}))
})
.collect::<Vec<_>>(),
Expand Down
2 changes: 1 addition & 1 deletion lib/c-api/src/wasm_c_api/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub unsafe extern "C" fn wasm_val_copy(
wasm_valkind_enum::WASM_ANYREF => wasm_val_inner { wref: val.of.wref },
wasm_valkind_enum::WASM_FUNCREF => wasm_val_inner { wref: val.of.wref },
}
}));
}); otherwise ());
}

#[no_mangle]
Expand Down
2 changes: 1 addition & 1 deletion lib/c-api/src/wasm_c_api/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ fn wasi_get_imports_inner(
module: Option<&wasm_module_t>,
imports: &mut wasm_extern_vec_t,
) -> Option<()> {
let store = store?;
let mut store = store?;
let mut store_mut = store.store.store_mut();
let module = module?;

Expand Down

0 comments on commit 4eff8bd

Please sign in to comment.