This project is read-only.

Wrapping .NET Types

Dec 18, 2013 at 11:44 AM
I am just starting to experiment with V8.NET (I am also new to V8 in general) with the aim of eventually integrating it in to an existing product as a scripting and automation engine.

I am attempting to register a .NET type with the engine to allow it to be created from the script, I have tried to use the example you have provided in a previous discussion ( - the second post, your first "method") but I am unable to get it to work.

Since I am using existing types I would prefer not to modify their inheritance hierarchy by adding V8NativeObject or V8ManagedObject if at all possible.

My code looks like:
private void InitEngine()
    _engine = new V8Engine();
    var printFunc = _engine.CreateFunctionTemplate().GetFunctionObject(Print);
    _engine.GlobalObject.SetProperty("print", printFunc, V8PropertyAttributes.Locked);

    _engine.RegisterType(typeof(DataFrame), null, true, ScriptMemberSecurity.Permanent);
    _engine.GlobalObject.SetProperty(typeof(DataFrame), V8PropertyAttributes.Locked);

I then use:
string typeTest = "var dataFrame = new DataFrame();return dataFrame;";
V8.Net.Handle script = _engine.Execute(typeTest);
    Print(string.Format("Result: {0}", script.AsString));
to test the function.
The exception that occurs states that:
Error: Failed to invoke constructor: => Message: Unable to cast object of type 'Js.Net.DataFrame' to type 'V8.Net.IV8NativeObject'.

=> Stack Trace: at V8.Net.ObjectBinder.Initialize(Boolean isConstructCall, InternalHandle[] args) in c:\Data\Visual Studio\Projects\V8.NET\Source\V8.Net\V8Engine_Binding.cs:line 1747
at V8.Net.TypeBinder.<>c__DisplayClass4e.<_BindTypeMembers>b__4d(V8Engine engine, Boolean isConstructCall, InternalHandle _this, InternalHandle[] args) in c:\Data\Visual Studio\Projects\V8.NET\Source\V8.Net\V8Engine_Binding.cs:line 1521
Line: 1 Column: 16
I would assume that I am simply doing something wrong because of my ignorance, however I am at a bit of a loss as to what I can try? As I said, I would ideally like to be able to work with a number of different types that already exist in our product without having to modify them. Obviously if I have to then I will do, but I assumed I wouldn't need to after reading your post.

This is using the latest code in the development branch by the way.

Dec 18, 2013 at 1:16 PM
Looks correct, can you zip and send the whole example? I'll take a closer look.
Dec 18, 2013 at 1:26 PM

Thanks for the quick response James – I would do but I can’t seem to find your e-mail address anywhere!

I have uploaded the project here:

For the sake of clarity I am using the binaries included in Git revision c8ea5c3a (development branch).



Dec 19, 2013 at 2:54 AM
Edited Dec 19, 2013 at 2:39 PM
Ok, there was a bug requiring you to implement the IV8NativeObject interface, but I fixed it. The dev branch has been updated, including the binaries.

Another point to note: You cannot use "return" to return anything from the global script. You must do this:
string typeTest = "var dataFrame = new DataFrame(); dataFrame;";
Hope that helps. :)
Dec 20, 2013 at 8:24 AM
Thanks for the quick turnaround James, I'm just checking out the updated code so I'll let you know if that fixes it.
Hopefully I will start to familiarise myself with the codebase and what exactly is going on under the covers so that I can submit meaningful pull requests in future rather than just "it doesn't work"! :)