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

Function main

example/memory.c:96–221  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

94
95
96int main(int argc, const char* argv[]) {
97 // Initialize.
98 printf("Initializing...\n");
99 wasm_engine_t* engine = wasm_engine_new();
100 wasm_store_t* store = wasm_store_new(engine);
101
102 // Load binary.
103 printf("Loading binary...\n");
104 FILE* file = fopen("memory.wasm", "rb");
105 if (!file) {
106 printf("> Error loading module!\n");
107 return 1;
108 }
109 fseek(file, 0L, SEEK_END);
110 size_t file_size = ftell(file);
111 fseek(file, 0L, SEEK_SET);
112 wasm_byte_vec_t binary;
113 wasm_byte_vec_new_uninitialized(&binary, file_size);
114 if (fread(binary.data, file_size, 1, file) != 1) {
115 printf("> Error loading module!\n");
116 return 1;
117 }
118 fclose(file);
119
120 // Compile.
121 printf("Compiling module...\n");
122 own wasm_module_t* module = wasm_module_new(store, &binary);
123 if (!module) {
124 printf("> Error compiling module!\n");
125 return 1;
126 }
127
128 wasm_byte_vec_delete(&binary);
129
130 // Instantiate.
131 printf("Instantiating module...\n");
132 wasm_extern_vec_t imports = WASM_EMPTY_VEC;
133 own wasm_instance_t* instance =
134 wasm_instance_new(store, module, &imports, NULL);
135 if (!instance) {
136 printf("> Error instantiating module!\n");
137 return 1;
138 }
139
140 // Extract export.
141 printf("Extracting exports...\n");
142 own wasm_extern_vec_t exports;
143 wasm_instance_exports(instance, &exports);
144 size_t i = 0;
145 wasm_memory_t* memory = get_export_memory(&exports, i++);
146 wasm_func_t* size_func = get_export_func(&exports, i++);
147 wasm_func_t* load_func = get_export_func(&exports, i++);
148 wasm_func_t* store_func = get_export_func(&exports, i++);
149
150 wasm_module_delete(module);
151
152 // Try cloning.
153 own wasm_memory_t* copy = wasm_memory_copy(memory);

Callers

nothing calls this directly

Calls 15

wasm_engine_newFunction · 0.85
wasm_store_newFunction · 0.85
wasm_module_newFunction · 0.85
wasm_instance_newFunction · 0.85
wasm_instance_exportsFunction · 0.85
wasm_memory_sizeFunction · 0.85
wasm_memory_data_sizeFunction · 0.85
wasm_memory_dataFunction · 0.85
check_call0Function · 0.85
check_call1Function · 0.85
check_trap1Function · 0.85
check_ok2Function · 0.85

Tested by

no test coverage detected