Variable argument length from JS to CLR

Dec 29, 2009 at 5:29 PM
Edited Dec 29, 2009 at 5:31 PM

I've been having the need to be able to call a .NET function with a variable amount of arguments from Javascript, like this:

   foo("bar", "baz");

Where foo is some function assigned through JintEngine.SetFunction(name, delegate), the fix I came up with - which probably won't work everywhere was to expand the clrParameters object array in ClrFunction.Execute() in Native/ClrFunction.cs to always hold the amount of parameters the delegate has, no matter how many parameters was passed from javascript.

So I changed line #23 in Native/ClrFunction.cs to look like this:

   int clrParameterCount = Delegate.Method.GetParameters().Length;
   object[] clrParameters = new object[clrParameterCount];

Instead of the old one that used parameters.Length, what this will do is call the delegate - all the arguments you don't specify any parameters for will be null.

Dec 29, 2009 at 5:48 PM

Good one. We'll try to implement it. If it works it will be in the next release.

Thanks !