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);
16 return wasm_extern_as_memory(exports->data[i]);
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);
24 return wasm_extern_as_func(exports->data[i]);
28 void check(bool success) {
30 printf("> Error, expected success\n");
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");
43 void check_call0(wasm_func_t* func, int32_t expected) {
44 check_call(func, NULL, expected);
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);
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}}
57 check_call(func, args, expected);
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");
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}}
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);
79 printf("> Error on result, expected trap\n");
82 wasm_trap_delete(trap);
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);
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}}
95 check_trap(func, args);
99 int main(int argc, const char* argv[]) {
101 printf("Initializing...\n");
102 wasm_engine_t* engine = wasm_engine_new();
103 wasm_store_t* store = wasm_store_new(engine);
106 printf("Loading binary...\n");
107 FILE* file = fopen("memory.wasm", "r");
109 printf("> Error loading module!\n");
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");
124 printf("Compiling module...\n");
125 own wasm_module_t* module = wasm_module_new(store, &binary);
127 printf("> Error compiling module!\n");
131 wasm_byte_vec_delete(&binary);
134 printf("Instantiating module...\n");
135 own wasm_instance_t* instance = wasm_instance_new(store, module, NULL, NULL);
137 printf("> Error instantiating module!\n");
142 printf("Extracting exports...\n");
143 own wasm_extern_vec_t exports;
144 wasm_instance_exports(instance, &exports);
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++);
151 wasm_module_delete(module);
154 own wasm_memory_t* copy = wasm_memory_copy(memory);
155 assert(wasm_memory_same(memory, copy));
156 wasm_memory_delete(copy);
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);
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);
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);
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);
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);
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);
195 check(! wasm_memory_grow(memory, 1));
196 check(wasm_memory_grow(memory, 0));
198 wasm_extern_vec_delete(&exports);
199 wasm_instance_delete(instance);
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));
211 wasm_memorytype_delete(memorytype);
212 wasm_memory_delete(memory2);
215 printf("Shutting down...\n");
216 wasm_store_delete(store);
217 wasm_engine_delete(engine);