1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
| // wasm部分
#include<stdio.h>
int main()
{
char s[20] = {0};
scanf("%s", s);
puts(s);
return 0;
}
// c部分
#include <stdio.h>
#include "wasmer.h"
#include <assert.h>
#include <stdint.h>
// Function to print the most recent error string from Wasmer if we have them
void print_wasmer_error()
{
int error_len = wasmer_last_error_length();
char *error_str = malloc(error_len);
wasmer_last_error_message(error_str, error_len);
printf("Error: `%s`\n", error_str);
}
int main()
{
FILE *file = fopen("hello.wasm", "r");
assert(file != NULL);
fseek(file, 0, SEEK_END);
long len = ftell(file);
uint8_t *bytes = (uint8_t*)malloc(len);
fseek(file, 0, SEEK_SET);
fread(bytes, 1, len, file);
fclose(file);
wasmer_module_t *module = NULL;
wasmer_result_t compile_result = wasmer_compile(&module, bytes, len);
if (compile_result != WASMER_OK)
{
print_wasmer_error();
return -1;
}
wasmer_import_object_t *wasi_import_obj = wasmer_wasi_generate_default_import_object();
// find out what version of WASI the module is
Version wasi_version = wasmer_wasi_get_version(module);
// char* progname = "ProgramName";
// wasmer_byte_array args[] = { { .bytes = progname; .bytes_len = sizeof(progname); } };
wasmer_import_object_t * import_object = wasmer_wasi_generate_import_object_for_version(wasi_version, 0, 1, NULL, 0, NULL, 0, NULL, 0);
// Instantiate a WebAssembly Instance from Wasm bytes and imports
wasmer_instance_t *instance = NULL;
// clock_gettime(CLOCK_REALTIME, &start);
wasmer_result_t instantiate_result = wasmer_module_import_instantiate(&instance, module, import_object);
if(instantiate_result != WASMER_OK)
{
print_wasmer_error();
return -1;
}
wasmer_value_t arguments[] = {0};
wasmer_value_t results[] = {0};
// Call the `sum` function with the prepared arguments and the return value.
wasmer_result_t call_result = wasmer_instance_call(instance, "_start", arguments, 0, results, 1);
int response_value = results[0].value.I32;
printf("%d\n", response_value);
wasmer_instance_destroy(instance);
return 0;
}
|