|
| 1 | +package scala.tools.nsc |
| 2 | +package backend.jvm |
| 3 | +package opt |
| 4 | + |
| 5 | +import org.junit.runner.RunWith |
| 6 | +import org.junit.runners.JUnit4 |
| 7 | +import org.junit.Test |
| 8 | +import scala.collection.generic.Clearable |
| 9 | +import scala.tools.asm.Opcodes._ |
| 10 | +import org.junit.Assert._ |
| 11 | + |
| 12 | +import scala.tools.asm.tree._ |
| 13 | +import scala.tools.nsc.reporters.StoreReporter |
| 14 | + |
| 15 | +import CodeGenTools._ |
| 16 | +import scala.tools.partest.ASMConverters |
| 17 | +import ASMConverters._ |
| 18 | +import AsmUtils._ |
| 19 | + |
| 20 | +import BackendReporting._ |
| 21 | + |
| 22 | +import scala.collection.convert.decorateAsScala._ |
| 23 | +import scala.tools.testing.ClearAfterClass |
| 24 | + |
| 25 | +object IndySammyTest extends ClearAfterClass.Clearable { |
| 26 | + var compiler = newCompiler() |
| 27 | + |
| 28 | + def compile(scalaCode: String, javaCode: List[(String, String)] = Nil, allowMessage: StoreReporter#Info => Boolean = _ => false): List[ClassNode] = |
| 29 | + compileClasses(compiler)(scalaCode, javaCode, allowMessage) |
| 30 | + |
| 31 | + def clear(): Unit = { compiler = null } |
| 32 | +} |
| 33 | + |
| 34 | +@RunWith(classOf[JUnit4]) |
| 35 | +class IndySammyTest extends ClearAfterClass { |
| 36 | + ClearAfterClass.stateToClear = IndySammyTest |
| 37 | + import IndySammyTest._ |
| 38 | + |
| 39 | + import compiler.genBCode.bTypes._ |
| 40 | + import compiler.genBCode.bTypes.backendUtils._ |
| 41 | + |
| 42 | + val inlineOnlyCompiler = IndySammyTest.inlineOnlyCompiler |
| 43 | + |
| 44 | + def funName(from: String, to: String) = s"Fun$from$to" |
| 45 | + def lamName(from: String, to: String) = s"lam$from$to" |
| 46 | + def appName(from: String, to: String, actual: String) = s"app$from$to_$actual" |
| 47 | + def classPrologue(from: String, to: String) = |
| 48 | + s"class VC(private val i: Int) extends AnyVal\ntrait ${funName(from, to)} { def apply(a: $from): $to}" |
| 49 | + |
| 50 | + def lamDef(from: String, to: String, body: String => String) = |
| 51 | + s"""def ${lamName(from, to)} = (x => ${body("x")}): ${funName(from, to)}""" |
| 52 | + |
| 53 | + def appDef(from: String, to: String, arg: String = "new VC(1)", argTp: String) = |
| 54 | + s"""def ${appName(from, to, argTp)} = ${lamName(from, to)}($arg)""" |
| 55 | + |
| 56 | + def test(from: String, to: String, arg: String, argTp: String, body: String => String = x => x): (Method, Method) = { |
| 57 | + compile($"{classPrologue(from, to}; ${lamDef(from, to, body)}; ${appDef(from, to, arg, argTp)}") |
| 58 | + find methods lamName(from, to) appName(from, to, argTp) |
| 59 | + } |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +//trait FunVC_VC { def apply(a: VC): VC } // int apply(int var1) |
| 64 | +//trait FunInt_VC { def apply(a: Int): VC } // int apply(int var1) |
| 65 | +//trait FunAny_VC { def apply(a: Any): VC } // int apply(Object var1) |
| 66 | +//trait FunVC_Any { def apply(a: VC): Any } // Object apply(int var1) |
| 67 | +//trait FunVC_Int { def apply(a: VC): Int } // int apply(int var1) |
| 68 | +//trait FunVC_Unit { def apply(a: VC): Unit } // void apply(int var1) |
| 69 | +// |
| 70 | +// |
| 71 | +//class C { |
| 72 | +// def fun_vc_vc = (x => x): FunVC_VC |
| 73 | +// def app_vc_vc__vc = fun_vc_vc(new VC(1)) |
| 74 | +// |
| 75 | +// def fun_vc_any = (x => x): FunVC_Any |
| 76 | +// def app_vc_any__vc = fun_vc_any(new VC(1)) |
| 77 | +// |
| 78 | +// def fun_any_vc = (x => new VC(x.asInstanceOf[Int])): FunAny_VC |
| 79 | +// def app_vc_any__I = fun_any_vc(1) |
| 80 | +// |
| 81 | +// def fun_int_vc = (x => new VC(x)): FunInt_VC |
| 82 | +// def app_int_vc__I = fun_int_vc(1) |
| 83 | +// |
| 84 | +// def fun_vc_int = (x => 1): FunVC_Int |
| 85 | +// def app_vc_int__vc = fun_vc_int(new VC(1)) |
| 86 | +// |
| 87 | +// def fun_vc_unit = (x => x): FunVC_Unit |
| 88 | +// def app_vc_unit__vc = fun_vc_unit(new VC(1)) |
| 89 | +//} |
| 90 | + |
| 91 | +// TODO: turn into junit test that checks this compiles to the corresponding java code: |
| 92 | +// FunAny_VC fun_any_vc() { return x -> BoxesRunTime.unboxToInt((Object)x); } |
| 93 | +// FunInt_VC fun_int_vc() { return x -> x; } |
| 94 | +// FunVC_Any fun_vc_any() { return x -> new VC(x); } |
| 95 | +// FunVC_Int fun_vc_int() { return x -> 1; } |
| 96 | +// FunVC_Unit fun_vc_unit() { return x -> { } } |
| 97 | +// FunVC_VC fun_vc_vc() { return x -> x; } |
| 98 | +// |
| 99 | +// int app_vc_any__I() { return this.fun_any_vc().apply(BoxesRunTime.boxToInteger((int)1)); |
| 100 | +// int app_int_vc__I() { return this.fun_int_vc().apply(1); |
| 101 | +// Object app_vc_any__vc() { return this.fun_vc_any().apply(1); |
| 102 | +// int app_vc_int__vc() { return this.fun_vc_int().apply(1); |
| 103 | +// void app_vc_unit__vc() { this.fun_vc_unit().apply(1); |
| 104 | +// int app_vc_vc__vc() { return this.fun_vc_vc().apply(1); |
| 105 | + |
0 commit comments