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

Function main

example/trap.c:33–158  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

31
32
33int main(int argc, const char* argv[]) {
34 // Initialize.
35 printf("Initializing...\n");
36 wasm_engine_t* engine = wasm_engine_new();
37 wasm_store_t* store = wasm_store_new(engine);
38
39 // Load binary.
40 printf("Loading binary...\n");
41 FILE* file = fopen("trap.wasm", "rb");
42 if (!file) {
43 printf("> Error loading module!\n");
44 return 1;
45 }
46 fseek(file, 0L, SEEK_END);
47 size_t file_size = ftell(file);
48 fseek(file, 0L, SEEK_SET);
49 wasm_byte_vec_t binary;
50 wasm_byte_vec_new_uninitialized(&binary, file_size);
51 if (fread(binary.data, file_size, 1, file) != 1) {
52 printf("> Error loading module!\n");
53 return 1;
54 }
55 fclose(file);
56
57 // Compile.
58 printf("Compiling module...\n");
59 own wasm_module_t* module = wasm_module_new(store, &binary);
60 if (!module) {
61 printf("> Error compiling module!\n");
62 return 1;
63 }
64
65 wasm_byte_vec_delete(&binary);
66
67 // Create external print functions.
68 printf("Creating callback...\n");
69 own wasm_functype_t* fail_type =
70 wasm_functype_new_0_1(wasm_valtype_new_i32());
71 own wasm_func_t* fail_func =
72 wasm_func_new_with_env(store, fail_type, fail_callback, store, NULL);
73
74 wasm_functype_delete(fail_type);
75
76 // Instantiate.
77 printf("Instantiating module...\n");
78 wasm_extern_t* externs[] = { wasm_func_as_extern(fail_func) };
79 wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
80 own wasm_instance_t* instance =
81 wasm_instance_new(store, module, &imports, NULL);
82 if (!instance) {
83 printf("> Error instantiating module!\n");
84 return 1;
85 }
86
87 wasm_func_delete(fail_func);
88
89 // Extract export.
90 printf("Extracting exports...\n");

Callers

nothing calls this directly

Calls 13

wasm_engine_newFunction · 0.85
wasm_store_newFunction · 0.85
wasm_module_newFunction · 0.85
wasm_func_new_with_envFunction · 0.85
wasm_func_as_externFunction · 0.85
wasm_instance_newFunction · 0.85
wasm_instance_exportsFunction · 0.85
wasm_extern_as_funcFunction · 0.85
wasm_func_callFunction · 0.85
wasm_trap_messageFunction · 0.85
wasm_trap_originFunction · 0.85
wasm_trap_traceFunction · 0.85

Tested by

no test coverage detected