-
Notifications
You must be signed in to change notification settings - Fork 863
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
Coercing undefined to java Object #760
Comments
To add to this, JSON.stringify converts undefined to null when found in an array. |
I agree that it's weird to have JSON.stringify convert undefined in an
array to null, but JavaScript is full of weird things ;-) In this case, V8
seems to do the same thing so that is usually a good sign that Rhino is
following the spec.
As for changing the way that conversions to Java are handled, if you can
think of a specific change that can be done in a backward-compatible way,
or via a flag or a new class, then please open an issue -- small,
well-defined issues seem to be things that people on this list can
implement.
OTOH, there is a gigantic amount of code out there that depends on Rhino
Java library behavior going back decades, so a big change in this area
without a flag is not something that we can do.
…On Wed, Sep 16, 2020 at 11:33 AM tonygermano ***@***.***> wrote:
To add to this, JSON.stringify converts undefined to null when found in an
array.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#760 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAD7I24HWDJAUEXHQDDZJQTSGEAHXANCNFSM4QCVDU6A>
.
|
Actually, converting undefined to null when found in an array was the part I thought was normal 😁. I was pointing that out in contrast to undefined becoming a string in Java, which is not expected behavior. In java a declared unset variable is null. In javascript a declared unset variable is undefined. You would think they would be roughly equivalent. Another indicator that they should be is that This just doesn't seem right.
I think you would agree that it would be quite odd if this,
returned Funnily enough, it appears
I understand the need for a flag if someone may be depending on the current behavior. I'd argue that the correct behavior should be converting undefined to null (as JSON.stringify and NativeArray#get both do) and the current behavior should be enabled by the flag. Where would a flag such as this typically be set? I believe |
Note about the background of this: that JavaScript (However section 2.3.7 of https://www.oracle.com/java/technologies/javase/liveconnect-docs.html#JS_NULL suggests it ought to be null) I guess they messed things up a bit back then and we're here to live with the consequenses :-) |
We had some more Tests with rhino 1.7.14 and found scripts returning "undefined" instead of null. With rhino 1.7.9 we used the sun/oracle phobos RhinoScriptEngine code for integration of rhino and there is this little method called for the return value after a script is evaluated, converting Undefined to null Object unwrapReturnValue(Object result) {
if (result instanceof Wrapper) {
result = ((Wrapper) result).unwrap();
}
return result instanceof Undefined ? null : result;
} where now there is a call to Context.jsToJava() calling NativeJavaObject.coerceTypeImpl() and thus returning "undefined". |
This issue leads to very strange behaviors. Like:
I know it was defined at some point that |
Would be pretty straightforward I think to add a Context flag that would alter the behaviour of NativeJavaObject.coerceTypeImpl() to return the Or allow to choose between returning "undefined", null or I don't think we can change the default behaviour, so some config option it must be. Need to think how users of the Rhino Shell or through the JSR-232 Script Engine interface can opt into these different behaviours |
When it comes to java interop, I'm of the opinion of evolving the defaults to keep up with the language spec and put the odd legacy behaviors behind the feature flags. The most logical behaviors and common use cases should not be behind feature flags. js Another example of this that I was planning on bringing up is that a java Yet another questionable default... why does the rhino shell still use language version 180 by default instead of 200? I think it's ok to change default behaviors to make the library more usable for the majority of people, and put the onus of making configuration changes on the legacy users (assuming they even upgrade the library regularly.) |
Got no problem changing the default behaviour of something, for example how
With regards to the language version in the shell: see #1279, we just need someone to create the PR to do it Wrt to long > BigInt mapping: don't think we have an explicit case for this, so maybe create one? And a PR would be great as well 🙂 Dunno if whether that conversion occurs or not ought to be configurable or not and what the default should be, but once we have an actual PR, it'll likely get more people involved in the discussion |
Is there a reason why when coercing undefined to a java object it becomes the string "undefined" rather than null?
This is very confusing and unexpected, for example, when placing it into a Collection or Map,
This also means the the following won't work as expected
Yet it works correctly with null
It defeats the purpose of using an Optional if you are required to check for undefined before creating it.
If there's a reason for the current behavior, perhaps it could be altered with a flag? I wouldn't think this is what most people want.
The text was updated successfully, but these errors were encountered: