diff --git a/crates/c-api/include/wasmtime.h b/crates/c-api/include/wasmtime.h index 8a8c81a87077..63359a8eab17 100644 --- a/crates/c-api/include/wasmtime.h +++ b/crates/c-api/include/wasmtime.h @@ -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 diff --git a/crates/c-api/src/func.rs b/crates/c-api/src/func.rs index 5f62ec30b270..244daaeb4353 100644 --- a/crates/c-api/src/func.rs +++ b/crates/c-api/src/func.rs @@ -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> { diff --git a/crates/c-api/src/linker.rs b/crates/c-api/src/linker.rs index 0152d2d49b21..613201370d86 100644 --- a/crates/c-api/src/linker.rs +++ b/crates/c-api/src/linker.rs @@ -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, @@ -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, @@ -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> { - 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(), @@ -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> { + 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 })) + }) +}