Skip to content

Commit

Permalink
Allows null values in maps
Browse files Browse the repository at this point in the history
  • Loading branch information
rPraml committed May 10, 2021
1 parent cf84dbf commit 0849085
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/org/mozilla/javascript/NativeJavaMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Object get(String name, Scriptable start) {
if (cx != null && cx.hasFeature(Context.FEATURE_ENABLE_JAVA_MAP_ACCESS)) {
if (map.containsKey(name)) {
Object obj = map.get(name);
return cx.getWrapFactory().wrap(cx, this, obj, obj.getClass());
return cx.getWrapFactory().wrap(cx, this, obj, obj == null ? null : obj.getClass());
}
}
return super.get(name, start);
Expand All @@ -78,7 +78,7 @@ public Object get(int index, Scriptable start) {
if (cx != null && cx.hasFeature(Context.FEATURE_ENABLE_JAVA_MAP_ACCESS)) {
if (map.containsKey(Integer.valueOf(index))) {
Object obj = map.get(Integer.valueOf(index));
return cx.getWrapFactory().wrap(cx, this, obj, obj.getClass());
return cx.getWrapFactory().wrap(cx, this, obj, obj == null ? null : obj.getClass());
}
}
return super.get(index, start);
Expand Down
9 changes: 9 additions & 0 deletions testsrc/org/mozilla/javascript/tests/NativeJavaMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public NativeJavaMapTest() {
global.init(ContextFactory.getGlobal());
}

public void testAccessingNullValues() {
Map<Object, Number> map = new HashMap<>();
map.put("a", null);
map.put(1, null);
assertEquals(2, runScriptAsInt("value.size()", map, true));
assertEquals(null, runScript("value.a", map, true));
assertEquals(null, runScript("value[1]", map, true));
}

public void testAccessingJavaMapIntegerValues() {
Map<Number, Number> map = new HashMap<>();
map.put(0, 1);
Expand Down

0 comments on commit 0849085

Please # to comment.