[WIP] Embed .NET (Core) in your D app

Jonathan Marler johnnymarler at gmail.com
Tue Feb 11 08:35:36 UTC 2020


On Tuesday, 11 February 2020 at 07:05:28 UTC, evilrat wrote:
> On Tuesday, 11 February 2020 at 05:41:21 UTC, Jonathan Marler 
> wrote:
>>
>> I started a project just over a month ago that fills the same 
>> role:
>>
>> https://github.com/marler8997/clrbridge
>>
>> I started with the dflat project found here 
>> (https://github.com/thewilsonator/dflat) and eventually 
>> changed the design to create this new one.  I'll have to take 
>> a look at your project and see if we can share/mutually 
>> benefit from our approaches.
>
> Yes, when I started it I was looking at dflat first, but 
> instantly rejected it because of complexity and extra steps 
> involved.

After spending some time working on dflat I came to the same 
conclusion.  Since coreclr only supports static functions, Dflat 
was using Cecil to generate wrapper assemblies in order to access 
non-static functions.  However, it's simpler to have just one 
library that can do this for any assembly rather than generating 
a new unique wrapper assembly for each one.

>
> Your project seems to aim to be a complete solution, it looks 
> far more robust but also much more complex.
>
> While my library is basically a .NET Core API wrapper over 5 
> native functions with extra 7 or so C# functions. No external 
> tools are required, no patching, no intermediate steps. Just 
> provide type declarations in D and that's it.

The code generation side adds complexity.  However, all the 
generated code ends up calling into a small simple library called 
ClrBridge.dll.

https://github.com/marler8997/clrbridge/blob/master/dotnetlib/ClrBridge.cs

It defines a handful static functions to load assemblies, get 
types, methods, call methods, marshal values, box values and 
create arrays.  The bare minimum you need to have access to the 
entire Clr if you're only able to call static methods through the 
coreclr library.

You can have access to the full CLR with this library without any 
code generation as well.  Here's an example that doesn't use any 
code generation:

https://github.com/marler8997/clrbridge/blob/master/dlang/noCodegenExample.d


> The most complex part is IL generator that weaves in custom 
> marshaling logic that avoids current CLR implementation 
> limitations (which may or may not be resolved in .NET 5), but 
> it also means delegates generated on demand, and the next 
> biggest chunk is huge D template to glue this together and hide 
> memory management details.

Interesting.  I haven't found a case yet where I've needed to 
generate IL code to marshal types.  I don't anticipate needing to 
either as I already have a design that should work for marshaling 
any type.  What types are you needing to generate IL to marshal 
for?


More information about the Digitalmars-d mailing list