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

Exception when trying to pass object as method parameter #1094

Closed
KubaZ2 opened this issue Feb 18, 2022 · 5 comments · Fixed by #1098
Closed

Exception when trying to pass object as method parameter #1094

KubaZ2 opened this issue Feb 18, 2022 · 5 comments · Fixed by #1098

Comments

@KubaZ2
Copy link

KubaZ2 commented Feb 18, 2022

Cannot pass DiscordId as method parameter. It throws System.ArgumentException: 'Object of type 'System.String' cannot be converted to type 'System.UInt64'.'

@lahma
Copy link
Collaborator

lahma commented Feb 18, 2022

Would you like to create a PR fixing the issue, accompanied with a test case? Might be enough to create custom ITypeConverter for your case or ideally if there's generic convertible pattern to adhere and then support it automatically in Jint.

@KubaZ2
Copy link
Author

KubaZ2 commented Feb 18, 2022

I'm new to jint and I don't really know how it works. I don't think I can fix it 😕

@lahma
Copy link
Collaborator

lahma commented Feb 18, 2022

All good, thanks for the bug report. We'll try to investigate when we find the time.

@lahma
Copy link
Collaborator

lahma commented Mar 6, 2022

@KubaZ2 there was a bug in resolution algorithm that caused Jint to choose wrong overload in this case. You also need to set AllowOperatorOverloading to true in order for implicit/explicit conversions to kick in.

[Fact]
public void ShouldBeAbleToUseConvertibleStructAsMethodParameter()
{
var engine = new Engine(options => options.AllowOperatorOverloading());
engine.SetValue("test", new DiscordTestClass());
engine.SetValue("id", new DiscordId("12345"));
Assert.Equal("12345", engine.Evaluate("String(id)").AsString());
Assert.Equal("12345", engine.Evaluate("test.echo('12345')").AsString());
Assert.Equal("12345", engine.Evaluate("test.create(12345)").AsString());
}

There should be a build on MyGet feed soon (on NuGet later), see README on repo root how to access it.

@lahma
Copy link
Collaborator

lahma commented Mar 6, 2022

And with #1099 merged you shouldn't even need to allow operator overloading, it's allowed by default with latest soon to be on MyGet version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants