10 const int iterations = 100000;
14 void finalize(void* data) {
16 if (i % (iterations / 10) == 0) printf("Finalizing #%d...\n", i);
20 void run_in_store(wasm_store_t* store) {
22 printf("Loading binary...\n");
23 FILE* file = fopen("finalize.wasm", "r");
25 printf("> Error loading module!\n");
28 fseek(file, 0L, SEEK_END);
29 size_t file_size = ftell(file);
30 fseek(file, 0L, SEEK_SET);
31 wasm_byte_vec_t binary;
32 wasm_byte_vec_new_uninitialized(&binary, file_size);
33 if (fread(binary.data, file_size, 1, file) != 1) {
34 printf("> Error loading module!\n");
40 printf("Compiling module...\n");
41 own wasm_module_t* module = wasm_module_new(store, &binary);
43 printf("> Error compiling module!\n");
47 wasm_byte_vec_delete(&binary);
50 printf("Instantiating modules...\n");
51 for (int i = 0; i <= iterations; ++i) {
52 if (i % (iterations / 10) == 0) printf("%d\n", i);
53 own wasm_instance_t* instance =
54 wasm_instance_new(store, module, NULL, NULL);
56 printf("> Error instantiating module %d!\n", i);
59 void* data = (void*)(intptr_t)i;
60 wasm_instance_set_host_info_with_finalizer(instance, data, &finalize);
61 wasm_instance_delete(instance);
65 wasm_module_delete(module);
68 int main(int argc, const char* argv[]) {
70 printf("Initializing...\n");
71 wasm_engine_t* engine = wasm_engine_new();
73 printf("Live count %d\n", live_count);
74 printf("Creating store 1...\n");
75 wasm_store_t* store1 = wasm_store_new(engine);
77 printf("Running in store 1...\n");
79 printf("Live count %d\n", live_count);
81 printf("Creating store 2...\n");
82 wasm_store_t* store2 = wasm_store_new(engine);
84 printf("Running in store 2...\n");
86 printf("Live count %d\n", live_count);
88 printf("Deleting store 2...\n");
89 wasm_store_delete(store2);
90 printf("Live count %d\n", live_count);
92 printf("Running in store 1...\n");
94 printf("Live count %d\n", live_count);
96 printf("Deleting store 1...\n");
97 wasm_store_delete(store1);
98 printf("Live count %d\n", live_count);
100 assert(live_count == 0);
103 printf("Shutting down...\n");
104 wasm_engine_delete(engine);