aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/runtime/vc/VCObjectPrototype.scala
blob: fd0c8728a5233cbc453ed5de95aecd251fcc6805 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package dotty.runtime.vc

import scala.reflect.ClassTag

import scala.runtime.Statics

abstract class VCObjectPrototype(val underlying: Object) extends VCPrototype {}

abstract class VCObjectCasePrototype(underlying: Object) extends VCObjectPrototype(underlying) with Product1[Object] {

  final def _1: Object = underlying

  override final def hashCode(): Int = {
    underlying.hashCode()
  }

  override final def toString: String = {
    s"$productPrefix($underlying)"
  }

  // subclasses are expected to implement equals, productPrefix, and canEqual
}

abstract class VCObjectCompanion[T <: VCObjectPrototype] extends ClassTag[T] {
  def box(underlying: Object): T
  final def unbox(boxed: T) = boxed.underlying

  implicit def classTag: this.type = this
  override def newArray(len: Int): Array[T] =
    new VCObjectArray(this, len).asInstanceOf[Array[T]]


  final def _1$extension(underlying: Object)       = underlying
  final def hashCode$extension(underlying: Object) = underlying.hashCode()
  final def toString$extension(underlying: Object) = s"${productPrefix$extension(underlying)}($underlying)"
  def productPrefix$extension(underlying: Object): String
}

final class VCObjectArray[T <: VCObjectPrototype] private (val arr: Array[Object], val ct: VCObjectCompanion[T])
  extends VCArrayPrototype[T] {
  def this(ct: VCObjectCompanion[T], sz: Int) =
    this(new Array[Object](sz), ct)

  def apply(idx: Int) =
    ct.box(arr(idx))
  def update(idx: Int, elem: T) =
    arr(idx) = ct.unbox(elem)
  def length: Int = arr.length

  override def clone(): VCObjectArray[T] = {
    new VCObjectArray[T](arr.clone(), ct)
  }

  override def toString: String = {
    "[" + ct.runtimeClass
  }

  // Todo: what was the reason for 255 classes in my original proposal? arr.toString!
  // todo: need to discuss do we want to be compatible with ugly format of jvm here?
}