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

Function main

example/start.c:21–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19
20
21int main(int argc, const char* argv[]) {
22 // Initialize.
23 printf("Initializing...\n");
24 wasm_engine_t* engine = wasm_engine_new();
25 wasm_store_t* store = wasm_store_new(engine);
26
27 // Load binary.
28 printf("Loading binary...\n");
29 FILE* file = fopen("start.wasm", "rb");
30 if (!file) {
31 printf("> Error loading module!\n");
32 return 1;
33 }
34 fseek(file, 0L, SEEK_END);
35 size_t file_size = ftell(file);
36 fseek(file, 0L, SEEK_SET);
37 wasm_byte_vec_t binary;
38 wasm_byte_vec_new_uninitialized(&binary, file_size);
39 if (fread(binary.data, file_size, 1, file) != 1) {
40 printf("> Error loading module!\n");
41 return 1;
42 }
43 fclose(file);
44
45 // Compile.
46 printf("Compiling module...\n");
47 own wasm_module_t* module = wasm_module_new(store, &binary);
48 if (!module) {
49 printf("> Error compiling module!\n");
50 return 1;
51 }
52
53 wasm_byte_vec_delete(&binary);
54
55 // Instantiate.
56 printf("Instantiating module...\n");
57 wasm_extern_vec_t imports = WASM_EMPTY_VEC;
58 own wasm_trap_t* trap = NULL;
59 own wasm_instance_t* instance =
60 wasm_instance_new(store, module, &imports, &trap);
61 if (instance || !trap) {
62 printf("> Error instantiating module, expected trap!\n");
63 return 1;
64 }
65
66 wasm_module_delete(module);
67
68 // Print result.
69 printf("Printing message...\n");
70 own wasm_name_t message;
71 wasm_trap_message(trap, &message);
72 printf("> %s\n", message.data);
73
74 printf("Printing origin...\n");
75 own wasm_frame_t* frame = wasm_trap_origin(trap);
76 if (frame) {
77 print_frame(frame);
78 wasm_frame_delete(frame);

Callers

nothing calls this directly

Calls 8

wasm_engine_newFunction · 0.85
wasm_store_newFunction · 0.85
wasm_module_newFunction · 0.85
wasm_instance_newFunction · 0.85
wasm_trap_messageFunction · 0.85
wasm_trap_originFunction · 0.85
wasm_trap_traceFunction · 0.85
print_frameFunction · 0.70

Tested by

no test coverage detected