mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-06 20:58:28 +00:00
test(c-api) Start testing the instance module.
This commit is contained in:
@@ -110,3 +110,110 @@ pub unsafe extern "C" fn wasm_instance_exports(
|
||||
|
||||
mem::forget(extern_vec);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use inline_c::assert_c;
|
||||
|
||||
#[test]
|
||||
fn test_instance_new() {
|
||||
(assert_c! {
|
||||
#include "tests/wasmer_wasm.h"
|
||||
|
||||
// The `sum` host function implementation.
|
||||
wasm_trap_t* sum_callback(
|
||||
const wasm_val_vec_t* arguments,
|
||||
wasm_val_vec_t* results
|
||||
) {
|
||||
uint32_t sum = arguments->data[0].of.i32 + arguments->data[1].of.i32;
|
||||
wasm_val_t result = {
|
||||
.kind = WASM_I32,
|
||||
.of = result
|
||||
};
|
||||
results->data[0] = result;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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 \"math\" \"sum\" (func $sum (param i32 i32) (result i32)))\n"
|
||||
" (func (export \"add_one\") (param i32) (result i32)\n"
|
||||
" local.get 0\n"
|
||||
" i32.const 1\n"
|
||||
" call $sum))"
|
||||
);
|
||||
wasm_byte_vec_t* wasm = wat2wasm(&wat);
|
||||
|
||||
// Create the module.
|
||||
wasm_module_t* module = wasm_module_new(store, wasm);
|
||||
|
||||
assert(module);
|
||||
|
||||
// Prepare the imports.
|
||||
// Create the type for the `sum` host function.
|
||||
wasm_functype_t* sum_type = wasm_functype_new_2_1(
|
||||
wasm_valtype_new_i32(),
|
||||
wasm_valtype_new_i32(),
|
||||
wasm_valtype_new_i32()
|
||||
);
|
||||
|
||||
// Create the `sum` host function.
|
||||
wasm_func_t* sum_function = wasm_func_new(store, sum_type, sum_callback);
|
||||
|
||||
// Create the imports.
|
||||
wasm_extern_t* externs[] = { wasm_func_as_extern(sum_function) };
|
||||
wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
|
||||
|
||||
// Instantiate the module.
|
||||
wasm_trap_t* traps = NULL;
|
||||
wasm_instance_t* instance = wasm_instance_new(store, module, &imports, &traps);
|
||||
|
||||
assert(instance);
|
||||
|
||||
// Run the exported function.
|
||||
wasm_extern_vec_t exports;
|
||||
wasm_instance_exports(instance, &exports);
|
||||
|
||||
assert(exports.size == 1);
|
||||
|
||||
// Get the `add_one` exported function.
|
||||
const wasm_func_t* run_function = wasm_extern_as_func(exports.data[0]);
|
||||
assert(run_function);
|
||||
|
||||
// And call it as `add_one(1)`.
|
||||
wasm_val_t arguments[1] = { WASM_I32_VAL(1) };
|
||||
wasm_val_t results[1] = { WASM_INIT_VAL };
|
||||
|
||||
wasm_val_vec_t arguments_as_array = WASM_ARRAY_VEC(arguments);
|
||||
wasm_val_vec_t results_as_array = WASM_ARRAY_VEC(results);
|
||||
|
||||
wasm_trap_t* trap = wasm_func_call(run_function, &arguments_as_array, &results_as_array);
|
||||
|
||||
// Check the result!
|
||||
assert(trap == NULL);
|
||||
assert(results[0].of.i32 == 2);
|
||||
|
||||
// Free everything.
|
||||
wasm_func_delete(sum_function);
|
||||
wasm_instance_delete(instance);
|
||||
wasm_module_delete(module);
|
||||
wasm_byte_vec_delete(wasm);
|
||||
wasm_byte_vec_delete(&wat);
|
||||
wasm_store_delete(store);
|
||||
wasm_engine_delete(engine);
|
||||
|
||||
return 0;
|
||||
}
|
||||
})
|
||||
.success();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user