Search code examples
c#javascriptc++-clifirebreath

C# classes exposed from javascript through Firebreath Framework


I need to create the classes in C# and call that classes from javascript through C++CLI and Firebreath Framework .. create the complex hierarchy class structure and expose it from javacsript

The flow should be :

Javascript <-- C++(FireBreath)<-- C#

C#-->C++(Firebreath)-->Javascript

I have to create the generalized solution for this problem.

Then how should i implement this? If you have any solution,sort of information ,ways to solve this problem then please let me know..

Suppose my Class Library in C# which includes the classes like :

public class TestImage
{
}

public class DrawImage
{ 
 public void ShowImage(TestImage testImage)
 {
 }
}

Here I need to call ShowImage(TestImage testImage) method from JavaScript page of Firebreath Framewaork. I already created the wrapper but I dont have an idea to expose the class object as argument to the method like the above ShowImage () in tjhe JavaScript page of the Fireabreath Framework.

If you have any idea related to this please let me know.


Solution

  • When you say "generalized solution"... do you mean a tool or process that automates this?

    I believe this is possible. Here's how I would do it:

    I'm assuming you have gotten started with FireBreath and have some understanding of it. I'm glossing over countless problems you'll run into integrating all this within a FireBreath solution; that would take days! So this is just architectural advice. I'm sorry to omit so many details.

    I would write a tool that dynamically loads your .NET assembly or assemblies, and uses reflection to traverse the 'complex hierarchy class structure'. This tool would generate two things: A C++/CLI wrapper for your .NET library, and a set of native C++ FireBreath classes that bind from Javascript to that C++/CLI wrapper.

    The C++/CLI wrapper (see enter link description here) makes your .NET library callable from the native C++ of FireBreath. Actually, here is a tool on CodePlex that claims to generate such a wrapper.

    The Javascript adapter is a set of .cpp modules (probably one for each of your library/C++/CLI classes). Each of these is a C++ class derived from FB::JSAPIAuto, which allows these classes to be instantiated as Javascript objects. Within the constructor for each of these classes, the automated tool inserts code to create the object's Javascript API. Code that looks like this:

      registerMethod("Start", make_method(this, &thisClass::Start));
      registerMethod("Abort", make_method(this, &thisClass::Abort));
      registerProperty("Size", make_property(this, &thisClass::get_Size,&thisClass::set_Size));
    

    The automated tool must synthesize these methods of the class, like thisClass::Start and thisClass::set_Size. Their parameters and return types are the Javascript-compatible types supported by FireBreath - like int and double and bool, but also std::string, FB::VariantMap and FB::VariantList. In the body of each such method, the tool generates code to call the corresponding C++/CLI wrapper API, doing any necessary conversion between parameters and returns.

    I suppose that each FB::JSAPIAuto-derived class inherits from, has as a member, or holds a pointer to, the C++/CLI class/object it represents.

    As a FireBreath project, your .NET library is ultimately represented by a GUID - this is how Javascript finds its way into your library, by creating a root object from that GUID. It then calls methods or reads properties of that object to get other objects, and so on to access your entire library API.

    I suppose there will be some issues mapping between Javascript and C#. You will have to study the Javascript parameter and return types supported by FireBreath, and limit your C# API accordingly. Probably key is figuring out how Javascript objects and arrays are represented as they cross the C++/CLI layer.