How to expose C# Indexers to Javascript?

Aug 31, 2013 at 10:43 PM
Edited Aug 31, 2013 at 10:52 PM
I tried the following, but it returns NaN. If I replace the indexer with a method called GetItem(int index); it works.

Node.JS does something like this with v8, to implement their Buffer class - http://nodejs.org/api/buffer.html#buffer_buf_index
public class JsBuffer
{
    static int _i;

    [ScriptMember]
    public int this[int index]
    {
        get { return _i++; }
    }
}
class Program
{
    static void Main(string[] args)
    {
        RunJavascript();
        Console.WriteLine("Press any key to continue ...");
        Console.ReadLine();
    }

    static void RunJavascript()
    {
        var jse = new V8Engine();
        jse.WithContextScope = () =>
        {
            jse.GlobalObject.SetProperty(typeof(JsBuffer), "Buffer", false,
                V8PropertyAttributes.Locked);

            Console.WriteLine(Environment.NewLine + "Creating 'dump(obj)' function ...");
            jse.ConsoleExecute(@"dump = function(o) { var s=''; if (typeof(o)=='undefined')
                return 'undefined';
            for (var p in o) s+='* '+(o.valueOf())+'.'+p+' = ('+o[p]+')\r\n';
            return s; }");

            jse.ConsoleExecute("dump(this);");

// ***************************************************************************************
// I cannot access the JsBuffer indexer property from the following Javascript:
// ***************************************************************************************
            jse.ConsoleExecute(@"(function() {
var b = new Buffer();
var x = b[0];
var y = b[1];
var z = b[2];
return x + y + z;
})();");
        };
    }
}
C# Indexers - http://msdn.microsoft.com/en-us/library/vstudio/6x16t2tx.aspx
Coordinator
Aug 31, 2013 at 11:07 PM
Edited Aug 31, 2013 at 11:09 PM
Indexers are not yet supported, but I'll make sure it gets in the next release, which is coming soon! :) I'll post an update here once it's ready.
Sep 1, 2013 at 3:18 AM
Great, thank you!
Coordinator
Sep 1, 2013 at 10:22 PM
This feature now exists in the recent source code push. I have to finish off one other security based feature, then I'll be ready to create another binary release.
Sep 8, 2013 at 4:03 AM
I just looked through your commits for the past week and it looks like you're rockin' and rollin'. Great work! Looking forward to your next release. (Hope your're not working too hard and enjoying the work :)

One other thing - could you distribute the XML documentation files for the two major .NET libraries, in your binary release?

Thank You.
Coordinator
Sep 8, 2013 at 7:35 AM
Thanks! :) I do find it fun actually, especially testing .Net in JS with the console. ;)

I assume you mean to export the help comments to an XML file? I've never done it before, what method/tool do you recommend?
Sep 8, 2013 at 2:11 PM
Edited Sep 8, 2013 at 2:12 PM
Visual Studio does it during build. For the release build of your .NET projects, open Properties -> Build -> Output section (near bottom) and put a check next to "XML documentation file" and optionally point the path where the binaries end up.
Coordinator
Sep 8, 2013 at 4:00 PM
Ah ok - never needed to do that before. I figured there must be an easy way from VS but never looked yet (I'm a seasoned developer, but this is my first open source project). Good thing I comment religiously. ;)

Sep 8, 2013 at 4:28 PM
Edited Sep 8, 2013 at 4:29 PM
No doubt that you've got a lot of experience! Writing high quality, reusable components is tough enough, combining three languages and three different runtime environments into a SET of reusable components - the complexity is exponential - and it's looking great :) At work all of my internal reusable libs are usually in the same solution as the main project and you get "XML-comment intellisense tooltips" without having to export that file.

I did manage to get the main .NET projects to build without having to build the C++ projects, but I had to comment out a bunch of dll imports. Do you have any notes on how to setup the build environments for the C++ projects? For instance - what version or commit # of the V8 code do I have to get? Do I just need V8 or are there any other C++ dependencies? (I don't work with C++ very often.)
Coordinator
Sep 8, 2013 at 4:39 PM
Edited Sep 8, 2013 at 5:22 PM
In the native V8.NET-Proxy directory there is a V8 subdirectory with V8Update.cmd that will aid in downloading and compiling the V8 source. It's mentioned on the right side of the documents page. ;) Are you using Windows?

Tip: You can disable the native proxy projects just fine, you won't need it of you already have the binaries. :)
Sep 8, 2013 at 4:43 PM
Edited Sep 8, 2013 at 4:45 PM
Here is my simple, probably naive way to pull in dependency code bases and document the exact commit # that I'm using all in one file.

In my project repo, I make a sub-directory called /dep or whatever. Inside there I place one batch file per external repo that I want to pull in. Here is an example batch file for DockPanelSuite:
::
:: Clone a specific branch and reset to a specific known-working commit, so that we have a stable environment.
:: TODO: Ask the user what to do if the local repo clone directory exists.
::
git clone -b development_3.0 https://github.com/dockpanelsuite/dockpanelsuite.git DockPanelSuite
cd DockPanelSuite
git reset --hard abf7855eee138459332d6f457bf5f757182892bf
cd ..
As you can see, it clones the DockPanelSuite repo into /dep/DockPanelSuite and then resets that repo to a specific commit.

Then I put a .gitignore file at /dep/.gitignore which specifically ignores the DockPanelSuite/ sub-directory so all of that code doesn't get commited to my own projects git repo.
Sep 8, 2013 at 4:46 PM
Oh, sweet you already got it setup! I didn't see that there.
Coordinator
Sep 8, 2013 at 5:28 PM
I did forget to put a ".gitignore" in there though since the source control move - thanks for the reminder. ;)
Coordinator
Sep 8, 2013 at 6:24 PM
Ok, done. The XML docs are now included in the output. ;)
Coordinator
Sep 10, 2013 at 3:20 PM
FYI: New release out, which now includes indexer support. :)
Sep 10, 2013 at 5:01 PM
Looking forward to porting my project to this from the Noesis implementation of v8. I stopped working on it since Noesis didn't support indexers, so I'm really happy that you've got this implemented.

I think maybe some of your other projects might be very similar to what I'm working on (x-plat TypeScript driven run-time thing) - once I get my project finished a little more I'll show you what I've got.

Thank You.
Coordinator
Sep 10, 2013 at 5:13 PM
Sounds great. ;)