Skip to content

Commit 7823558

Browse files
committed
Implement the identity interop trait for both ImmutableRubyObject and RubyDynamicObject
* Consistently with what's used for the default Kernel#hash.
1 parent c4bb3fb commit 7823558

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

spec/truffle/interop/special_forms_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@
304304

305305
it description['.object_id', :identityHashCode, [], 'when `hasIdentity()` is true (which might not be unique)'] do
306306
pfo, obj, l = proxy[Object.new]
307-
pfo.object_id.should == obj.object_id
307+
pfo.object_id.should == Truffle::Interop.identity_hash_code(obj)
308308
l.log.should include(["isIdentical", pfo, :InteropLibrary]) # hasIdentity()
309309
l.log.should include(["identityHashCode"])
310310
end
@@ -318,7 +318,7 @@
318318

319319
it description['.__id__', :identityHashCode, [], 'when `hasIdentity()` is true (which might not be unique)'] do
320320
pfo, obj, l = proxy[Object.new]
321-
pfo.__id__.should == obj.__id__
321+
pfo.__id__.should == Truffle::Interop.identity_hash_code(obj)
322322
l.log.should include(["isIdentical", pfo, :InteropLibrary]) # hasIdentity()
323323
l.log.should include(["identityHashCode"])
324324
end
@@ -332,7 +332,7 @@
332332

333333
it description['.hash', :identityHashCode, [], 'when `hasIdentity()` is true (which might not be unique)'] do
334334
pfo, obj, l = proxy[Object.new]
335-
pfo.hash.should == obj.__id__
335+
pfo.hash.should == Truffle::Interop.identity_hash_code(obj)
336336
l.log.should include(["isIdentical", pfo, :InteropLibrary]) # hasIdentity()
337337
l.log.should include(["identityHashCode"])
338338
end

src/main/java/org/truffleruby/language/ImmutableRubyObject.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
1313
import com.oracle.truffle.api.library.CachedLibrary;
14+
import com.oracle.truffle.api.profiles.ConditionProfile;
15+
import com.oracle.truffle.api.utilities.TriState;
1416
import org.truffleruby.RubyContext;
1517
import org.truffleruby.RubyLanguage;
1618
import org.truffleruby.cext.ValueWrapper;
@@ -84,6 +86,23 @@ public String toDisplayString(boolean allowSideEffects) {
8486
throw new AbstractMethodError();
8587
}
8688

89+
// region Identity
90+
@ExportMessage
91+
public int identityHashCode() {
92+
return System.identityHashCode(this);
93+
}
94+
95+
@ExportMessage
96+
public TriState isIdenticalOrUndefined(Object other,
97+
@Exclusive @Cached ConditionProfile immutableRubyObjectProfile) {
98+
if (immutableRubyObjectProfile.profile(other instanceof ImmutableRubyObject)) {
99+
return TriState.valueOf(this == other);
100+
} else {
101+
return TriState.UNDEFINED;
102+
}
103+
}
104+
// endregion
105+
87106
// region Members
88107
@ExportMessage
89108
public boolean hasMembers() {

src/main/java/org/truffleruby/language/RubyDynamicObject.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.truffleruby.RubyContext;
1616
import org.truffleruby.RubyLanguage;
1717
import org.truffleruby.core.array.ArrayUtils;
18-
import org.truffleruby.core.basicobject.BasicObjectNodes.ObjectIDNode;
1918
import org.truffleruby.core.cast.BooleanCastNode;
2019
import org.truffleruby.core.cast.IntegerCastNode;
2120
import org.truffleruby.core.cast.LongCastNode;
@@ -139,19 +138,16 @@ public Object toDisplayString(boolean allowSideEffects,
139138
}
140139

141140
// region Identity
142-
/** Like {@link org.truffleruby.core.hash.HashingNodes} but simplified since {@link ObjectIDNode} for
143-
* RubyDynamicObject can only return long. */
144141
@ExportMessage
145-
public int identityHashCode(
146-
@Cached ObjectIDNode objectIDNode) {
147-
return (int) objectIDNode.execute(this);
142+
public int identityHashCode() {
143+
return System.identityHashCode(this);
148144
}
149145

150146
@ExportMessage
151147
public TriState isIdenticalOrUndefined(Object other,
152148
@Exclusive @Cached ConditionProfile rubyObjectProfile) {
153149
if (rubyObjectProfile.profile(other instanceof RubyDynamicObject)) {
154-
return this == other ? TriState.TRUE : TriState.FALSE;
150+
return TriState.valueOf(this == other);
155151
} else {
156152
return TriState.UNDEFINED;
157153
}

0 commit comments

Comments
 (0)