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

Function main

example/serialize.c:20–127  ·  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("serialize.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 // Compile.
45 printf("Compiling module...\n");
46 own wasm_module_t* module = wasm_module_new(store, &binary);
47 if (!module) {
48 printf("> Error compiling module!\n");
49 return 1;
50 }
51
52 wasm_byte_vec_delete(&binary);
53
54 // Serialize module.
55 printf("Serializing module...\n");
56 own wasm_byte_vec_t serialized;
57 wasm_module_serialize(module, &serialized);
58
59 wasm_module_delete(module);
60
61 // Deserialize module.
62 printf("Deserializing module...\n");
63 own wasm_module_t* deserialized = wasm_module_deserialize(store, &serialized);
64 if (!deserialized) {
65 printf("> Error deserializing module!\n");
66 return 1;
67 }
68
69 wasm_byte_vec_delete(&serialized);
70
71 // Create external print functions.
72 printf("Creating callback...\n");
73 own wasm_functype_t* hello_type = wasm_functype_new_0_0();
74 own wasm_func_t* hello_func =
75 wasm_func_new(store, hello_type, hello_callback);
76
77 wasm_functype_delete(hello_type);

Callers

nothing calls this directly

Calls 11

wasm_engine_newFunction · 0.85
wasm_store_newFunction · 0.85
wasm_module_newFunction · 0.85
wasm_module_serializeFunction · 0.85
wasm_module_deserializeFunction · 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