mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-13 00:51:20 +00:00
Add test to import function and call it
This commit is contained in:
3
lib/runtime-c-api/tests/.gitignore
vendored
3
lib/runtime-c-api/tests/.gitignore
vendored
@ -9,4 +9,5 @@ install_manifest.txt
|
||||
compile_commands.json
|
||||
CTestTestfile.cmake
|
||||
_deps
|
||||
test-instantiate
|
||||
test-instantiate
|
||||
test-import-function
|
@ -2,10 +2,15 @@ cmake_minimum_required (VERSION 2.6)
|
||||
project (WasmerCApiTests)
|
||||
|
||||
add_executable(test-instantiate test-instantiate.c)
|
||||
add_executable(test-import-function test-import-function.c)
|
||||
|
||||
target_link_libraries(test-instantiate
|
||||
general "${CMAKE_SOURCE_DIR}/../../../target/debug/libwasmer_runtime_c_api.dylib")
|
||||
|
||||
target_link_libraries(test-import-function
|
||||
general "${CMAKE_SOURCE_DIR}/../../../target/debug/libwasmer_runtime_c_api.dylib")
|
||||
|
||||
enable_testing()
|
||||
add_test(test-instantiate test-instantiate)
|
||||
add_test(test-import-function test-import-function)
|
||||
|
||||
|
46
lib/runtime-c-api/tests/test-import-function.c
Normal file
46
lib/runtime-c-api/tests/test-import-function.c
Normal file
@ -0,0 +1,46 @@
|
||||
#include <stdio.h>
|
||||
#include "../wasmer.h"
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static print_str_called = false;
|
||||
|
||||
void print_str(int32_t ptr, int32_t len, wasmer_instance_context_t *ctx) {
|
||||
printf("In print_str\n");
|
||||
print_str_called = true;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
wasmer_import_object_t *import_object = wasmer_import_object_new();
|
||||
wasmer_imports_set_import_func(import_object, "env", "print_str", print_str);
|
||||
|
||||
// Read the wasm file bytes
|
||||
FILE *file = fopen("wasm_sample_app.wasm", "r");
|
||||
fseek(file, 0, SEEK_END);
|
||||
long len = ftell(file);
|
||||
uint8_t *bytes = malloc(len);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
fread(bytes, 1, len, file);
|
||||
fclose(file);
|
||||
|
||||
wasmer_instance_t *instance = NULL;
|
||||
wasmer_compile_result_t compile_result = wasmer_instantiate(&instance, bytes, len, import_object);
|
||||
printf("Compile result: %d\n", compile_result);
|
||||
assert(compile_result == WASMER_COMPILE_OK);
|
||||
|
||||
wasmer_value_t params[] = {};
|
||||
wasmer_value_t results[] = {};
|
||||
wasmer_call_result_t call_result = wasmer_instance_call(instance, "hello_wasm", params, 0, results, 0);
|
||||
printf("Call result: %d\n", call_result);
|
||||
assert(call_result == WASMER_CALL_OK);
|
||||
|
||||
assert(print_str_called);
|
||||
|
||||
// TODO review import object ownership, instantiate moves it
|
||||
printf("Destroy instance\n");
|
||||
//wasmer_instance_destroy(instance); // TODO error here
|
||||
printf("Destroy import object\n");
|
||||
//wasmer_import_object_destroy(import_object); // TODO error here
|
||||
return 0;
|
||||
}
|
BIN
lib/runtime-c-api/tests/wasm_sample_app.wasm
Executable file
BIN
lib/runtime-c-api/tests/wasm_sample_app.wasm
Executable file
Binary file not shown.
Reference in New Issue
Block a user