mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-20 12:16:30 +00:00
test(runtime-c-api) Test wasmer_import_trap
.
This commit is contained in:
1
lib/runtime-c-api/tests/.gitignore
vendored
1
lib/runtime-c-api/tests/.gitignore
vendored
@ -15,6 +15,7 @@ test-exported-memory
|
|||||||
test-exports
|
test-exports
|
||||||
test-globals
|
test-globals
|
||||||
test-import-function
|
test-import-function
|
||||||
|
test-import-trap
|
||||||
test-import-object
|
test-import-object
|
||||||
test-imports
|
test-imports
|
||||||
test-instantiate
|
test-instantiate
|
||||||
|
@ -5,6 +5,7 @@ add_executable(test-exported-memory test-exported-memory.c)
|
|||||||
add_executable(test-exports test-exports.c)
|
add_executable(test-exports test-exports.c)
|
||||||
add_executable(test-globals test-globals.c)
|
add_executable(test-globals test-globals.c)
|
||||||
add_executable(test-import-function test-import-function.c)
|
add_executable(test-import-function test-import-function.c)
|
||||||
|
add_executable(test-import-trap test-import-trap.c)
|
||||||
add_executable(test-imports test-imports.c)
|
add_executable(test-imports test-imports.c)
|
||||||
add_executable(test-import-object test-import-object.c)
|
add_executable(test-import-object test-import-object.c)
|
||||||
add_executable(test-instantiate test-instantiate.c)
|
add_executable(test-instantiate test-instantiate.c)
|
||||||
@ -64,6 +65,10 @@ target_link_libraries(test-import-function general ${WASMER_LIB})
|
|||||||
target_compile_options(test-import-function PRIVATE ${COMPILER_OPTIONS})
|
target_compile_options(test-import-function PRIVATE ${COMPILER_OPTIONS})
|
||||||
add_test(test-import-function test-import-function)
|
add_test(test-import-function test-import-function)
|
||||||
|
|
||||||
|
target_link_libraries(test-import-trap general ${WASMER_LIB})
|
||||||
|
target_compile_options(test-import-trap PRIVATE ${COMPILER_OPTIONS})
|
||||||
|
add_test(test-import-trap test-import-trap)
|
||||||
|
|
||||||
target_link_libraries(test-imports general ${WASMER_LIB})
|
target_link_libraries(test-imports general ${WASMER_LIB})
|
||||||
target_compile_options(test-imports PRIVATE ${COMPILER_OPTIONS})
|
target_compile_options(test-imports PRIVATE ${COMPILER_OPTIONS})
|
||||||
add_test(test-imports test-imports)
|
add_test(test-imports test-imports)
|
||||||
|
@ -9,7 +9,6 @@ static int memory_len = 0;
|
|||||||
static int ptr_len = 0;
|
static int ptr_len = 0;
|
||||||
static char actual_str[14] = {};
|
static char actual_str[14] = {};
|
||||||
static int actual_context_data_value = 0;
|
static int actual_context_data_value = 0;
|
||||||
static const char *trap_error_message = "hello";
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int value;
|
int value;
|
||||||
@ -38,8 +37,6 @@ void print_str(wasmer_instance_context_t *ctx, int32_t ptr, int32_t len)
|
|||||||
ptr_len = len;
|
ptr_len = len;
|
||||||
|
|
||||||
actual_context_data_value = ((context_data *) wasmer_instance_context_data_get(ctx))->value;
|
actual_context_data_value = ((context_data *) wasmer_instance_context_data_get(ctx))->value;
|
||||||
|
|
||||||
wasmer_import_trap(ctx, trap_error_message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
|
80
lib/runtime-c-api/tests/test-import-trap.c
Normal file
80
lib/runtime-c-api/tests/test-import-trap.c
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "../wasmer.h"
|
||||||
|
#include <assert.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static const char *trap_error_message = "Hello";
|
||||||
|
|
||||||
|
void print_str(wasmer_instance_context_t *ctx, int32_t ptr, int32_t len)
|
||||||
|
{
|
||||||
|
wasmer_import_trap(ctx, trap_error_message);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
wasmer_value_tag params_sig[] = {WASM_I32, WASM_I32};
|
||||||
|
wasmer_value_tag returns_sig[] = {};
|
||||||
|
|
||||||
|
printf("Creating new func\n");
|
||||||
|
wasmer_import_func_t *func = wasmer_import_func_new((void (*)(void *)) print_str, params_sig, 2, returns_sig, 0);
|
||||||
|
|
||||||
|
char *module_name = "env";
|
||||||
|
wasmer_byte_array module_name_bytes = {
|
||||||
|
.bytes = (const uint8_t *) module_name,
|
||||||
|
.bytes_len = strlen(module_name),
|
||||||
|
};
|
||||||
|
|
||||||
|
char *import_name = "print_str";
|
||||||
|
wasmer_byte_array import_name_bytes = {
|
||||||
|
.bytes = (const uint8_t *) import_name,
|
||||||
|
.bytes_len = strlen(import_name),
|
||||||
|
};
|
||||||
|
|
||||||
|
wasmer_import_t import = {
|
||||||
|
.module_name = module_name_bytes,
|
||||||
|
.import_name = import_name_bytes,
|
||||||
|
.tag = WASM_FUNCTION,
|
||||||
|
.value.func = func,
|
||||||
|
};
|
||||||
|
|
||||||
|
wasmer_import_t imports[] = {import};
|
||||||
|
|
||||||
|
// Read the wasm file bytes
|
||||||
|
FILE *file = fopen("assets/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);
|
||||||
|
|
||||||
|
printf("Instantiating\n");
|
||||||
|
wasmer_instance_t *instance = NULL;
|
||||||
|
wasmer_result_t compile_result = wasmer_instantiate(&instance, bytes, len, imports, 1);
|
||||||
|
printf("Compile result: %d\n", compile_result);
|
||||||
|
|
||||||
|
assert(compile_result == WASMER_OK);
|
||||||
|
|
||||||
|
wasmer_value_t params[] = {};
|
||||||
|
wasmer_value_t results[] = {};
|
||||||
|
wasmer_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_ERROR);
|
||||||
|
|
||||||
|
int error_len = wasmer_last_error_length();
|
||||||
|
printf("Error len: `%d`\n", 20);
|
||||||
|
char *error_str = malloc(error_len);
|
||||||
|
wasmer_last_error_message(error_str, error_len);
|
||||||
|
printf("Error str: `%s`\n", error_str);
|
||||||
|
|
||||||
|
assert(0 == strcmp(error_str, "Call error: \"Hello\""));
|
||||||
|
|
||||||
|
printf("Destroying func\n");
|
||||||
|
wasmer_import_func_destroy(func);
|
||||||
|
printf("Destroy instance\n");
|
||||||
|
wasmer_instance_destroy(instance);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Reference in New Issue
Block a user