This project is read-only.

How to pass V8Engine to the managed function?

Mar 21, 2014 at 4:01 PM
Edited Mar 21, 2014 at 4:05 PM
Hi,

I am trying to make the following code in JavaScript:
myFunc(42);
to invoke the following managed member:
void MyFunc(int param, V8Engine engine) { ... }
According to this code in V8Engine_Binding.cs:
else if (ExpectedType == typeof(V8Engine))
{
    Value = ArgInfoSource.Engine;
    Type = typeof(V8Engine);
}
this should work. However, due to this code in the same file:
for (var i = 0; i < length; i++)
{
    argInfoItems[i] = new ArgInfo(
        i < handlesLength ? handles[handlesOffset + i] : InternalHandle.Empty,
        expectedParameters != null ? expectedParameters[i] : null);
}
InternalHandle.Empty is passed for the second argument, which has it's engine set to null. Is this a bug of a feature?
Mar 21, 2014 at 10:34 PM
Edited Mar 21, 2014 at 10:35 PM
You are not passing anything in that parameter, so it's undefined/null. That code simply converts a parameter value to an engine value by pulling out the associated engine reference when an engine type is required. Normally, you just have to pass SOMETHING, and I recommend to create a variable called "Engine" set to the global "this" reference (value doesn't actually matter) and use that instead. So you could type "myFunc(42, Engine);".

Hope that helps! :)
Mar 24, 2014 at 9:14 PM
It sounds like a workaround that I have to pass an arbitrary value just to make V8 pass the V8Engine in it's place. However, it's your design and certainly you know better :-).
Mar 24, 2014 at 10:43 PM
Edited Mar 24, 2014 at 10:44 PM
Actually, the original idea was to convert, best as possible, all supplied arguments (not add any missing). That is just naturally how it ended up; although converting 'null' to 'V8Engine' does seem odd. ;) However, internally, all values (even null) know the engine value. Consider this:
void MyFunc(int param1, string param2, V8Engine engine1, string param4, V8Engine engine2) { ... }
If 3 arguments are supplied, should I apply them only to param1, param2, and param4, and skip the others? There's no restriction on the parameter types. I suppose I could have checked if the first argument expected is an engine type (allowing it only as the first parameter), put it in, then append the other arguments, but that would force people to expect it as the first parameter (you have it as the second), and add a performance hit having to prepend to the beginning of an argument array. In the end, it was just simpler to pop it in quickly the way it is; but it does make more sense to just expect it as the first parameter. Perhaps I'll end up detecting and inserting it anyhow - it does make more sense. :)