Skip to content

Commit

Permalink
javascript 'Set' cannot handle wrapped java objects properly (#1387)
Browse files Browse the repository at this point in the history
* Set cannot handle wrapped java objects properly

* Satisfy spotbugs
  • Loading branch information
rPraml authored Oct 6, 2023
1 parent d421fe3 commit dd9c638
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
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";

0 comments on commit dd9c638

Please sign in to comment.