How C#-Js binding is implemented?

Oct 15, 2014 at 2:09 PM
Hello,

First of all let me tell you that it's a great project.

I'm playing now with the Chromium Embedded Framework to create an application that contains both C# and HTML+JS code. I want to implement some kind of dependency injection, so that I could replace JS code with C#.

I want to get inspired by going trough and understanding your code, how you have implemented the seamless binding you provide, e.g. host objets, ... (it'd be a great starting point for me because Chromium uses V8 under the hood just like you do)

If you could give me advice where to look at in the code, where can I find detailed documentation, that'd be appreciated.

Thanks in advance
Balazs
Coordinator
Oct 16, 2014 at 1:03 PM
Hello Balazs,

Thanks for your kind words! Unfortunately there's no detailed documentation beyond the code itself.

ClearScript's V8 binding consists of several layers. The best way to understand it is to follow a code path from V8 to a target host object. Some tools you might find helpful are ClearScriptConsole, which provides a script command line with access to the core .NET class library, and the Visual Studio debugger, which lets you step through both native and managed code in the same process.

Here are some notes to get you started.

Interception is the key V8 mechanism that enables host object access from script code. A script talks to a host object through a native JavaScript proxy that invokes native ClearScript callbacks for all property access. You can find the callback methods in the V8ContextImpl class; e.g., GetHostObjectProperty(), SetHostObjectProperty(), etc.

If you set a breakpoint there, you can step through the code as it makes its way to the managed side and eventually ends up in HostItem, the managed wrapper for all exposed host resources. HostItem then fulfills the request using a variety of APIs such as reflection, the C# Runtime Binder, the .NET dynamic infrastructure, COM interop, etc. It has a lot of plumbing to handle a variety of binding scenarios.

Good luck, and please let us know if you have more questions!