diff options
author | Haoyi Li <haoyi@haoyi-mbp.corp.dropbox.com> | 2014-11-26 00:45:31 -0800 |
---|---|---|
committer | Haoyi Li <haoyi@haoyi-mbp.corp.dropbox.com> | 2014-11-26 00:45:31 -0800 |
commit | 24f31e120f9537faede7a174bb09ee35f64e1ce4 (patch) | |
tree | 06ffc3ecc7847789008352b7e2b7c040dad48907 /examples/scala-js/library-aux/src | |
parent | b89ce9cbf79363f8cab09186a5d7ba94bc0af02a (diff) | |
parent | 2c4b142503bd2d871e6818b5cab8c38627d9e4a0 (diff) | |
download | hands-on-scala-js-24f31e120f9537faede7a174bb09ee35f64e1ce4.tar.gz hands-on-scala-js-24f31e120f9537faede7a174bb09ee35f64e1ce4.tar.bz2 hands-on-scala-js-24f31e120f9537faede7a174bb09ee35f64e1ce4.zip |
Merge commit '2c4b142503bd2d871e6818b5cab8c38627d9e4a0' as 'examples/scala-js'
Diffstat (limited to 'examples/scala-js/library-aux/src')
4 files changed, 288 insertions, 0 deletions
diff --git a/examples/scala-js/library-aux/src/main/scala/scala/runtime/ArrayRuntime.scala b/examples/scala-js/library-aux/src/main/scala/scala/runtime/ArrayRuntime.scala new file mode 100644 index 0000000..ceda199 --- /dev/null +++ b/examples/scala-js/library-aux/src/main/scala/scala/runtime/ArrayRuntime.scala @@ -0,0 +1,16 @@ +package scala.runtime + +/** Not for public consumption. Usage by the runtime only. + */ + +object ArrayRuntime { + def cloneArray(array: Array[Boolean]): Array[Boolean] = array.clone() + def cloneArray(array: Array[Char]): Array[Char] = array.clone() + def cloneArray(array: Array[Byte]): Array[Byte] = array.clone() + def cloneArray(array: Array[Short]): Array[Short] = array.clone() + def cloneArray(array: Array[Int]): Array[Int] = array.clone() + def cloneArray(array: Array[Long]): Array[Long] = array.clone() + def cloneArray(array: Array[Float]): Array[Float] = array.clone() + def cloneArray(array: Array[Double]): Array[Double] = array.clone() + def cloneArray(array: Array[Object]): Array[Object] = array.clone() +} diff --git a/examples/scala-js/library-aux/src/main/scala/scala/runtime/BoxedUnit.scala b/examples/scala-js/library-aux/src/main/scala/scala/runtime/BoxedUnit.scala new file mode 100644 index 0000000..b6ac773 --- /dev/null +++ b/examples/scala-js/library-aux/src/main/scala/scala/runtime/BoxedUnit.scala @@ -0,0 +1,18 @@ +package scala.runtime + +/* This is a hijacked class. Its only instance is the value 'undefined'. + * Constructors are not emitted. + */ +class BoxedUnit private { + @inline override def equals(that: Any): Boolean = + this eq that.asInstanceOf[AnyRef] + + @inline override def hashCode(): Int = 0 + + @inline override def toString(): String = "undefined" +} + +object BoxedUnit { + val UNIT: BoxedUnit = sys.error("stub") + val TYPE: Class[Void] = sys.error("stub") +} diff --git a/examples/scala-js/library-aux/src/main/scala/scala/runtime/RefTypes.scala b/examples/scala-js/library-aux/src/main/scala/scala/runtime/RefTypes.scala new file mode 100644 index 0000000..4724d13 --- /dev/null +++ b/examples/scala-js/library-aux/src/main/scala/scala/runtime/RefTypes.scala @@ -0,0 +1,165 @@ +package scala.runtime + +import java.io.Serializable + +@inline +class BooleanRef(var elem: Boolean) extends Serializable { + override def toString() = String.valueOf(elem) +} +object BooleanRef { + def create(elem: Boolean): BooleanRef = new BooleanRef(elem) + def zero(): BooleanRef = new BooleanRef(false) +} + +@inline +class VolatileBooleanRef(var elem: Boolean) extends Serializable { + override def toString() = String.valueOf(elem) +} +object VolatileBooleanRef { + def create(elem: Boolean): VolatileBooleanRef = new VolatileBooleanRef(elem) + def zero(): VolatileBooleanRef = new VolatileBooleanRef(false) +} + +@inline +class CharRef(var elem: Char) extends Serializable { + override def toString() = String.valueOf(elem) +} +object CharRef { + def create(elem: Char): CharRef = new CharRef(elem) + def zero(): CharRef = new CharRef(0.toChar) +} + +@inline +class VolatileCharRef(var elem: Char) extends Serializable { + override def toString() = String.valueOf(elem) +} +object VolatileCharRef { + def create(elem: Char): VolatileCharRef = new VolatileCharRef(elem) + def zero(): VolatileCharRef = new VolatileCharRef(0.toChar) +} + +@inline +class ByteRef(var elem: Byte) extends Serializable { + override def toString() = String.valueOf(elem) +} +object ByteRef { + def create(elem: Byte): ByteRef = new ByteRef(elem) + def zero(): ByteRef = new ByteRef(0) +} + +@inline +class VolatileByteRef(var elem: Byte) extends Serializable { + override def toString() = String.valueOf(elem) +} +object VolatileByteRef { + def create(elem: Byte): VolatileByteRef = new VolatileByteRef(elem) + def zero(): VolatileByteRef = new VolatileByteRef(0) +} + +@inline +class ShortRef(var elem: Short) extends Serializable { + override def toString() = String.valueOf(elem) +} +object ShortRef { + def create(elem: Short): ShortRef = new ShortRef(elem) + def zero(): ShortRef = new ShortRef(0) +} + +@inline +class VolatileShortRef(var elem: Short) extends Serializable { + override def toString() = String.valueOf(elem) +} +object VolatileShortRef { + def create(elem: Short): VolatileShortRef = new VolatileShortRef(elem) + def zero(): VolatileShortRef = new VolatileShortRef(0) +} + +@inline +class IntRef(var elem: Int) extends Serializable { + override def toString() = String.valueOf(elem) +} +object IntRef { + def create(elem: Int): IntRef = new IntRef(elem) + def zero(): IntRef = new IntRef(0) +} + +@inline +class VolatileIntRef(var elem: Int) extends Serializable { + override def toString() = String.valueOf(elem) +} +object VolatileIntRef { + def create(elem: Int): VolatileIntRef = new VolatileIntRef(elem) + def zero(): VolatileIntRef = new VolatileIntRef(0) +} + +@inline +class LongRef(var elem: Long) extends Serializable { + override def toString() = String.valueOf(elem) +} +object LongRef { + def create(elem: Long): LongRef = new LongRef(elem) + def zero(): LongRef = new LongRef(0) +} + +@inline +class VolatileLongRef(var elem: Long) extends Serializable { + override def toString() = String.valueOf(elem) +} +object VolatileLongRef { + def create(elem: Long): VolatileLongRef = new VolatileLongRef(elem) + def zero(): VolatileLongRef = new VolatileLongRef(0) +} + +@inline +class FloatRef(var elem: Float) extends Serializable { + override def toString() = String.valueOf(elem) +} +object FloatRef { + def create(elem: Float): FloatRef = new FloatRef(elem) + def zero(): FloatRef = new FloatRef(0) +} + +@inline +class VolatileFloatRef(var elem: Float) extends Serializable { + override def toString() = String.valueOf(elem) +} +object VolatileFloatRef { + def create(elem: Float): VolatileFloatRef = new VolatileFloatRef(elem) + def zero(): VolatileFloatRef = new VolatileFloatRef(0) +} + +@inline +class DoubleRef(var elem: Double) extends Serializable { + override def toString() = String.valueOf(elem) +} +object DoubleRef { + def create(elem: Double): DoubleRef = new DoubleRef(elem) + def zero(): DoubleRef = new DoubleRef(0) +} + +@inline +class VolatileDoubleRef(var elem: Double) extends Serializable { + override def toString() = String.valueOf(elem) +} +object VolatileDoubleRef { + def create(elem: Double): VolatileDoubleRef = new VolatileDoubleRef(elem) + def zero(): VolatileDoubleRef = new VolatileDoubleRef(0) +} + +@inline +class ObjectRef[A](var elem: A) extends Serializable { + override def toString() = String.valueOf(elem) +} +object ObjectRef { + def create[A](elem: A): ObjectRef[A] = new ObjectRef(elem) + def zero(): ObjectRef[Object] = new ObjectRef(null) +} + +@inline +class VolatileObjectRef[A](var elem: A) extends Serializable { + override def toString() = String.valueOf(elem) +} +object VolatileObjectRef { + def create[A](elem: A): VolatileObjectRef[A] = new VolatileObjectRef(elem) + def zero(): VolatileObjectRef[Object] = new VolatileObjectRef(null) +} diff --git a/examples/scala-js/library-aux/src/main/scala/scala/runtime/Statics.scala b/examples/scala-js/library-aux/src/main/scala/scala/runtime/Statics.scala new file mode 100644 index 0000000..b4e7e52 --- /dev/null +++ b/examples/scala-js/library-aux/src/main/scala/scala/runtime/Statics.scala @@ -0,0 +1,89 @@ +package scala.runtime + +/** Not for public consumption. Usage by the runtime only. + */ + +object Statics { + def mix(hash: Int, data: Int): Int = { + var h = mixLast(hash, data) + h = Integer.rotateLeft(h, 13) + (h * 5) + 0xe6546b64 + } + + def mixLast(hash: Int, data: Int): Int = { + var k = data + k *= 0xcc9e2d51 + k = Integer.rotateLeft(k, 15) + k *= 0x1b873593 + hash ^ k + } + + def finalizeHash(hash: Int, length: Int): Int = { + avalanche(hash ^ length) + } + + /** Force all bits of the hash to avalanche. Used for finalizing the hash. */ + def avalanche(h0: Int): Int = { + var h = h0 + h ^= h >>> 16 + h *= 0x85ebca6b + h ^= h >>> 13 + h *= 0xc2b2ae35 + h ^= h >>> 16 + h + } + + def longHash(lv: Long): Int = { + lv.toInt + /* + val iv = lv.toInt | 0 + if (iv == lv) iv + else (lv ^ (lv >>> 32)).toInt | 0 + */ + } + + def doubleHash(dv: Double): Int = { + dv.toInt + /* + val iv = dv.toInt | 0 + if (iv == dv) + return iv + + val fv = dv.toFloat + if (fv == dv) + return java.lang.Float.floatToIntBits(fv) + + val lv = dv.toLong + if (lv == dv) + return lv.toInt | 0 + + val lv2 == java.lang.Double.doubleToLongBits(dv) + return (lv2 ^ (lv2 >>> 32)).toInt | 0 + */ + } + + def floatHash(fv: Float): Int = { + fv.toInt + /* + val iv = fv.toInt + if (iv == fv) + return iv + + val lv = fv.toLong + if (lv == fv) + return (lv ^ (lv >>> 32)).toInt | 0 + + return java.lang.Float.floatToIntBits(fv) + */ + } + + def anyHash(x: Any): Int = { + x match { + case null => 0 + case x: Long => longHash(x) + case x: Double => doubleHash(x) + case x: Float => floatHash(x) + case _ => x.hashCode() + } + } +} |