diff options
author | Sébastien Doeraene <sjrdoeraene@gmail.com> | 2016-03-23 11:51:25 +0100 |
---|---|---|
committer | Dmitry Petrashko <dmitry.petrashko@gmail.com> | 2016-04-18 14:44:23 +0200 |
commit | 27deb1e0d41a4dfc24a4cac8a311c30471d454e5 (patch) | |
tree | ef9001d9b1bf141f4e4887d1525315f8d20e404b /src/dotty/runtime/Arrays.scala | |
parent | 88baa04f2bc6b89d3e65226973d7b72fdf715095 (diff) | |
download | dotty-27deb1e0d41a4dfc24a4cac8a311c30471d454e5.tar.gz dotty-27deb1e0d41a4dfc24a4cac8a311c30471d454e5.tar.bz2 dotty-27deb1e0d41a4dfc24a4cac8a311c30471d454e5.zip |
Fix #1167: Remove the magic from Arrays.newRefArray.
Previously, the method `Arrays.newRefArray` was one of the only 3
methods that are kept generic after erasure. This commit removes
this magic, by making it take an actual `j.l.Class[T]` as
parameter.
Moreover, the methods `newXArray` all receive an actual body,
implemented on top of Java reflection, which means that a back-end
does not *have to* special-case those methods for correctness.
It might still be required for performance, though, depending on
the back-end.
The JVM back-end is made non-optimal in this commit, precisely
because it does not specialize that method anymore. Doing so
requires modifying the fork of scalac that we use, which should
be done separately.
The JS back-end is adapted simply by doing nothing at all on any
of the newXArray methods. It will normally call the user-space
implementations which use reflection. The Scala.js optimizer will
inline and intrinsify the reflective calls, producing optimal
code, at the end of the day.
Diffstat (limited to 'src/dotty/runtime/Arrays.scala')
-rw-r--r-- | src/dotty/runtime/Arrays.scala | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/src/dotty/runtime/Arrays.scala b/src/dotty/runtime/Arrays.scala index 4469dced7..2771df66b 100644 --- a/src/dotty/runtime/Arrays.scala +++ b/src/dotty/runtime/Arrays.scala @@ -2,6 +2,8 @@ package dotty.runtime import scala.reflect.ClassTag +import java.lang.{reflect => jlr} + /** All but the first two operations should be short-circuited and implemented specially by * the backend. */ @@ -22,35 +24,44 @@ object Arrays { arr } - /** Create an array of type T. T must be of form Array[E], with - * E being a reference type. + /** Create an array of a reference type T. */ - def newRefArray[T](length: Int): T = ??? + def newRefArray[T](componentType: Class[T])(length: Int): Array[T] = + jlr.Array.newInstance(componentType, length).asInstanceOf[Array[T]] /** Create a Byte[] array */ - def newByteArray(length: Int): Array[Byte] = ??? + def newByteArray(length: Int): Array[Byte] = + jlr.Array.newInstance(classOf[Byte], length).asInstanceOf[Array[Byte]] /** Create a Short[] array */ - def newShortArray(length: Int): Array[Short] = ??? + def newShortArray(length: Int): Array[Short] = + jlr.Array.newInstance(classOf[Short], length).asInstanceOf[Array[Short]] /** Create a Char[] array */ - def newCharArray(length: Int): Array[Char] = ??? + def newCharArray(length: Int): Array[Char] = + jlr.Array.newInstance(classOf[Char], length).asInstanceOf[Array[Char]] /** Create an Int[] array */ - def newIntArray(length: Int): Array[Int] = ??? + def newIntArray(length: Int): Array[Int] = + jlr.Array.newInstance(classOf[Int], length).asInstanceOf[Array[Int]] /** Create a Long[] array */ - def newLongArray(length: Int): Array[Long] = ??? + def newLongArray(length: Int): Array[Long] = + jlr.Array.newInstance(classOf[Long], length).asInstanceOf[Array[Long]] /** Create a Float[] array */ - def newFloatArray(length: Int): Array[Float] = ??? + def newFloatArray(length: Int): Array[Float] = + jlr.Array.newInstance(classOf[Float], length).asInstanceOf[Array[Float]] /** Create a Double[] array */ - def newDoubleArray(length: Int): Array[Double] = ??? + def newDoubleArray(length: Int): Array[Double] = + jlr.Array.newInstance(classOf[Double], length).asInstanceOf[Array[Double]] /** Create a Boolean[] array */ - def newBooleanArray(length: Int): Array[Boolean] = ??? + def newBooleanArray(length: Int): Array[Boolean] = + jlr.Array.newInstance(classOf[Boolean], length).asInstanceOf[Array[Boolean]] /** Create a scala.runtime.BoxedUnit[] array */ - def newUnitArray(length: Int): Array[Unit] = ??? + def newUnitArray(length: Int): Array[Unit] = + jlr.Array.newInstance(classOf[scala.runtime.BoxedUnit], length).asInstanceOf[Array[Unit]] } |