dotnet / corert

This repo contains CoreRT, an experimental .NET Core runtime optimized for AOT (ahead of time compilation) scenarios, with the accompanying compiler toolchain.
http://dot.net
MIT License
2.91k stars 508 forks source link

Embedding interface #768

Open BearishSun opened 8 years ago

BearishSun commented 8 years ago

Hello, this is not an issue, just a question. Do you plan on supporting an embedding interface like Mono does? (for example: http://docs.go-mono.com/?link=xhtml%3adeploy%2fmono-api-object.html). It allows C++ code easily call into C# and to C# to call into C++ (without automatic marshalling, which is important for performance reasons). It also allows a variety of others features - essentially the interface is very similar to Reflection on the C# side, where it allows you to query object types, create objects, enumerate and access methods, fields, properties, etc.

I am the creator of Banshee Engine (https://github.com/bearishsun/bansheeengine) and it uses the Mono embedding interface. However Mono is licensed under LGPL and this poses a problem if I want to use my engine on mobiles and consoles - they offer a commercial license for those situations but I would much rather be able to use a fully open source solution like CoreRT.

CoreCLR offers a very basic interface which basically just allows you to call methods from C# and the other way around, but doesn't really provide any information about how marshalling is done, nor does it appear to give you control over the GC on the C++ side (e.g. for keeping a reference in C++ code). It also doesn't support any of the complex operations I listed, which generally involves emulating them through Reflection and similar, which ends up being much more expensive than doing them through the runtime itself. And performance is of very high important for a game engine. (I might be wrong about CoreCLR, I have just look at it briefly).

Anyway, to repeat my question, do you think CoreRT will support something like Mono's embedding API? If so, to what extent? And if not, how hard would it be to add (I might be willing to do it myself if it possible and I'm pointed in the right direction)? If you do plan on implementing it I'm also available for any feedback as I'm intimately familiar with Mono's embedding interface.

jkotas commented 8 years ago

We plan to provide NativeCallable attribute. It allows creating extern "C" exports for managed methods. The NativeCallable attribute works for blittable types only. There is still GC mode transition for these methods, but no argument marshalling. Take a look at current uses of this attribute in the CoreRT repo.

We do not plan to allow manipulation of object references in unmanaged code to "just work". All object references will either need to be pinned or wrapped in GC handle before being passed to unmanaged code. Allowing object references in ummanaged code to "just work" puts a number of constrains on GC (performance, complexity, bug-prone) that we do not believe are good trade-offs. I believe that mono is moving in direction of having a clean seperation of running managed code vs. unmanaged code with introduction of cooperative GC mode. How does the cooperative GC mode work together with the embedding interface?

Also, we do plan open source MCG tool that allows customization of marshalling (e.g. it should allow automatic generation of the code for the pinning of object references or wrapping them in GC handles).

It should be possible to build a functional equivalent to mono embedding interface. If you would like to give it a try, it should be added as a new .dll, e.g. System.Private.Embedding. I believe we have enough in place to prototype simple methods like new_object or new_array.

Thank you for your interest in CoreRT and offering help!

cc @yizhang82 @manu-silicon

BearishSun commented 8 years ago

NativeCallable sounds great. It would be nice if you could also pass object references through it automatically, but it sounds like your marshalling tool will eventually take care of that.

I do not know how cooperative GC mode in mono works with embedding, I've yet to use it, but I don't remember seeing any special notes about it changing how embedding works, so I can assume it remains the same as so far, but I know little about the topic.

I'll dig deeper into your codebase and see what I can figure out. Thanks for your response!

proftom commented 8 years ago

Unintelligent person here. My scenario is I have a non-WinRT native legacy application and I want to write new business logic in C#, but not have to load the .net run time and use a COM interface to marshall data back and forth. Please correct me if I'm wrong, but NativeCallable will allow me to export my class library so they can be called from my native application as if it was a native dll (think LoadLibrary then GetProcAddress), providing I run it through your AOT compiler and I link MTR.dll? What's the catch? Is this only going to be for WinRT?

jkotas commented 8 years ago

Yes, NativeCallable will allow you to do this. The catch is that it will take a while until the CoreRT compiler and frameworks are complete and production quality (we do not have date yet).

cc @yizhang82

proftom commented 8 years ago

Neat. This is a big deal. Just to clarify, this _will _be for non-WinRT (e.g. 'normal' .NET class libraries)? Are you planning to do a WinRT release first ?

yizhang82 commented 8 years ago

@proftom NativeCallable is a general mechanism that allows managed code to be exposed to native code. It is not WinRT specific. The more feature-rich interop (like reverse pinvoke) is built on top of it.

realvictorprm commented 6 years ago

Did anyone already implemented the fun? If not, is this still on the roadmap / considered?

jkotas commented 6 years ago

NativeCallable attribute works fine if it is what you are asking about. There was discussion about some of the details on https://gitter.im/dotnet/corert last week.

xgalaxy commented 5 years ago

Any news on the MCG tool?