mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-07 21:28:21 +00:00
test(c-api) Fix tests.
This commit is contained in:
@@ -131,12 +131,11 @@ mod tests {
|
|||||||
const wasm_val_vec_t* arguments,
|
const wasm_val_vec_t* arguments,
|
||||||
wasm_val_vec_t* results
|
wasm_val_vec_t* results
|
||||||
) {
|
) {
|
||||||
uint32_t sum = arguments->data[0].of.i32 + arguments->data[1].of.i32;
|
wasm_val_t sum = {
|
||||||
wasm_val_t result = {
|
|
||||||
.kind = WASM_I32,
|
.kind = WASM_I32,
|
||||||
.of = result
|
.of = { arguments->data[0].of.i32 + arguments->data[1].of.i32 },
|
||||||
};
|
};
|
||||||
results->data[0] = result;
|
results->data[0] = sum;
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -157,25 +156,21 @@ mod tests {
|
|||||||
" i32.const 1\n"
|
" i32.const 1\n"
|
||||||
" call $sum))"
|
" call $sum))"
|
||||||
);
|
);
|
||||||
wasm_byte_vec_t* wasm = wat2wasm(&wat);
|
wasm_byte_vec_t wasm;
|
||||||
|
wat2wasm(&wat, &wasm);
|
||||||
|
|
||||||
// Create the module.
|
// Create the module.
|
||||||
wasm_module_t* module = wasm_module_new(store, wasm);
|
wasm_module_t* module = wasm_module_new(store, &wasm);
|
||||||
|
|
||||||
assert(module);
|
assert(module);
|
||||||
|
|
||||||
// Prepare the imports.
|
// Prepare the imports.
|
||||||
// Create the type for the `sum` host function.
|
|
||||||
wasm_functype_t* sum_type = wasm_functype_new_2_1(
|
wasm_functype_t* sum_type = wasm_functype_new_2_1(
|
||||||
wasm_valtype_new_i32(),
|
wasm_valtype_new_i32(),
|
||||||
wasm_valtype_new_i32(),
|
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);
|
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_t* externs[] = { wasm_func_as_extern(sum_function) };
|
||||||
wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
|
wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
|
||||||
|
|
||||||
@@ -191,11 +186,10 @@ mod tests {
|
|||||||
|
|
||||||
assert(exports.size == 1);
|
assert(exports.size == 1);
|
||||||
|
|
||||||
// Get the `add_one` exported function.
|
|
||||||
const wasm_func_t* run_function = wasm_extern_as_func(exports.data[0]);
|
const wasm_func_t* run_function = wasm_extern_as_func(exports.data[0]);
|
||||||
|
|
||||||
assert(run_function);
|
assert(run_function);
|
||||||
|
|
||||||
// And call it as `add_one(1)`.
|
|
||||||
wasm_val_t arguments[1] = { WASM_I32_VAL(1) };
|
wasm_val_t arguments[1] = { WASM_I32_VAL(1) };
|
||||||
wasm_val_t results[1] = { WASM_INIT_VAL };
|
wasm_val_t results[1] = { WASM_INIT_VAL };
|
||||||
|
|
||||||
@@ -204,15 +198,15 @@ mod tests {
|
|||||||
|
|
||||||
wasm_trap_t* trap = wasm_func_call(run_function, &arguments_as_array, &results_as_array);
|
wasm_trap_t* trap = wasm_func_call(run_function, &arguments_as_array, &results_as_array);
|
||||||
|
|
||||||
// Check the result!
|
|
||||||
assert(trap == NULL);
|
assert(trap == NULL);
|
||||||
assert(results[0].of.i32 == 2);
|
assert(results[0].of.i32 == 2);
|
||||||
|
|
||||||
// Free everything.
|
// Free everything.
|
||||||
wasm_func_delete(sum_function);
|
|
||||||
wasm_instance_delete(instance);
|
wasm_instance_delete(instance);
|
||||||
|
wasm_func_delete(sum_function);
|
||||||
|
wasm_functype_delete(sum_type);
|
||||||
wasm_module_delete(module);
|
wasm_module_delete(module);
|
||||||
wasm_byte_vec_delete(wasm);
|
wasm_byte_vec_delete(&wasm);
|
||||||
wasm_byte_vec_delete(&wat);
|
wasm_byte_vec_delete(&wat);
|
||||||
wasm_store_delete(store);
|
wasm_store_delete(store);
|
||||||
wasm_engine_delete(engine);
|
wasm_engine_delete(engine);
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ pub unsafe extern "C" fn wasm_module_delete(_module: Option<Box<wasm_module_t>>)
|
|||||||
/// wat2wasm(&wat, &wasm);
|
/// wat2wasm(&wat, &wasm);
|
||||||
///
|
///
|
||||||
/// // Validate that the WebAssembly module is correct.
|
/// // Validate that the WebAssembly module is correct.
|
||||||
/// assert(wasm_module_validate(store, wasm));
|
/// assert(wasm_module_validate(store, &wasm));
|
||||||
///
|
///
|
||||||
/// // Free everything.
|
/// // Free everything.
|
||||||
/// wasm_byte_vec_delete(&wasm);
|
/// wasm_byte_vec_delete(&wasm);
|
||||||
@@ -188,7 +188,7 @@ pub unsafe extern "C" fn wasm_module_validate(
|
|||||||
/// wat2wasm(&wat, &wasm);
|
/// wat2wasm(&wat, &wasm);
|
||||||
///
|
///
|
||||||
/// // Create the module.
|
/// // Create the module.
|
||||||
/// wasm_module_t* module = wasm_module_new(store, wasm);
|
/// wasm_module_t* module = wasm_module_new(store, &wasm);
|
||||||
/// assert(module);
|
/// assert(module);
|
||||||
///
|
///
|
||||||
/// // Extract the types exported by this module.
|
/// // Extract the types exported by this module.
|
||||||
@@ -312,7 +312,7 @@ pub unsafe extern "C" fn wasm_module_exports(
|
|||||||
/// wat2wasm(&wat, &wasm);
|
/// wat2wasm(&wat, &wasm);
|
||||||
///
|
///
|
||||||
/// // Create the module.
|
/// // Create the module.
|
||||||
/// wasm_module_t* module = wasm_module_new(store, wasm);
|
/// wasm_module_t* module = wasm_module_new(store, &wasm);
|
||||||
/// assert(module);
|
/// assert(module);
|
||||||
///
|
///
|
||||||
/// // Extract the types imported by the module.
|
/// // Extract the types imported by the module.
|
||||||
@@ -464,7 +464,7 @@ pub unsafe extern "C" fn wasm_module_imports(
|
|||||||
/// wat2wasm(&wat, &wasm);
|
/// wat2wasm(&wat, &wasm);
|
||||||
///
|
///
|
||||||
/// // Create the module.
|
/// // Create the module.
|
||||||
/// wasm_module_t* module = wasm_module_new(store, wasm);
|
/// wasm_module_t* module = wasm_module_new(store, &wasm);
|
||||||
/// assert(module);
|
/// assert(module);
|
||||||
///
|
///
|
||||||
/// // Serialize the module into bytes.
|
/// // Serialize the module into bytes.
|
||||||
|
|||||||
Reference in New Issue
Block a user