Skip to content

Commit

Permalink
C API: expose wasmtime_linker_get_one_by_name() (#1897)
Browse files Browse the repository at this point in the history
* C API: expose wasmtime_linker_get_one_by_name()

* C API: remove unnecessary 'unsafe' qualifiers

* C API: avoid unnecessary mutable borrows of the Linker
  • Loading branch information
thibaultcha authored Jun 23, 2020
1 parent 9751b96 commit 8082aea
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
7 changes: 7 additions & 0 deletions crates/c-api/include/wasmtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ WASM_API_EXTERN own wasmtime_error_t* wasmtime_linker_get_default(
own wasm_func_t **func
);

WASM_API_EXTERN own wasmtime_error_t* wasmtime_linker_get_one_by_name(
const wasmtime_linker_t *linker,
const wasm_name_t *module,
const wasm_name_t *name,
own wasm_extern_t **item
);

///////////////////////////////////////////////////////////////////////////////
//
// wasmtime_caller_t extension, binding the `Caller` type in the Rust API
Expand Down
2 changes: 1 addition & 1 deletion crates/c-api/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ pub extern "C" fn wasm_func_as_extern(f: &mut wasm_func_t) -> &mut wasm_extern_t
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_caller_export_get(
pub extern "C" fn wasmtime_caller_export_get(
caller: &wasmtime_caller_t,
name: &wasm_name_t,
) -> Option<Box<wasm_extern_t>> {
Expand Down
38 changes: 33 additions & 5 deletions crates/c-api/src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub extern "C" fn wasmtime_linker_define_instance(
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_instantiate(
pub extern "C" fn wasmtime_linker_instantiate(
linker: &wasmtime_linker_t,
module: &wasm_module_t,
instance_ptr: &mut *mut wasm_instance_t,
Expand All @@ -92,7 +92,7 @@ pub unsafe extern "C" fn wasmtime_linker_instantiate(
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_module(
pub extern "C" fn wasmtime_linker_module(
linker: &mut wasmtime_linker_t,
name: &wasm_name_t,
module: &wasm_module_t,
Expand All @@ -106,12 +106,12 @@ pub unsafe extern "C" fn wasmtime_linker_module(
}

#[no_mangle]
pub unsafe extern "C" fn wasmtime_linker_get_default(
linker: &mut wasmtime_linker_t,
pub extern "C" fn wasmtime_linker_get_default(
linker: &wasmtime_linker_t,
name: &wasm_name_t,
func: &mut *mut wasm_func_t,
) -> Option<Box<wasmtime_error_t>> {
let linker = &mut linker.linker;
let linker = &linker.linker;
let name = match str::from_utf8(name.as_slice()) {
Ok(s) => s,
Err(_) => return bad_utf8(),
Expand All @@ -120,3 +120,31 @@ pub unsafe extern "C" fn wasmtime_linker_get_default(
*func = Box::into_raw(Box::new(HostRef::new(linker.store(), f).into()))
})
}

#[no_mangle]
pub extern "C" fn wasmtime_linker_get_one_by_name(
linker: &wasmtime_linker_t,
module: &wasm_name_t,
name: &wasm_name_t,
item_ptr: &mut *mut wasm_extern_t,
) -> Option<Box<wasmtime_error_t>> {
let linker = &linker.linker;
let module = match str::from_utf8(module.as_slice()) {
Ok(s) => s,
Err(_) => return bad_utf8(),
};
let name = match str::from_utf8(name.as_slice()) {
Ok(s) => s,
Err(_) => return bad_utf8(),
};
handle_result(linker.get_one_by_name(module, name), |item| {
let store = linker.store();
let which = match item {
Extern::Func(f) => ExternHost::Func(HostRef::new(&store, f)),
Extern::Global(g) => ExternHost::Global(HostRef::new(&store, g)),
Extern::Memory(m) => ExternHost::Memory(HostRef::new(&store, m)),
Extern::Table(t) => ExternHost::Table(HostRef::new(&store, t)),
};
*item_ptr = Box::into_raw(Box::new(wasm_extern_t { which }))
})
}

0 comments on commit 8082aea

Please sign in to comment.