3
3
#include <time.h>
4
4
5
5
#include "m3.h"
6
- #include "m3_host .h"
6
+ #include "m3_api_wasi .h"
7
7
#include "m3_env.h"
8
8
9
- #define FATAL (msg , ...) { printf("Fatal: " msg "\n", ##__VA_ARGS__); return 1 ; }
9
+ #define FATAL (msg , ...) { printf("Error: [Fatal] " msg "\n", ##__VA_ARGS__); goto _onfatal ; }
10
10
11
11
int main (int i_argc , const char * i_argv [])
12
12
{
@@ -31,32 +31,23 @@ int main (int i_argc, const char * i_argv [])
31
31
wasm = (u8 * ) malloc (fsize );
32
32
fread (wasm , 1 , fsize , f );
33
33
fclose (f );
34
+ } else {
35
+ FATAL ("cannot open file" );
34
36
}
35
37
36
38
IM3Module module ;
37
39
result = m3_ParseModule (& module , wasm , fsize );
38
40
if (result ) FATAL ("m3_ParseModule: %s" , result );
39
41
42
+ // TODO: Detect stack exhaustion
40
43
IM3Runtime env = m3_NewRuntime (4096 );
41
44
if (!env ) FATAL ("m3_NewRuntime" );
42
45
43
46
result = m3_LoadModule (env , module );
44
47
if (result ) FATAL ("m3_LoadModule: %s" , result );
45
48
46
- /*
47
- m3_LinkFunction (module, "_m3TestOut", "v(iFi)", (void *) m3TestOut);
48
- m3_LinkFunction (module, "_m3StdOut", "v(*)", (void *) m3Output);
49
- m3_LinkFunction (module, "_m3Export", "v(*i)", (void *) m3Export);
50
- m3_LinkFunction (module, "_m3Out_f64", "v(F)", (void *) m3Out_f64);
51
- m3_LinkFunction (module, "_m3Out_i32", "v(i)", (void *) m3Out_i32);
52
- m3_LinkFunction (module, "_TestReturn", "F(i)", (void *) TestReturn);
53
-
54
- m3_LinkFunction (module, "abortStackOverflow", "v(i)", (void *) m3_abort);
55
-
56
- result = m3_LinkCStd (module); if (result) FATAL("m3_LinkCStd: %s", result);
57
-
58
- m3_PrintRuntimeInfo (env);
59
- */
49
+ result = m3_LinkWASI (module );
50
+ if (result ) FATAL ("m3_LinkWASI: %s" , result );
60
51
61
52
IM3Function func ;
62
53
result = m3_FindFunction (& func , env , "__post_instantiate" );
@@ -78,6 +69,7 @@ int main (int i_argc, const char * i_argv [])
78
69
i_argc -= 2 ;
79
70
i_argv += 2 ;
80
71
result = m3_CallMain (func , i_argc , i_argv );
72
+ if (result ) FATAL ("m3_CallMain: %s" , result );
81
73
82
74
m3stack_t stack = (m3stack_t )(env -> stack );
83
75
return stack [0 ];
@@ -91,7 +83,15 @@ int main (int i_argc, const char * i_argv [])
91
83
92
84
free (wasm );
93
85
94
- if (result ) FATAL ("m3_Call: %s" , result );
86
+ _onfatal :
87
+ if (result ) {
88
+ printf ("Error: %s" , result );
89
+ if (env )
90
+ {
91
+ M3ErrorInfo info = m3_GetErrorInfo (env );
92
+ printf (" (%s)" , info .message );
93
+ }
94
+ }
95
95
96
96
printf ("\n" );
97
97
0 commit comments