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

Function main

example/global.c:50–240  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48
49
50int main(int argc, const char* argv[]) {
51 // Initialize.
52 printf("Initializing...\n");
53 wasm_engine_t* engine = wasm_engine_new();
54 wasm_store_t* store = wasm_store_new(engine);
55
56 // Load binary.
57 printf("Loading binary...\n");
58 FILE* file = fopen("global.wasm", "rb");
59 if (!file) {
60 printf("> Error loading module!\n");
61 return 1;
62 }
63 fseek(file, 0L, SEEK_END);
64 size_t file_size = ftell(file);
65 fseek(file, 0L, SEEK_SET);
66 wasm_byte_vec_t binary;
67 wasm_byte_vec_new_uninitialized(&binary, file_size);
68 if (fread(binary.data, file_size, 1, file) != 1) {
69 printf("> Error loading module!\n");
70 return 1;
71 }
72 fclose(file);
73
74 // Compile.
75 printf("Compiling module...\n");
76 own wasm_module_t* module = wasm_module_new(store, &binary);
77 if (!module) {
78 printf("> Error compiling module!\n");
79 return 1;
80 }
81
82 wasm_byte_vec_delete(&binary);
83
84 // Create external globals.
85 printf("Creating globals...\n");
86 own wasm_globaltype_t* const_f32_type = wasm_globaltype_new(
87 wasm_valtype_new(WASM_F32), WASM_CONST);
88 own wasm_globaltype_t* const_i64_type = wasm_globaltype_new(
89 wasm_valtype_new(WASM_I64), WASM_CONST);
90 own wasm_globaltype_t* var_f32_type = wasm_globaltype_new(
91 wasm_valtype_new(WASM_F32), WASM_VAR);
92 own wasm_globaltype_t* var_i64_type = wasm_globaltype_new(
93 wasm_valtype_new(WASM_I64), WASM_VAR);
94
95 wasm_val_t val_f32_1 = WASM_F32_VAL(1);
96 own wasm_global_t* const_f32_import =
97 wasm_global_new(store, const_f32_type, &val_f32_1);
98 wasm_val_t val_i64_2 = WASM_I64_VAL(2);
99 own wasm_global_t* const_i64_import =
100 wasm_global_new(store, const_i64_type, &val_i64_2);
101 wasm_val_t val_f32_3 = WASM_F32_VAL(3);
102 own wasm_global_t* var_f32_import =
103 wasm_global_new(store, var_f32_type, &val_f32_3);
104 wasm_val_t val_i64_4 = WASM_I64_VAL(4);
105 own wasm_global_t* var_i64_import =
106 wasm_global_new(store, var_i64_type, &val_i64_4);
107

Callers

nothing calls this directly

Calls 14

wasm_engine_newFunction · 0.85
wasm_store_newFunction · 0.85
wasm_module_newFunction · 0.85
wasm_globaltype_newFunction · 0.85
wasm_valtype_newFunction · 0.85
wasm_global_newFunction · 0.85
wasm_global_as_externFunction · 0.85
wasm_instance_newFunction · 0.85
wasm_instance_exportsFunction · 0.85
wasm_global_setFunction · 0.85
wasm_func_callFunction · 0.85
get_export_globalFunction · 0.70

Tested by

no test coverage detected