Add get exports from instance

This commit is contained in:
Brandon Fish
2019-02-13 20:02:11 -06:00
parent 7f5ca34138
commit 93979aeae2
7 changed files with 261 additions and 5 deletions

View File

@ -10,6 +10,7 @@ compile_commands.json
CTestTestfile.cmake
_deps
test-globals
test-exports
test-instantiate
test-import-function
test-memory

View File

@ -1,6 +1,7 @@
cmake_minimum_required (VERSION 2.6)
project (WasmerCApiTests)
add_executable(test-exports test-exports.c)
add_executable(test-globals test-globals.c)
add_executable(test-instantiate test-instantiate.c)
add_executable(test-import-function test-import-function.c)
@ -17,6 +18,8 @@ if(NOT WASMER_LIB)
message(FATAL_ERROR "wasmer library not found")
endif()
target_link_libraries(test-exports
general ${WASMER_LIB})
target_link_libraries(test-globals
general ${WASMER_LIB})
target_link_libraries(test-instantiate
@ -31,6 +34,7 @@ target_link_libraries(test-tables
general ${WASMER_LIB})
enable_testing()
add_test(test-exports test-exports)
add_test(test-globals test-globals)
add_test(test-instantiate test-instantiate)
add_test(test-import-function test-import-function)

View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include "../wasmer.h"
#include <assert.h>
#include <stdint.h>
int main()
{
wasmer_import_object_t *import_object = wasmer_import_object_new();
// Read the wasm file bytes
FILE *file = fopen("sum.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_result_t compile_result = wasmer_instantiate(&instance, bytes, len, import_object);
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);
assert(exports_len == 1);
wasmer_export_t *export = wasmer_exports_get(exports, 0);
wasmer_import_export_kind kind = wasmer_export_kind(export);
assert(kind == WASM_FUNCTION);
printf("Destroy instance\n");
wasmer_instance_destroy(instance);
printf("Destroy import object\n");
wasmer_import_object_destroy(import_object);
printf("Destroy exports\n");
wasmer_exports_destroy(exports);
return 0;
}

View File

@ -8,11 +8,13 @@ static memory_len = 0;
static ptr_len = 0;
static char actual_str[14] = {};
void print_str(int32_t ptr, int32_t len, wasmer_instance_context_t *ctx) {
void print_str(int32_t ptr, int32_t len, wasmer_instance_context_t *ctx)
{
wasmer_memory_t *memory = wasmer_instance_context_memory(ctx, 0);
uint32_t mem_len = wasmer_memory_length(memory);
uint8_t *mem_bytes = wasmer_memory_data(memory);
for(int32_t idx = 0; idx < len; idx++){
for (int32_t idx = 0; idx < len; idx++)
{
actual_str[idx] = mem_bytes[ptr + idx];
}
actual_str[13] = '\0';
@ -54,8 +56,8 @@ int main()
assert(ptr_len == 13);
assert(0 == strcmp(actual_str, "Hello, World!"));
printf("Destroy instance\n");
wasmer_instance_destroy(instance);
// printf("Destroy instance\n");
// wasmer_instance_destroy(instance);
printf("Destroy import object\n");
wasmer_import_object_destroy(import_object);
return 0;