RyanLamansky / dotnet-webassembly

Create, read, modify, write and execute WebAssembly (WASM) files from .NET-based applications.
Apache License 2.0
789 stars 74 forks source link

Consider saving DLLs with Mono.Cecil #25

Open michaelkruglos opened 4 years ago

michaelkruglos commented 4 years ago

Have you considered Mono.Cecil for assembly generation? It can write DLLs, which can later be used with .net core. If you copy the <appname>.runtimeconfig.json from any existing .net core app as <dllname>.runtimeconfig.json, you can run that dll with dotnet <dllname>.dll. There's no point in waiting for Microsoft to implement it. They mentioned it not being a must have for NET 5. In the mean-time Mono.Cecil does an excellent job and comes with the ability to optimize methods.

Examples:

RyanLamansky commented 4 years ago

I won't personally do it but I'm planning to add extension points to the compilation process to enable this scenario. There's definitely interest, and I suspect someone might take this on.

My most pressing concern right now is to be able to compile complex real-world WASMs--being able to write DLLs is of no help to anyone if they don't work.

kekekeks commented 3 years ago

Note that Mono.Cecil has several huge API differences, especially when it comes to resolving .NET Types. In XamlX compiler we have some kind of an abstraction for SRE and Mono.Cecil APIs that allows to access .NET type system and emit MSIL in a unified way, a full example that a emits a class can be found here.

It also comes with a sanity checking IL emitter proxy that checks basic things like stack balance at label instructions, unmarked labels, etc.

It's possible to extract said abstraction to a separate library if that would help.

yself commented 3 years ago

I have done some experiments with saving the generated assembly using Lokad.ILPack which does some magic to save the System.Reflection.Emit assemblies even on .NET Core, where the feature is directly missing. Should be as simple as follwing, but definitely needs more testing.

var gen = new Lokad.ILPack.AssemblyGenerator();
gen.GenerateAssembly(module.Assembly, "generated.dll");