diff options
Diffstat (limited to 'examples/scala-js/compiler/src/test/scala/scala/scalajs/compiler/test/OptimizationTest.scala')
-rw-r--r-- | examples/scala-js/compiler/src/test/scala/scala/scalajs/compiler/test/OptimizationTest.scala | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/examples/scala-js/compiler/src/test/scala/scala/scalajs/compiler/test/OptimizationTest.scala b/examples/scala-js/compiler/src/test/scala/scala/scalajs/compiler/test/OptimizationTest.scala new file mode 100644 index 0000000..7f15c7a --- /dev/null +++ b/examples/scala-js/compiler/src/test/scala/scala/scalajs/compiler/test/OptimizationTest.scala @@ -0,0 +1,109 @@ +package scala.scalajs.compiler.test + +import util._ + +import org.junit.Test + +import scala.scalajs.ir.{Trees => js, Types => jstpe} + +class OptimizationTest extends JSASTTest { + + @Test + def unwrapScalaFunWrapper: Unit = { + + // Make sure we do not wrap and unwrap right away + """ + import scala.scalajs.js + + class A { + val jsFun: js.Function = (x: Int) => x * 2 + } + """. + hasNot("runtime.AnonFunction ctor") { + case js.New(jstpe.ClassType("sjsr_AnonFunction1"), _, _) => + } + + // Make sure our wrapper matcher has the right name + """ + import scala.scalajs.js + + class A { + val scalaFun = (x: Int) => x * 2 + val jsFun: js.Function = scalaFun + } + """. + has("runtime.AnonFunction ctor") { + case js.New(jstpe.ClassType("sjsr_AnonFunction1"), _, _) => + } + + /* Make sure js.Array(...) is optimized away completely for several kinds + * of data types. + */ + """ + import scala.scalajs.js + + class VC(val x: Int) extends AnyVal + + class A { + val a = js.Array(5, 7, 9, -3) + val b = js.Array("hello", "world") + val c = js.Array('a', 'b') + val d = js.Array(Nil) + val e = js.Array(new VC(151189)) + } + """. + hasNot("any of the wrapArray methods") { + case js.Apply(_, js.Ident(name, _), _) + if name.startsWith("wrap") && name.endsWith("__scm_WrappedArray") => + } + + /* Make sure varargs are optimized to use js.WrappedArray instead of + * scm.WrappedArray, for various data types. + */ + """ + import scala.scalajs.js + + class VC(val x: Int) extends AnyVal + + class A { + val a = List(5, 7, 9, -3) + val b = List("hello", "world") + val c = List('a', 'b') + val d = List(Nil) + val e = List(new VC(151189)) + } + """. + hasNot("any of the wrapArray methods") { + case js.Apply(_, js.Ident(name, _), _) + if name.startsWith("wrap") && name.endsWith("__scm_WrappedArray") => + } + + // Make sure our wrapper matcher has the right name + """ + import scala.scalajs.js + + class A { + val a: Seq[Int] = new Array[Int](5) + } + """. + has("one of the wrapArray methods") { + case js.Apply(_, js.Ident(name, _), _) + if name.startsWith("wrap") && name.endsWith("__scm_WrappedArray") => + } + + // Verify the optimized emitted code for 'new js.Object' and 'new js.Array' + """ + import scala.scalajs.js + + class A { + val o = new js.Object + val a = new js.Array + } + """. + hasNot("any reference to the global scope") { + case js.JSEnvInfo() => + } + + } + +} |