diff options
Diffstat (limited to 'examples/scala-js/scalalib/overrides')
7 files changed, 691 insertions, 0 deletions
diff --git a/examples/scala-js/scalalib/overrides/scala/App.scala b/examples/scala-js/scalalib/overrides/scala/App.scala new file mode 100644 index 0000000..c49817b --- /dev/null +++ b/examples/scala-js/scalalib/overrides/scala/App.scala @@ -0,0 +1,83 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2010-2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala + +import scala.compat.Platform.currentTime +import scala.collection.mutable.ListBuffer + +import scala.scalajs.js.annotation.JSExport + +/** The `App` trait can be used to quickly turn objects + * into executable programs. Here is an example: + * {{{ + * object Main extends App { + * Console.println("Hello World: " + (args mkString ", ")) + * } + * }}} + * Here, object `Main` inherits the `main` method of `App`. + * + * `args` returns the current command line arguments as an array. + * + * ==Caveats== + * + * '''''It should be noted that this trait is implemented using the [[DelayedInit]] + * functionality, which means that fields of the object will not have been initialized + * before the main method has been executed.''''' + * + * It should also be noted that the `main` method will not normally need to be overridden: + * the purpose is to turn the whole class body into the “main method”. You should only + * chose to override it if you know what you are doing. + * + * @author Martin Odersky + * @version 2.1, 15/02/2011 + */ +trait App extends DelayedInit { + + /** The time when the execution of this program started, in milliseconds since 1 + * January 1970 UTC. */ + val executionStart: Long = currentTime + + /** The command line arguments passed to the application's `main` method. + */ + protected def args: Array[String] = _args + + private var _args: Array[String] = _ + + private val initCode = new ListBuffer[() => Unit] + + /** The init hook. This saves all initialization code for execution within `main`. + * This method is normally never called directly from user code. + * Instead it is called as compiler-generated code for those classes and objects + * (but not traits) that inherit from the `DelayedInit` trait and that do not + * themselves define a `delayedInit` method. + * @param body the initialization code to be stored for later execution + */ + override def delayedInit(body: => Unit) { + initCode += (() => body) + } + + /** The main method. + * This stores all argument so that they can be retrieved with `args` + * and the executes all initialization code segments in the order they were + * passed to `delayedInit` + * + * @param args the arguments passed to the main method + */ + def main(args: Array[String]) = { + this._args = args + for (proc <- initCode) proc() + + /* DELETED for Scala.js + if (util.Properties.propIsSet("scala.time")) { + val total = currentTime - executionStart + Console.println("[total " + total + "ms]") + } + */ + } +} diff --git a/examples/scala-js/scalalib/overrides/scala/Enumeration.scala b/examples/scala-js/scalalib/overrides/scala/Enumeration.scala new file mode 100644 index 0000000..bdc1701 --- /dev/null +++ b/examples/scala-js/scalalib/overrides/scala/Enumeration.scala @@ -0,0 +1,284 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2002-2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala + +import scala.collection.{ mutable, immutable, generic, SortedSetLike, AbstractSet } +import java.lang.reflect.{ Modifier, Method => JMethod, Field => JField } +import scala.reflect.NameTransformer._ +import java.util.regex.Pattern + +/** Defines a finite set of values specific to the enumeration. Typically + * these values enumerate all possible forms something can take and provide + * a lightweight alternative to case classes. + * + * Each call to a `Value` method adds a new unique value to the enumeration. + * To be accessible, these values are usually defined as `val` members of + * the evaluation. + * + * All values in an enumeration share a common, unique type defined as the + * `Value` type member of the enumeration (`Value` selected on the stable + * identifier path of the enumeration instance). + * + * @example {{{ + * object Main extends App { + * + * object WeekDay extends Enumeration { + * type WeekDay = Value + * val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value + * } + * import WeekDay._ + * + * def isWorkingDay(d: WeekDay) = ! (d == Sat || d == Sun) + * + * WeekDay.values filter isWorkingDay foreach println + * } + * // output: + * // Mon + * // Tue + * // Wed + * // Thu + * // Fri + * }}} + * + * @param initial The initial value from which to count the integers that + * identifies values at run-time. + * @author Matthias Zenger + */ +@SerialVersionUID(8476000850333817230L) +abstract class Enumeration (initial: Int) extends Serializable { + thisenum => + + def this() = this(0) + + /* Note that `readResolve` cannot be private, since otherwise + the JVM does not invoke it when deserializing subclasses. */ + protected def readResolve(): AnyRef = ??? + + /** The name of this enumeration. + */ + override def toString = + (getClass.getName.stripSuffix("$").split('.')).last.split('$').last + + /** The mapping from the integer used to identify values to the actual + * values. */ + private val vmap: mutable.Map[Int, Value] = new mutable.HashMap + + /** The cache listing all values of this enumeration. */ + @transient private var vset: ValueSet = null + @transient @volatile private var vsetDefined = false + + /** The mapping from the integer used to identify values to their + * names. */ + private val nmap: mutable.Map[Int, String] = new mutable.HashMap + + /** The values of this enumeration as a set. + */ + def values: ValueSet = { + if (!vsetDefined) { + vset = (ValueSet.newBuilder ++= vmap.values).result() + vsetDefined = true + } + vset + } + + /** The integer to use to identify the next created value. */ + protected var nextId: Int = initial + + /** The string to use to name the next created value. */ + protected var nextName: Iterator[String] = _ + + private def nextNameOrNull = + if (nextName != null && nextName.hasNext) nextName.next() else null + + /** The highest integer amongst those used to identify values in this + * enumeration. */ + private var topId = initial + + /** The lowest integer amongst those used to identify values in this + * enumeration, but no higher than 0. */ + private var bottomId = if(initial < 0) initial else 0 + + /** The one higher than the highest integer amongst those used to identify + * values in this enumeration. */ + final def maxId = topId + + /** The value of this enumeration with given id `x` + */ + final def apply(x: Int): Value = vmap(x) + + /** Return a `Value` from this `Enumeration` whose name matches + * the argument `s`. The names are determined automatically via reflection. + * + * @param s an `Enumeration` name + * @return the `Value` of this `Enumeration` if its name matches `s` + * @throws NoSuchElementException if no `Value` with a matching + * name is in this `Enumeration` + */ + final def withName(s: String): Value = { + val (unnamed, named) = values partition { + _.toString().startsWith("<Unknown name for enum field ") + } + + named.find(_.toString == s) match { + case Some(v) => v + // If we have unnamed values, we issue a detailed error message + case None if unnamed.nonEmpty => + throw new NoSuchElementException( + s"""Couldn't find enum field with name $s. + |However, there were the following unnamed fields: + |${unnamed.mkString(" ","\n ","")}""".stripMargin) + // Normal case (no unnamed Values) + case _ => None.get + } + } + + /** Creates a fresh value, part of this enumeration. */ + protected final def Value: Value = Value(nextId) + + /** Creates a fresh value, part of this enumeration, identified by the + * integer `i`. + * + * @param i An integer that identifies this value at run-time. It must be + * unique amongst all values of the enumeration. + * @return Fresh value identified by `i`. + */ + protected final def Value(i: Int): Value = Value(i, nextNameOrNull) + + /** Creates a fresh value, part of this enumeration, called `name`. + * + * @param name A human-readable name for that value. + * @return Fresh value called `name`. + */ + protected final def Value(name: String): Value = Value(nextId, name) + + /** Creates a fresh value, part of this enumeration, called `name` + * and identified by the integer `i`. + * + * @param i An integer that identifies this value at run-time. It must be + * unique amongst all values of the enumeration. + * @param name A human-readable name for that value. + * @return Fresh value with the provided identifier `i` and name `name`. + */ + protected final def Value(i: Int, name: String): Value = new Val(i, name) + + /** The type of the enumerated values. */ + @SerialVersionUID(7091335633555234129L) + abstract class Value extends Ordered[Value] with Serializable { + /** the id and bit location of this enumeration value */ + def id: Int + /** a marker so we can tell whose values belong to whom come reflective-naming time */ + private[Enumeration] val outerEnum = thisenum + + override def compare(that: Value): Int = + if (this.id < that.id) -1 + else if (this.id == that.id) 0 + else 1 + override def equals(other: Any) = other match { + case that: Enumeration#Value => (outerEnum eq that.outerEnum) && (id == that.id) + case _ => false + } + override def hashCode: Int = id.## + + /** Create a ValueSet which contains this value and another one */ + def + (v: Value) = ValueSet(this, v) + } + + /** A class implementing the [[scala.Enumeration.Value]] type. This class + * can be overridden to change the enumeration's naming and integer + * identification behaviour. + */ + @SerialVersionUID(0 - 3501153230598116017L) + protected class Val(i: Int, name: String) + extends Value with Serializable { + + def this(i: Int) = this(i, nextNameOrNull) + def this(name: String) = this(nextId, name) + def this() = this(nextId) + + assert(!vmap.isDefinedAt(i), "Duplicate id: " + i) + vmap(i) = this + vsetDefined = false + nextId = i + 1 + if (nextId > topId) topId = nextId + if (i < bottomId) bottomId = i + def id = i + override def toString() = + if (name != null) name + // Scala.js specific + else s"<Unknown name for enum field #$i of class ${getClass}>" + + protected def readResolve(): AnyRef = { + val enum = thisenum.readResolve().asInstanceOf[Enumeration] + if (enum.vmap == null) this + else enum.vmap(i) + } + } + + /** An ordering by id for values of this set */ + object ValueOrdering extends Ordering[Value] { + def compare(x: Value, y: Value): Int = x compare y + } + + /** A class for sets of values. + * Iterating through this set will yield values in increasing order of their ids. + * + * @param nnIds The set of ids of values (adjusted so that the lowest value does + * not fall below zero), organized as a `BitSet`. + */ + class ValueSet private[ValueSet] (private[this] var nnIds: immutable.BitSet) + extends AbstractSet[Value] + with immutable.SortedSet[Value] + with SortedSetLike[Value, ValueSet] + with Serializable { + + implicit def ordering: Ordering[Value] = ValueOrdering + def rangeImpl(from: Option[Value], until: Option[Value]): ValueSet = + new ValueSet(nnIds.rangeImpl(from.map(_.id - bottomId), until.map(_.id - bottomId))) + + override def empty = ValueSet.empty + def contains(v: Value) = nnIds contains (v.id - bottomId) + def + (value: Value) = new ValueSet(nnIds + (value.id - bottomId)) + def - (value: Value) = new ValueSet(nnIds - (value.id - bottomId)) + def iterator = nnIds.iterator map (id => thisenum.apply(bottomId + id)) + // This is only defined in 2.11. We change its implementation so it also + // compiles on 2.10. + def keysIteratorFrom(start: Value) = from(start).keySet.toIterator + //nnIds keysIteratorFrom start.id map (id => thisenum.apply(bottomId + id)) + override def stringPrefix = thisenum + ".ValueSet" + /** Creates a bit mask for the zero-adjusted ids in this set as a + * new array of longs */ + def toBitMask: Array[Long] = nnIds.toBitMask + } + + /** A factory object for value sets */ + object ValueSet { + import generic.CanBuildFrom + + /** The empty value set */ + val empty = new ValueSet(immutable.BitSet.empty) + /** A value set consisting of given elements */ + def apply(elems: Value*): ValueSet = (newBuilder ++= elems).result() + /** A value set containing all the values for the zero-adjusted ids + * corresponding to the bits in an array */ + def fromBitMask(elems: Array[Long]): ValueSet = new ValueSet(immutable.BitSet.fromBitMask(elems)) + /** A builder object for value sets */ + def newBuilder: mutable.Builder[Value, ValueSet] = new mutable.Builder[Value, ValueSet] { + private[this] val b = new mutable.BitSet + def += (x: Value) = { b += (x.id - bottomId); this } + def clear() = b.clear() + def result() = new ValueSet(b.toImmutable) + } + /** The implicit builder for value sets */ + implicit def canBuildFrom: CanBuildFrom[ValueSet, Value, ValueSet] = + new CanBuildFrom[ValueSet, Value, ValueSet] { + def apply(from: ValueSet) = newBuilder + def apply() = newBuilder + } + } +}
\ No newline at end of file diff --git a/examples/scala-js/scalalib/overrides/scala/Symbol.scala b/examples/scala-js/scalalib/overrides/scala/Symbol.scala new file mode 100644 index 0000000..1af9d28 --- /dev/null +++ b/examples/scala-js/scalalib/overrides/scala/Symbol.scala @@ -0,0 +1,117 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala + +import scala.scalajs.js + +/** This class provides a simple way to get unique objects for equal strings. + * Since symbols are interned, they can be compared using reference equality. + * Instances of `Symbol` can be created easily with Scala's built-in quote + * mechanism. + * + * For instance, the [[http://scala-lang.org/#_top Scala]] term `'mysym` will + * invoke the constructor of the `Symbol` class in the following way: + * `Symbol("mysym")`. + * + * @author Martin Odersky, Iulian Dragos + * @version 1.8 + */ +final class Symbol private (val name: String) extends Serializable { + /** Converts this symbol to a string. + */ + override def toString(): String = "'" + name + + @throws(classOf[java.io.ObjectStreamException]) + private def readResolve(): Any = Symbol.apply(name) + override def hashCode = name.hashCode() + override def equals(other: Any) = this eq other.asInstanceOf[AnyRef] +} + +// Modified to use Scala.js specific cache +object Symbol extends JSUniquenessCache[Symbol] { + override def apply(name: String): Symbol = super.apply(name) + protected def valueFromKey(name: String): Symbol = new Symbol(name) + protected def keyFromValue(sym: Symbol): Option[String] = Some(sym.name) +} + +private[scala] abstract class JSUniquenessCache[V] +{ + private val map = js.Dictionary.empty[js.Any] // V | js.Undefined + + protected def valueFromKey(k: String): V + protected def keyFromValue(v: V): Option[String] + + def apply(name: String): V = { + val cachedSym = map(name) + if (js.isUndefined(cachedSym)) { + val sym = valueFromKey(name) + map(name) = sym.asInstanceOf[js.Any] + sym.asInstanceOf[V] + } else { + cachedSym.asInstanceOf[V] + } + } + + def unapply(other: V): Option[String] = keyFromValue(other) +} + +/** This is private so it won't appear in the library API, but + * abstracted to offer some hope of reusability. */ +/* DELETED for Scala.js +private[scala] abstract class UniquenessCache[K >: js.String, V >: Null] +{ + + import java.lang.ref.WeakReference + import java.util.WeakHashMap + import java.util.concurrent.locks.ReentrantReadWriteLock + + private val rwl = new ReentrantReadWriteLock() + private val rlock = rwl.readLock + private val wlock = rwl.writeLock + private val map = new WeakHashMap[K, WeakReference[V]] + + protected def valueFromKey(k: K): V + protected def keyFromValue(v: V): Option[K] + + def apply(name: K): V = { + def cached(): V = { + rlock.lock + try { + val reference = map get name + if (reference == null) null + else reference.get // will be null if we were gc-ed + } + finally rlock.unlock + } + def updateCache(): V = { + wlock.lock + try { + val res = cached() + if (res != null) res + else { + // If we don't remove the old String key from the map, we can + // wind up with one String as the key and a different String as + // as the name field in the Symbol, which can lead to surprising + // GC behavior and duplicate Symbols. See SI-6706. + map remove name + val sym = valueFromKey(name) + map.put(name, new WeakReference(sym)) + sym + } + } + finally wlock.unlock + } + + val res = cached() + if (res == null) updateCache() + else res + } + def unapply(other: V): Option[K] = keyFromValue(other) +} +*/ diff --git a/examples/scala-js/scalalib/overrides/scala/concurrent/impl/AbstractPromise.scala b/examples/scala-js/scalalib/overrides/scala/concurrent/impl/AbstractPromise.scala new file mode 100644 index 0000000..8ea135e --- /dev/null +++ b/examples/scala-js/scalalib/overrides/scala/concurrent/impl/AbstractPromise.scala @@ -0,0 +1,29 @@ +package scala.concurrent.impl + +/** + * JavaScript specific implementation of AbstractPromise + * + * This basically implements a "CAS" in Scala for JavaScript. Its + * implementation is trivial because there is no multi-threading. + * + * @author Tobias Schlatter + */ +abstract class AbstractPromise { + + private var state: AnyRef = _ + + protected final + def updateState(oldState: AnyRef, newState: AnyRef): Boolean = { + if (state eq oldState) { + state = newState + true + } else false + } + + protected final def getState: AnyRef = state + +} + +object AbstractPromise { + protected def updater = ??? +} diff --git a/examples/scala-js/scalalib/overrides/scala/math/ScalaNumber.scala b/examples/scala-js/scalalib/overrides/scala/math/ScalaNumber.scala new file mode 100644 index 0000000..811346d --- /dev/null +++ b/examples/scala-js/scalalib/overrides/scala/math/ScalaNumber.scala @@ -0,0 +1,21 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + + + +package scala.math + +/** A marker class for Number types introduced by Scala + * @author Martin Odersky, Paul Phillips + * @version 2.8 + * @since 2.8 + */ +abstract class ScalaNumber extends java.lang.Number { + protected def isWhole(): Boolean + def underlying(): Object +} diff --git a/examples/scala-js/scalalib/overrides/scala/runtime/BoxesRunTime.scala b/examples/scala-js/scalalib/overrides/scala/runtime/BoxesRunTime.scala new file mode 100644 index 0000000..5df7fd1 --- /dev/null +++ b/examples/scala-js/scalalib/overrides/scala/runtime/BoxesRunTime.scala @@ -0,0 +1,124 @@ +package scala.runtime + +import scala.math.ScalaNumber + +object BoxesRunTime { + def boxToCharacter(c: Char): java.lang.Character = + java.lang.Character.valueOf(c) + + def unboxToChar(c: Object): Char = + if (c eq null) 0 + else c.asInstanceOf[java.lang.Character].charValue() + + def equals(x: Object, y: Object): Boolean = + if (x eq y) true + else equals2(x, y) + + @inline // only called by equals(), not by codegen + def equals2(x: Object, y: Object): Boolean = { + x match { + case xn: java.lang.Number => equalsNumObject(xn, y) + case xc: java.lang.Character => equalsCharObject(xc, y) + case null => y eq null + case _ => x.equals(y) + } + } + + def equalsNumObject(xn: java.lang.Number, y: Object): Boolean = { + y match { + case yn: java.lang.Number => equalsNumNum(xn, yn) + case yc: java.lang.Character => equalsNumChar(xn, yc) + case _ => + if (xn eq null) + y eq null + else + xn.equals(y) + } + } + + def equalsNumNum(xn: java.lang.Number, yn: java.lang.Number): Boolean = { + (xn: Any) match { + case xn: Double => + (yn: Any) match { + case yn: Double => xn == yn + case yn: Long => xn == yn + case yn: ScalaNumber => yn.equals(xn) // xn is not a ScalaNumber + case _ => false // xn.equals(yn) must be false here + } + case xn: Long => + (yn: Any) match { + case yn: Long => xn == yn + case yn: Double => xn == yn + case yn: ScalaNumber => yn.equals(xn) // xn is not a ScalaNumber + case _ => false // xn.equals(yn) must be false here + } + case null => yn eq null + case _ => xn.equals(yn) + } + } + + def equalsCharObject(xc: java.lang.Character, y: Object): Boolean = { + y match { + case yc: java.lang.Character => xc.charValue() == yc.charValue() + case yn: java.lang.Number => equalsNumChar(yn, xc) + case _ => + if (xc eq null) + y eq null + else + false // xc.equals(y) must be false here, because y is not a Char + } + } + + @inline + private def equalsNumChar(xn: java.lang.Number, yc: java.lang.Character): Boolean = { + (xn: Any) match { + case xn: Double => xn == yc.charValue() + case xn: Long => xn == yc.charValue() + case _ => + if (xn eq null) yc eq null + else xn.equals(yc) + } + } + + def hashFromLong(n: java.lang.Long): Int = { + val iv = n.intValue() + if (iv == n.longValue()) iv + else n.hashCode() + } + + def hashFromDouble(n: java.lang.Double): Int = { + val iv = n.intValue() + val dv = n.doubleValue() + if (iv == dv) { + iv + } else { + val lv = n.longValue() + if (lv == dv) { + java.lang.Long.valueOf(lv).hashCode() + } else { + // don't test the case floatValue() == dv + n.hashCode() + } + } + } + + def hashFromFloat(n: java.lang.Float): Int = { + hashFromDouble(java.lang.Double.valueOf(n.doubleValue())) + } + + def hashFromNumber(n: java.lang.Number): Int = { + (n: Any) match { + case n: Int => n + case n: java.lang.Long => hashFromLong(n) + case n: java.lang.Double => hashFromDouble(n) + case n => n.hashCode() + } + } + + def hashFromObject(a: Object): Int = { + a match { + case a: java.lang.Number => hashFromNumber(a) + case a => a.hashCode() + } + } +} diff --git a/examples/scala-js/scalalib/overrides/scala/util/control/NoStackTrace.scala b/examples/scala-js/scalalib/overrides/scala/util/control/NoStackTrace.scala new file mode 100644 index 0000000..bcc2839 --- /dev/null +++ b/examples/scala-js/scalalib/overrides/scala/util/control/NoStackTrace.scala @@ -0,0 +1,33 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala +package util.control + +/** A trait for exceptions which, for efficiency reasons, do not + * fill in the stack trace. Stack trace suppression can be disabled + * on a global basis via a system property wrapper in + * [[scala.sys.SystemProperties]]. + * + * @author Paul Phillips + * @since 2.8 + */ +trait NoStackTrace extends Throwable { + override def fillInStackTrace(): Throwable = + if (NoStackTrace.noSuppression) super.fillInStackTrace() + else this +} + +object NoStackTrace { + final def noSuppression = _noSuppression + + // two-stage init to make checkinit happy, since sys.SystemProperties.noTraceSupression.value calls back into NoStackTrace.noSuppression + final private var _noSuppression = false + // !!! Disabled in Scala.js because SystemProperties is not supported + //_noSuppression = sys.SystemProperties.noTraceSupression.value +} |