Skip to content

Commit d3c0e2e

Browse files
author
Nicolas Laurent
committed
ensure primitive map keys are promoted to the widest possible type to avoid
problems with equals
1 parent b14eb61 commit d3c0e2e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/main/java/org/truffleruby/core/objectspace/WeakMapNodes.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ protected int size(RubyWeakMap map) {
5555
@CoreMethod(names = { "key?", "member?", "include?" }, required = 1)
5656
public abstract static class MemberNode extends CoreMethodArrayArgumentsNode {
5757

58+
@Specialization // ensures all integer keys are promoted to long
59+
protected Object isMember(RubyWeakMap map, long key) {
60+
return isMember(map, (Object) key);
61+
}
62+
63+
@Specialization // ensures all floating keys are promoted to double
64+
protected Object isMember(RubyWeakMap map, double key) {
65+
return isMember(map, (Object) key);
66+
}
67+
5868
@Specialization
5969
protected boolean isMember(RubyWeakMap map, Object key) {
6070
return map.storage.get(key) != null;
@@ -64,6 +74,16 @@ protected boolean isMember(RubyWeakMap map, Object key) {
6474
@CoreMethod(names = "[]", required = 1)
6575
public abstract static class GetIndexNode extends CoreMethodArrayArgumentsNode {
6676

77+
@Specialization // ensures all integer keys are promoted to long
78+
protected Object get(RubyWeakMap map, long key) {
79+
return get(map, (Object) key);
80+
}
81+
82+
@Specialization // ensures all floating keys are promoted to double
83+
protected Object get(RubyWeakMap map, double key) {
84+
return get(map, (Object) key);
85+
}
86+
6787
@Specialization
6888
protected Object get(RubyWeakMap map, Object key) {
6989
Object value = map.storage.get(key);
@@ -74,6 +94,16 @@ protected Object get(RubyWeakMap map, Object key) {
7494
@Primitive(name = "weakmap_aset")
7595
public abstract static class SetIndexNode extends CoreMethodArrayArgumentsNode {
7696

97+
@Specialization // ensures all integer keys are promoted to long
98+
protected Object set(RubyWeakMap map, long key, Object value) {
99+
return set(map, (Object) key, value);
100+
}
101+
102+
@Specialization // ensures all floating keys are promoted to double
103+
protected Object set(RubyWeakMap map, double key, Object value) {
104+
return set(map, (Object) key, value);
105+
}
106+
77107
@Specialization
78108
protected Object set(RubyWeakMap map, Object key, Object value) {
79109
map.storage.put(key, value);

0 commit comments

Comments
 (0)