@@ -525,8 +525,22 @@ trait GenJSExports[G <: Global with Singleton] extends SubComponent {
525
525
} else {
526
526
// Sort them so that, e.g., isInstanceOf[String]
527
527
// comes before isInstanceOf[Object]
528
- val sortedAltsByTypeTest = topoSortDistinctsBy(
529
- altsByTypeTest)(_._1)(RTTypeTest .Ordering )
528
+ val sortedAltsByTypeTest = topoSortDistinctsWith(altsByTypeTest) { (lhs, rhs) =>
529
+ (lhs._1, rhs._1) match {
530
+ // NoTypeTest is always last
531
+ case (_, NoTypeTest ) => true
532
+ case (NoTypeTest , _) => false
533
+
534
+ case (PrimitiveTypeTest (_, rank1), PrimitiveTypeTest (_, rank2)) =>
535
+ rank1 <= rank2
536
+
537
+ case (InstanceOfTypeTest (t1), InstanceOfTypeTest (t2)) =>
538
+ t1 <:< t2
539
+
540
+ case (_ : PrimitiveTypeTest , _ : InstanceOfTypeTest ) => true
541
+ case (_ : InstanceOfTypeTest , _ : PrimitiveTypeTest ) => false
542
+ }
543
+ }
530
544
531
545
val defaultCase = genThrowTypeError()
532
546
@@ -946,47 +960,16 @@ trait GenJSExports[G <: Global with Singleton] extends SubComponent {
946
960
947
961
private case object NoTypeTest extends RTTypeTest
948
962
949
- private object RTTypeTest {
950
- implicit object Ordering extends PartialOrdering [RTTypeTest ] {
951
- override def tryCompare (lhs : RTTypeTest , rhs : RTTypeTest ): Option [Int ] = {
952
- if (lteq(lhs, rhs)) if (lteq(rhs, lhs)) Some (0 ) else Some (- 1 )
953
- else if (lteq(rhs, lhs)) Some (1 ) else None
954
- }
955
-
956
- override def lteq (lhs : RTTypeTest , rhs : RTTypeTest ): Boolean = {
957
- (lhs, rhs) match {
958
- // NoTypeTest is always last
959
- case (_, NoTypeTest ) => true
960
- case (NoTypeTest , _) => false
961
-
962
- case (PrimitiveTypeTest (_, rank1), PrimitiveTypeTest (_, rank2)) =>
963
- rank1 <= rank2
964
-
965
- case (InstanceOfTypeTest (t1), InstanceOfTypeTest (t2)) =>
966
- t1 <:< t2
967
-
968
- case (_ : PrimitiveTypeTest , _ : InstanceOfTypeTest ) => true
969
- case (_ : InstanceOfTypeTest , _ : PrimitiveTypeTest ) => false
970
- }
971
- }
972
-
973
- override def equiv (lhs : RTTypeTest , rhs : RTTypeTest ): Boolean = {
974
- lhs == rhs
975
- }
976
- }
977
- }
978
-
979
963
// Very simple O(n²) topological sort for elements assumed to be distinct
980
- private def topoSortDistinctsBy [A <: AnyRef , B ](coll : List [A ])(f : A => B )(
981
- implicit ord : PartialOrdering [B ]): List [A ] = {
982
-
964
+ private def topoSortDistinctsWith [A <: AnyRef ](coll : List [A ])(
965
+ lteq : (A , A ) => Boolean ): List [A ] = {
983
966
@ scala.annotation.tailrec
984
967
def loop (coll : List [A ], acc : List [A ]): List [A ] = {
985
968
if (coll.isEmpty) acc
986
969
else if (coll.tail.isEmpty) coll.head :: acc
987
970
else {
988
971
val (lhs, rhs) = coll.span(x => ! coll.forall(
989
- y => (x eq y) || ! ord. lteq(f(x), f(y) )))
972
+ y => (x eq y) || ! lteq(x, y )))
990
973
assert(! rhs.isEmpty, s " cycle while ordering $coll" )
991
974
loop(lhs ::: rhs.tail, rhs.head :: acc)
992
975
}
0 commit comments