Skip to content

Commit

Permalink
Change new paramAray test methods in BindingTest.html to execute imme…
Browse files Browse the repository at this point in the history
…diately (same as all the majority)

Revert changes to NuGet.config
Remove unused using statement from ScriptedMethodsBoundObject
Rename JavascriptMethod.Parameters to just JavascriptMethod.Parameters (it's in the context of the method so naming was redundant)
Pre compute HasParamArray for minor (very minor) performance improvement - relevant when executing method many times in a tight loop
  • Loading branch information
amaitland committed Apr 5, 2016
1 parent 7cf5f17 commit 669404f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 44 deletions.
6 changes: 3 additions & 3 deletions CefSharp.Example/BoundObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,15 @@ public SubBoundObject GetSubObject()
/// <param name="args">Params Argument</param>
public string MethodWithParams(string name, params object[] args)
{
return String.Join(", ", args.ToArray());
return "Name:" + name + ";Args:" + string.Join(", ", args.ToArray());
}

public string MethodWithoutParams(string name, string arg2)
{
return String.Format("{0}, {1}", name, arg2);
return string.Format("{0}, {1}", name, arg2);
}

public string methodWithoutAnything()
public string MethodWithoutAnything()
{
return "Method without anything called and returned successfully.";
}
Expand Down
29 changes: 5 additions & 24 deletions CefSharp.Example/Resources/BindingTest.html
Original file line number Diff line number Diff line change
Expand Up @@ -240,31 +240,12 @@
<p>
Javascript function sending variable number of parameters to Bound Object. (ParamArray Test)
<script type="text/javascript">
function invokeParamArrayMethod(func) {
var result = func();
document.getElementById('paramArrayResults').innerHTML = result;
}
document.write(bound.methodWithParams('With 1 Params', 'hello-world') + "<br/>");
document.write(bound.methodWithParams('With 2 Params', 'hello-world', 'chris was here') + "<br/>");
document.write(bound.methodWithParams('With no Params') + "<br/>");
document.write(bound.methodWithoutParams('Normal Method', 'hello') + "<br/>");
document.write(bound.methodWithoutAnything() + "<br/>");
</script>
<ul>
<li style="list-style:none;display:inline-block;">
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithParams('test', 'hello-world')})">With 1 Params</button>
</li>
<li style="list-style:none;display:inline-block;">
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithParams('test', 'hello-world', 'chris was here')})">With 2 Params</button>
</li>
<li style="list-style:none;display:inline-block;">
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithParams('test')})">With no Params</button>
</li>
<li style="list-style:none;display:inline-block;">
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithoutParams('test', 'hello')})">Normal Method</button>
</li>
<li style="list-style:none;display:inline-block;">
<button onclick="invokeParamArrayMethod(function(){return bound.methodWithoutAnything()})">Normal Method No Params</button>
</li>
</ul>
<span>ParamArray Results</span>
<br />
<span id="paramArrayResults"></span>
</p>
</body>
</html>
1 change: 0 additions & 1 deletion CefSharp.Example/ScriptedMethodsBoundObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Linq;

namespace CefSharp.Example
{
Expand Down
7 changes: 6 additions & 1 deletion CefSharp/Internals/JavascriptMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public class JavascriptMethod
[DataMember]
public string JavascriptName { get; set; }

public List<MethodParameter> MethodParameters { get; set; }
/// <summary>
/// Params this method expects
/// </summary>
public List<MethodParameter> Parameters { get; set; }

public bool HasParamArray { get;set; }

/// <summary>
/// Number of Params this function exepects
Expand Down
23 changes: 12 additions & 11 deletions CefSharp/Internals/JavascriptObjectRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,32 +104,32 @@ public bool TryCallMethod(long objectId, string name, object[] parameters, out o

try
{
//Added param array support. Current implementation support param Object only. 04/04/2016
//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.
// 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.MethodParameters.LastOrDefault(t => t.IsParamArray) != null)
if (missingParams > 0 || method.HasParamArray)
{
var paramList = new List<object>(method.MethodParameters.Count);
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.MethodParameters.Count; i++)
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")
object jsParam = parameters.ElementAtOrDefault(i);
if (jsParam == null && !method.MethodParameters[i].IsParamArray)
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)
//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.MethodParameters[i].IsParamArray)
else if (method.Parameters[i].IsParamArray)
{
List<object> convertedParams = new List<object>();
var convertedParams = new List<object>();
for (var s = i; s < parameters.Length; s++)
{
convertedParams.Add(parameters[s]);
Expand Down Expand Up @@ -314,12 +314,13 @@ private static JavascriptMethod CreateJavaScriptMethod(MethodInfo methodInfo, bo
jsMethod.JavascriptName = GetJavascriptName(methodInfo.Name, camelCaseJavascriptNames);
jsMethod.Function = methodInfo.Invoke;
jsMethod.ParameterCount = methodInfo.GetParameters().Length;
jsMethod.MethodParameters = methodInfo.GetParameters()
jsMethod.Parameters = methodInfo.GetParameters()
.Select(t => new MethodParameter()
{
IsParamArray = t.GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0
}).ToList();

//Pre compute HasParamArray for a very minor performance gain
jsMethod.HasParamArray = jsMethod.Parameters.LastOrDefault(t => t.IsParamArray) != null;

return jsMethod;
}
Expand Down
4 changes: 0 additions & 4 deletions NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@
</activePackageSource>
<packageSources>
<add key="cefsharp-myget" value="https://www.myget.org/F/cefsharp/" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
<disabledPackageSources>
<add key="nuget.org" value="true" />
</disabledPackageSources>
</configuration>

0 comments on commit 669404f

Please sign in to comment.