Skip to content

Commit

Permalink
Merge pull request #343 from scala-native/topic/equality-virtual-disp…
Browse files Browse the repository at this point in the history
…atch

Fix #260
  • Loading branch information
densh committed Oct 20, 2016
2 parents 3541b3a + acae8fe commit 4c0fcfa
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 34 deletions.
44 changes: 20 additions & 24 deletions javalib/src/main/scala/java/lang/String.scala
Original file line number Diff line number Diff line change
Expand Up @@ -202,34 +202,30 @@ final class _String()
def endsWith(suffix: _String): scala.Boolean =
regionMatches(count - suffix.count, suffix, 0, suffix.count)

override def equals(obj: Any): scala.Boolean = {
if (obj == this) {
override def equals(obj: Any): scala.Boolean = obj match {
case s: _String if s eq this =>
true
} else {
obj match {
case s: _String =>
val thisHash = this.hashCode()
val thatHash = s.hashCode()

if (count != s.count ||
(thisHash != thatHash && thisHash != 0 && thatHash != 0)) {
false
case s: _String =>
val thisHash = this.hashCode()
val thatHash = s.hashCode()

if (count != s.count ||
(thisHash != thatHash && thisHash != 0 && thatHash != 0)) {
false
} else {
var i = 0
while (i < count) {
if (value(offset + i) != s.value(s.offset + i)) {
return false
} else {
var i = 0
while (i < count) {
if (value(offset + i) != s.value(s.offset + i)) {
return false
} else {
i += 1
}
}

true
i += 1
}
case _ =>
false
}

true
}
}
case _ =>
false
}

def equalsIgnoreCase(string: _String): scala.Boolean = {
Expand Down
2 changes: 1 addition & 1 deletion nativelib/src/main/scala/java/lang/Class.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class _Class[A](val ty: Ptr[Type]) {

override def equals(other: Any): scala.Boolean = other match {
case other: _Class[_] =>
ty.cast[Long] == other.ty.cast[Long]
ty == other.ty
case _ =>
false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1160,11 +1160,18 @@ abstract class NirCodeGen
val comp = if (negated) Comp.Ine else Comp.Ieq
right withOp Op.Comp(comp, Rt.Object, left.value, right.value)
} else {
val equals = genMethodCall(NObjectEqualsMethod,
statically = true,
left.value,
Seq(rightp),
left)
val isnull = left withOp Op.Comp(Comp.Ieq, Rt.Object, left.value, Val.Zero(Rt.Object))
val cond = ValTree(isnull.value)
val thenp = ContTree { focus =>
val right = genExpr(rightp, focus)
right withOp Op.Comp(Comp.Ieq, Rt.Object, right.value, Val.Zero(Rt.Object))
}
val elsep = ContTree { focus =>
genMethodCall(NObjectEqualsMethod, statically = false,
left.value, Seq(rightp), focus)
}
val equals = genIf(Type.Bool, cond, thenp, elsep, isnull)

if (negated) negateBool(equals.value, equals)
else equals
}
Expand Down
4 changes: 1 addition & 3 deletions sandbox/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import scalanative.native._, stdlib._, stdio._

object Test {
def main(args: Array[String]): Unit = {
val W = 800
val H = 600
fprintf(stdout, c"P3\n%d %d\n%d\n", W, H, 255)
println("Hello, world!")
}
}
23 changes: 23 additions & 0 deletions unit-tests/src/main/scala/scala/EqualitySuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package scala

object EqualitySuite extends tests.Suite {
case class O(m: Int)

test("case class equality") {
assert(O(5) == O(5))
}

test("null equals null") {
assert((null: Object) == (null: Object))
}

test("null does not equal object") {
val obj = new Object
assert((null: Object) != obj)
}

test("object does not equal null") {
val obj = new Object
assert(obj != (null: Object))
}
}
15 changes: 15 additions & 0 deletions unit-tests/src/main/scala/scala/scalanative/issues/_260.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package scala.scalanative.issues

object _260 extends tests.Suite {
test("https://github.com/scala-native/scala-native/issues/260") {
def getStr(): String = {
val bytes = Array('h'.toByte, 'o'.toByte, 'l'.toByte, 'a'.toByte)
new String(bytes)
}

val sz = getStr()

assert("hola" == sz)
assert("hola".equals(sz))
}
}
5 changes: 4 additions & 1 deletion unit-tests/src/main/scala/tests/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ object Main {
java.lang.FloatSuite,
java.lang.DoubleSuite,
java.util.RandomSuite,
scala.scalanative.issues._260,
scala.scalanative.issues._314,
scala.scalanative.issues._337,
scala.scalanative.native.CStringSuite,
scala.scalanative.native.CInteropSuite,
scala.scalanative.native.InstanceOfSuite,
scala.ArrayIntCopySuite,
scala.ArrayDoubleCopySuite,
scala.ArrayObjectCopySuite
scala.ArrayObjectCopySuite,
scala.EqualitySuite
)

def main(args: Array[String]): Unit = {
Expand Down

0 comments on commit 4c0fcfa

Please sign in to comment.