MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / main

Function main

examples/hello.c:35–125  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

33}
34
35int main() {
36 int ret = 0;
37 // Set up our compilation context. Note that we could also work with a
38 // `wasm_config_t` here to configure what feature are enabled and various
39 // compilation settings.
40 printf("Initializing...\n");
41 wasm_engine_t *engine = wasm_engine_new();
42 assert(engine != NULL);
43
44 // With an engine we can create a *store* which is a long-lived group of wasm
45 // modules. Note that we allocate some custom data here to live in the store,
46 // but here we skip that and specify NULL.
47 wasmtime_store_t *store = wasmtime_store_new(engine, NULL, NULL);
48 assert(store != NULL);
49 wasmtime_context_t *context = wasmtime_store_context(store);
50
51 // Read our input file, which in this case is a wasm text file.
52 FILE *file = fopen("examples/hello.wat", "r");
53 assert(file != NULL);
54 fseek(file, 0L, SEEK_END);
55 size_t file_size = ftell(file);
56 fseek(file, 0L, SEEK_SET);
57 wasm_byte_vec_t wat;
58 wasm_byte_vec_new_uninitialized(&wat, file_size);
59 if (fread(wat.data, file_size, 1, file) != 1) {
60 printf("> Error loading module!\n");
61 return 1;
62 }
63 fclose(file);
64
65 // Parse the wat into the binary wasm format
66 wasm_byte_vec_t wasm;
67 wasmtime_error_t *error = wasmtime_wat2wasm(wat.data, wat.size, &wasm);
68 if (error != NULL)
69 exit_with_error("failed to parse wat", error, NULL);
70 wasm_byte_vec_delete(&wat);
71
72 // Now that we've got our binary webassembly we can compile our module.
73 printf("Compiling module...\n");
74 wasmtime_module_t *module = NULL;
75 error = wasmtime_module_new(engine, (uint8_t *)wasm.data, wasm.size, &module);
76 wasm_byte_vec_delete(&wasm);
77 if (error != NULL)
78 exit_with_error("failed to compile module", error, NULL);
79
80 // Next up we need to create the function that the wasm module imports. Here
81 // we'll be hooking up a thunk function to the `hello_callback` native
82 // function above. Note that we can assign custom data, but we just use NULL
83 // for now).
84 printf("Creating callback...\n");
85 wasm_functype_t *hello_ty = wasm_functype_new_0_0();
86 wasmtime_func_t hello;
87 wasmtime_func_new(context, hello_ty, hello_callback, NULL, NULL, &hello);
88 wasm_functype_delete(hello_ty);
89
90 // With our callback function we can now instantiate the compiled module,
91 // giving us an instance we can then execute exports from. Note that
92 // instantiation can trap due to execution of the `start` function, so we need

Callers

nothing calls this directly

Calls 11

wasm_engine_newFunction · 0.85
assertFunction · 0.85
wasmtime_store_newFunction · 0.85
wasmtime_store_contextFunction · 0.85
wasmtime_wat2wasmFunction · 0.85
wasmtime_module_newFunction · 0.85
wasmtime_func_newFunction · 0.85
wasmtime_instance_newFunction · 0.85
wasmtime_func_callFunction · 0.85
exit_with_errorFunction · 0.70

Tested by

no test coverage detected