I gave some screenshots and a description in an earlier post of what I called “generic object embedding”. I’ve since forked this functionality into a “development” codebase because this aspect of spidermonkey-dotnet is still volatile. Release 0.2.0 [codeplex.com] is available, which also includes a javascript shell implemented with this new technique.

Now let’s take a look at our friend the Customer object refactored and with a few attributes added to the methods we want to embed:


public class Customer
{
    private int m_age;
    private string m_name;

    [JSMember()]
    public int Age
    {
        get { return m_age; }
        set { m_age = value; }
    }

    [JSMember()]
    public string Name
    {
        get { return m_name; }
        set { m_name = value; }
    }

    [JSMember()]
    public void SetName(string newName)
    {
        m_name = newName;
    }

    [JSMember()]
    public int ComputeReduction()
    {
        if (m_age < 25)
            return 10;
        else
            return 5;
    }
}

Now the only code needed to embed this object is the following line:

JSEmbedder.JSObjectInitialize(cxPtr, globPtr, IntPtr.Zero, typeof(Customer), true);

Here’s a screenshot of the shell enabled with the Customer object:

Advertisement