mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-12 16:41:21 +00:00
test(runtime-c-api) New test suite for wasmer_export_to_memory
.
This commit is contained in:
1
lib/runtime-c-api/tests/.gitignore
vendored
1
lib/runtime-c-api/tests/.gitignore
vendored
@ -10,6 +10,7 @@ compile_commands.json
|
||||
CTestTestfile.cmake
|
||||
_deps
|
||||
rust-build
|
||||
test-exported-memory
|
||||
test-exports
|
||||
test-globals
|
||||
test-import-function
|
||||
|
@ -1,6 +1,7 @@
|
||||
cmake_minimum_required (VERSION 2.6)
|
||||
project (WasmerRuntimeCApiTests)
|
||||
|
||||
add_executable(test-exported-memory test-exported-memory.c)
|
||||
add_executable(test-exports test-exports.c)
|
||||
add_executable(test-globals test-globals.c)
|
||||
add_executable(test-import-function test-import-function.c)
|
||||
@ -35,6 +36,10 @@ set(
|
||||
"/WX" >
|
||||
)
|
||||
|
||||
target_link_libraries(test-exported-memory general ${WASMER_LIB})
|
||||
target_compile_options(test-exported-memory PRIVATE ${COMPILER_OPTIONS})
|
||||
add_test(test-exported-memory test-exported-memory)
|
||||
|
||||
target_link_libraries(test-exports general ${WASMER_LIB})
|
||||
target_compile_options(test-exports PRIVATE ${COMPILER_OPTIONS})
|
||||
add_test(test-exports test-exports)
|
||||
|
4
lib/runtime-c-api/tests/assets/return_hello.rs
Normal file
4
lib/runtime-c-api/tests/assets/return_hello.rs
Normal file
@ -0,0 +1,4 @@
|
||||
#[no_mangle]
|
||||
pub extern "C" fn return_hello() -> *const u8 {
|
||||
b"Hello, World!\0"[..].as_ptr()
|
||||
}
|
BIN
lib/runtime-c-api/tests/assets/return_hello.wasm
Normal file
BIN
lib/runtime-c-api/tests/assets/return_hello.wasm
Normal file
Binary file not shown.
73
lib/runtime-c-api/tests/test-exported-memory.c
Normal file
73
lib/runtime-c-api/tests/test-exported-memory.c
Normal file
@ -0,0 +1,73 @@
|
||||
#include <stdio.h>
|
||||
#include "../wasmer.h"
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
// Read the wasm file bytes
|
||||
FILE *file = fopen("assets/return_hello.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);
|
||||
|
||||
// Instantiate the module.
|
||||
wasmer_import_t imports[] = {};
|
||||
wasmer_instance_t *instance = NULL;
|
||||
wasmer_result_t compile_result = wasmer_instantiate(&instance, bytes, len, imports, 0);
|
||||
printf("Compile result: %d\n", compile_result);
|
||||
assert(compile_result == WASMER_OK);
|
||||
|
||||
// Call the `return_hello` function.
|
||||
wasmer_value_t params[] = {};
|
||||
wasmer_value_t result;
|
||||
wasmer_value_t results[] = {result};
|
||||
|
||||
wasmer_result_t call_result = wasmer_instance_call(instance, "return_hello", params, 0, results, 1);
|
||||
printf("Call result: %d\n", call_result);
|
||||
printf("Result: %d\n", results[0].value.I32);
|
||||
assert(call_result == WASMER_OK);
|
||||
assert(results[0].value.I32 == 1048576);
|
||||
|
||||
// Get all exports.
|
||||
wasmer_exports_t *exports = NULL;
|
||||
wasmer_instance_exports(instance, &exports);
|
||||
|
||||
int export_length = wasmer_exports_len(exports);
|
||||
printf("exports_length: %d\n", export_length);
|
||||
assert(export_length == 5);
|
||||
|
||||
// Get the `memory` export.
|
||||
wasmer_export_t *export = wasmer_exports_get(exports, 1);
|
||||
wasmer_import_export_kind kind = wasmer_export_kind(export);
|
||||
assert(kind == WASM_MEMORY);
|
||||
|
||||
wasmer_byte_array export_name = wasmer_export_name(export);
|
||||
printf("export_name: `%.*s`\n", export_name.bytes_len, export_name.bytes);
|
||||
|
||||
// Cast the export into a memory.
|
||||
wasmer_memory_t *memory;
|
||||
wasmer_result_t export_to_memory_result = wasmer_export_to_memory(export, &memory);
|
||||
printf("Export to memory result: %d\n", export_to_memory_result);
|
||||
printf("Memory pointer: %p\n", memory);
|
||||
assert(export_to_memory_result == WASMER_OK);
|
||||
|
||||
uint32_t memory_length = wasmer_memory_length(memory);
|
||||
assert(memory_length == 17);
|
||||
|
||||
// Read the data from the memory.
|
||||
uint8_t *memory_data = wasmer_memory_data(memory);
|
||||
uint8_t *returned_string = memory_data + results[0].value.I32;
|
||||
|
||||
printf("Returned string from Wasm: %s\n", returned_string);
|
||||
assert(strcmp("Hello, World!", (const char *) returned_string) == 0);
|
||||
|
||||
printf("Destroy instance\n");
|
||||
wasmer_instance_destroy(instance);
|
||||
|
||||
return 0;
|
||||
}
|
@ -17,14 +17,14 @@ int main()
|
||||
wasmer_import_t imports[] = {};
|
||||
wasmer_instance_t *instance = NULL;
|
||||
wasmer_result_t compile_result = wasmer_instantiate(&instance, bytes, len, imports, 0);
|
||||
printf("Compile result: %d\n", compile_result);
|
||||
printf("Compile result: %d\n", compile_result);
|
||||
assert(compile_result == WASMER_OK);
|
||||
|
||||
wasmer_exports_t *exports = NULL;
|
||||
wasmer_instance_exports(instance, &exports);
|
||||
|
||||
int exports_len = wasmer_exports_len(exports);
|
||||
printf("exports_len: %d\n", exports_len);
|
||||
printf("exports_len: %d\n", exports_len);
|
||||
assert(exports_len == 1);
|
||||
|
||||
wasmer_export_t *export = wasmer_exports_get(exports, 0);
|
||||
|
Reference in New Issue
Block a user