v8dotnet icon indicating copy to clipboard operation
v8dotnet copied to clipboard

params keyword not working

Open Exxenoz opened this issue 5 years ago • 2 comments

Using the params keyword throws the following exception:

V8ExecutionErrorException: Uncaught Error: Failed to invoke constructor: => Message: Types.ChangeType(): Cannot convert value "Hello" (type: 'System.String') to type 'System.String[]'. If you are developing the source type yourself, implement the 'IConvertible' interface.

Sample:

/// <summary>
/// The MFString field contains zero or more strings.
/// </summary>
[ScriptObject("MFString", ScriptMemberSecurity.NoAcccess)]
public class MFString
{
	private List<string> values = new List<string>();

	public MFString(params string[] values)
	{
		this.values.AddRange(values);
	}

	[ScriptMember("push", ScriptMemberSecurity.Locked)]
	private void Push(params string[] values)
	{
		this.values.AddRange(values);
	}

	[ScriptMember("toString", ScriptMemberSecurity.Locked)]
	public override string ToString()
	{
		return string.Join(", ", values);
	}
}
Engine.RegisterType<MFString>(null, null, ScriptMemberSecurity.Locked);
Engine.GlobalObject.SetProperty(typeof(MFString));

Engine.Execute
(@"
	function test() {
		var s = new MFString('Hello', 'World');
		Browser.println(s);
		s.push('!', 'Test', 'Test2');
		Browser.println(s);
	}

	test();
", "V8.NET", true, 0);

Exxenoz avatar May 09 '19 10:05 Exxenoz

Hi, seems GitHub is unreliable in sending email notices. I just saw this now. The "params" keyword is just sugar in C# for accepting an array of parameters. As far as JavaScript world is concerned, and the binding, an array of strings is expected, not a parameter list (i.e. var s = new MFString(['Hello', 'World']);). I will have to mark this as a todo item and review if this can be supported better, thanks.

rjamesnw avatar May 22 '19 05:05 rjamesnw

Hi James, sorry for the late reply! Unfortunately array parameters don't work as well. Seems like only primitive and registered types get converted correctly. I will do some research and look into this further. Maybe I can come up with a solution that fixes both problems.

Exxenoz avatar Jan 02 '21 21:01 Exxenoz