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

Improve overloaded method invocation when null is passed. #1352

Closed
wants to merge 3 commits into from

Conversation

rPraml
Copy link
Contributor

@rPraml rPraml commented Jul 6, 2023

Problem

We often have to deal with overloaded methods like

void setValue(String value)
void setValue(Object value)

When invoking such methods from rhino, everything is fine, as long as the value is not undefined or null

v.value = 123; // will call setValue(Object)
v.value = "foo"; // will call setValue(String)
v.value = null; // will fail with ambiguous method call

Currently, there's no way to pass null to an overloaded value, as javascript cannot determine, which method should be called.

Possible fix

Adjusting the conversion weight for JSTYPE_UNDEFINED and JSTYPE_NULL:
Converting them to String should weight less than converting them to Object (this means, that setValue(String) will be preferred)

Note and room for discussion

I know, the choiche to favor String over Object is closely related to our use-case (and I'm OK if we can find a consent in which order such conversion should happen)

What I really want is to invoke overloaded methods with "null" values.
Unfortunately I cannot do a setValue((String)null) or setValue((Object)null) in javascript

It may be possible to construct some hacky null type for java classes (e.g. String.null Integer.null and so on)

@p-bakker
Copy link
Collaborator

p-bakker commented Aug 18, 2024

Can't you use the LiveConnect syntax from JavaScript to specify which overloaded variant you want to invoke like someInstance['setValue(String)'](null)?

I think the LiveConnect spec also defined priorities for selecting which overloaded method to take based on the provided params if no specific overload is specified

@rPraml
Copy link
Contributor Author

rPraml commented Aug 22, 2024

Thanks @p-bakker for pointing me in the right direction. Didn't know that syntax and may help in some other use cases

Yes, it is mentioned here: https://www-archive.mozilla.org/js/liveconnect/lc3_proposal.html

I think the LiveConnect spec also defined priorities for selecting which overloaded method to take based on the provided params if no specific overload is specified

From the docs:

When choosing between two or more applicable methods, an algorithm is used that is similar in spirit to the ones used in Java and C++
...
conversion to type uj is preferred to the conversion to type sj when converting from tj
...
If there is more than one maximally preferred method, an error occurs.
...
There is no preference among Java types for converting from the JavaScript undefined value.
There is no preference among Java types for converting from the JavaScript null value.

Yes, I see this PR will violate the spec. 🤔
I have to discuss that issue with my team what we do now, because we had this use case very often, and I think we cannot change the spec.
I think we have to go the someInstance['setValue(String)'](null) way, although if this makes the code harder to read.

@p-bakker
Copy link
Collaborator

K, if this PR ends up being obsolete, please don't forget to close it

@rPraml
Copy link
Contributor Author

rPraml commented Aug 22, 2024

Closing this PR. We must use the extended LiveConnect syntax

@rPraml rPraml closed this Aug 22, 2024
@p-bakker
Copy link
Collaborator

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

Successfully merging this pull request may close these issues.

2 participants