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

Function main

example/hello.c:20–118  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18
19
20int main(int argc, const char* argv[]) {
21 // Initialize.
22 printf("Initializing...\n");
23 wasm_engine_t* engine = wasm_engine_new();
24 wasm_store_t* store = wasm_store_new(engine);
25
26 // Load binary.
27 printf("Loading binary...\n");
28 FILE* file = fopen("hello.wasm", "rb");
29 if (!file) {
30 printf("> Error loading module!\n");
31 return 1;
32 }
33 fseek(file, 0L, SEEK_END);
34 size_t file_size = ftell(file);
35 fseek(file, 0L, SEEK_SET);
36 wasm_byte_vec_t binary;
37 wasm_byte_vec_new_uninitialized(&binary, file_size);
38 if (fread(binary.data, file_size, 1, file) != 1) {
39 printf("> Error loading module!\n");
40 return 1;
41 }
42 fclose(file);
43
44 // Validate.
45 printf("Validating module...\n");
46 if (!wasm_module_validate(store, &binary)) {
47 printf("> Error validating module!\n");
48 return 1;
49 }
50
51 // Compile.
52 printf("Compiling module...\n");
53 own wasm_module_t* module = wasm_module_new(store, &binary);
54 if (!module) {
55 printf("> Error compiling module!\n");
56 return 1;
57 }
58
59 wasm_byte_vec_delete(&binary);
60
61 // Create external print functions.
62 printf("Creating callback...\n");
63 own wasm_functype_t* hello_type = wasm_functype_new_0_0();
64 own wasm_func_t* hello_func =
65 wasm_func_new(store, hello_type, hello_callback);
66
67 wasm_functype_delete(hello_type);
68
69 // Instantiate.
70 printf("Instantiating module...\n");
71 wasm_extern_t* externs[] = { wasm_func_as_extern(hello_func) };
72 wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
73 own wasm_instance_t* instance =
74 wasm_instance_new(store, module, &imports, NULL);
75 if (!instance) {
76 printf("> Error instantiating module!\n");
77 return 1;

Callers

nothing calls this directly

Calls 10

wasm_engine_newFunction · 0.85
wasm_store_newFunction · 0.85
wasm_module_validateFunction · 0.85
wasm_module_newFunction · 0.85
wasm_func_newFunction · 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

Tested by

no test coverage detected