Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

params keyword not working #15

Open
Exxenoz opened this issue May 9, 2019 · 2 comments
Open

params keyword not working #15

Exxenoz opened this issue May 9, 2019 · 2 comments
Labels
enhancement todo worksasdesigned The feature is working as expected.

Comments

@Exxenoz
Copy link

Exxenoz commented May 9, 2019

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);
@rjamesnw
Copy link
Owner

rjamesnw commented May 22, 2019

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 rjamesnw added enhancement worksasdesigned The feature is working as expected. labels May 22, 2019
@rjamesnw rjamesnw added the todo label Jul 3, 2019
@Exxenoz
Copy link
Author

Exxenoz commented Jan 2, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement todo worksasdesigned The feature is working as expected.
Projects
None yet
Development

No branches or pull requests

2 participants