2019-02-02 04:10:36 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include "../wasmer.h"
|
2019-02-02 06:26:10 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdint.h>
|
2019-03-06 12:14:40 +00:00
|
|
|
#include <string.h>
|
2019-02-02 04:10:36 +00:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2019-02-02 06:26:10 +00:00
|
|
|
// Read the wasm file bytes
|
2019-03-27 08:42:56 +00:00
|
|
|
FILE *file = fopen("assets/sum.wasm", "r");
|
2019-02-02 06:26:10 +00:00
|
|
|
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);
|
|
|
|
|
2019-02-15 15:40:28 +00:00
|
|
|
wasmer_import_t imports[] = {};
|
2019-02-02 06:26:10 +00:00
|
|
|
wasmer_instance_t *instance = NULL;
|
2019-02-15 15:40:28 +00:00
|
|
|
wasmer_result_t compile_result = wasmer_instantiate(&instance, bytes, len, imports, 0);
|
2019-02-02 06:26:10 +00:00
|
|
|
printf("Compile result: %d\n", compile_result);
|
2019-02-12 01:07:28 +00:00
|
|
|
assert(compile_result == WASMER_OK);
|
2019-02-02 06:26:10 +00:00
|
|
|
|
2019-02-02 20:53:07 +00:00
|
|
|
wasmer_value_t param_one;
|
|
|
|
param_one.tag = WASM_I32;
|
|
|
|
param_one.value.I32 = 7;
|
|
|
|
wasmer_value_t param_two;
|
|
|
|
param_two.tag = WASM_I32;
|
|
|
|
param_two.value.I32 = 8;
|
|
|
|
wasmer_value_t params[] = {param_one, param_two};
|
|
|
|
|
|
|
|
wasmer_value_t result_one;
|
|
|
|
wasmer_value_t results[] = {result_one};
|
|
|
|
|
2019-02-12 01:07:28 +00:00
|
|
|
wasmer_result_t call_result = wasmer_instance_call(instance, "sum", params, 2, results, 1);
|
2019-02-02 06:26:10 +00:00
|
|
|
printf("Call result: %d\n", call_result);
|
2019-02-02 20:53:07 +00:00
|
|
|
printf("Result: %d\n", results[0].value.I32);
|
|
|
|
assert(results[0].value.I32 == 15);
|
2019-02-12 01:07:28 +00:00
|
|
|
assert(call_result == WASMER_OK);
|
2019-02-02 06:26:10 +00:00
|
|
|
|
2019-02-10 23:57:23 +00:00
|
|
|
|
2019-02-12 01:07:28 +00:00
|
|
|
wasmer_result_t call_result2 = wasmer_instance_call(instance, "sum", params, 1, results, 1);
|
2019-02-10 23:57:23 +00:00
|
|
|
printf("Call result bad: %d\n", call_result2);
|
2019-02-12 01:07:28 +00:00
|
|
|
assert(call_result2 == WASMER_ERROR);
|
2019-02-10 23:57:23 +00:00
|
|
|
|
|
|
|
int error_len = wasmer_last_error_length();
|
|
|
|
printf("Error len: `%d`\n", error_len);
|
|
|
|
char *error_str = malloc(error_len);
|
2019-05-13 08:52:55 +00:00
|
|
|
int error_result = wasmer_last_error_message(error_str, error_len);
|
|
|
|
assert(error_len == error_result);
|
2019-02-10 23:57:23 +00:00
|
|
|
printf("Error str: `%s`\n", error_str);
|
2019-02-16 01:47:00 +00:00
|
|
|
assert(0 == strcmp(error_str, "Call error: Parameters of type [I32] did not match signature [I32, I32] -> [I32]"));
|
2019-02-10 23:57:23 +00:00
|
|
|
free(error_str);
|
|
|
|
|
2019-02-02 06:26:10 +00:00
|
|
|
printf("Destroy instance\n");
|
2019-02-05 01:49:28 +00:00
|
|
|
wasmer_instance_destroy(instance);
|
2019-02-02 04:10:36 +00:00
|
|
|
return 0;
|
2019-03-06 12:14:40 +00:00
|
|
|
}
|