Skip to content

Commit

Permalink
Use IsSameObject instead of reference comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Apr 18, 2023
1 parent 4b3baa3 commit 55ceaba
Showing 1 changed file with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
@BuiltinMethod(
type = "Any",
name = "==",
description = """
description =
"""
Compares self with other object and returns True iff `self` is exactly the same as
the other object, including all its transitively accessible properties or fields,
False otherwise.
Expand All @@ -32,9 +33,9 @@
Does not throw dataflow errors or panics.
Note that this is different than `Meta.is_same_object`, which checks whether two
references point to the same object on the heap.
"""
)
references point to the same object on the heap. However `Any.==` implies
`Meta.is_same_object` for all object with the exception of `Number.nan`.
""")
@GenerateUncached
public abstract class EqualsNode extends Node {

Expand All @@ -44,11 +45,7 @@ public static EqualsNode build() {

public abstract boolean execute(@AcceptsError Object self, @AcceptsError Object right);

/**
* Primitive values
**/


/** Primitive values */
@Specialization
boolean equalsBoolBool(boolean self, boolean other) {
return self == other;
Expand Down Expand Up @@ -166,10 +163,7 @@ boolean equalsLongBigInt(long self, EnsoBigInteger other) {
}
}

@Specialization(guards = {
"selfText.is_normalized()",
"otherText.is_normalized()"
})
@Specialization(guards = {"selfText.is_normalized()", "otherText.is_normalized()"})
boolean equalsTextText(Text selfText, Text otherText) {
return selfText.toString().compareTo(otherText.toString()) == 0;
}
Expand Down Expand Up @@ -199,13 +193,14 @@ boolean equalsTextBigInt(Text self, EnsoBigInteger other) {
* normalization. See {@code Text_Utils.compare_to}.
*/
@TruffleBoundary
@Specialization(guards = {
"selfInterop.isString(selfString)",
"otherInterop.isString(otherString)"
}, limit = "3")
boolean equalsStrings(Object selfString, Object otherString,
@CachedLibrary("selfString") InteropLibrary selfInterop,
@CachedLibrary("otherString") InteropLibrary otherInterop) {
@Specialization(
guards = {"selfInterop.isString(selfString)", "otherInterop.isString(otherString)"},
limit = "3")
boolean equalsStrings(
Object selfString,
Object otherString,
@CachedLibrary("selfString") InteropLibrary selfInterop,
@CachedLibrary("otherString") InteropLibrary otherInterop) {
String selfJavaString;
String otherJavaString;
try {
Expand All @@ -230,18 +225,21 @@ boolean equalsAtomConstructors(AtomConstructor self, AtomConstructor other) {
}

@Specialization
boolean equalsAtoms(Atom self, Atom other, @Cached EqualsAtomNode equalsNode) {
return self == other || equalsNode.execute(self, other);
boolean equalsAtoms(
Atom self,
Atom other,
@Cached EqualsAtomNode equalsNode,
@Cached IsSameObjectNode isSameObjectNode) {
return isSameObjectNode.execute(self, other) || equalsNode.execute(self, other);
}

@Specialization(guards = "isNotPrimitive(self, other, interop, warnings)")
boolean equalsComplex(
Object self,
Object other,
@Cached EqualsComplexNode equalsComplex,
@CachedLibrary(limit = "10") InteropLibrary interop,
@CachedLibrary(limit = "10") WarningsLibrary warnings) {
return self == other || equalsComplex.execute(self, other);
@Cached IsSameObjectNode isSameObjectNode) {
return isSameObjectNode.execute(self, other) || equalsComplex.execute(self, other);
}

static boolean isNotPrimitive(
Expand Down

0 comments on commit 55ceaba

Please sign in to comment.