This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from tanishiking/fix-virtual-dispatch
fix: Add tests for virtual dispatch + fix bug for abstract class
- Loading branch information
Showing
9 changed files
with
294 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
test-suite/src/main/scala/testsuite/core/InterfaceCall.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package testsuite.core.interfacecall | ||
|
||
import scala.scalajs.js.annotation._ | ||
|
||
object InterfaceCall { | ||
def main(): Unit = { val _ = test() } | ||
|
||
@JSExportTopLevel("interfaceCall") | ||
def test(): Boolean = { | ||
val c = new Concrete() | ||
c.plus(c.zero, 1) == 1 && c.minus(1, c.zero) == 1 | ||
} | ||
|
||
class Concrete extends AddSub with Zero { | ||
override def zero: Int = 0 | ||
} | ||
|
||
trait Adder { | ||
def plus(a: Int, b: Int) = a + b | ||
} | ||
|
||
trait Sub { | ||
def minus(a: Int, b: Int): Int = a - b | ||
} | ||
|
||
trait AddSub extends Adder with Sub | ||
|
||
trait Zero { | ||
def zero: Int | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
test-suite/src/main/scala/testsuite/core/VirtualDispatch.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package testsuite.core.virtualdispatch | ||
|
||
import scala.scalajs.js.annotation._ | ||
|
||
object VirtualDispatch { | ||
def main(): Unit = { val _ = test() } | ||
|
||
@JSExportTopLevel("virtualDispatch") | ||
def test(): Boolean = { | ||
val a = new A | ||
val b = new B | ||
|
||
testA(a) && | ||
testB(a, isInstanceOfA = true) && | ||
testB(b, isInstanceOfA = false) && | ||
testC(a, isInstanceOfA = true) && | ||
testC(b, isInstanceOfA = false) | ||
} | ||
|
||
def testA(a: A): Boolean = { | ||
a.a == 2 && a.impl == 2 && a.b == 1 && a.c == 1 | ||
} | ||
|
||
def testB(b: B, isInstanceOfA: Boolean): Boolean = { | ||
if (isInstanceOfA) { | ||
b.b == 1 && b.c == 1 && b.impl == 2 | ||
} else { | ||
b.b == 1 && b.c == 1 && b.impl == 0 | ||
} | ||
} | ||
|
||
def testC(c: C, isInstanceOfA: Boolean): Boolean = { | ||
if (isInstanceOfA) { | ||
c.c == 1 && c.impl == 2 | ||
} else { | ||
c.c == 1 && c.impl == 0 | ||
} | ||
} | ||
|
||
class A extends B { | ||
def a: Int = 2 | ||
override def impl = 2 | ||
} | ||
|
||
class B extends C { | ||
def b: Int = 1 | ||
override def c: Int = 1 | ||
} | ||
|
||
abstract class C { | ||
def c: Int | ||
def impl: Int = 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.