This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmain.c
44 lines (34 loc) · 1.55 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <string.h>
#include <mono-wasi/driver.h>
#include <mono/metadata/assembly.h>
// This symbol's implementation is generated during the build
const char* dotnet_wasi_getentrypointassemblyname();
// These are generated by EmitWasmBundleObjectFile
const char* dotnet_wasi_getbundledfile(const char* name, int* out_length);
void dotnet_wasi_registerbundledassemblies();
// TODO: This should actually go in driver.c in the runtime
void mono_marshal_ilgen_init() {}
#ifdef WASI_AFTER_RUNTIME_LOADED_DECLARATIONS
// This is supplied from the MSBuild itemgroup @(WasiAfterRuntimeLoaded)
WASI_AFTER_RUNTIME_LOADED_DECLARATIONS
#endif
int main() {
dotnet_wasi_registerbundledassemblies();
mono_wasm_load_runtime("", 0);
#ifdef WASI_AFTER_RUNTIME_LOADED_CALLS
// This is supplied from the MSBuild itemgroup @(WasiAfterRuntimeLoaded)
WASI_AFTER_RUNTIME_LOADED_CALLS
#endif
// TODO: Consider passing through the args
MonoArray* args = mono_wasm_string_array_new(0);
void* entry_method_params[] = { args };
MonoAssembly* assembly = mono_assembly_open(dotnet_wasi_getentrypointassemblyname(), NULL);
MonoMethod* entry_method = mono_wasm_assembly_get_entry_point(assembly);
MonoObject* out_exc = NULL;
mono_wasm_invoke_method(entry_method, NULL, entry_method_params, &out_exc);
// Note: if the return value isn't an int (e.g., it's async main, returning a Task)
// then the following will result in an obscure error.
// TODO: Handle all entrypoint method types
// return mono_unbox_int(exit_code);
return out_exc ? 1 : 0;
}