Skip to content

Commit

Permalink
Split paramArray and missing arg logic out into two if statements. Pr…
Browse files Browse the repository at this point in the history
…eviously null was being replaced by Type.Missing.

Two seperate if statement branches allows for a method that has options params and a param array

Resolve cefsharp#1673
amaitland committed May 3, 2016
1 parent 5c20c7e commit 16df485
Showing 3 changed files with 35 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CefSharp.Example/BoundObject.cs
Original file line number Diff line number Diff line change
@@ -291,5 +291,10 @@ public string MethodWithoutAnything()
{
return "Method without anything called and returned successfully.";
}

public string MethodWithThreeParamsOneOptionalOneArray(string name, string optionalParam = null, params object[] args)
{
return "MethodWithThreeParamsOneOptionalOneArray:" + (name ?? "No Name Specified") + " - " + (optionalParam ?? "No Optional Param Specified") + ";Args:" + string.Join(", ", args.ToArray());
}
}
}
5 changes: 5 additions & 0 deletions CefSharp.Example/Resources/BindingTest.html
Original file line number Diff line number Diff line change
@@ -245,6 +245,11 @@
document.write(bound.methodWithParams('With no Params') + "<br/>");
document.write(bound.methodWithoutParams('Normal Method', 'hello') + "<br/>");
document.write(bound.methodWithoutAnything() + "<br/>");

document.write(bound.methodWithThreeParamsOneOptionalOneArray(null) + "<br/>");
document.write(bound.methodWithThreeParamsOneOptionalOneArray(null, null) + "<br/>");
document.write(bound.methodWithThreeParamsOneOptionalOneArray("Test", null) + "<br/>");
document.write(bound.methodWithThreeParamsOneOptionalOneArray(null, null, "Arg1", "Arg2") + "<br/>");
</script>
</p>
</body>
41 changes: 25 additions & 16 deletions CefSharp/Internals/JavascriptObjectRepository.cs
Original file line number Diff line number Diff line change
@@ -104,30 +104,21 @@ public bool TryCallMethod(long objectId, string name, object[] parameters, out o

try
{
//Added param array support #1644
var missingParams = method.ParameterCount - parameters.Length;
//CONDITION #1 : Check for parameter count missmatch between the parameters on the javascript function and the
// number of parameters on the bound object method. (This is relevant for methods that have default values)
//CONDITION #2 : Check if the bound object method contains a ParamArray as the last parameter on the method signature.
if (missingParams > 0 || method.HasParamArray)
//Check if the bound object method contains a ParamArray as the last parameter on the method signature.
//NOTE: No additional parameters are permitted after the params keyword in a method declaration,
//and only one params keyword is permitted in a method declaration.
//https://msdn.microsoft.com/en-AU/library/w5zay9db.aspx
if (method.HasParamArray)
{
var paramList = new List<object>(method.Parameters.Count);

//Loop through all of the method parameters on the bound object.
for (var i = 0; i < method.Parameters.Count; i++)
{
//Attempt to get the javascript function param at the current bound object parameter index.
//If the javascript function param is missing IE: NULL, then add Type.Missing.
//This will allow for default bound object parameters. IE: (string someParameter = "someValue")
var jsParam = parameters.ElementAtOrDefault(i);
if (jsParam == null && !method.Parameters[i].IsParamArray)
{
paramList.Add(Type.Missing);
}
//If the method parameter is a paramArray IE: (params Object[] someParameter)
//If the method parameter is a paramArray IE: (params string[] args)
//grab the parameters from the javascript function starting at the current bound object parameter index
//and add create an array that will be passed in as the last bound object method parameter.
else if (method.Parameters[i].IsParamArray)
if (method.Parameters[i].IsParamArray)
{
var convertedParams = new List<object>();
for (var s = i; s < parameters.Length; s++)
@@ -138,12 +129,30 @@ public bool TryCallMethod(long objectId, string name, object[] parameters, out o
}
else
{
var jsParam = parameters.ElementAtOrDefault(i);
paramList.Add(jsParam);
}
}

parameters = paramList.ToArray();
}

//Check for parameter count missmatch between the parameters on the javascript function and the
//number of parameters on the bound object method. (This is relevant for methods that have default values)
//NOTE it's possible to have default params and a paramArray, so check missing params last
var missingParams = method.ParameterCount - parameters.Length;

if(missingParams > 0)
{
var paramList = new List<object>(parameters);

for (var i = 0; i < missingParams; i++)
{
paramList.Add(Type.Missing);
}

parameters = paramList.ToArray();
}

try
{

0 comments on commit 16df485

Please sign in to comment.