Skip to content

Commit

Permalink
Fix #253 (#420)
Browse files Browse the repository at this point in the history
Provides initial implementation for java.lang.Class::getComponentType.
  • Loading branch information
densh committed Dec 1, 2016
1 parent 40afd21 commit 2c32244
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
17 changes: 14 additions & 3 deletions nativelib/src/main/scala/java/lang/Class.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package java.lang

import scala.scalanative.native.Ptr
import scala.scalanative.runtime.Type
import scalanative.native.Ptr
import scalanative.runtime.{Array => _, _}

final class _Class[A](val ty: Ptr[Type]) {
def getName(): String = (!ty).name
Expand All @@ -15,10 +15,21 @@ final class _Class[A](val ty: Ptr[Type]) {
false
}

def getComponentType(): _Class[_] = {
if (ty == typeof[BooleanArray]) classOf[scala.Boolean]
else if (ty == typeof[CharArray]) classOf[scala.Char]
else if (ty == typeof[ByteArray]) classOf[scala.Byte]
else if (ty == typeof[ShortArray]) classOf[scala.Short]
else if (ty == typeof[IntArray]) classOf[scala.Int]
else if (ty == typeof[LongArray]) classOf[scala.Long]
else if (ty == typeof[FloatArray]) classOf[scala.Float]
else if (ty == typeof[DoubleArray]) classOf[scala.Double]
else classOf[java.lang.Object]
}

// TODO:
def getInterfaces(): Array[_Class[_]] = ???
def getSuperclass(): _Class[_] = ???
def getComponentType(): _Class[_] = ???
def isArray(): scala.Boolean = ???
}

Expand Down
17 changes: 17 additions & 0 deletions unit-tests/src/main/scala/java/lang/ClassSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package java.lang

object ClassSuite extends tests.Suite {
test("getComponentType") {
assert(Array(false).getClass.getComponentType == classOf[scala.Boolean])
assert(Array('0').getClass.getComponentType == classOf[scala.Char])
assert(Array(0.toByte).getClass.getComponentType == classOf[scala.Byte])
assert(Array(0.toShort).getClass.getComponentType == classOf[scala.Short])
assert(Array(0).getClass.getComponentType == classOf[scala.Int])
assert(Array(0L).getClass.getComponentType == classOf[scala.Long])
assert(Array(0F).getClass.getComponentType == classOf[scala.Float])
assert(Array(0D).getClass.getComponentType == classOf[scala.Double])
assert(
Array(new java.lang.Object).getClass.getComponentType == classOf[
java.lang.Object])
}
}
13 changes: 13 additions & 0 deletions unit-tests/src/main/scala/scala/scalanative/issues/_253Suite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package scala.scalanative.issues

object _253Suite extends tests.Suite {
test("#253") {
class Cell(val value: Int)

val arr = Array(new Cell(1), new Cell(2))
val reverse = arr.reverse

assert(reverse(0).value == 2)
assert(reverse(1).value == 1)
}
}

0 comments on commit 2c32244

Please sign in to comment.