mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-30 00:51:34 +00:00
Merge remote-tracking branch 'origin/master' into feature/llvm-osr
This commit is contained in:
4
lib/runtime-c-api/tests/.gitignore
vendored
4
lib/runtime-c-api/tests/.gitignore
vendored
@ -22,4 +22,6 @@ test-module-exports
|
||||
test-module-imports
|
||||
test-module-serialize
|
||||
test-tables
|
||||
test-validate
|
||||
test-validate
|
||||
test-context
|
||||
test-module-import-instantiate
|
||||
|
@ -14,6 +14,8 @@ add_executable(test-module-imports test-module-imports.c)
|
||||
add_executable(test-module-serialize test-module-serialize.c)
|
||||
add_executable(test-tables test-tables.c)
|
||||
add_executable(test-validate test-validate.c)
|
||||
add_executable(test-context test-context.c)
|
||||
add_executable(test-module-import-instantiate test-module-import-instantiate.c)
|
||||
|
||||
find_library(
|
||||
WASMER_LIB NAMES libwasmer_runtime_c_api.dylib libwasmer_runtime_c_api.so libwasmer_runtime_c_api.dll
|
||||
@ -87,3 +89,11 @@ add_test(test-tables test-tables)
|
||||
target_link_libraries(test-validate general ${WASMER_LIB})
|
||||
target_compile_options(test-validate PRIVATE ${COMPILER_OPTIONS})
|
||||
add_test(test-validate test-validate)
|
||||
|
||||
target_link_libraries(test-context general ${WASMER_LIB})
|
||||
target_compile_options(test-context PRIVATE ${COMPILER_OPTIONS})
|
||||
add_test(test-context test-context)
|
||||
|
||||
target_link_libraries(test-module-import-instantiate general ${WASMER_LIB})
|
||||
target_compile_options(test-module-import-instantiate PRIVATE ${COMPILER_OPTIONS})
|
||||
add_test(test-module-import-instantiate test-module-import-instantiate)
|
||||
|
BIN
lib/runtime-c-api/tests/a.out
Executable file
BIN
lib/runtime-c-api/tests/a.out
Executable file
Binary file not shown.
BIN
lib/runtime-c-api/tests/assets/inc.wasm
Normal file
BIN
lib/runtime-c-api/tests/assets/inc.wasm
Normal file
Binary file not shown.
12
lib/runtime-c-api/tests/assets/inc.wast
Normal file
12
lib/runtime-c-api/tests/assets/inc.wast
Normal file
@ -0,0 +1,12 @@
|
||||
(module
|
||||
(func $inc (import "env" "inc"))
|
||||
(func $mul (import "env" "mul"))
|
||||
(func $get (import "env" "get") (result i32))
|
||||
|
||||
(func (export "inc_and_get") (result i32)
|
||||
call $inc
|
||||
call $get)
|
||||
|
||||
(func (export "mul_and_get") (result i32)
|
||||
call $mul
|
||||
call $get))
|
@ -4,19 +4,17 @@ use std::process::Command;
|
||||
fn test_c_api() {
|
||||
let project_tests_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/tests");
|
||||
|
||||
run_command("cmake", project_tests_dir, Some("."));
|
||||
run_command("make", project_tests_dir, Some("-Wdev -Werror=dev"));
|
||||
run_command("make", project_tests_dir, Some("test"));
|
||||
run_command("cmake", project_tests_dir, vec!["."]);
|
||||
run_command("make", project_tests_dir, vec!["-Wdev", "-Werror=dev"]);
|
||||
run_command("make", project_tests_dir, vec!["test", "ARGS=\"-V\""]);
|
||||
}
|
||||
|
||||
fn run_command(command_str: &str, dir: &str, arg: Option<&str>) {
|
||||
println!("Running command: `{}` arg: {:?}", command_str, arg);
|
||||
fn run_command(command_str: &str, dir: &str, args: Vec<&str>) {
|
||||
println!("Running command: `{}` args: {:?}", command_str, args);
|
||||
|
||||
let mut command = Command::new(command_str);
|
||||
|
||||
if let Some(a) = arg {
|
||||
command.arg(a);
|
||||
}
|
||||
command.args(&args);
|
||||
|
||||
command.current_dir(dir);
|
||||
|
||||
|
137
lib/runtime-c-api/tests/test-context.c
Normal file
137
lib/runtime-c-api/tests/test-context.c
Normal file
@ -0,0 +1,137 @@
|
||||
#include <stdio.h>
|
||||
#include "../wasmer.h"
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
int32_t amount;
|
||||
int32_t value;
|
||||
} counter_data;
|
||||
|
||||
typedef struct {
|
||||
uint8_t* bytes;
|
||||
long bytes_len;
|
||||
} wasm_file_t;
|
||||
|
||||
wasm_file_t read_wasm_file(const char* file_name) {
|
||||
wasm_file_t wasm_file;
|
||||
|
||||
FILE *file = fopen(file_name, "r");
|
||||
fseek(file, 0, SEEK_END);
|
||||
wasm_file.bytes_len = ftell(file);
|
||||
|
||||
wasm_file.bytes = malloc(wasm_file.bytes_len);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
fread(wasm_file.bytes, 1, wasm_file.bytes_len, file);
|
||||
fclose(file);
|
||||
|
||||
return wasm_file;
|
||||
}
|
||||
|
||||
void inc_counter(wasmer_instance_context_t *ctx) {
|
||||
counter_data* data = (counter_data*)wasmer_instance_context_data_get(ctx);
|
||||
data->value = data->value + data->amount;
|
||||
}
|
||||
|
||||
void mul_counter(wasmer_instance_context_t *ctx) {
|
||||
counter_data* data = (counter_data*)wasmer_instance_context_data_get(ctx);
|
||||
data->value = data->value * data->amount;
|
||||
}
|
||||
|
||||
int32_t get_counter(wasmer_instance_context_t *ctx) {
|
||||
counter_data* data = (counter_data*)wasmer_instance_context_data_get(ctx);
|
||||
return data->value;
|
||||
}
|
||||
|
||||
counter_data *init_counter(int32_t value, int32_t amount) {
|
||||
counter_data* counter = malloc(sizeof(counter_data));
|
||||
counter->value = value;
|
||||
counter->amount = amount;
|
||||
return counter;
|
||||
}
|
||||
|
||||
void assert_counter(wasmer_instance_t *instance, int32_t expected) {
|
||||
wasmer_value_t result_one;
|
||||
wasmer_value_t params[] = {};
|
||||
wasmer_value_t results[] = {result_one};
|
||||
|
||||
wasmer_result_t call1_result = wasmer_instance_call(instance, "inc_and_get", params, 0, results, 1);
|
||||
printf("Call result: %d\n", call1_result);
|
||||
printf("Result: %d\n", results[0].value.I32);
|
||||
assert(results[0].value.I32 == expected);
|
||||
assert(call1_result == WASMER_OK);
|
||||
|
||||
const wasmer_instance_context_t *ctx = wasmer_instance_context_get(instance);
|
||||
counter_data *cd = (counter_data*)wasmer_instance_context_data_get(ctx);
|
||||
assert(cd->value == expected);
|
||||
}
|
||||
|
||||
wasmer_import_t create_import(char* module_name, char* import_name, wasmer_import_func_t *func) {
|
||||
wasmer_import_t import;
|
||||
wasmer_byte_array module_name_bytes;
|
||||
wasmer_byte_array import_name_bytes;
|
||||
|
||||
module_name_bytes.bytes = (const uint8_t *) module_name;
|
||||
module_name_bytes.bytes_len = strlen(module_name);
|
||||
|
||||
import_name_bytes.bytes = (const uint8_t *) import_name;
|
||||
import_name_bytes.bytes_len = strlen(import_name);
|
||||
|
||||
import.module_name = module_name_bytes;
|
||||
import.import_name = import_name_bytes;
|
||||
|
||||
import.tag = WASM_FUNCTION;
|
||||
import.value.func = func;
|
||||
|
||||
return import;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// Prepare Imports
|
||||
wasmer_value_tag inc_params_sig[] = {};
|
||||
wasmer_value_tag inc_returns_sig[] = {};
|
||||
wasmer_import_func_t *inc_func = wasmer_import_func_new((void (*)(void *)) inc_counter, inc_params_sig, 0, inc_returns_sig, 0);
|
||||
wasmer_import_t inc_import = create_import("env", "inc", inc_func);
|
||||
|
||||
wasmer_value_tag mul_params_sig[] = {};
|
||||
wasmer_value_tag mul_returns_sig[] = {};
|
||||
wasmer_import_func_t *mul_func = wasmer_import_func_new((void (*)(void *)) mul_counter, mul_params_sig, 0, mul_returns_sig, 0);
|
||||
wasmer_import_t mul_import = create_import("env", "mul", mul_func);
|
||||
|
||||
wasmer_value_tag get_params_sig[] = {};
|
||||
wasmer_value_tag get_returns_sig[] = {WASM_I32};
|
||||
wasmer_import_func_t *get_func = wasmer_import_func_new((void (*)(void *)) get_counter, get_params_sig, 0, get_returns_sig, 1);
|
||||
wasmer_import_t get_import = create_import("env", "get", get_func);
|
||||
|
||||
wasmer_import_t imports[] = {inc_import, mul_import, get_import};
|
||||
|
||||
// Read the wasm file
|
||||
wasm_file_t wasm_file = read_wasm_file("assets/inc.wasm");
|
||||
|
||||
// Instantiate instance
|
||||
printf("Instantiating\n");
|
||||
wasmer_instance_t *instance = NULL;
|
||||
wasmer_result_t instantiate_res = wasmer_instantiate(&instance, wasm_file.bytes, wasm_file.bytes_len, imports, 3);
|
||||
printf("Compile result: %d\n", instantiate_res);
|
||||
assert(instantiate_res == WASMER_OK);
|
||||
|
||||
// Init counter
|
||||
counter_data *counter = init_counter(2, 5);
|
||||
wasmer_instance_context_data_set(instance, counter);
|
||||
|
||||
// Run `instance.inc_and_get` and assert
|
||||
assert_counter(instance, 7);
|
||||
assert_counter(instance, 12);
|
||||
assert_counter(instance, 17);
|
||||
|
||||
// Clear resources
|
||||
wasmer_import_func_destroy(inc_func);
|
||||
wasmer_import_func_destroy(get_func);
|
||||
wasmer_instance_destroy(instance);
|
||||
free(counter);
|
||||
free(wasm_file.bytes);
|
||||
|
||||
return 0;
|
||||
}
|
@ -42,8 +42,9 @@ int main()
|
||||
assert(export_length == 5);
|
||||
|
||||
// Get the `memory` export.
|
||||
wasmer_export_t *export = wasmer_exports_get(exports, 1);
|
||||
wasmer_export_t *export = wasmer_exports_get(exports, 0);
|
||||
wasmer_import_export_kind kind = wasmer_export_kind(export);
|
||||
printf("Wasmer import export kind: %d (Memory is %d)\n", kind, WASM_MEMORY);
|
||||
assert(kind == WASM_MEMORY);
|
||||
|
||||
wasmer_byte_array export_name = wasmer_export_name(export);
|
||||
|
149
lib/runtime-c-api/tests/test-module-import-instantiate.c
Normal file
149
lib/runtime-c-api/tests/test-module-import-instantiate.c
Normal file
@ -0,0 +1,149 @@
|
||||
#include <stdio.h>
|
||||
#include "../wasmer.h"
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
int32_t amount;
|
||||
int32_t value;
|
||||
} counter_data;
|
||||
|
||||
typedef struct {
|
||||
uint8_t* bytes;
|
||||
long bytes_len;
|
||||
} wasm_file_t;
|
||||
|
||||
wasm_file_t read_wasm_file(const char* file_name) {
|
||||
wasm_file_t wasm_file;
|
||||
|
||||
FILE *file = fopen(file_name, "r");
|
||||
fseek(file, 0, SEEK_END);
|
||||
wasm_file.bytes_len = ftell(file);
|
||||
|
||||
wasm_file.bytes = malloc(wasm_file.bytes_len);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
fread(wasm_file.bytes, 1, wasm_file.bytes_len, file);
|
||||
fclose(file);
|
||||
|
||||
return wasm_file;
|
||||
}
|
||||
|
||||
void inc_counter(wasmer_instance_context_t *ctx) {
|
||||
counter_data* data = (counter_data*)wasmer_instance_context_data_get(ctx);
|
||||
data->value = data->value + data->amount;
|
||||
}
|
||||
|
||||
void mul_counter(wasmer_instance_context_t *ctx) {
|
||||
counter_data* data = (counter_data*)wasmer_instance_context_data_get(ctx);
|
||||
data->value = data->value * data->amount;
|
||||
}
|
||||
|
||||
int32_t get_counter(wasmer_instance_context_t *ctx) {
|
||||
counter_data* data = (counter_data*)wasmer_instance_context_data_get(ctx);
|
||||
return data->value;
|
||||
}
|
||||
|
||||
counter_data *init_counter(int32_t value, int32_t amount) {
|
||||
counter_data* counter = malloc(sizeof(counter_data));
|
||||
counter->value = value;
|
||||
counter->amount = amount;
|
||||
return counter;
|
||||
}
|
||||
|
||||
wasmer_import_t create_import(char* module_name, char* import_name, wasmer_import_func_t *func) {
|
||||
wasmer_import_t import;
|
||||
wasmer_byte_array module_name_bytes;
|
||||
wasmer_byte_array import_name_bytes;
|
||||
|
||||
module_name_bytes.bytes = (const uint8_t *) module_name;
|
||||
module_name_bytes.bytes_len = strlen(module_name);
|
||||
|
||||
import_name_bytes.bytes = (const uint8_t *) import_name;
|
||||
import_name_bytes.bytes_len = strlen(import_name);
|
||||
|
||||
import.module_name = module_name_bytes;
|
||||
import.import_name = import_name_bytes;
|
||||
|
||||
import.tag = WASM_FUNCTION;
|
||||
import.value.func = func;
|
||||
|
||||
return import;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// Prepare Imports
|
||||
wasmer_value_tag inc_params_sig[] = {};
|
||||
wasmer_value_tag inc_returns_sig[] = {};
|
||||
wasmer_import_func_t *inc_func = wasmer_import_func_new((void (*)(void *)) inc_counter, inc_params_sig, 0, inc_returns_sig, 0);
|
||||
wasmer_import_t inc_import = create_import("env", "inc", inc_func);
|
||||
|
||||
wasmer_value_tag mul_params_sig[] = {};
|
||||
wasmer_value_tag mul_returns_sig[] = {};
|
||||
wasmer_import_func_t *mul_func = wasmer_import_func_new((void (*)(void *)) mul_counter, mul_params_sig, 0, mul_returns_sig, 0);
|
||||
wasmer_import_t mul_import = create_import("env", "mul", mul_func);
|
||||
|
||||
wasmer_value_tag get_params_sig[] = {};
|
||||
wasmer_value_tag get_returns_sig[] = {WASM_I32};
|
||||
wasmer_import_func_t *get_func = wasmer_import_func_new((void (*)(void *)) get_counter, get_params_sig, 0, get_returns_sig, 1);
|
||||
wasmer_import_t get_import = create_import("env", "get", get_func);
|
||||
|
||||
// Read the wasm file
|
||||
wasm_file_t wasm_file = read_wasm_file("assets/inc.wasm");
|
||||
|
||||
// Compile module
|
||||
wasmer_module_t *module = NULL;
|
||||
wasmer_result_t compile_res = wasmer_compile(&module, wasm_file.bytes, wasm_file.bytes_len);
|
||||
assert(compile_res == WASMER_OK);
|
||||
|
||||
// Prepare Import Object
|
||||
wasmer_import_object_t *import_object = wasmer_import_object_new();
|
||||
|
||||
// First, we import `inc_counter` and `mul_counter`
|
||||
wasmer_import_t imports[] = {inc_import, mul_import};
|
||||
wasmer_result_t extend_res = wasmer_import_object_extend(import_object, imports, 2);
|
||||
assert(extend_res == WASMER_OK);
|
||||
|
||||
// Now, we'll import `inc_counter` and `mul_counter`
|
||||
wasmer_import_t more_imports[] = {get_import};
|
||||
wasmer_result_t extend_res2 = wasmer_import_object_extend(import_object, more_imports, 1);
|
||||
assert(extend_res2 == WASMER_OK);
|
||||
|
||||
// Same `wasmer_import_object_extend` as the first, doesn't affect anything
|
||||
wasmer_result_t extend_res3 = wasmer_import_object_extend(import_object, imports, 2);
|
||||
assert(extend_res3 == WASMER_OK);
|
||||
|
||||
// Instantiate instance
|
||||
printf("Instantiating\n");
|
||||
wasmer_instance_t *instance = NULL;
|
||||
wasmer_result_t instantiate_res = wasmer_module_import_instantiate(&instance, module, import_object);
|
||||
printf("Compile result: %d\n", instantiate_res);
|
||||
assert(instantiate_res == WASMER_OK);
|
||||
|
||||
// Init counter
|
||||
counter_data *counter = init_counter(2, 5);
|
||||
wasmer_instance_context_data_set(instance, counter);
|
||||
|
||||
wasmer_value_t result_one;
|
||||
wasmer_value_t params[] = {};
|
||||
wasmer_value_t results[] = {result_one};
|
||||
|
||||
wasmer_result_t call1_result = wasmer_instance_call(instance, "inc_and_get", params, 0, results, 1);
|
||||
printf("Call result: %d\n", call1_result);
|
||||
printf("Result: %d\n", results[0].value.I32);
|
||||
|
||||
wasmer_result_t call2_result = wasmer_instance_call(instance, "mul_and_get", params, 0, results, 1);
|
||||
printf("Call result: %d\n", call2_result);
|
||||
printf("Result: %d\n", results[0].value.I32);
|
||||
|
||||
// Clear resources
|
||||
wasmer_import_func_destroy(inc_func);
|
||||
wasmer_import_func_destroy(mul_func);
|
||||
wasmer_import_func_destroy(get_func);
|
||||
wasmer_instance_destroy(instance);
|
||||
free(counter);
|
||||
free(wasm_file.bytes);
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user