Skip to content

Commit 287751d

Browse files
committedNov 25, 2019
Initial WASI support wasm3#7
1 parent 37877ab commit 287751d

File tree

5 files changed

+1127
-28
lines changed

5 files changed

+1127
-28
lines changed
 

‎platforms/app/main.c

+17-17
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
#include <time.h>
44

55
#include "m3.h"
6-
#include "m3_host.h"
6+
#include "m3_api_wasi.h"
77
#include "m3_env.h"
88

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; }
1010

1111
int main (int i_argc, const char * i_argv [])
1212
{
@@ -31,32 +31,23 @@ int main (int i_argc, const char * i_argv [])
3131
wasm = (u8*) malloc(fsize);
3232
fread (wasm, 1, fsize, f);
3333
fclose (f);
34+
} else {
35+
FATAL("cannot open file");
3436
}
3537

3638
IM3Module module;
3739
result = m3_ParseModule (& module, wasm, fsize);
3840
if (result) FATAL("m3_ParseModule: %s", result);
3941

42+
// TODO: Detect stack exhaustion
4043
IM3Runtime env = m3_NewRuntime (4096);
4144
if (!env) FATAL("m3_NewRuntime");
4245

4346
result = m3_LoadModule (env, module);
4447
if (result) FATAL("m3_LoadModule: %s", result);
4548

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);
6051

6152
IM3Function func;
6253
result = m3_FindFunction (& func, env, "__post_instantiate");
@@ -78,6 +69,7 @@ int main (int i_argc, const char * i_argv [])
7869
i_argc -= 2;
7970
i_argv += 2;
8071
result = m3_CallMain (func, i_argc, i_argv);
72+
if (result) FATAL("m3_CallMain: %s", result);
8173

8274
m3stack_t stack = (m3stack_t)(env->stack);
8375
return stack[0];
@@ -91,7 +83,15 @@ int main (int i_argc, const char * i_argv [])
9183

9284
free (wasm);
9385

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+
}
9595

9696
printf ("\n");
9797

0 commit comments

Comments
 (0)
Please sign in to comment.