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

javascript 'Set' cannot handle wrapped java objects properly #1387

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/org/mozilla/javascript/NativeJavaArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.mozilla.javascript;

import java.lang.reflect.Array;
import java.util.Objects;

/**
* This class reflects Java arrays into the JavaScript environment.
Expand Down Expand Up @@ -143,6 +144,17 @@ public Scriptable getPrototype() {
return prototype;
}

@Override
public boolean equals(Object obj) {
return (obj instanceof NativeJavaArray)
&& Objects.equals(((NativeJavaArray) obj).array, array);
}

@Override
public int hashCode() {
return array == null ? 0 : array.hashCode();
}

Object array;
int length;
Class<?> cls;
Expand Down
10 changes: 10 additions & 0 deletions src/org/mozilla/javascript/NativeJavaClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,14 @@ private static Class<?> findNestedClass(Class<?> parentClass, String name) {
}

private Map<String, FieldAndMethods> staticFieldAndMethods;

@Override
public boolean equals(Object obj) {
return super.equals(obj);
}

@Override
public int hashCode() {
return super.hashCode();
}
}
10 changes: 10 additions & 0 deletions src/org/mozilla/javascript/NativeJavaList.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,14 @@ public Object[] getIds() {
private boolean isWithValidIndex(int index) {
return index >= 0 && index < list.size();
}

@Override
public boolean equals(Object obj) {
return super.equals(obj);
}

@Override
public int hashCode() {
return super.hashCode();
}
}
10 changes: 10 additions & 0 deletions src/org/mozilla/javascript/NativeJavaMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ public Object[] getIds() {
return super.getIds();
}

@Override
public boolean equals(Object obj) {
return super.equals(obj);
}

@Override
public int hashCode() {
return super.hashCode();
}

private static Callable symbol_iterator =
(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) -> {
if (!(thisObj instanceof NativeJavaMap)) {
Expand Down
13 changes: 13 additions & 0 deletions src/org/mozilla/javascript/NativeJavaObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

/**
* This class reflects non-Array Java objects into the JavaScript environment. It reflect fields
Expand Down Expand Up @@ -986,4 +987,16 @@ protected String getTag() {
}
}
}

@Override
public boolean equals(Object obj) {
return obj != null
&& obj.getClass().equals(getClass())
&& Objects.equals(((NativeJavaObject) obj).javaObject, javaObject);
}

@Override
public int hashCode() {
return javaObject == null ? 0 : javaObject.hashCode();
}
}
9 changes: 8 additions & 1 deletion testsrc/jstests/es6/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ load("testsrc/assert.js");
res = "";

function logElement(value, key) {
res += "set[" + key + "] (" + this + ") ";
res += "set[" + key + "] (" + this + ") ";
}

(function TestForEach() {
Expand Down Expand Up @@ -81,4 +81,11 @@ function logElement(value, key) {
}
})();

(function TestSetAddJavaEnums() {
var mySet = new Set();
mySet.add(java.nio.file.AccessMode.READ);
mySet.add(java.nio.file.AccessMode.READ);
assertEquals(1, mySet.size);
})();

"success";