Browse Source

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.
pull/3960/head
Alex Crichton 3 years ago
committed by GitHub
parent
commit
13ec5ff64c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      crates/c-api/include/wasmtime/module.h
  2. 34
      crates/c-api/src/module.rs

16
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.
*

34
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<wasm_shared_module_t> {
Box::new(wasm_shared_module_t {
@ -144,6 +150,22 @@ pub extern "C" fn wasmtime_module_clone(module: &wasmtime_module_t) -> Box<wasmt
Box::new(module.clone())
}
#[no_mangle]
pub extern "C" fn wasmtime_module_exports(
module: &wasmtime_module_t,
out: &mut wasm_exporttype_vec_t,
) {
fill_exports(&module.module, out);
}
#[no_mangle]
pub extern "C" fn wasmtime_module_imports(
module: &wasmtime_module_t,
out: &mut wasm_importtype_vec_t,
) {
fill_imports(&module.module, out);
}
#[no_mangle]
pub unsafe extern "C" fn wasmtime_module_validate(
engine: &wasm_engine_t,

Loading…
Cancel
Save