af2b9414d3850151cf08f594f3ac7e103d9f6059
[platform/framework/web/lwnode.git] /
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <inttypes.h>
5
6 #include "wasm.h"
7
8 #define own
9
10
11 wasm_memory_t* get_export_memory(const wasm_extern_vec_t* exports, size_t i) {
12   if (exports->size <= i || !wasm_extern_as_memory(exports->data[i])) {
13     printf("> Error accessing memory export %zu!\n", i);
14     exit(1);
15   }
16   return wasm_extern_as_memory(exports->data[i]);
17 }
18
19 wasm_func_t* get_export_func(const wasm_extern_vec_t* exports, size_t i) {
20   if (exports->size <= i || !wasm_extern_as_func(exports->data[i])) {
21     printf("> Error accessing function export %zu!\n", i);
22     exit(1);
23   }
24   return wasm_extern_as_func(exports->data[i]);
25 }
26
27
28 void check(bool success) {
29   if (!success) {
30     printf("> Error, expected success\n");
31     exit(1);
32   }
33 }
34
35 void check_call(wasm_func_t* func, wasm_val_t args[], int32_t expected) {
36   wasm_val_t results[1];
37   if (wasm_func_call(func, args, results) || results[0].of.i32 != expected) {
38     printf("> Error on result\n");
39     exit(1);
40   }
41 }
42
43 void check_call0(wasm_func_t* func, int32_t expected) {
44   check_call(func, NULL, expected);
45 }
46
47 void check_call1(wasm_func_t* func, int32_t arg, int32_t expected) {
48   wasm_val_t args[] = { {.kind = WASM_I32, .of = {.i32 = arg}} };
49   check_call(func, args, expected);
50 }
51
52 void check_call2(wasm_func_t* func, int32_t arg1, int32_t arg2, int32_t expected) {
53   wasm_val_t args[2] = {
54     {.kind = WASM_I32, .of = {.i32 = arg1}},
55     {.kind = WASM_I32, .of = {.i32 = arg2}}
56   };
57   check_call(func, args, expected);
58 }
59
60 void check_ok(wasm_func_t* func, wasm_val_t args[]) {
61   if (wasm_func_call(func, args, NULL)) {
62     printf("> Error on result, expected empty\n");
63     exit(1);
64   }
65 }
66
67 void check_ok2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
68   wasm_val_t args[2] = {
69     {.kind = WASM_I32, .of = {.i32 = arg1}},
70     {.kind = WASM_I32, .of = {.i32 = arg2}}
71   };
72   check_ok(func, args);
73 }
74
75 void check_trap(wasm_func_t* func, wasm_val_t args[]) {
76   wasm_val_t results[1];
77   own wasm_trap_t* trap = wasm_func_call(func, args, results);
78   if (! trap) {
79     printf("> Error on result, expected trap\n");
80     exit(1);
81   }
82   wasm_trap_delete(trap);
83 }
84
85 void check_trap1(wasm_func_t* func, int32_t arg) {
86   wasm_val_t args[1] = { {.kind = WASM_I32, .of = {.i32 = arg}} };
87   check_trap(func, args);
88 }
89
90 void check_trap2(wasm_func_t* func, int32_t arg1, int32_t arg2) {
91   wasm_val_t args[2] = {
92     {.kind = WASM_I32, .of = {.i32 = arg1}},
93     {.kind = WASM_I32, .of = {.i32 = arg2}}
94   };
95   check_trap(func, args);
96 }
97
98
99 int main(int argc, const char* argv[]) {
100   // Initialize.
101   printf("Initializing...\n");
102   wasm_engine_t* engine = wasm_engine_new();
103   wasm_store_t* store = wasm_store_new(engine);
104
105   // Load binary.
106   printf("Loading binary...\n");
107   FILE* file = fopen("memory.wasm", "r");
108   if (!file) {
109     printf("> Error loading module!\n");
110     return 1;
111   }
112   fseek(file, 0L, SEEK_END);
113   size_t file_size = ftell(file);
114   fseek(file, 0L, SEEK_SET);
115   wasm_byte_vec_t binary;
116   wasm_byte_vec_new_uninitialized(&binary, file_size);
117   if (fread(binary.data, file_size, 1, file) != 1) {
118     printf("> Error loading module!\n");
119     return 1;
120   }
121   fclose(file);
122
123   // Compile.
124   printf("Compiling module...\n");
125   own wasm_module_t* module = wasm_module_new(store, &binary);
126   if (!module) {
127     printf("> Error compiling module!\n");
128     return 1;
129   }
130
131   wasm_byte_vec_delete(&binary);
132
133   // Instantiate.
134   printf("Instantiating module...\n");
135   own wasm_instance_t* instance = wasm_instance_new(store, module, NULL, NULL);
136   if (!instance) {
137     printf("> Error instantiating module!\n");
138     return 1;
139   }
140
141   // Extract export.
142   printf("Extracting exports...\n");
143   own wasm_extern_vec_t exports;
144   wasm_instance_exports(instance, &exports);
145   size_t i = 0;
146   wasm_memory_t* memory = get_export_memory(&exports, i++);
147   wasm_func_t* size_func = get_export_func(&exports, i++);
148   wasm_func_t* load_func = get_export_func(&exports, i++);
149   wasm_func_t* store_func = get_export_func(&exports, i++);
150
151   wasm_module_delete(module);
152
153   // Try cloning.
154   own wasm_memory_t* copy = wasm_memory_copy(memory);
155   assert(wasm_memory_same(memory, copy));
156   wasm_memory_delete(copy);
157
158   // Check initial memory.
159   printf("Checking memory...\n");
160   check(wasm_memory_size(memory) == 2);
161   check(wasm_memory_data_size(memory) == 0x20000);
162   check(wasm_memory_data(memory)[0] == 0);
163   check(wasm_memory_data(memory)[0x1000] == 1);
164   check(wasm_memory_data(memory)[0x1003] == 4);
165
166   check_call0(size_func, 2);
167   check_call1(load_func, 0, 0);
168   check_call1(load_func, 0x1000, 1);
169   check_call1(load_func, 0x1003, 4);
170   check_call1(load_func, 0x1ffff, 0);
171   check_trap1(load_func, 0x20000);
172
173   // Mutate memory.
174   printf("Mutating memory...\n");
175   wasm_memory_data(memory)[0x1003] = 5;
176   check_ok2(store_func, 0x1002, 6);
177   check_trap2(store_func, 0x20000, 0);
178
179   check(wasm_memory_data(memory)[0x1002] == 6);
180   check(wasm_memory_data(memory)[0x1003] == 5);
181   check_call1(load_func, 0x1002, 6);
182   check_call1(load_func, 0x1003, 5);
183
184   // Grow memory.
185   printf("Growing memory...\n");
186   check(wasm_memory_grow(memory, 1));
187   check(wasm_memory_size(memory) == 3);
188   check(wasm_memory_data_size(memory) == 0x30000);
189
190   check_call1(load_func, 0x20000, 0);
191   check_ok2(store_func, 0x20000, 0);
192   check_trap1(load_func, 0x30000);
193   check_trap2(store_func, 0x30000, 0);
194
195   check(! wasm_memory_grow(memory, 1));
196   check(wasm_memory_grow(memory, 0));
197
198   wasm_extern_vec_delete(&exports);
199   wasm_instance_delete(instance);
200
201   // Create stand-alone memory.
202   // TODO(wasm+): Once Wasm allows multiple memories, turn this into import.
203   printf("Creating stand-alone memory...\n");
204   wasm_limits_t limits = {5, 5};
205   own wasm_memorytype_t* memorytype = wasm_memorytype_new(&limits);
206   own wasm_memory_t* memory2 = wasm_memory_new(store, memorytype);
207   check(wasm_memory_size(memory2) == 5);
208   check(! wasm_memory_grow(memory2, 1));
209   check(wasm_memory_grow(memory2, 0));
210
211   wasm_memorytype_delete(memorytype);
212   wasm_memory_delete(memory2);
213
214   // Shut down.
215   printf("Shutting down...\n");
216   wasm_store_delete(store);
217   wasm_engine_delete(engine);
218
219   // All done.
220   printf("Done.\n");
221   return 0;
222 }