Function wasmer_c_api::wasm_c_api::module::wasm_module_imports
source · #[no_mangle]
pub unsafe extern "C" fn wasm_module_imports(
module: &wasm_module_t,
out: &mut wasm_importtype_vec_t,
)
Expand description
Returns an array of the imported types in the module.
The order of the imports is guaranteed to be the same as in the WebAssembly bytecode.
§Example
int main() {
// Create the engine and the store.
wasm_engine_t* engine = wasm_engine_new();
wasm_store_t* store = wasm_store_new(engine);
// Create a WebAssembly module from a WAT definition.
wasm_byte_vec_t wat;
wasmer_byte_vec_new_from_string(
&wat,
"(module\n"
" (import \"ns\" \"function\" (func))\n"
" (import \"ns\" \"global\" (global f32))\n"
" (import \"ns\" \"table\" (table 1 2 funcref))\n"
" (import \"ns\" \"memory\" (memory 3 4)))"
);
wasm_byte_vec_t wasm;
wat2wasm(&wat, &wasm);
// Create the module.
wasm_module_t* module = wasm_module_new(store, &wasm);
assert(module);
// Extract the types imported by the module.
wasm_importtype_vec_t import_types;
wasm_module_imports(module, &import_types);
// We have 4 of them.
assert(import_types.size == 4);
// The first one is a function. Use
// `wasm_externtype_as_functype_const` to continue to inspect the
// type.
{
const wasm_importtype_t* import_type = import_types.data[0];
const wasm_name_t* import_module = wasm_importtype_module(import_type);
wasmer_assert_name(import_module, "ns");
const wasm_name_t* import_name = wasm_importtype_name(import_type);
wasmer_assert_name(import_name, "function");
const wasm_externtype_t* extern_type = wasm_importtype_type(import_type);
assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_FUNC);
}
// The second one is a global. Use
// `wasm_externtype_as_globaltype_const` to continue to inspect
// the type.
{
const wasm_importtype_t* import_type = import_types.data[1];
const wasm_name_t* import_module = wasm_importtype_module(import_type);
wasmer_assert_name(import_module, "ns");
const wasm_name_t* import_name = wasm_importtype_name(import_type);
wasmer_assert_name(import_name, "global");
const wasm_externtype_t* extern_type = wasm_importtype_type(import_type);
assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_GLOBAL);
}
// The third one is a table. Use
// `wasm_externtype_as_tabletype_const` to continue to inspect
// the type.
{
const wasm_importtype_t* import_type = import_types.data[2];
const wasm_name_t* import_module = wasm_importtype_module(import_type);
wasmer_assert_name(import_module, "ns");
const wasm_name_t* import_name = wasm_importtype_name(import_type);
wasmer_assert_name(import_name, "table");
const wasm_externtype_t* extern_type = wasm_importtype_type(import_type);
assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_TABLE);
}
// The fourth one is a memory. Use
// `wasm_externtype_as_memorytype_const` to continue to inspect
// the type.
{
const wasm_importtype_t* import_type = import_types.data[3];
const wasm_name_t* import_module = wasm_importtype_module(import_type);
wasmer_assert_name(import_module, "ns");
const wasm_name_t* import_name = wasm_importtype_name(import_type);
wasmer_assert_name(import_name, "memory");
const wasm_externtype_t* extern_type = wasm_importtype_type(import_type);
assert(wasm_externtype_kind(extern_type) == WASM_EXTERN_MEMORY);
const wasm_memorytype_t* memory_type = wasm_externtype_as_memorytype_const(extern_type);
const wasm_limits_t* memory_limits = wasm_memorytype_limits(memory_type);
assert(memory_limits->min == 3);
assert(memory_limits->max == 4);
}
// Free everything.
wasm_importtype_vec_delete(&import_types);
wasm_module_delete(module);
wasm_byte_vec_delete(&wasm);
wasm_byte_vec_delete(&wat);
wasm_store_delete(store);
wasm_engine_delete(engine);
return 0;
}