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

Function run

example/threads.c:29–97  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27} thread_args;
28
29void* run(void* args_abs) {
30 thread_args* args = (thread_args*)args_abs;
31
32 // Recreate store and module.
33 own wasm_store_t* store = wasm_store_new(args->engine);
34 own wasm_module_t* module = wasm_module_obtain(store, args->module);
35
36 // Run the example N times.
37 for (int i = 0; i < N_REPS; ++i) {
38 usleep(100000);
39
40 // Create imports.
41 own wasm_functype_t* func_type = wasm_functype_new_1_0(wasm_valtype_new_i32());
42 own wasm_func_t* func = wasm_func_new(store, func_type, callback);
43 wasm_functype_delete(func_type);
44
45 wasm_val_t val = WASM_I32_VAL((int32_t)args->id);
46 own wasm_globaltype_t* global_type =
47 wasm_globaltype_new(wasm_valtype_new_i32(), WASM_CONST);
48 own wasm_global_t* global = wasm_global_new(store, global_type, &val);
49 wasm_globaltype_delete(global_type);
50
51 // Instantiate.
52 wasm_extern_t* externs[] = {
53 wasm_func_as_extern(func), wasm_global_as_extern(global),
54 };
55 wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
56 own wasm_instance_t* instance =
57 wasm_instance_new(store, module, &imports, NULL);
58 if (!instance) {
59 printf("> Error instantiating module!\n");
60 return NULL;
61 }
62
63 wasm_func_delete(func);
64 wasm_global_delete(global);
65
66 // Extract export.
67 own wasm_extern_vec_t exports;
68 wasm_instance_exports(instance, &exports);
69 if (exports.size == 0) {
70 printf("> Error accessing exports!\n");
71 return NULL;
72 }
73 const wasm_func_t *run_func = wasm_extern_as_func(exports.data[0]);
74 if (run_func == NULL) {
75 printf("> Error accessing export!\n");
76 return NULL;
77 }
78
79 wasm_instance_delete(instance);
80
81 // Call.
82 wasm_val_vec_t empty = WASM_EMPTY_VEC;
83 if (wasm_func_call(run_func, &empty, &empty)) {
84 printf("> Error calling function!\n");
85 return NULL;
86 }

Callers

nothing calls this directly

Calls 11

wasm_store_newFunction · 0.85
wasm_module_obtainFunction · 0.85
wasm_func_newFunction · 0.85
wasm_globaltype_newFunction · 0.85
wasm_global_newFunction · 0.85
wasm_func_as_externFunction · 0.85
wasm_global_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