e17429bdd2468806c83a8000b9f1067d27b073ad
[platform/framework/web/lwnode.git] /
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <inttypes.h>
5
6 #include "wasm.h"
7
8 #define own
9
10 // Print a Wasm value
11 void wasm_val_print(wasm_val_t val) {
12   switch (val.kind) {
13     case WASM_I32: {
14       printf("%" PRIu32, val.of.i32);
15     } break;
16     case WASM_I64: {
17       printf("%" PRIu64, val.of.i64);
18     } break;
19     case WASM_F32: {
20       printf("%f", val.of.f32);
21     } break;
22     case WASM_F64: {
23       printf("%g", val.of.f64);
24     } break;
25     case WASM_ANYREF:
26     case WASM_FUNCREF: {
27       if (val.of.ref == NULL) {
28         printf("null");
29       } else {
30         printf("ref(%p)", val.of.ref);
31       }
32     } break;
33   }
34 }
35
36 // A function to be called from Wasm code.
37 own wasm_trap_t* print_callback(
38   const wasm_val_t args[], wasm_val_t results[]
39 ) {
40   printf("Calling back...\n> ");
41   wasm_val_print(args[0]);
42   printf("\n");
43
44   wasm_val_copy(&results[0], &args[0]);
45   return NULL;
46 }
47
48
49 // A function closure.
50 own wasm_trap_t* closure_callback(
51   void* env, const wasm_val_t args[], wasm_val_t results[]
52 ) {
53   int i = *(int*)env;
54   printf("Calling back closure...\n");
55   printf("> %d\n", i);
56
57   results[0].kind = WASM_I32;
58   results[0].of.i32 = (int32_t)i;
59   return NULL;
60 }
61
62
63 int main(int argc, const char* argv[]) {
64   // Initialize.
65   printf("Initializing...\n");
66   wasm_engine_t* engine = wasm_engine_new();
67   wasm_store_t* store = wasm_store_new(engine);
68
69   // Load binary.
70   printf("Loading binary...\n");
71   FILE* file = fopen("callback.wasm", "r");
72   if (!file) {
73     printf("> Error loading module!\n");
74     return 1;
75   }
76   fseek(file, 0L, SEEK_END);
77   size_t file_size = ftell(file);
78   fseek(file, 0L, SEEK_SET);
79   wasm_byte_vec_t binary;
80   wasm_byte_vec_new_uninitialized(&binary, file_size);
81   if (fread(binary.data, file_size, 1, file) != 1) {
82     printf("> Error loading module!\n");
83     return 1;
84   }
85   fclose(file);
86
87   // Compile.
88   printf("Compiling module...\n");
89   own wasm_module_t* module = wasm_module_new(store, &binary);
90   if (!module) {
91     printf("> Error compiling module!\n");
92     return 1;
93   }
94
95   wasm_byte_vec_delete(&binary);
96
97   // Create external print functions.
98   printf("Creating callback...\n");
99   own wasm_functype_t* print_type = wasm_functype_new_1_1(wasm_valtype_new_i32(), wasm_valtype_new_i32());
100   own wasm_func_t* print_func = wasm_func_new(store, print_type, print_callback);
101
102   int i = 42;
103   own wasm_functype_t* closure_type = wasm_functype_new_0_1(wasm_valtype_new_i32());
104   own wasm_func_t* closure_func = wasm_func_new_with_env(store, closure_type, closure_callback, &i, NULL);
105
106   wasm_functype_delete(print_type);
107   wasm_functype_delete(closure_type);
108
109   // Instantiate.
110   printf("Instantiating module...\n");
111   const wasm_extern_t* imports[] = {
112     wasm_func_as_extern(print_func), wasm_func_as_extern(closure_func)
113   };
114   own wasm_instance_t* instance =
115     wasm_instance_new(store, module, imports, NULL);
116   if (!instance) {
117     printf("> Error instantiating module!\n");
118     return 1;
119   }
120
121   wasm_func_delete(print_func);
122   wasm_func_delete(closure_func);
123
124   // Extract export.
125   printf("Extracting export...\n");
126   own wasm_extern_vec_t exports;
127   wasm_instance_exports(instance, &exports);
128   if (exports.size == 0) {
129     printf("> Error accessing exports!\n");
130     return 1;
131   }
132   const wasm_func_t* run_func = wasm_extern_as_func(exports.data[0]);
133   if (run_func == NULL) {
134     printf("> Error accessing export!\n");
135     return 1;
136   }
137
138   wasm_module_delete(module);
139   wasm_instance_delete(instance);
140
141   // Call.
142   printf("Calling export...\n");
143   wasm_val_t args[2];
144   args[0].kind = WASM_I32;
145   args[0].of.i32 = 3;
146   args[1].kind = WASM_I32;
147   args[1].of.i32 = 4;
148   wasm_val_t results[1];
149   if (wasm_func_call(run_func, args, results)) {
150     printf("> Error calling function!\n");
151     return 1;
152   }
153
154   wasm_extern_vec_delete(&exports);
155
156   // Print result.
157   printf("Printing result...\n");
158   printf("> %u\n", results[0].of.i32);
159
160   // Shut down.
161   printf("Shutting down...\n");
162   wasm_store_delete(store);
163   wasm_engine_delete(engine);
164
165   // All done.
166   printf("Done.\n");
167   return 0;
168 }