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

Disable Java Map accessing by properties #836

Merged
merged 3 commits into from
Feb 20, 2021
Merged

Disable Java Map accessing by properties #836

merged 3 commits into from
Feb 20, 2021

Conversation

tuchida
Copy link
Contributor

@tuchida tuchida commented Feb 7, 2021

Closes #820
This will have an impact on these Pull Requests. #824, #827


#713 had compatibility issues because entry access by properties took precedence over Java methods. If Java methods are prioritized, compatibility issues will probably disappear.
But here's what happens.

// Java
public Map getMap() {
    return new TreeMap();
}
// Rhino
var m = Foo.getMap();
m.firstKey();  // TreeMap method

getMap returns java.util.Map, but an instance of it is java.util.TreeMap, so you can use the java.util.TreeMap method. If Java methods are prioritized, in order to access it by property, you need to avoid the TreeMap method instead of Map. You always need to know what the instance is. Also, if getMap returns a different Map implementation, your existing code may not work.


Nashorn has probably extended the use of indirect-eval-call to distinguish between property access and method invocation. I'm also checking for behavior such as the distinction between m[0] and m['0'], and that Java methods are not JavaScript Functions.

Copy link
Collaborator

@gbrail gbrail left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this is the next PR that we should merge as we make List and Map work better. Can you please take a look at a few improvements? Thanks!

src/org/mozilla/javascript/Context.java Show resolved Hide resolved
@Override
public boolean has(String name, Scriptable start) {
if (map.containsKey(name)) {
return true;
Context cx = Context.getContext();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"getContext" will throw a RuntimeError if there is no Context on the stack. That's usually incorrect but I know that there's code out there that might do it. Could you please replace these tests with:

Context cx = Context.getCurrentContext();
if (cx != null && cx.hasFeature(Context.FEATURE_ENABLE_JAVA_MAP_ACCESS))

(or make a helper function in Context or ScriptRuntime since we do that ll the time!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dc43827
I fixed NativeJavaMap and NativeJavaList since I based it on #713.

Copy link
Collaborator

@gbrail gbrail left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, those changes help. Thanks!

src/org/mozilla/javascript/Context.java Show resolved Hide resolved
@gbrail gbrail merged commit 358cbd5 into mozilla:master Feb 20, 2021
@tuchida tuchida deleted the 820/disable-java-map-access branch May 27, 2021 08:14
@p-bakker p-bakker added bug Issues considered a bug Java Interop Issues related to the interaction between Java and JavaScript labels Oct 13, 2021
@p-bakker p-bakker added this to the Release 1.7.14 milestone Oct 13, 2021
@p-bakker p-bakker removed this from the Release 1.7.14 milestone Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues considered a bug Java Interop Issues related to the interaction between Java and JavaScript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Backward compatibility java.util.{List,Map} interop
3 participants