SteveSandersonMS / dotnet-wasi-sdk

Packages for building .NET projects as standalone WASI-compliant modules
518 stars 36 forks source link

How does dotnet-wasi-sdk compile C# to WebAssembly? #22

Closed hannesd closed 2 years ago

hannesd commented 2 years ago

Hi,

I'm trying to find a performant and mature way to build a WebAssembly from C# source. I have found lots of projects on the Web, and for most I understand the compilation pipeline. E.g. NativeAOT compiles the C# MSIL using RyuJit, which somehow can produce LLVM IL which is then compiled to WASM by Emscripten. But for dotnet-wasi-sdk I'm clueless.

I've browsed through this repo's sources, but being new to C#, could not really figure out what's going on. I saw mono being mentioned here and there and the wasi-sdk. I assume mono provides the native runtime and wasi-sdk produces the WASM. But as far as I can tell, wasi-sdk only accepts C(++) code, not C#?

It would be great if someone could provide me with some pointers.

marinasundstrom commented 2 years ago

@hannesd I had to check that too. To me it seems like they are compiling the runtime to WASM (there is an entrypoint using Monos API, as you alluded to) and then just bundling the DLLs in the resulting file.

I don't see how ASP.NET Core would work without reflection. So it probably does not compile the IL of the application.

SteveSandersonMS commented 2 years ago

To me it seems like they are compiling the runtime to WASM (there is an entrypoint using Monos API, as you alluded to) and then just bundling the DLLs in the resulting file.

Exactly correct

I don't see how ASP.NET Core would work without reflection. So it probably does not compile the IL of the application.

Reflection still works even with the AOT compilation we have for Blazor WebAssembly. When AOT is integrated here, Reflection should still work here too. In this case, all the IL would be converted to wasm bytecode, but the bundle still contains enough metadata describing the .NET type system that reflection continues to work.