From 13ec5ff64c27e10e0d710edba9ce75cd7b2c295d Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 24 Mar 2022 13:32:56 -0500 Subject: [PATCH] c-api: Add a way to get type of `wasmtime_module_t` (#3959) My previous PR at #3958 accidentally removed the only way to get type information from a `wasmtime_module_t`, so this commit re-adds methods back in to continue to be able to get import/export information from a compiled module. --- crates/c-api/include/wasmtime/module.h | 16 ++++++++++++ crates/c-api/src/module.rs | 34 +++++++++++++++++++++----- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/crates/c-api/include/wasmtime/module.h b/crates/c-api/include/wasmtime/module.h index 39b89fa779..deb6bcec1e 100644 --- a/crates/c-api/include/wasmtime/module.h +++ b/crates/c-api/include/wasmtime/module.h @@ -59,6 +59,22 @@ WASM_API_EXTERN void wasmtime_module_delete(wasmtime_module_t *m); */ WASM_API_EXTERN wasmtime_module_t *wasmtime_module_clone(wasmtime_module_t *m); +/** + * \brief Same as #wasm_module_imports, but for #wasmtime_module_t. + */ +WASM_API_EXTERN void wasmtime_module_imports( + const wasmtime_module_t *module, + wasm_importtype_vec_t *out +); + +/** + * \brief Same as #wasm_module_exports, but for #wasmtime_module_t. + */ +WASM_API_EXTERN void wasmtime_module_exports( + const wasmtime_module_t *module, + wasm_exporttype_vec_t *out +); + /** * \brief Validate a WebAssembly binary. * diff --git a/crates/c-api/src/module.rs b/crates/c-api/src/module.rs index 988440f143..1efa1abbd5 100644 --- a/crates/c-api/src/module.rs +++ b/crates/c-api/src/module.rs @@ -47,10 +47,8 @@ pub unsafe extern "C" fn wasm_module_validate( Module::validate(store.store.context().engine(), binary.as_slice()).is_ok() } -#[no_mangle] -pub extern "C" fn wasm_module_exports(module: &wasm_module_t, out: &mut wasm_exporttype_vec_t) { +fn fill_exports(module: &Module, out: &mut wasm_exporttype_vec_t) { let exports = module - .module .exports() .map(|e| { Some(Box::new(wasm_exporttype_t::new( @@ -62,10 +60,8 @@ pub extern "C" fn wasm_module_exports(module: &wasm_module_t, out: &mut wasm_exp out.set_buffer(exports); } -#[no_mangle] -pub extern "C" fn wasm_module_imports(module: &wasm_module_t, out: &mut wasm_importtype_vec_t) { +fn fill_imports(module: &Module, out: &mut wasm_importtype_vec_t) { let imports = module - .module .imports() .map(|i| { Some(Box::new(wasm_importtype_t::new( @@ -78,6 +74,16 @@ pub extern "C" fn wasm_module_imports(module: &wasm_module_t, out: &mut wasm_imp out.set_buffer(imports); } +#[no_mangle] +pub extern "C" fn wasm_module_exports(module: &wasm_module_t, out: &mut wasm_exporttype_vec_t) { + fill_exports(&module.module, out); +} + +#[no_mangle] +pub extern "C" fn wasm_module_imports(module: &wasm_module_t, out: &mut wasm_importtype_vec_t) { + fill_imports(&module.module, out); +} + #[no_mangle] pub extern "C" fn wasm_module_share(module: &wasm_module_t) -> Box { Box::new(wasm_shared_module_t { @@ -144,6 +150,22 @@ pub extern "C" fn wasmtime_module_clone(module: &wasmtime_module_t) -> Box