MCPcopy Create free account
hub / github.com/WebAssembly/wasm-c-api / run_in_store

Function run_in_store

example/finalize.c:20–67  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18}
19
20void run_in_store(wasm_store_t* store) {
21 // Load binary.
22 printf("Loading binary...\n");
23 FILE* file = fopen("finalize.wasm", "rb");
24 if (!file) {
25 printf("> Error loading module!\n");
26 exit(1);
27 }
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");
35 exit(1);
36 }
37 fclose(file);
38
39 // Compile.
40 printf("Compiling module...\n");
41 own wasm_module_t* module = wasm_module_new(store, &binary);
42 if (!module) {
43 printf("> Error compiling module!\n");
44 exit(1);
45 }
46
47 wasm_byte_vec_delete(&binary);
48
49 // Instantiate.
50 printf("Instantiating modules...\n");
51 for (int i = 0; i <= iterations; ++i) {
52 if (i % (iterations / 10) == 0) printf("%d\n", i);
53 wasm_extern_vec_t imports = WASM_EMPTY_VEC;
54 own wasm_instance_t* instance =
55 wasm_instance_new(store, module, &imports, NULL);
56 if (!instance) {
57 printf("> Error instantiating module %d!\n", i);
58 exit(1);
59 }
60 void* data = (void*)(intptr_t)i;
61 wasm_instance_set_host_info_with_finalizer(instance, data, &finalize);
62 wasm_instance_delete(instance);
63 ++live_count;
64 }
65
66 wasm_module_delete(module);
67}
68
69int main(int argc, const char* argv[]) {
70 // Initialize.

Callers 1

mainFunction · 0.70

Calls 2

wasm_module_newFunction · 0.85
wasm_instance_newFunction · 0.85

Tested by

no test coverage detected