Skip to content

Commit 953d917

Browse files
committed
Add JSB support for Methods with optional params
1 parent 1d7cc92 commit 953d917

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

CefSharp.Example/BoundObject.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public string Repeat(string str, int n)
3434
return result;
3535
}
3636

37+
public string EchoParamOrDefault(string param = "This is the default value")
38+
{
39+
return param;
40+
}
41+
3742
public void EchoVoid()
3843
{
3944
}

CefSharp/Internals/JavascriptMethod.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public class JavascriptMethod
3333
[DataMember]
3434
public string JavascriptName { get; set; }
3535

36+
/// <summary>
37+
/// Number of Params this function exepects
38+
/// </summary>
39+
public int ParameterCount { get; set; }
40+
3641
public override string ToString()
3742
{
3843
return ManagedName ?? base.ToString();

CefSharp/Internals/JavascriptObjectRepository.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ public bool TryCallMethod(long objectId, string name, object[] parameters, out o
6565

6666
try
6767
{
68+
// Do we have enough arguments? Add Type.Missing for any that we don't have incase of optional params
69+
var missingParams = method.ParameterCount - parameters.Length;
70+
if (missingParams > 0)
71+
{
72+
var paramList = new List<object>(parameters);
73+
74+
for (var i = 0; i < missingParams; i++)
75+
{
76+
paramList.Add(Type.Missing);
77+
}
78+
79+
parameters = paramList.ToArray();
80+
}
81+
6882
result = method.Function(obj.Value, parameters);
6983

7084
if(result != null && IsComplexType(result.GetType()))
@@ -216,6 +230,7 @@ private static JavascriptMethod CreateJavaScriptMethod(MethodInfo methodInfo)
216230
jsMethod.ManagedName = methodInfo.Name;
217231
jsMethod.JavascriptName = LowercaseFirst(methodInfo.Name);
218232
jsMethod.Function = methodInfo.Invoke;
233+
jsMethod.ParameterCount = methodInfo.GetParameters().Length;
219234

220235
return jsMethod;
221236
}

0 commit comments

Comments
 (0)