diff options
Diffstat (limited to 'examples/scala-js/library/src/main/scala/scala/scalajs/js')
45 files changed, 4110 insertions, 0 deletions
diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/Array.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/Array.scala new file mode 100644 index 0000000..4c9cf23 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/Array.scala @@ -0,0 +1,173 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + +/** + * All doc-comments marked as "MDN" are by Mozilla Contributors, + * distributed under the Creative Commons Attribution-ShareAlike license from + * https://developer.mozilla.org/en-US/docs/Web/Reference/API + */ +package scala.scalajs.js + +import annotation._ + +/** + * Arrays are list-like objects whose prototype has methods to perform + * traversal and mutation operations. Neither the length of a JavaScript + * array nor the types of its elements are fixed. Since an array's size + * length grow or shrink at any time, JavaScript arrays are not guaranteed + * to be dense. In general, these are convenient characteristics; but if + * these features are not desirable for your particular use, you might + * consider using typed arrays. + * + * MDN + * + * To construct a new array with uninitialized elements, use the constructor + * of this class. To construct a new array with specified elements, as if + * you used the array literal syntax in JavaScript, use the + * [[Array$.apply Array.apply]] method instead. + * + * @tparam A Type of the elements of the array + * + * @constructor Creates a new array of length 0. + */ +class Array[A] extends Object { + /** Creates a new array with the given length. + * @param arrayLength Initial length of the array. + */ + def this(arrayLength: Int) = this() + + // Do not expose this one - use js.Array(item1, item2, ...) instead + // def this(items: A*) = this() + + /** Length of the array. */ + def length: Int = native + + /** Sets the length of the array. + * If the new length is bigger than the old length, created slots are + * filled with `undefined` (irrespective of the type argument `A`!). + * If the new length is smaller than the old length, the array is shrunk. + */ + def length_=(v: Int): Unit = native + + /** Access the element at the given index. */ + @JSBracketAccess + def apply(index: Int): A = native + /** Set the element at the given index. */ + @JSBracketAccess + def update(index: Int, value: A): Unit = native + + /** + * concat creates a new array consisting of the elements in the this object + * on which it is called, followed in order by, for each argument, the + * elements of that argument (if the argument is an array) or the argument + * itself (if the argument is not an array). + * + * MDN + */ + def concat[B >: A](items: Array[_ <: B]*): Array[B] = native + + /** + * The join() method joins all elements of an array into a string. + * + * separator Specifies a string to separate each element of the array. + * The separator is converted to a string if necessary. If omitted, the + * array elements are separated with a comma. + */ + def join(seperator: String = ","): String = native + + /** + * The pop() method removes the last element from an array and returns that + * element. + * + * MDN + */ + def pop(): A = native + + /** + * The push() method mutates an array by appending the given elements and + * returning the new length of the array. + * + * MDN + */ + def push(items: A*): Int = native + + /** + * The reverse() method reverses an array in place. The first array element + * becomes the last and the last becomes the first. + * + * MDN + */ + @JSName("reverse") + def reverseInPlace(): Array[A] = native + + /** + * The shift() method removes the first element from an array and returns that + * element. This method changes the length of the array. + * + * MDN + */ + def shift(): A = native + + /** + * The slice() method returns a shallow copy of a portion of an array. + * + * MDN + */ + @JSName("slice") + def jsSlice(start: Int = 0, end: Int = Int.MaxValue): Array[A] = native + + /** + * The sort() method sorts the elements of an array in place and returns the + * array. The sort is not necessarily stable. The default sort order is + * lexicographic (not numeric). + * + * If compareFunction is not supplied, elements are sorted by converting them + * to strings and comparing strings in lexicographic ("dictionary" or "telephone + * book," not numerical) order. For example, "80" comes before "9" in + * lexicographic order, but in a numeric sort 9 comes before 80. + * + * MDN + */ + def sort(compareFn: Function2[A, A, Int] = ???): Array[A] = native + + /** Removes and adds new elements at a given index in the array. + * + * This method first removes `deleteCount` elements starting from the index + * `index`, then inserts the new elements `items` at that index. + * + * If `index` is negative, it is treated as that number of elements starting + * from the end of the array. + * + * @param index Index where to start changes + * @param deleteCount Number of elements to delete from index + * @param items Elements to insert at index + * @return An array of the elements that were deleted + */ + def splice(index: Int, deleteCount: Int, items: A*): Array[A] = native + + /** + * The unshift() method adds one or more elements to the beginning of an array + * and returns the new length of the array. + * + * MDN + */ + def unshift(items: A*): Int = native +} + +/** Factory for [[js.Array]] objects. */ +object Array extends Object { + // Do not expose this one - use new Array(len) instead + // def apply[A](arrayLength: Int): Array[A] = native + + /** Creates a new array with the given items. */ + def apply[A](items: A*): Array[A] = sys.error("stub") + + /** Returns true if the given value is an array. */ + def isArray(arg: Any): Boolean = native +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/ArrayOps.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/ArrayOps.scala new file mode 100644 index 0000000..f7948ca --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/ArrayOps.scala @@ -0,0 +1,119 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + +package scala.scalajs.js + +import scala.annotation.tailrec + +import scala.collection.mutable +import mutable.Builder + +/** Equivalent of scm.ArrayOps for js.Array */ +@inline +final class ArrayOps[A](private[this] val array: Array[A]) + extends mutable.ArrayLike[A, Array[A]] + with Builder[A, Array[A]] { + + import ArrayOps._ + + /** Creates a new empty [[ArrayOps]]. */ + def this() = this(Array()) + + // Implementation of ArrayLike + + @inline def apply(index: Int): A = array(index) + @inline def length: Int = array.length + @inline def update(index: Int, element: A): Unit = array(index) = element + + def seq: IndexedSeq[A] = new WrappedArray(array) + + override def repr: Array[A] = array + + override protected[this] def thisCollection: mutable.IndexedSeq[A] = + toCollection(array) + override protected[this] def toCollection( + repr: Array[A]): mutable.IndexedSeq[A] = new WrappedArray(repr) + + protected[this] def newBuilder: Builder[A, Array[A]] = + new ArrayOps[A] + + // Implementation of Builder + + @inline def +=(elem: A): this.type = { + array.push(elem) + this + } + + @inline def clear(): Unit = + array.length = 0 + + @inline def result(): Array[A] = array + + // Scala notation for a fast concat() + + @inline def ++[B >: A](that: Array[_ <: B]): Array[B] = + concat(array, that) + + // Methods whose inherited implementations do not play nice with the optimizer + + override def reduceLeft[B >: A](op: (B, A) => B): B = { + val length = this.length + if (length <= 0) + throwUnsupported("empty.reduceLeft") + + @inline + @tailrec + def loop(start: Int, z: B): B = + if (start == length) z + else loop(start+1, op(z, this(start))) + + loop(1, this(0)) + } + + override def reduceRight[B >: A](op: (A, B) => B): B = { + val length = this.length + if (length <= 0) + throwUnsupported("empty.reduceRight") + + @inline + @tailrec + def loop(end: Int, z: B): B = + if (end == 0) z + else loop(end-1, op(this(end-1), z)) + + loop(length-1, this(length-1)) + } + +} + +object ArrayOps { + + /** Extract the throw in a separate, non-inlineable method. */ + private def throwUnsupported(msg: String): Nothing = + throw new UnsupportedOperationException(msg) + + /** Non-inlined implementation of [[ArrayOps.++]]. */ + private def concat[A](left: Array[_ <: A], right: Array[_ <: A]): Array[A] = { + val leftLength = left.length + val rightLength = right.length + val result = new Array[A](leftLength + rightLength) + + @inline + @tailrec + def loop(src: Array[_ <: A], i: Int, len: Int, offset: Int): Unit = + if (i != len) { + result(i+offset) = src(i) + loop(src, i+1, len, offset) + } + + loop(left, 0, leftLength, 0) + loop(right, 0, rightLength, leftLength) + result + } + +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/Date.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/Date.scala new file mode 100644 index 0000000..18795c7 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/Date.scala @@ -0,0 +1,225 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + +/** + * All doc-comments marked as "MDN" are by Mozilla Contributors, + * distributed under the Creative Commons Attribution-ShareAlike license from + * https://developer.mozilla.org/en-US/docs/Web/Reference/API + */ +package scala.scalajs.js + +/** + * Creates a JavaScript Date instance that represents a single moment in time. + * Date objects are based on a time value that is the number of milliseconds + * since 1 January, 1970 UTC. + * + * MDN + */ +class Date extends Object { + + def this(value: Double) = this() + def this(value: String) = this() + + def this(year: Int, month: Int, date: Int = 1, hours: Int = 0, + minutes: Int = 0, seconds: Int = 0, ms: Int = 0) = this() + + def toDateString(): String = native + def toTimeString(): String = native + def toLocaleDateString(): String = native + def toLocaleTimeString(): String = native + + override def valueOf(): Double = native + + def getTime(): Double = native + + /** + * Returns the year (4 digits for 4-digit years) of the specified date according to local time. + * + * MDN + */ + def getFullYear(): Int = native + + /** + * Returns the year (4 digits for 4-digit years) in the specified date according to universal time. + * + * MDN + */ + def getUTCFullYear(): Int = native + + /** + * Returns the month (0-11) in the specified date according to local time. + * + * MDN + */ + def getMonth(): Int = native + + /** + * Returns the month (0-11) in the specified date according to universal time. + * + * MDN + */ + def getUTCMonth(): Int = native + + /** + * Returns the day of the month (1-31) for the specified date according to local time. + * + * MDN + */ + def getDate(): Int = native + + /** + * Returns the day (date) of the month (1-31) in the specified date according to universal time. + * + * MDN + */ + def getUTCDate(): Int = native + + /** + * Returns the day of the week (0-6) for the specified date according to local time. + * + * MDN + */ + def getDay(): Int = native + + /** + * Returns the day of the week (0-6) in the specified date according to universal time. + * MDN + */ + def getUTCDay(): Int = native + + /** + * Returns the hour (0-23) in the specified date according to local time. + * + * MDN + */ + def getHours(): Int = native + + /** + * Returns the hours (0-23) in the specified date according to universal time. + * + * MDN + */ + def getUTCHours(): Int = native + + /** + * Returns the minutes (0-59) in the specified date according to local time. + * + * MDN + */ + def getMinutes(): Int = native + + /** + * Returns the minutes (0-59) in the specified date according to universal time. + * + * MDN + */ + def getUTCMinutes(): Int = native + + /** + * Returns the seconds (0-59) in the specified date according to local time. + * + * MDN + */ + def getSeconds(): Int = native + + /** + * Returns the seconds (0-59) in the specified date according to universal time. + * + * MDN + */ + def getUTCSeconds(): Int = native + + /** + * Returns the milliseconds (0-999) in the specified date according to local time. + * + * MDN + */ + def getMilliseconds(): Int = native + + /** + * Returns the milliseconds (0-999) in the specified date according to universal time. + * + * MDN + */ + def getUTCMilliseconds(): Int = native + + /** + * Returns the time-zone offset in minutes for the current locale. + * + * MDN + */ + def getTimezoneOffset(): Int = native + + def setTime(time: Double): Unit = native + def setMilliseconds(ms: Int): Unit = native + def setUTCMilliseconds(ms: Int): Unit = native + def setSeconds(sec: Int, ms: Int = getMilliseconds()): Unit = native + def setUTCSeconds(sec: Int, ms: Int = getMilliseconds()): Unit = native + def setMinutes(min: Int, sec: Int = getSeconds(), + ms: Int = getMilliseconds()): Unit = native + def setUTCMinutes(min: Int, sec: Int = getSeconds(), + ms: Int = getMilliseconds()): Unit = native + def setHours(hours: Int, min: Int = getMinutes(), + sec: Int = getSeconds(), ms: Int = getMilliseconds()): Unit = native + def setUTCHours(hours: Int, min: Int = getMinutes(), + sec: Int = getSeconds(), ms: Int = getMilliseconds()): Unit = native + + def setDate(date: Int): Unit = native + def setUTCDate(date: Int): Unit = native + def setMonth(month: Int, date: Int = getDate()): Unit = native + def setUTCMonth(month: Int, date: Int = getDate()): Unit = native + def setFullYear(year: Int, month: Int = getMonth(), + date: Int = getDate()): Unit = native + def setUTCFullYear(year: Int, month: Int = getMonth(), + date: Int = getDate()): Unit = native + + def toUTCString(): String = native + def toISOString(): String = native + def toJSON(key: Any): String = native + def toJSON(): String = native +} + +/** Factory for [[js.Date]] objects. */ +object Date extends Object { + def apply(): String = native + + /** + * Parses a string representation of a date and returns the number of + * milliseconds since 1 January, 1970, 00:00:00, local time. + * + * The parse method takes a date string (such as "Dec 25, 1995") and returns + * the number of milliseconds since January 1, 1970, 00:00:00 UTC. The local + * time zone is used to interpret arguments that do not contain time zone + * information. This function is useful for setting date values based on + * string values, for example in conjunction with the setTime() method and + * the Date object. + * + * Given a string representing a time, parse returns the time value. It + * accepts the RFC2822 / IETF date syntax (RFC2822 Section 3.3), e.g. + * "Mon, 25 Dec 1995 13:30:00 GMT". It understands the continental US time- + * zone abbreviations, but for general use, use a time-zone offset, for + * example, "Mon, 25 Dec 1995 13:30:00 +0430" (4 hours, 30 minutes east of + * the Greenwich meridian). If you do not specify a time zone, the local time + * zone is assumed. GMT and UTC are considered equivalent. + * + * MDN + */ + def parse(s: String): Int = native + + def UTC(year: Int, month: Int, date: Int = 1, hours: Int = 0, + minutes: Int = 0, seconds: Int = 0, ms: Int = 0): Double = native + + /** + * Returns the numeric value corresponding to the current time - the number + * of milliseconds elapsed since 1 January 1970 00:00:00 UTC. + * + * MDN + */ + def now(): Double = native +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/Dictionary.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/Dictionary.scala new file mode 100644 index 0000000..fa68d08 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/Dictionary.scala @@ -0,0 +1,92 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + +/** + * All doc-comments marked as "MDN" are by Mozilla Contributors, + * distributed under the Creative Commons Attribution-ShareAlike license from + * https://developer.mozilla.org/en-US/docs/Web/Reference/API + */ +package scala.scalajs.js + +import annotation.JSBracketAccess + +/** Dictionary "view" of a JavaScript value. + * + * Using objects as dictionaries (maps from strings to values) through their + * properties is a common idiom in JavaScript. This trait lets you treat an + * object as such a dictionary. + * + * To use it, cast your object, say `x`, into a [[Dictionary]] using + * {{{ + * val xDict = x.asInstanceOf[js.Dictionary[Int]] + * }}} + * then use it as + * {{{ + * xDict("prop") = 5 + * println(xDict("prop")) // displays 5 + * xDict.delete("prop") // removes the property "prop" + * println(xDict("prop")) // displays undefined + * }}} + * + * To enumerate all the keys of a dictionary, use [[js.Object.keys]], which + * returns a [[js.Array]] of the properties. It can be used in a for + * comprehension like this: + * {{{ + * for (prop <- js.Object.keys(xDict)) { + * val value = xDict(prop) + * println(prop + " -> " + value) + * } + * }}} + */ +sealed trait Dictionary[A] extends Object { + /** Reads a field of this object by its name. + * + * This will fail with a ClassCastException if the key doesn't exist and + * the return type doesn't allow js.undefined as value. If the return type + * does allow js.undefined, applying with a non-existent key will return + * js.undefined. + */ + @JSBracketAccess + def apply(key: String): A = native + + /** Reads a field of this object by its name. + * + * This will return undefined if the key doesn't exist. It will also return + * undefined if the value associated with the key is undefined. To truly + * check for the existence of a property, use [[js.Object.hasOwnProperty]]. + */ + @JSBracketAccess + def get(key: String): UndefOr[A] = native + + /** Writes a field of this object by its name. */ + @JSBracketAccess + def update(key: String, value: A): Unit = native + + /** Deletes a property of this object by its name. + * The property must be configurable. + * This method is equivalent to the "delete" keyword in JavaScript. + * + * Since we are using strict mode, this throws an exception, if the property + * isn't configurable. + */ + def delete(key: String): Unit = sys.error("stub") +} + +/** Factory for [[Dictionary]] instances. */ +object Dictionary { + /** Returns a new empty dictionary */ + def empty[A]: Dictionary[A] = (new Object).asInstanceOf[Dictionary[A]] + + def apply[A](properties: (String, A)*): Dictionary[A] = { + val result = empty[A] + for ((key, value) <- properties) + result(key) = value + result + } +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/Error.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/Error.scala new file mode 100644 index 0000000..129ff41 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/Error.scala @@ -0,0 +1,114 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + +/** + * All doc-comments marked as "MDN" are by Mozilla Contributors, + * distributed under the Creative Commons Attribution-ShareAlike license from + * https://developer.mozilla.org/en-US/docs/Web/Reference/API + */ +package scala.scalajs.js + +class Error(message0: String = "") extends Object { + val name: String = native + /** + * Human-readable description of the error + * + * MDN + */ + val message: String = native +} + +object Error extends Object { + def apply(message: String = ""): Error = native +} + +/** + * An instance representing an error that occurs regarding the global function + * eval() + * + * MDN + */ +class EvalError(message: String = "") extends Error + +object EvalError extends Object { + def apply(message: String = ""): EvalError = native +} + +/** + * An instance representing an error that occurs when a numeric variable or + * parameter is outside of its valid range. + * + * A RangeError is thrown when trying to pass a number as an argument to a + * function that does not allow a range that includes that number. This can + * be encountered when to create an array of an illegal length with the Array + * constructor, or when passing bad values to the numeric methods toExponential, + * toFixed, or toPrecision. + * + * MDN + */ +class RangeError(message: String = "") extends Error + +object RangeError extends Object { + def apply(message: String = ""): RangeError = native +} + +/** + * Represents an error when a non-existent variable is referenced. + * + * A ReferenceError is thrown when trying to dereference a variable that has + * not been declared. + * + * MDN + */ +class ReferenceError(message: String = "") extends Error + +object ReferenceError extends Object { + def apply(message: String = ""): ReferenceError = native +} + +/** + * Represents an error when trying to interpret syntactically invalid code. + * + * A SyntaxError is thrown when the JavaScript engine encounters tokens or + * token order that does not conform to the syntax of the language when parsing code. + * + * MDN + */ +class SyntaxError(message: String = "") extends Error + +object SyntaxError extends Object { + def apply(message: String = ""): SyntaxError = native +} + +/** + * Represents an error when a value is not of the expected type. + * + * A TypeError is thrown when an operand or argument passed to a function is + * incompatible with the type expected by that operator or function. + * + * MDN + */ +class TypeError(message: String = "") extends Error + +object TypeError extends Object { + def apply(message: String = ""): TypeError = native +} + +/** + * Represents an error when a malformed URI is encountered. + * + * A URIError is thrown when the URI handling functions are passed a malformed URI. + * + * MDN + */ +class URIError(message: String = "") extends Error + +object URIError extends Object { + def apply(message: String = ""): URIError = native +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/Function.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/Function.scala new file mode 100644 index 0000000..4a7d1d9 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/Function.scala @@ -0,0 +1,194 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + +/** + * All doc-comments marked as "MDN" are by Mozilla Contributors, + * distributed under the Creative Commons Attribution-ShareAlike license from + * https://developer.mozilla.org/en-US/docs/Web/Reference/API + */ +package scala.scalajs.js + +/** + * The Function constructor creates a new Function object. In JavaScript every + * function is actually a Function object. + * + * Function objects created with the Function constructor are parsed when the + * function is created. This is less efficient than declaring a function and calling + * it within your code, because functions declared with the function statement + * are parsed with the rest of the code. + * + * All arguments passed to the function are treated as the names of the + * identifiers of the parameters in the function to be created, in the order + * in which they are passed. + * + * Note: Functions created with the Function constructor do not create closures + * to their creation contexts; they always are created in the global scope. + * When running them, they will only be able to access their own local + * variables and global ones, not the ones from the scope in which the Function + * constructor was called. This is different from using eval with code for + * a function expression. + * + * Invoking the Function constructor as a function (without using the new + * operator) has the same effect as invoking it as a constructor. + * + * MDN + */ +class Function(args: String*) extends Object { + /** + * length is a property of a function object, and indicates how many arguments + * the function expects, i.e. the number of formal parameters. This number + * does not include the rest parameter. By contrast, arguments.length is local + * to a function and provides the number of arguments actually passed to the + * function. + * + * MDN + */ + val length: Int = native + + /** + * The call() method calls a function with a given this value and arguments + * provided individually. + * + * You can assign a different this object when calling an existing function. + * this refers to the current object, the calling object. With call, you + * can write a method once and then inherit it in another object, without + * having to rewrite the method for the new object. + * + * apply is very similar to call(), except for the type of arguments it supports. + * You can use an arguments array instead of a named set of parameters. With + * apply, you can use an array literal, for example, + * + * fun.apply(this, ['eat', 'bananas']) + * + * or an Array object, for example, + * + * fun.apply(this, new Array('eat', 'bananas')). + * + * MDN + * + * Scala.js-specific note: call() can be used instead of the apply() method + * available in JavaScript. Simply use the :_* notation to expand a Seq as + * variadic arguments, e.g., + * + * {{{ + * someFun.call(thisArg, argSeq: _*) + * }}} + * + */ + def call(thisArg: Any, argArray: Any*): Dynamic = native + + // Do not expose apply: use call(thisArg, argArray: _*) instead. + // def apply[A](thisArg: Any, argArray: Array[A]): Dynamic = native + // def apply(thisArg: Any): Dynamic = native + + /** + * The bind() method creates a new function that, when called, has its this + * keyword set to the provided value, with a given sequence of arguments + * preceding any provided when the new function is called. + * + * MDN + */ + def bind(thisArg: Any, argArray: Any*): Dynamic = native +} + +object Function extends Object { + def apply(args: String*): Function = native +} + +trait Function0[+R] extends Function { + def apply(): R +} + +trait Function1[-T1, +R] extends Function { + def apply(arg1: T1): R +} + +trait Function2[-T1, -T2, +R] extends Function { + def apply(arg1: T1, arg2: T2): R +} + +trait Function3[-T1, -T2, -T3, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3): R +} + +trait Function4[-T1, -T2, -T3, -T4, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4): R +} + +trait Function5[-T1, -T2, -T3, -T4, -T5, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5): R +} + +trait Function6[-T1, -T2, -T3, -T4, -T5, -T6, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6): R +} + +trait Function7[-T1, -T2, -T3, -T4, -T5, -T6, -T7, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7): R +} + +trait Function8[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8): R +} + +trait Function9[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9): R +} + +trait Function10[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10): R +} + +trait Function11[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11): R +} + +trait Function12[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12): R +} + +trait Function13[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13): R +} + +trait Function14[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14): R +} + +trait Function15[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15): R +} + +trait Function16[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, -T16, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15, arg16: T16): R +} + +trait Function17[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, -T16, -T17, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15, arg16: T16, arg17: T17): R +} + +trait Function18[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, -T16, -T17, -T18, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15, arg16: T16, arg17: T17, arg18: T18): R +} + +trait Function19[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, -T16, -T17, -T18, -T19, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15, arg16: T16, arg17: T17, arg18: T18, arg19: T19): R +} + +trait Function20[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, -T16, -T17, -T18, -T19, -T20, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15, arg16: T16, arg17: T17, arg18: T18, arg19: T19, arg20: T20): R +} + +trait Function21[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, -T16, -T17, -T18, -T19, -T20, -T21, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15, arg16: T16, arg17: T17, arg18: T18, arg19: T19, arg20: T20, arg21: T21): R +} + +trait Function22[-T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, -T16, -T17, -T18, -T19, -T20, -T21, -T22, +R] extends Function { + def apply(arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15, arg16: T16, arg17: T17, arg18: T18, arg19: T19, arg20: T20, arg21: T21, arg22: T22): R +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/GlobalScope.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/GlobalScope.scala new file mode 100644 index 0000000..67c09ed --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/GlobalScope.scala @@ -0,0 +1,21 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + + +package scala.scalajs.js + +/** Marker trait for top-level objects representing the JS global scope. + * + * When calling method on a top-level object or package object that is a + * subtype of GlobalScope, the receiver is dropped, and the JavaScript global + * scope is used instead. + * + * @see [[http://www.scala-js.org/doc/calling-javascript.html Calling JavaScript from Scala.js]] + */ +trait GlobalScope extends Object diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/JSApp.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/JSApp.scala new file mode 100644 index 0000000..fd12207 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/JSApp.scala @@ -0,0 +1,20 @@ +package scala.scalajs.js + +import annotation.{JSExport, JSExportDescendentObjects} + +/** Base class for top-level, entry point main objects. + * + * Objects inheriting from [[JSApp]] are automatically exported to JavaScript + * under their fully qualified name, and their [[main]] method as well. + * + * [[JSApp]] is typically used to mark the entry point of a Scala.js + * application. As such, the sbt plugin also recognizes top-level objects + * extending [[JSApp]]. It allows to run their [[main]] method with `sbt run`, + * and can also generate a tiny JavaScript launcher snippet executing the + * [[main]] method of one specific [[JSApp]] object. + */ +@JSExportDescendentObjects +trait JSApp { + @JSExport + def main(): Unit +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/JSArrayOps.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/JSArrayOps.scala new file mode 100644 index 0000000..71f705d --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/JSArrayOps.scala @@ -0,0 +1,264 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + +/** + * All doc-comments marked as "MDN" are by Mozilla Contributors, + * distributed under the Creative Commons Attribution-ShareAlike license from + * https://developer.mozilla.org/en-US/docs/Web/Reference/API + */ +package scala.scalajs.js + +import scala.language.implicitConversions + +import scala.scalajs.js.annotation._ + +/** Discouraged native JavaScript Array methods. + * + * In general, you should prefer the Scala collection methods available + * implicitly through [[ArrayOps]], because they are inlineable, and hence + * faster. + * + * To enable the use of these functions on js.[[Array]]s, import the implicit + * conversion [[JSArrayOps.jsArrayOps]]. + */ +trait JSArrayOps[A] extends Object { + + /** + * The indexOf() method returns the first index at which a given element can + * be found in the array, or -1 if it is not present. + * + * MDN + */ + @JSName("indexOf") + def jsIndexOf(searchElement: A, fromIndex: Int): Int = native + @JSName("indexOf") + def jsIndexOf(searchElement: A): Int = native + + /** + * The lastIndexOf() method returns the last index at which a given element + * can be found in the array, or -1 if it is not present. The array is + * searched backwards, starting at fromIndex. + * + * MDN + */ + @JSName("lastIndexOf") + def jsLastIndexOf(searchElement: A, fromIndex: Int): Int = native + @JSName("lastIndexOf") + def jsLastIndexOf(searchElement: A): Int = native + + /** + * The every method executes the provided callback function once for each + * element present in the array until it finds one where callback returns + * a falsy value (a value that becomes false when converted to a Boolean). + * If such an element is found, the every method immediately returns false. + * Otherwise, if callback returned a true value for all elements, every + * will return true. callback is invoked only for indexes of the array + * which have assigned values; it is not invoked for indexes which have been + * deleted or which have never been assigned values. + * + * callback is invoked with three arguments: + * + * - the value of the element + * - the index of the element + * - and the Array object being traversed. + * + * If a thisObject parameter is provided to every, it will be used as the + * this for each invocation of the callback. If it is not provided, or is + * null, the global object associated with callback is used instead. + * + * every does not mutate the array on which it is called. + * + * every acts like the "for all" quantifier in mathematics. In particular, for + * an empty array, it returns true. (It is vacuously true that all elements of + * the empty set satisfy any given condition.) + * + * MDN + */ + @JSName("every") + def jsEvery[T](callbackfn: ThisFunction3[T, A, Int, Array[A], Boolean], + thisArg: T): Boolean = native + @JSName("every") + def jsEvery(callbackfn: Function3[A, Int, Array[A], Boolean]): Boolean = native + + /** + * some executes the callback function once for each element present in the + * array until it finds one where callback returns a true value. If such an + * element is found, some immediately returns true. Otherwise, some returns + * false. callback is invoked only for indexes of the array which have assigned + * values; it is not invoked for indexes which have been deleted or which + * have never been assigned values. + * + * callback is invoked with three arguments: the value of the element, the index + * of the element, and the Array object being traversed. + * + * If a thisObject parameter is provided to some, it will be used as the this + * for each invocation of the callback. If it is not provided, or is null, + * the global object associated with callback is used instead. + * + * some does not mutate the array on which it is called. + * + * MDN + */ + @JSName("some") + def jsSome[T](callbackfn: ThisFunction3[T, A, Int, Array[A], Boolean], + thisArg: T): Boolean = native + @JSName("some") + def jsSome(callbackfn: Function3[A, Int, Array[A], Boolean]): Boolean = native + @JSName("some") + def jsSome(callbackfn: Function2[A, Int, Boolean]): Boolean = native + @JSName("some") + def jsSome(callbackfn: Function1[A, Boolean]): Boolean = native + + /** + * forEach executes the provided callback once for each element of the array + * with an assigned value. It is not invoked for indexes which have been deleted + * or which have been initialized to undefined. + * + * callback is invoked with three arguments: + * + * - the element value + * - the element index + * - the array being traversed + * + * If a thisArg parameter is provided to forEach, it will be used as the + * this value for each callback invocation as if callback.call(thisArg, + * element, index, array) was called. If thisArg is undefined or null, + * the this value within the function depends on whether the function + * is in strict mode or not (passed value if in strict mode, global object + * if in non-strict mode). + * + * MDN + */ + @JSName("forEach") + def jsForEach[T](callbackfn: ThisFunction3[T, A, Int, Array[A], _], + thisArg: T): Unit = native + @JSName("forEach") + def jsForEach(callbackfn: Function3[A, Int, Array[A], _]): Unit = native + @JSName("forEach") + def jsForEach(callbackfn: Function2[A, Int, _]): Unit = native + @JSName("forEach") + def jsForEach(callbackfn: Function1[A, _]): Unit = native + + /** + * map calls a provided callback function once for each element in an array, + * in order, and constructs a new array from the results. callback is + * invoked only for indexes of the array which have assigned values; it is + * not invoked for indexes which have been deleted or which have never been + * assigned values. + * + * callback is invoked with three arguments: the value of the element, the + * index of the element, and the Array object being traversed. + * + * If a thisArg parameter is provided to map, it will be used as the this for + * each invocation of the callback. If it is not provided, or is null, the + * global object associated with callback is used instead. + * + * map does not mutate the array on which it is called. + * + * MDN + */ + @JSName("map") + def jsMap[B, T](callbackfn: ThisFunction3[T, A, Int, Array[A], B], + thisArg: T): Array[B] = native + @JSName("map") + def jsMap[B](callbackfn: Function3[A, Int, Array[A], B]): Array[B] = native + @JSName("map") + def jsMap[B](callbackfn: Function2[A, Int, B]): Array[B] = native + @JSName("map") + def jsMap[B](callbackfn: Function1[A, B]): Array[B] = native + + /** + * filter calls a provided callback function once for each element in an array, + * and constructs a new array of all the values for which callback returns a true + * value. callback is invoked only for indexes of the array which have assigned + * values; it is not invoked for indexes which have been deleted or which have + * never been assigned values. Array elements which do not pass the callback + * test are simply skipped, and are not included in the new array. + * + * callback is invoked with three arguments: + * + * - the value of the element + * - the index of the element + * - the Array object being traversed + * + * If a thisObject parameter is provided to filter, it will be used as the this + * for each invocation of the callback. If it is not provided, or is null, the + * global object associated with callback is used instead. + * + * filter does not mutate the array on which it is called. + * + * MDN + */ + @JSName("filter") + def jsFilter[T](callbackfn: ThisFunction3[T, A, Int, Array[A], Boolean], + thisArg: T): Array[A] = native + @JSName("filter") + def jsFilter(callbackfn: Function3[A, Int, Array[A], Boolean]): Array[A] = native + @JSName("filter") + def jsFilter(callbackfn: Function2[A, Int, Boolean]): Array[A] = native + @JSName("filter") + def jsFilter(callbackfn: Function1[A, Boolean]): Array[A] = native + + /** + * reduce executes the callback function once for each element present in + * the array, excluding holes in the array, receiving four arguments: the + * initial value (or value from the previous callback call), the value of + * the current element, the current index, and the array over which + * iteration is occurring. + * + * The first time the callback is called, previousValue and currentValue can + * be one of two values. If initialValue is provided in the call to reduce, + * then previousValue will be equal to initialValue and currentValue will be + * equal to the first value in the array. If no initialValue was provided, + * then previousValue will be equal to the first value in the array and + * currentValue will be equal to the second. + * + * MDN + */ + @JSName("reduce") + def jsReduce[B](callbackfn: Function4[B, A, Int, Array[A], B], initialValue: B): B = native + @JSName("reduce") + def jsReduce[B](callbackfn: Function3[B, A, Int, B], initialValue: B): B = native + @JSName("reduce") + def jsReduce[B](callbackfn: Function2[B, A, B], initialValue: B): B = native + @JSName("reduce") + def jsReduce[B](callbackfn: Function4[B, A, Int, Array[A], B]): B = native + @JSName("reduce") + def jsReduce[B](callbackfn: Function3[B, A, Int, B]): B = native + @JSName("reduce") + def jsReduce[B](callbackfn: Function2[B, A, B]): B = native + + /** + * reduceRight executes the callback function once for each element present + * in the array, excluding holes in the array, receiving four arguments: + * the initial value (or value from the previous callback call), the value + * of the current element, the current index, and the array over which + * iteration is occurring. + * + * MDN + */ + @JSName("reduceRight") + def jsReduceRight[B](callbackfn: Function4[B, A, Int, Array[A], B], initialValue: B): B = native + @JSName("reduceRight") + def jsReduceRight[B](callbackfn: Function3[B, A, Int, B], initialValue: B): B = native + @JSName("reduceRight") + def jsReduceRight[B](callbackfn: Function2[B, A, B], initialValue: B): B = native + @JSName("reduceRight") + def jsReduceRight[B](callbackfn: Function4[B, A, Int, Array[A], B]): B = native + @JSName("reduceRight") + def jsReduceRight[B](callbackfn: Function3[B, A, Int, B]): B = native + @JSName("reduceRight") + def jsReduceRight[B](callbackfn: Function2[B, A, B]): B = native + +} + +object JSArrayOps { + @inline implicit def jsArrayOps[A](array: Array[A]): JSArrayOps[A] = + array.asInstanceOf[JSArrayOps[A]] +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/JSConverters.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/JSConverters.scala new file mode 100644 index 0000000..1b21d61 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/JSConverters.scala @@ -0,0 +1,54 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + +package scala.scalajs.js + +import scala.language.implicitConversions + +import scala.collection._ + +import scala.scalajs.runtime.genTraversableOnce2jsArray + +/** A collection of decorators that allow converting Scala types to + * corresponding JS facade types + */ +object JSConverters { + + implicit class JSRichOption[T](val opt: Option[T]) extends AnyVal { + @inline final def orUndefined: UndefOr[T] = + opt.fold[UndefOr[T]](undefined)(v => v) + } + + implicit class JSRichGenTraversableOnce[T]( + val col: GenTraversableOnce[T]) extends AnyVal { + @inline final def toJSArray: Array[T] = genTraversableOnce2jsArray(col) + } + + implicit class JSRichGenMap[T](val map: GenMap[String, T]) extends AnyVal { + @inline final def toJSDictionary: Dictionary[T] = { + val result = Dictionary.empty[T] + map.foreach { case (key, value) => result(key) = value } + result + } + } + + @inline + implicit def genTravConvertible2JSRichGenTrav[T, C](coll: C)( + implicit ev: C => GenTraversableOnce[T]): JSRichGenTraversableOnce[T] = + new JSRichGenTraversableOnce(coll) + + /** Special case for scala.Array of [[genTravConvertible2JSRichGenTrav]]. + * Needed for the 2.10.x series. + */ + @inline + implicit def array2JSRichGenTrav[T]( + arr: scala.Array[T]): JSRichGenTraversableOnce[T] = + new JSRichGenTraversableOnce(arr) + +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/JSON.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/JSON.scala new file mode 100644 index 0000000..8404f40 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/JSON.scala @@ -0,0 +1,51 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + +/** + * All doc-comments marked as "MDN" are by Mozilla Contributors, + * distributed under the Creative Commons Attribution-ShareAlike license from + * https://developer.mozilla.org/en-US/docs/Web/Reference/API + */ +package scala.scalajs.js + +/** + * The JSON object contains methods for converting values to JavaScript Object + * Notation (JSON) and for converting JSON to values. + * + * MDN + */ +object JSON extends Object { + /** + * Parse a string as JSON, optionally transforming the value produced by parsing. + * @param text The string to parse as JSON. See the JSON object for a + * description of JSON syntax. + * @param reviver If a function, prescribes how the value originally produced + * by parsing is transformed, before being returned. + * + * MDN + */ + def parse(text: String, reviver: Function2[Any, Any, Any] = ???): Dynamic = native + + /** + * Convert a value to JSON, optionally replacing values if a replacer function + * is specified, or optionally including only the specified properties if a + * replacer array is specified. + * + * @param value The value to convert to a JSON string. + * @param replacer If a function, transforms values and properties encountered + * while stringifying; if an array, specifies the set of + * properties included in objects in the final string. + * @param space Causes the resulting string to be pretty-printed. + * + * MDN + */ + def stringify(value: Any, replacer: Function2[String, Any, Any] = ???, space: Any = ???): String = native + def stringify(value: Any, replacer: Array[Any]): String = native + def stringify(value: Any, replacer: Array[Any], space: Any): String = native +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/JavaScriptException.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/JavaScriptException.scala new file mode 100644 index 0000000..e4803c8 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/JavaScriptException.scala @@ -0,0 +1,20 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + + +package scala.scalajs.js + +case class JavaScriptException(exception: scala.Any) extends RuntimeException { + override def toString() = exception.toString() + + override def fillInStackTrace(): Throwable = { + scala.scalajs.runtime.StackTrace.captureState(this, exception) + this + } +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/Math.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/Math.scala new file mode 100644 index 0000000..02caaa0 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/Math.scala @@ -0,0 +1,277 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + +/** + * All doc-comments marked as "MDN" are by Mozilla Contributors, + * distributed under the Creative Commons Attribution-ShareAlike license from + * https://developer.mozilla.org/en-US/docs/Web/Reference/API + */ +package scala.scalajs.js + +/** + * Math is a built-in object that has properties and methods for mathematical + * constants and functions. Not a function object. + * + * MDN + */ +object Math extends Object { + /** + * Euler's constant and the base of natural logarithms, approximately 2.718. + * + * MDN + */ + val E: Double = native + /** + * Natural logarithm of 10, approximately 2.303. + * + * MDN + */ + val LN10: Double = native + /** + * Natural logarithm of 2, approximately 0.693. + * + * MDN + */ + val LN2: Double = native + /** + * Base 2 logarithm of E, approximately 1.443. + * + * MDN + */ + val LOG2E: Double = native + /** + * Base 10 logarithm of E, approximately 0.434. + * + * MSN + */ + val LOG10E: Double = native + /** + * Ratio of the circumference of a circle to its diameter, approximately 3.14159. + * + * MDN + */ + val PI: Double = native + + /** + * Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707. + * + * MDN + */ + val SQRT1_2: Double = native + + /** + * Square root of 2, approximately 1.414. + * + * MDN + */ + val SQRT2: Double = native + + /** + * Returns the absolute value of a number. + * + * Passing a non-numeric string or undefined/empty variable returns NaN. + * Passing null returns 0. + * + * MDN + */ + def abs(x: Int): Int = native + + /** + * Returns the absolute value of a number. + * + * Passing a non-numeric string or undefined/empty variable returns NaN. + * Passing null returns 0. + * + * MDN + */ + def abs(x: Double): Double = native + + /** + * The Math.acos() function returns the arccosine (in radians) of a number. + * + * The acos method returns a numeric value between 0 and pi radians for x + * between -1 and 1. If the value of number is outside this range, it returns NaN. + * + * MDN + */ + def acos(x: Double): Double = native + + /** + * The Math.asin() function returns the arcsine (in radians) of a number. + * + * The asin method returns a numeric value between -pi/2 and pi/2 radians for x + * between -1 and 1. If the value of number is outside this range, it returns NaN. + * + * MDN + */ + def asin(x: Double): Double = native + + /** + * The Math.atan() function returns the arctangent (in radians) of a number. + * + * The atan method returns a numeric value between -pi/2 and pi/2 radians. + * + * MDN + */ + def atan(x: Double): Double = native + + /** + * The Math.atan2() function returns the arctangent of the quotient of its + * arguments. + * + * The atan2 method returns a numeric value between -pi and pi representing + * the angle theta of an (x,y) point. This is the counterclockwise angle, + * measured in radians, between the positive X axis, and the point (x,y). + * Note that the arguments to this function pass the y-coordinate first and + * the x-coordinate second. + * + * atan2 is passed separate x and y arguments, and atan is passed the ratio + * of those two arguments. + * + * MDN + */ + def atan2(y: Double, x: Double): Double = native + + /** + * The Math.ceil() function returns the smallest integer greater than or + * equal to a number. + * + * MDN + */ + def ceil(x: Double): Double = native + + /** + * The Math.cos() function returns the cosine of a number. + * + * The cos method returns a numeric value between -1 and 1, which represents + * the cosine of the angle. + * + * MDN + */ + def cos(x: Double): Double = native + + /** + * The Math.exp() function returns E^x, where x is the argument, and E is + * Euler's constant, the base of the natural logarithms. + * + * MDN + */ + def exp(x: Double): Double = native + + /** + * The Math.floor() function returns the largest integer less than or equal + * to a number. + * + * MDN + */ + def floor(x: Double): Double = native + + /** + * The Math.log() function returns the natural logarithm (base E) of a number. + * + * If the value of number is negative, the return value is always NaN. + * + * MDN + */ + def log(x: Double): Double = native + + /** + * The Math.max() function returns the largest of zero or more numbers. + * + * MDN + */ + def max(value1: Int, values: Int*): Int = native + + /** + * The Math.max() function returns the largest of zero or more numbers. + * + * If no arguments are given, the result is - Infinity. + * + * If at least one of arguments cannot be converted to a number, the result is NaN. + * + * MDN + */ + def max(values: Double*): Double = native + + /** + * The Math.min() function returns the smallest of zero or more numbers. + * + * MDN + */ + def min(value1: Int, values: Int*): Int = native + + /** + * The Math.min() function returns the smallest of zero or more numbers. + * + * If no arguments are given, the result is Infinity. + * + * If at least one of arguments cannot be converted to a number, the result is NaN. + * + * MDN + */ + def min(values: Double*): Double = native + + /** + * The Math.pow() function returns the base to the exponent Power, that is, base^^exponent. + * + * MDN + */ + def pow(x: Double, y: Double): Double = native + + /** + * The Math.random() function returns a floating-point, pseudo-random number in + * the range [0, 1) that is, from 0 (inclusive) up to but not including 1 + * (exclusive), which you can then scale to your desired range. + * + * The random number generator is seeded from the current time, as in Java. + * + * MDN + */ + def random(): Double = native + + /** + * The Math.round() function returns the value of a number rounded to the + * nearest integer. + * + * If the fractional portion of number is .5 or greater, the argument is + * rounded to the next higher integer. If the fractional portion of number + * is less than .5, the argument is rounded to the next lower integer. + * + * MDN + */ + def round(x: Double): Double = native + + /** + * The Math.sin() function returns the sine of a number. + * + * The sin method returns a numeric value between -1 and 1, which represents + * the sine of the angle given in radians. + * + * MDN + */ + def sin(x: Double): Double = native + + /** + * The Math.sqrt() function returns the square root (x\sqrt{x}) of a number. + * + * If the value of number is negative, sqrt returns NaN. + * + * MDN + */ + def sqrt(x: Double): Double = native + + /** + * The Math.tan() function returns the tangent of a number. + * + * The tan method returns a numeric value that represents the tangent of the angle. + * + * MDN + */ + def tan(x: Double): Double = native +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/Primitives.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/Primitives.scala new file mode 100644 index 0000000..1a3d88c --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/Primitives.scala @@ -0,0 +1,872 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + +/** + * All doc-comments marked as "MDN" are by Mozilla Contributors, + * distributed under the Creative Commons Attribution-ShareAlike license from + * https://developer.mozilla.org/en-US/docs/Web/Reference/API + */ +package scala.scalajs.js + +import annotation.JSBracketAccess + +import scala.language.{ dynamics, implicitConversions } +import scala.reflect.ClassTag +import scala.collection.{ immutable, mutable } +import scala.collection.generic.CanBuildFrom + +/** Super-type of all JavaScript values. + * + * All values of a subtype of this trait represent JavaScript values, without + * boxing of proxying of any kind. + */ +sealed trait Any extends scala.AnyRef { +} + +/** Provides implicit conversions from Scala values to JavaScript values. */ +object Any extends LowPrioAnyImplicits { + @inline implicit def fromUnit(value: Unit): prim.Undefined = + value.asInstanceOf[prim.Undefined] + @inline implicit def fromBoolean(value: scala.Boolean): prim.Boolean = + value.asInstanceOf[prim.Boolean] + @inline implicit def fromByte(value: scala.Byte): prim.Number = + value.asInstanceOf[prim.Number] + @inline implicit def fromShort(value: scala.Short): prim.Number = + value.asInstanceOf[prim.Number] + @inline implicit def fromInt(value: scala.Int): prim.Number = + value.asInstanceOf[prim.Number] + @inline implicit def fromLong(value: scala.Long): prim.Number = + value.toDouble.asInstanceOf[prim.Number] + @inline implicit def fromFloat(value: scala.Float): prim.Number = + value.asInstanceOf[prim.Number] + @inline implicit def fromDouble(value: scala.Double): prim.Number = + value.asInstanceOf[prim.Number] + @inline implicit def fromString(s: java.lang.String): prim.String = + s.asInstanceOf[prim.String] + + @inline implicit def toDouble(value: prim.Number): scala.Double = + value.asInstanceOf[scala.Double] + @inline implicit def toBoolean(value: prim.Boolean): scala.Boolean = + value.asInstanceOf[scala.Boolean] + @inline implicit def toScalaString(value: prim.String): java.lang.String = + value.asInstanceOf[java.lang.String] + + implicit def jsArrayOps[A](array: Array[A]): ArrayOps[A] = + new ArrayOps(array) + + implicit def canBuildFromArray[A]: CanBuildFrom[Array[_], A, Array[A]] = { + @inline + class CanBuildFromArray extends CanBuildFrom[Array[_], A, Array[A]] { + def apply(from: Array[_]): mutable.Builder[A, Array[A]] = + new ArrayOps[A] + def apply(): mutable.Builder[A, Array[A]] = + new ArrayOps[A] + } + new CanBuildFromArray + } + + implicit def fromFunction0[R](f: scala.Function0[R]): Function0[R] = sys.error("stub") + implicit def fromFunction1[T1, R](f: scala.Function1[T1, R]): Function1[T1, R] = sys.error("stub") + implicit def fromFunction2[T1, T2, R](f: scala.Function2[T1, T2, R]): Function2[T1, T2, R] = sys.error("stub") + implicit def fromFunction3[T1, T2, T3, R](f: scala.Function3[T1, T2, T3, R]): Function3[T1, T2, T3, R] = sys.error("stub") + implicit def fromFunction4[T1, T2, T3, T4, R](f: scala.Function4[T1, T2, T3, T4, R]): Function4[T1, T2, T3, T4, R] = sys.error("stub") + implicit def fromFunction5[T1, T2, T3, T4, T5, R](f: scala.Function5[T1, T2, T3, T4, T5, R]): Function5[T1, T2, T3, T4, T5, R] = sys.error("stub") + implicit def fromFunction6[T1, T2, T3, T4, T5, T6, R](f: scala.Function6[T1, T2, T3, T4, T5, T6, R]): Function6[T1, T2, T3, T4, T5, T6, R] = sys.error("stub") + implicit def fromFunction7[T1, T2, T3, T4, T5, T6, T7, R](f: scala.Function7[T1, T2, T3, T4, T5, T6, T7, R]): Function7[T1, T2, T3, T4, T5, T6, T7, R] = sys.error("stub") + implicit def fromFunction8[T1, T2, T3, T4, T5, T6, T7, T8, R](f: scala.Function8[T1, T2, T3, T4, T5, T6, T7, T8, R]): Function8[T1, T2, T3, T4, T5, T6, T7, T8, R] = sys.error("stub") + implicit def fromFunction9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R](f: scala.Function9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]): Function9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] = sys.error("stub") + implicit def fromFunction10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R](f: scala.Function10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]): Function10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R] = sys.error("stub") + implicit def fromFunction11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R](f: scala.Function11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R]): Function11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R] = sys.error("stub") + implicit def fromFunction12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R](f: scala.Function12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R]): Function12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R] = sys.error("stub") + implicit def fromFunction13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R](f: scala.Function13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R]): Function13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R] = sys.error("stub") + implicit def fromFunction14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R](f: scala.Function14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R]): Function14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R] = sys.error("stub") + implicit def fromFunction15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R](f: scala.Function15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R]): Function15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R] = sys.error("stub") + implicit def fromFunction16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R](f: scala.Function16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R]): Function16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R] = sys.error("stub") + implicit def fromFunction17[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R](f: scala.Function17[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R]): Function17[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R] = sys.error("stub") + implicit def fromFunction18[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R](f: scala.Function18[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R]): Function18[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R] = sys.error("stub") + implicit def fromFunction19[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R](f: scala.Function19[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R]): Function19[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R] = sys.error("stub") + implicit def fromFunction20[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R](f: scala.Function20[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R]): Function20[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R] = sys.error("stub") + implicit def fromFunction21[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R](f: scala.Function21[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R]): Function21[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R] = sys.error("stub") + implicit def fromFunction22[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R](f: scala.Function22[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R]): Function22[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R] = sys.error("stub") + + implicit def toFunction0[R](f: Function0[R]): scala.Function0[R] = () => f() + implicit def toFunction1[T1, R](f: Function1[T1, R]): scala.Function1[T1, R] = (x1) => f(x1) + implicit def toFunction2[T1, T2, R](f: Function2[T1, T2, R]): scala.Function2[T1, T2, R] = (x1, x2) => f(x1, x2) + implicit def toFunction3[T1, T2, T3, R](f: Function3[T1, T2, T3, R]): scala.Function3[T1, T2, T3, R] = (x1, x2, x3) => f(x1, x2, x3) + implicit def toFunction4[T1, T2, T3, T4, R](f: Function4[T1, T2, T3, T4, R]): scala.Function4[T1, T2, T3, T4, R] = (x1, x2, x3, x4) => f(x1, x2, x3, x4) + implicit def toFunction5[T1, T2, T3, T4, T5, R](f: Function5[T1, T2, T3, T4, T5, R]): scala.Function5[T1, T2, T3, T4, T5, R] = (x1, x2, x3, x4, x5) => f(x1, x2, x3, x4, x5) + implicit def toFunction6[T1, T2, T3, T4, T5, T6, R](f: Function6[T1, T2, T3, T4, T5, T6, R]): scala.Function6[T1, T2, T3, T4, T5, T6, R] = (x1, x2, x3, x4, x5, x6) => f(x1, x2, x3, x4, x5, x6) + implicit def toFunction7[T1, T2, T3, T4, T5, T6, T7, R](f: Function7[T1, T2, T3, T4, T5, T6, T7, R]): scala.Function7[T1, T2, T3, T4, T5, T6, T7, R] = (x1, x2, x3, x4, x5, x6, x7) => f(x1, x2, x3, x4, x5, x6, x7) + implicit def toFunction8[T1, T2, T3, T4, T5, T6, T7, T8, R](f: Function8[T1, T2, T3, T4, T5, T6, T7, T8, R]): scala.Function8[T1, T2, T3, T4, T5, T6, T7, T8, R] = (x1, x2, x3, x4, x5, x6, x7, x8) => f(x1, x2, x3, x4, x5, x6, x7, x8) + implicit def toFunction9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R](f: Function9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]): scala.Function9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9) + implicit def toFunction10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R](f: Function10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]): scala.Function10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) + implicit def toFunction11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R](f: Function11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R]): scala.Function11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11) + implicit def toFunction12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R](f: Function12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R]): scala.Function12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12) + implicit def toFunction13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R](f: Function13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R]): scala.Function13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13) + implicit def toFunction14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R](f: Function14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R]): scala.Function14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14) + implicit def toFunction15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R](f: Function15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R]): scala.Function15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15) + implicit def toFunction16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R](f: Function16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R]): scala.Function16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16) + implicit def toFunction17[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R](f: Function17[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R]): scala.Function17[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17) + implicit def toFunction18[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R](f: Function18[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R]): scala.Function18[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18) + implicit def toFunction19[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R](f: Function19[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R]): scala.Function19[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19) + implicit def toFunction20[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R](f: Function20[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R]): scala.Function20[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20) + implicit def toFunction21[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R](f: Function21[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R]): scala.Function21[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21) + implicit def toFunction22[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R](f: Function22[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R]): scala.Function22[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22) +} + +trait LowPrioAnyImplicits { + @inline implicit def richDouble(num: prim.Number): scala.runtime.RichDouble = + new scala.runtime.RichDouble(Any.toDouble(num)) + @inline implicit def richBoolean(b: prim.Boolean): scala.runtime.RichBoolean = + new scala.runtime.RichBoolean(Any.toBoolean(b)) + + @inline implicit def stringOps(string: prim.String): immutable.StringOps = + new immutable.StringOps(Any.toScalaString(string)) + + implicit def wrapArray[A](array: Array[A]): WrappedArray[A] = + new WrappedArray(array) + implicit def wrapDictionary[A](dict: Dictionary[A]): WrappedDictionary[A] = + new WrappedDictionary(dict) +} + +/** Dynamically typed JavaScript value. + * + * Values of this trait accept all possible JavaScript operations in a + * dynamically typed way. You can read and write any field, call any method, + * apply any JavaScript operator to values of this type. + */ +sealed trait Dynamic extends Any with scala.Dynamic { + /** Calls a method of this object. */ + def applyDynamic(name: java.lang.String)(args: Any*): Dynamic = sys.error("stub") + + /** Reads a field of this object. */ + def selectDynamic(name: java.lang.String): Dynamic = sys.error("stub") + + /** Writes a field of this object. */ + def updateDynamic(name: java.lang.String)(value: Any): Unit = sys.error("stub") + + /** Calls this object as a callable. */ + def apply(args: Any*): Dynamic = native + + import prim.Number + + def unary_!(): Boolean = native + + def unary_+(): Number = native + def unary_-(): Number = native + def unary_~(): Number = native + + def +(that: Number): Dynamic = native // could be a String if this is a String + def -(that: Number): Number = native + def *(that: Number): Number = native + def /(that: Number): Number = native + def %(that: Number): Number = native + def <<(that: Number): Number = native + def >>(that: Number): Number = native + def >>>(that: Number): Number = native + def &(that: Number): Number = native + def |(that: Number): Number = native + def ^(that: Number): Number = native + + def +(that: Dynamic): Dynamic = native // could be String if this or that is a String + def -(that: Dynamic): Number = native + def *(that: Dynamic): Number = native + def /(that: Dynamic): Number = native + def %(that: Dynamic): Number = native + def <<(that: Dynamic): Number = native + def >>(that: Dynamic): Number = native + def >>>(that: Dynamic): Number = native + def &(that: Dynamic): Number = native + def |(that: Dynamic): Number = native + def ^(that: Dynamic): Number = native + + def <(that: Number): Boolean = native + def >(that: Number): Boolean = native + def <=(that: Number): Boolean = native + def >=(that: Number): Boolean = native + + def <(that: String): Boolean = native + def >(that: String): Boolean = native + def <=(that: String): Boolean = native + def >=(that: String): Boolean = native + + def <(that: Dynamic): Boolean = native + def >(that: Dynamic): Boolean = native + def <=(that: Dynamic): Boolean = native + def >=(that: Dynamic): Boolean = native + + /* The result of (dyn && bool) and (dyn || bool) has, in theory, type + * (Dynamic v Boolean). This type cannot be expressed in Scala, but if it + * could, the operations one could apply on a (Dynamic v Boolean) would be + * the *intersection* of the operations one can apply on a Dynamic and on a + * Boolean. Since any operation can be applied on a Dynamic, this + * intersection is equal to the set of operations supported by Boolean. + * Hence the result type is restricted to Boolean. + */ + def &&(that: Boolean): Boolean = native + def ||(that: Boolean): Boolean = native + + def &&(that: Dynamic): Dynamic = native + def ||(that: Dynamic): Dynamic = native + + // Work around the annoying implicits in Predef in Scala 2.10. + def x: Dynamic = native + def x_=(value: Any): Unit = native +} + +/** Factory for dynamically typed JavaScript values. */ +object Dynamic { + /** Dynamic view of the global scope. */ + @inline def global: Dynamic = scala.scalajs.runtime.environmentInfo.global + + /** Instantiates a new object of a JavaScript class. */ + def newInstance(clazz: Dynamic)(args: Any*): Object with Dynamic = sys.error("stub") + + /** Creates a new object with a literal syntax. + * + * For example, + * js.Dynamic.literal(foo = 3, bar = "foobar") + * returns the JavaScript object + * {foo: 3, bar: "foobar"} + */ + object literal extends scala.Dynamic { + /** literal creation like this: + * js.Dynamic.literal(name1 = "value", name2 = "value") + */ + def applyDynamicNamed(name: java.lang.String)( + fields: (java.lang.String, Any)*): Object with Dynamic = sys.error("stub") + + /** literal creation like this: + * js.Dynamic.literal("name1" -> "value", "name2" -> "value") + * + * Note that this could be simply `def apply`, but this would make the + * applyDynamicNamed fail, since a call with named arguments would + * be routed to the `def apply`, rather than def dynamic version. + */ + def applyDynamic(name: java.lang.String)( + fields: (java.lang.String, Any)*): Object with Dynamic = sys.error("stub") + + } +} + +/** Base class of all JavaScript objects. */ +class Object extends Any { + def this(value: Any) = this() + + def toLocaleString(): String = native + def valueOf(): scala.Any = native + + /** Tests whether this object has the specified property as a direct property. + * + * Unlike [[js.Object.hasProperty]], this method does not check down the + * object's prototype chain. + * + * MDN + */ + def hasOwnProperty(v: String): Boolean = native + + /** Tests whether this object is in the prototype chain of another object. */ + def isPrototypeOf(v: Object): Boolean = native + + /** Tests whether the specified property in an object can be enumerated by a + * call to [[js.Object.properties]], with the exception of properties + * inherited through the prototype chain. If the object does not have the + * specified property, this method returns false. + * + * MDN + */ + def propertyIsEnumerable(v: String): Boolean = native +} + +/** The top-level `Object` JavaScript object. */ +object Object extends Object { + def apply(): Object = native + def apply(value: Any): Object = native + + /** Tests whether the object has a property on itself or in its prototype + * chain. This method is the equivalent of `p in o` in JavaScript. + */ + def hasProperty(o: Object, p: String): Boolean = sys.error("stub") + + /** + * The Object.getPrototypeOf() method returns the prototype (i.e. the + * internal [[Prototype]]) of the specified object. + * + * MDN + */ + def getPrototypeOf(o: Object): Object = native + + /** + * The Object.getOwnPropertyDescriptor() method returns a property descriptor + * for an own property (that is, one directly present on an object, not + * present by dint of being along an object's prototype chain) of a given object. + * + * MDN + */ + def getOwnPropertyDescriptor(o: Object, p: String): PropertyDescriptor = native + + /** + * Object.getOwnPropertyNames returns an array whose elements are strings + * corresponding to the enumerable and non-enumerable properties found + * directly upon obj. The ordering of the enumerable properties in the array + * is consistent with the ordering exposed by a for...in loop (or by Object.keys) + * over the properties of the object. The ordering of the non-enumerable + * properties in the array, and among the enumerable properties, is not defined. + * + * MDN + */ + def getOwnPropertyNames(o: Object): Array[String] = native + + /** + * The Object.create() method creates a new object with the specified + * prototype object and properties. + * + * MDN + */ + def create(o: Object, properties: Any): Object = native + def create(o: Object): Object = native + + /** + * The Object.defineProperty() method defines a new property directly on an + * object, or modifies an existing property on an object, and returns the + * object. + * + * This method allows precise addition to or modification of a property on an + * object. Normal property addition through assignment creates properties + * which show up during property enumeration (for...in loop or Object.keys method), + * whose values may be changed, and which may be deleted. This method allows + * these extra details to be changed from their defaults. + * + * Property descriptors present in objects come in two main flavors: data + * descriptors and accessor descriptors. A data descriptor is a property + * that has a value, which may or may not be writable. An accessor descriptor + * is a property described by a getter-setter pair of functions. A descriptor + * must be one of these two flavors; it cannot be both. + * + * MDN + */ + def defineProperty(o: Object, p: String, attributes: PropertyDescriptor): o.type = native + + /** + * The Object.defineProperties() method defines new or modifies existing + * properties directly on an object, returning the object. + * + * MDN + */ + def defineProperties(o: Object, properties: Any): o.type = native + + /** + * The Object.seal() method seals an object, preventing new properties from + * being added to it and marking all existing properties as non-configurable. + * Values of present properties can still be changed as long as they are + * writable. + * + * MDN + */ + def seal(o: Object): o.type = native + + /** + * The Object.freeze() method freezes an object: that is, prevents new properties + * from being added to it; prevents existing properties from being removed; + * and prevents existing properties, or their enumerability, configurability, + * or writability, from being changed. In essence the object is made effectively + * immutable. The method returns the object being frozen. + * + * MDN + */ + def freeze(o: Object): o.type = native + + /** + * The Object.preventExtensions() method prevents new properties from ever + * being added to an object (i.e. prevents future extensions to the object). + * + * An object is extensible if new properties can be added to it. preventExtensions + * marks an object as no longer extensible, so that it will never have + * properties beyond the ones it had at the time it was marked as non-extensible. + * Note that the properties of a non-extensible object, in general, may still be + * deleted. Attempting to add new properties to a non-extensible object will + * fail, either silently or by throwing a TypeError (most commonly, but not + * exclusively, when in strict mode). + * + * Object.preventExtensions only prevents addition of own properties. Properties + * can still be added to the object prototype. However, calling Object.preventExtensions + * on an object will also prevent extensions on its __proto__ property. + * + * MDN + */ + def preventExtensions(o: Object): o.type = native + + /** + * Returns true if the object is sealed, otherwise false. An object is sealed + * if it is not extensible and if all its properties are non-configurable and + * therefore not removable (but not necessarily non-writable). + * + * MDN + */ + def isSealed(o: Object): Boolean = native + + /** + * The Object.isFrozen() determines if an object is frozen. + * + * An object is frozen if and only if it is not extensible, all its properties + * are non-configurable, and all its data properties (that is, properties which + * are not accessor properties with getter or setter components) are non-writable. + * + * MDN + */ + def isFrozen(o: Object): Boolean = native + + /** + * Determines if extending of an object is allowed + * + * Objects are extensible by default: they can have new properties added to + * them, and (in engines that support __proto__ their __proto__ property) + * can be modified. An object can be marked as non-extensible using + * Object.preventExtensions, Object.seal, or Object.freeze + * + * MDN + */ + def isExtensible(o: Object): Boolean = native + + /** + * The Object.keys() method returns an array of a given object's own enumerable + * properties, in the same order as that provided by a for...in loop (the + * difference being that a for-in loop enumerates properties in the prototype + * chain as well). + * + * MDN + */ + def keys(o: Object): Array[String] = native + + /** Returns the names of all the enumerable properties of this object, + * including properties in its prototype chain. + * + * This method returns the same set of names that would be enumerated by + * a for-in loop in JavaScript, but not necessarily in the same order. + * + * If the underlying implementation guarantees an order for for-in loops, + * then this is guaranteed to be consistent with [[keys]], in the sense + * that the list returned by [[keys]] is a sublist of the list returned by + * this method (not just a subset). + */ + def properties(o: Any): Array[String] = sys.error("stub") +} + +package prim { + +/** Primitive JavaScript number. + * + * In most situations, you should not need this trait, and use + * [[scala.Double]] instead (or [[scala.Int]] where appropriate). + */ +sealed trait Number extends Any { + def unary_+(): Number = native + def unary_-(): Number = native + def unary_~(): Number = native + + def +(that: Number): Number = native + def -(that: Number): Number = native + def *(that: Number): Number = native + def /(that: Number): Number = native + def %(that: Number): Number = native + def <<(that: Number): Number = native + def >>(that: Number): Number = native + def >>>(that: Number): Number = native + def &(that: Number): Number = native + def |(that: Number): Number = native + def ^(that: Number): Number = native + + def +(that: Dynamic): Dynamic = native // could be a String if that is a String + def -(that: Dynamic): Number = native + def *(that: Dynamic): Number = native + def /(that: Dynamic): Number = native + def %(that: Dynamic): Number = native + def <<(that: Dynamic): Number = native + def >>(that: Dynamic): Number = native + def >>>(that: Dynamic): Number = native + def &(that: Dynamic): Number = native + def |(that: Dynamic): Number = native + def ^(that: Dynamic): Number = native + + def <(that: Number): Boolean = native + def >(that: Number): Boolean = native + def <=(that: Number): Boolean = native + def >=(that: Number): Boolean = native + + def <(that: Dynamic): Boolean = native + def >(that: Dynamic): Boolean = native + def <=(that: Dynamic): Boolean = native + def >=(that: Dynamic): Boolean = native + + def toString(radix: Number): String = native + + /** + * Returns a string representation of number that does not use exponential + * notation and has exactly digits digits after the decimal place. The number + * is rounded if necessary, and the fractional part is padded with zeros if + * necessary so that it has the specified length. If number is greater than + * 1e+21, this method simply calls Number.prototype.toString() and returns + * a string in exponential notation. + * + * MDN + */ + def toFixed(fractionDigits: Number): String = native + def toFixed(): String = native + + /** + * Returns a string representing a Number object in exponential notation with one + * digit before the decimal point, rounded to fractionDigits digits after the + * decimal point. If the fractionDigits argument is omitted, the number of + * digits after the decimal point defaults to the number of digits necessary + * to represent the value uniquely. + * + * If a number has more digits that requested by the fractionDigits parameter, + * the number is rounded to the nearest number represented by fractionDigits + * digits. See the discussion of rounding in the description of the toFixed() + * method, which also applies to toExponential(). + * + * MDN + */ + def toExponential(fractionDigits: Number): String = native + def toExponential(): String = native + + /** + * Returns a string representing a Number object in fixed-point or exponential + * notation rounded to precision significant digits. See the discussion of + * rounding in the description of the Number.prototype.toFixed() method, which + * also applies to toPrecision. + * + * If the precision argument is omitted, behaves as Number.prototype.toString(). + * If it is a non-integer value, it is rounded to the nearest integer. + * + * MDN + */ + def toPrecision(precision: Number): String = native + def toPrecision(): String = native +} + +/** The top-level `Number` JavaScript object */ +object Number extends Object { + /** + * The Number.MAX_VALUE property represents the maximum numeric value + * representable in JavaScript. + * + * The MAX_VALUE property has a value of approximately 1.79E+308. Values + * larger than MAX_VALUE are represented as "Infinity". + * + * MDN + */ + val MAX_VALUE: Double = native + /** + * The Number.MIN_VALUE property represents the smallest positive numeric + * value representable in JavaScript. + * + * The MIN_VALUE property is the number closest to 0, not the most negative + * number, that JavaScript can represent. + * + * MIN_VALUE has a value of approximately 5e-324. Values smaller than MIN_VALUE + * ("underflow values") are converted to 0. + * + * MDN + */ + val MIN_VALUE: Double = native + /** + * The Number.NaN property represents Not-A-Number. Equivalent of NaN. + * + * MDN + */ + val NaN: Double = native + + /** + * The Number.NEGATIVE_INFINITY property represents the negative Infinity value. + * + * MDN + */ + val NEGATIVE_INFINITY: Double = native + /** + * The Number.POSITIVE_INFINITY property represents the positive Infinity value. + * + * MDN + */ + val POSITIVE_INFINITY: Double = native +} + +/** Primitive JavaScript boolean. + * + * In most situations, you should not need this trait, and use + * [[scala.Boolean]] instead. + */ +sealed trait Boolean extends Any { + def unary_!(): scala.Boolean = native + + def &&(that: Boolean): Boolean = native + def ||(that: Boolean): Boolean = native + + // See the comment in `Dynamic` for the rationale of returning Boolean here. + def &&(that: Dynamic): Boolean = native + def ||(that: Dynamic): Boolean = native +} + +/** The top-level `Boolean` JavaScript object. */ +object Boolean extends Object + +/** Primitive JavaScript string. + * + * In most situations, you should not need this trait, and use + * [[java.lang.String]] instead. + */ +sealed trait String extends Any { + def +(that: String): String = native + def +(that: Any): String = native + def +(that: Dynamic): String = native + + def < (that: String): Boolean = native + def < (that: Dynamic): Boolean = native + + def > (that: String): Boolean = native + def > (that: Dynamic): Boolean = native + + def <=(that: String): Boolean = native + def <=(that: Dynamic): Boolean = native + + def >=(that: String): Boolean = native + def >=(that: Dynamic): Boolean = native + + /** + * This property returns the number of code units in the string. UTF-16, + * the string format used by JavaScript, uses a single 16-bit code unit to + * represent the most common characters, but needs to use two code units for + * less commonly-used characters, so it's possible for the value returned by + * length to not match the actual number of characters in the string. + * + * For an empty string, length is 0. + * + * MDN + */ + val length: Number = native + + /** + * The chartAt() method returns the specified character from a string. + * + * Characters in a string are indexed from left to right. The index of the + * first character is 0, and the index of the last character in a string + * called stringName is stringName.length - 1. If the index you supply is out + * of range, JavaScript returns an empty string. + * + * MDN + */ + def charAt(pos: Number): String = native + + /** + * The charCodeAt() method returns the numeric Unicode value of the character + * at the given index (except for unicode codepoints > 0x10000). + * + * MDN + */ + def charCodeAt(index: Number): Number = native + + /** + * concat combines the text from one or more strings and returns a new string. + * Changes to the text in one string do not affect the other string. + * MDN + */ + def concat(strings: String*): String = native + + /** + * Returns the index within the calling String object of the first occurrence + * of the specified value, starting the search at fromIndex, + * + * returns -1 if the value is not found. + * + * MDN + */ + def indexOf(searchString: String, position: Number): Number = native + def indexOf(searchString: String): Number = native + + /** + * Returns the index within the calling String object of the last occurrence + * of the specified value, or -1 if not found. The calling string is searched + * backward, starting at fromIndex. + * + * MDN + */ + def lastIndexOf(searchString: String, position: Number): Number = native + def lastIndexOf(searchString: String): Number = native + + /** + * Returns a number indicating whether a reference string comes before or + * after or is the same as the given string in sort order. The new locales + * and options arguments let applications specify the language whose sort + * order should be used and customize the behavior of the function. In older + * implementations, which ignore the locales and options arguments, the locale + * and sort order used are entirely implementation dependent. + * + * MDN + */ + def localeCompare(that: String): Number = native + + /** + * Used to retrieve the matches when matching a string against a regular + * expression. + * + * If the regular expression does not include the g flag, returns the same + * result as regexp.exec(string). The returned Array has an extra input + * property, which contains the original string that was parsed. In addition, + * it has an index property, which represents the zero-based index of the + * match in the string. + * + * If the regular expression includes the g flag, the method returns an Array + * containing all matches. If there were no matches, the method returns null. + * + * MDN + */ + def `match`(regexp: String): Array[String] = native + def `match`(regexp: RegExp): Array[String] = native + + /** + * Returns a new string with some or all matches of a pattern replaced by a + * replacement. The pattern can be a string or a RegExp, and the replacement + * can be a string or a function to be called for each match. + * + * This method does not change the String object it is called on. It simply + * returns a new string. + * + * To perform a global search and replace, either include the g switch in the + * regular expression or if the first parameter is a string, include g in the + * flags parameter. + * + * MDN + */ + def replace(searchValue: String, replaceValue: String): String = native + def replace(searchValue: String, replaceValue: Any): String = native + def replace(searchValue: RegExp, replaceValue: String): String = native + def replace(searchValue: RegExp, replaceValue: Any): String = native + + /** + * If successful, search returns the index of the regular expression inside + * the string. Otherwise, it returns -1. + * + * When you want to know whether a pattern is found in a string use search + * (similar to the regular expression test method); for more information + * (but slower execution) use match (similar to the regular expression exec + * method). + * + * MDN + */ + def search(regexp: String): Number = native + def search(regexp: RegExp): Number = native + + /** + * slice extracts the text from one string and returns a new string. Changes + * to the text in one string do not affect the other string. + * + * slice extracts up to but not including endSlice. string.slice(1,4) extracts + * the second character through the fourth character (characters indexed 1, 2, + * and 3). + * + * As an example, string.slice(2,-1) extracts the third character through the + * second to last character in the string. + * + * MDN + */ + def slice(start: Number, end: Number): String = native + def slice(start: Number): String = native + + /** + * Splits a String object into an array of strings by separating the string + * into substrings. + * + * When found, separator is removed from the string and the substrings are + * returned in an array. If separator is omitted, the array contains one + * element consisting of the entire string. If separator is an empty string, + * string is converted to an array of characters. + * + * If separator is a regular expression that contains capturing parentheses, + * then each time separator is matched, the results (including any undefined + * results) of the capturing parentheses are spliced into the output array. + * However, not all browsers support this capability. + * + * Note: When the string is empty, split returns an array containing one + * empty string, rather than an empty array. + * + * MDN + */ + def split(separator: String, limit: Number): Array[String] = native + def split(separator: String): Array[String] = native + def split(separator: RegExp, limit: Number): Array[String] = native + def split(separator: RegExp): Array[String] = native + + /** + * Returns a subset of a string between one index and another, or through + * the end of the string. + * + * MDN + */ + def substring(start: Number, end: Number): String = native + def substring(start: Number): String = native + + /** + * Returns the calling string value converted to lowercase. + * + * MDN + */ + def toLowerCase(): String = native + + /** + * The toLocaleLowerCase method returns the value of the string converted to + * lower case according to any locale-specific case mappings. toLocaleLowerCase + * does not affect the value of the string itself. In most cases, this will + * produce the same result as toLowerCase(), but for some locales, such as + * Turkish, whose case mappings do not follow the default case mappings in Unicode, + * there may be a different result. + * + * MDN + */ + def toLocaleLowerCase(): String = native + + /** + * Returns the calling string value converted to uppercase. + * + * MDN + */ + def toUpperCase(): String = native + + /** + * The toLocaleUpperCase method returns the value of the string converted to + * upper case according to any locale-specific case mappings. toLocaleUpperCase + * does not affect the value of the string itself. In most cases, this will + * produce the same result as toUpperCase(), but for some locales, such as + * Turkish, whose case mappings do not follow the default case mappings in Unicode, + * there may be a different result. + * + * MDN + */ + def toLocaleUpperCase(): String = native + + /** + * Removes whitespace from both ends of the string. + * + * MDN + */ + def trim(): String = native +} + +/** The top-level `String` JavaScript object. */ +object String extends Object { + def fromCharCode(codes: Int*): java.lang.String = native +} + +/** Primitive JavaScript undefined value. + * + * In most situations, you should not need this trait, and use + * [[scala.Unit]] instead. + */ +sealed trait Undefined extends Any + +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/PropertyDescriptor.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/PropertyDescriptor.scala new file mode 100644 index 0000000..b91e01d --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/PropertyDescriptor.scala @@ -0,0 +1,20 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + + +package scala.scalajs.js + +trait PropertyDescriptor extends Object { + var configurable: Boolean = native + var enumerable: Boolean = native + var value: Any = native + var writable: Boolean = native + var get: Function0[Any] = native + var set: Function1[Any, Any] = native +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/RegExp.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/RegExp.scala new file mode 100644 index 0000000..73f465a --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/RegExp.scala @@ -0,0 +1,108 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + +/** + * All doc-comments marked as "MDN" are by Mozilla Contributors, + * distributed under the Creative Commons Attribution-ShareAlike license from + * https://developer.mozilla.org/en-US/docs/Web/Reference/API + */ +package scala.scalajs.js + +/** + * The RegExp constructor creates a regular expression object for matching + * text with a pattern. + * + * MDN + */ +class RegExp(pattern: String, flags: String = "") extends Object { + /** + * The source property returns a String containing the text of the pattern, + * excluding the forward slashes. It is a read-only property of an individual + * regular expression instance. source does not contain any flags (like "g", + * "i" or "m") of the regular expression. + * + * MDN + */ + val source: String = native + /** + * The value of global is a Boolean and true if the "g" flag was used; + * otherwise, false. The "g" flag indicates that the regular expression + * should be tested against all possible matches in a string. + * + * MDN + */ + val global: Boolean = native + /** + * The value of ignoreCase is a Boolean and true if the "i" flag was used; + * otherwise, false. The "i" flag indicates that case should be ignored while + * attempting a match in a string. + * + * MDN + */ + val ignoreCase: Boolean = native + /** + * The value of multiline is a Boolean and is true if the "m" flag was used; + * otherwise, false. The "m" flag indicates that a multiline input string + * should be treated as multiple lines. For example, if "m" is used, "^" and + * "$" change from matching at only the start or end of the entire string to + * the start or end of any line within the string. + + * MDN + */ + val multiline: Boolean = native + + /** + * The lastIndex is a read/write integer property of regular expressions that + * specifies the index at which to start the next match. + * + * MDN + */ + var lastIndex: Int = native + + /** + * The exec() method executes a search for a match in a specified string. + * Returns a result array, or null. + * + * If you are executing a match simply to find true or false, use the + * RegExp.test() method or the String.search() method. + * + * If the match succeeds, the exec method returns an array and updates properties + * of the regular expression object. The returned array has the matched text + * as the first item, and then one item for each capturing parenthesis that + * matched containing the text that was captured. + * + * If the match fails, the exec method returns null. + * + * MDN + */ + def exec(string: String): RegExp.ExecResult = native + + /** + * The test() method executes a search for a match between a regular expression + * and a specified string. Returns true or false. + * + * You can use test() whenever want to know whether a pattern is found in a + * string (similar to the String.search method); for more information (but + * slower execution) use the exec method (similar to the String.match method). + * As with exec (or in combination with it), test called multiple times on the + * same global regular expression instance will advance past the previous match. + * + * MDN + */ + def test(string: String): Boolean = native +} + +object RegExp extends Object { + def apply(pattern: String, flags: String = ""): RegExp = native + + trait ExecResult extends Array[UndefOr[String]] { + var index: Int = native + var input: String = native + } +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/ThisFunction.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/ThisFunction.scala new file mode 100644 index 0000000..2506eed --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/ThisFunction.scala @@ -0,0 +1,160 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-js.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + +/** + * All doc-comments marked as "MDN" are by Mozilla Contributors, + * distributed under the Creative Commons Attribution-ShareAlike license from + * https://developer.mozilla.org/en-US/docs/Web/Reference/API + */ +package scala.scalajs.js + +import scala.language.implicitConversions + +/** A JavaScript function where `this` is considered as a first parameter. + * + * @see [[http://www.scala-js.org/doc/calling-javascript.html Calling JavaScript from Scala.js]] + */ +trait ThisFunction extends Function { +} + +object ThisFunction { + implicit def fromFunction1[T1, R](f: scala.Function1[T1, R]): ThisFunction0[T1, R] = sys.error("stub") + implicit def fromFunction2[T1, T2, R](f: scala.Function2[T1, T2, R]): ThisFunction1[T1, T2, R] = sys.error("stub") + implicit def fromFunction3[T1, T2, T3, R](f: scala.Function3[T1, T2, T3, R]): ThisFunction2[T1, T2, T3, R] = sys.error("stub") + implicit def fromFunction4[T1, T2, T3, T4, R](f: scala.Function4[T1, T2, T3, T4, R]): ThisFunction3[T1, T2, T3, T4, R] = sys.error("stub") + implicit def fromFunction5[T1, T2, T3, T4, T5, R](f: scala.Function5[T1, T2, T3, T4, T5, R]): ThisFunction4[T1, T2, T3, T4, T5, R] = sys.error("stub") + implicit def fromFunction6[T1, T2, T3, T4, T5, T6, R](f: scala.Function6[T1, T2, T3, T4, T5, T6, R]): ThisFunction5[T1, T2, T3, T4, T5, T6, R] = sys.error("stub") + implicit def fromFunction7[T1, T2, T3, T4, T5, T6, T7, R](f: scala.Function7[T1, T2, T3, T4, T5, T6, T7, R]): ThisFunction6[T1, T2, T3, T4, T5, T6, T7, R] = sys.error("stub") + implicit def fromFunction8[T1, T2, T3, T4, T5, T6, T7, T8, R](f: scala.Function8[T1, T2, T3, T4, T5, T6, T7, T8, R]): ThisFunction7[T1, T2, T3, T4, T5, T6, T7, T8, R] = sys.error("stub") + implicit def fromFunction9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R](f: scala.Function9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]): ThisFunction8[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] = sys.error("stub") + implicit def fromFunction10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R](f: scala.Function10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]): ThisFunction9[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R] = sys.error("stub") + implicit def fromFunction11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R](f: scala.Function11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R]): ThisFunction10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R] = sys.error("stub") + implicit def fromFunction12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R](f: scala.Function12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R]): ThisFunction11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R] = sys.error("stub") + implicit def fromFunction13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R](f: scala.Function13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R]): ThisFunction12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R] = sys.error("stub") + implicit def fromFunction14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R](f: scala.Function14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R]): ThisFunction13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R] = sys.error("stub") + implicit def fromFunction15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R](f: scala.Function15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R]): ThisFunction14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R] = sys.error("stub") + implicit def fromFunction16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R](f: scala.Function16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R]): ThisFunction15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R] = sys.error("stub") + implicit def fromFunction17[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R](f: scala.Function17[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R]): ThisFunction16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R] = sys.error("stub") + implicit def fromFunction18[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R](f: scala.Function18[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R]): ThisFunction17[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R] = sys.error("stub") + implicit def fromFunction19[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R](f: scala.Function19[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R]): ThisFunction18[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R] = sys.error("stub") + implicit def fromFunction20[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R](f: scala.Function20[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R]): ThisFunction19[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R] = sys.error("stub") + implicit def fromFunction21[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R](f: scala.Function21[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R]): ThisFunction20[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R] = sys.error("stub") + implicit def fromFunction22[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R](f: scala.Function22[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R]): ThisFunction21[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R] = sys.error("stub") + + implicit def toFunction1[T1, R](f: ThisFunction0[T1, R]): scala.Function1[T1, R] = (x1) => f(x1) + implicit def toFunction2[T1, T2, R](f: ThisFunction1[T1, T2, R]): scala.Function2[T1, T2, R] = (x1, x2) => f(x1, x2) + implicit def toFunction3[T1, T2, T3, R](f: ThisFunction2[T1, T2, T3, R]): scala.Function3[T1, T2, T3, R] = (x1, x2, x3) => f(x1, x2, x3) + implicit def toFunction4[T1, T2, T3, T4, R](f: ThisFunction3[T1, T2, T3, T4, R]): scala.Function4[T1, T2, T3, T4, R] = (x1, x2, x3, x4) => f(x1, x2, x3, x4) + implicit def toFunction5[T1, T2, T3, T4, T5, R](f: ThisFunction4[T1, T2, T3, T4, T5, R]): scala.Function5[T1, T2, T3, T4, T5, R] = (x1, x2, x3, x4, x5) => f(x1, x2, x3, x4, x5) + implicit def toFunction6[T1, T2, T3, T4, T5, T6, R](f: ThisFunction5[T1, T2, T3, T4, T5, T6, R]): scala.Function6[T1, T2, T3, T4, T5, T6, R] = (x1, x2, x3, x4, x5, x6) => f(x1, x2, x3, x4, x5, x6) + implicit def toFunction7[T1, T2, T3, T4, T5, T6, T7, R](f: ThisFunction6[T1, T2, T3, T4, T5, T6, T7, R]): scala.Function7[T1, T2, T3, T4, T5, T6, T7, R] = (x1, x2, x3, x4, x5, x6, x7) => f(x1, x2, x3, x4, x5, x6, x7) + implicit def toFunction8[T1, T2, T3, T4, T5, T6, T7, T8, R](f: ThisFunction7[T1, T2, T3, T4, T5, T6, T7, T8, R]): scala.Function8[T1, T2, T3, T4, T5, T6, T7, T8, R] = (x1, x2, x3, x4, x5, x6, x7, x8) => f(x1, x2, x3, x4, x5, x6, x7, x8) + implicit def toFunction9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R](f: ThisFunction8[T1, T2, T3, T4, T5, T6, T7, T8, T9, R]): scala.Function9[T1, T2, T3, T4, T5, T6, T7, T8, T9, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9) + implicit def toFunction10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R](f: ThisFunction9[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R]): scala.Function10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10) + implicit def toFunction11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R](f: ThisFunction10[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R]): scala.Function11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11) + implicit def toFunction12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R](f: ThisFunction11[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R]): scala.Function12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12) + implicit def toFunction13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R](f: ThisFunction12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R]): scala.Function13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13) + implicit def toFunction14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R](f: ThisFunction13[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R]): scala.Function14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14) + implicit def toFunction15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R](f: ThisFunction14[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R]): scala.Function15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15) + implicit def toFunction16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R](f: ThisFunction15[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R]): scala.Function16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16) + implicit def toFunction17[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R](f: ThisFunction16[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R]): scala.Function17[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17) + implicit def toFunction18[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R](f: ThisFunction17[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R]): scala.Function18[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18) + implicit def toFunction19[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R](f: ThisFunction18[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R]): scala.Function19[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19) + implicit def toFunction20[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R](f: ThisFunction19[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R]): scala.Function20[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20) + implicit def toFunction21[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R](f: ThisFunction20[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R]): scala.Function21[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21) + implicit def toFunction22[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R](f: ThisFunction21[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R]): scala.Function22[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19, T20, T21, T22, R] = (x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22) => f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21, x22) +} + +trait ThisFunction0[-T0, +R] extends ThisFunction { + def apply(thisArg: T0): R +} + +trait ThisFunction1[-T0, -T1, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1): R +} + +trait ThisFunction2[-T0, -T1, -T2, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2): R +} + +trait ThisFunction3[-T0, -T1, -T2, -T3, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3): R +} + +trait ThisFunction4[-T0, -T1, -T2, -T3, -T4, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4): R +} + +trait ThisFunction5[-T0, -T1, -T2, -T3, -T4, -T5, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5): R +} + +trait ThisFunction6[-T0, -T1, -T2, -T3, -T4, -T5, -T6, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6): R +} + +trait ThisFunction7[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7): R +} + +trait ThisFunction8[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8): R +} + +trait ThisFunction9[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9): R +} + +trait ThisFunction10[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10): R +} + +trait ThisFunction11[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11): R +} + +trait ThisFunction12[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12): R +} + +trait ThisFunction13[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13): R +} + +trait ThisFunction14[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14): R +} + +trait ThisFunction15[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15): R +} + +trait ThisFunction16[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, -T16, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15, arg16: T16): R +} + +trait ThisFunction17[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, -T16, -T17, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15, arg16: T16, arg17: T17): R +} + +trait ThisFunction18[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, -T16, -T17, -T18, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15, arg16: T16, arg17: T17, arg18: T18): R +} + +trait ThisFunction19[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, -T16, -T17, -T18, -T19, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15, arg16: T16, arg17: T17, arg18: T18, arg19: T19): R +} + +trait ThisFunction20[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, -T16, -T17, -T18, -T19, -T20, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15, arg16: T16, arg17: T17, arg18: T18, arg19: T19, arg20: T20): R +} + +trait ThisFunction21[-T0, -T1, -T2, -T3, -T4, -T5, -T6, -T7, -T8, -T9, -T10, -T11, -T12, -T13, -T14, -T15, -T16, -T17, -T18, -T19, -T20, -T21, +R] extends ThisFunction { + def apply(thisArg: T0, arg1: T1, arg2: T2, arg3: T3, arg4: T4, arg5: T5, arg6: T6, arg7: T7, arg8: T8, arg9: T9, arg10: T10, arg11: T11, arg12: T12, arg13: T13, arg14: T14, arg15: T15, arg16: T16, arg17: T17, arg18: T18, arg19: T19, arg20: T20, arg21: T21): R +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/UndefOr.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/UndefOr.scala new file mode 100644 index 0000000..b356e3a --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/UndefOr.scala @@ -0,0 +1,254 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + +package scala.scalajs.js + +import scala.language.implicitConversions + +/** Value of type A or the JS undefined value. + * In a type system with union types, this would really be + * `A | js.prim.Undefined`. Since Scala does not have union types, but this + * particular union is crucial to many interoperability scenarios, it is + * provided as this trait. + * + * An API similar to that of [[scala.Option]] is provided through the + * [[UndefOrOps]] implicit class, with the understanding that `undefined` is + * the None value. + */ +@scala.scalajs.js.annotation.RawJSType // Don't do this at home! +sealed trait UndefOr[+A] + +object UndefOr { + implicit def any2undefOrA[A](value: A): UndefOr[A] = + value.asInstanceOf[UndefOr[A]] + + implicit def undef2undefOr(value: prim.Undefined): UndefOr[Nothing] = + value.asInstanceOf[UndefOr[Nothing]] + + implicit def undefOr2ops[A](value: UndefOr[A]): UndefOrOps[A] = + new UndefOrOps(value) + + implicit def undefOr2jsAny[A](value: UndefOr[A])(implicit ev: A => Any): Any = + value.map(ev).asInstanceOf[Any] +} + +final class UndefOrOps[A](val self: UndefOr[A]) extends AnyVal { + import UndefOrOps._ + + /** Returns true if the option is `undefined`, false otherwise. + */ + @inline final def isEmpty: Boolean = isUndefined(self) + + /** Returns true if the option is not `undefined`, false otherwise. + */ + @inline final def isDefined: Boolean = !isEmpty + + /** Returns the option's value. + * @note The option must be nonEmpty. + * @throws Predef.NoSuchElementException if the option is empty. + */ + @inline final def get: A = + if (isEmpty) throw new NoSuchElementException("undefined.get") + else self.asInstanceOf[A] + + @inline final private def forceGet: A = self.asInstanceOf[A] + + /** Returns the option's value if the option is nonempty, otherwise + * return the result of evaluating `default`. + * + * @param default the default expression. + */ + @inline final def getOrElse[B >: A](default: => B): B = + if (isEmpty) default else this.forceGet + + /** Returns the option's value if it is nonempty, + * or `null` if it is empty. + * Although the use of null is discouraged, code written to use + * $option must often interface with code that expects and returns nulls. + * @example {{{ + * val initalText: Option[String] = getInitialText + * val textField = new JComponent(initalText.orNull,20) + * }}} + */ + @inline final def orNull[A1 >: A](implicit ev: Null <:< A1): A1 = + this getOrElse ev(null) + + /** Returns a $some containing the result of applying $f to this $option's + * value if this $option is nonempty. + * Otherwise return $none. + * + * @note This is similar to `flatMap` except here, + * $f does not need to wrap its result in an $option. + * + * @param f the function to apply + * @see flatMap + * @see foreach + */ + @inline final def map[B](f: A => B): UndefOr[B] = + if (isEmpty) undefined else f(this.forceGet) + + /** Returns the result of applying $f to this $option's + * value if the $option is nonempty. Otherwise, evaluates + * expression `ifEmpty`. + * + * @note This is equivalent to `$option map f getOrElse ifEmpty`. + * + * @param ifEmpty the expression to evaluate if empty. + * @param f the function to apply if nonempty. + */ + @inline final def fold[B](ifEmpty: => B)(f: A => B): B = + if (isEmpty) ifEmpty else f(this.forceGet) + + /** Returns the result of applying $f to this $option's value if + * this $option is nonempty. + * Returns $none if this $option is empty. + * Slightly different from `map` in that $f is expected to + * return an $option (which could be $none). + * + * @param f the function to apply + * @see map + * @see foreach + */ + @inline final def flatMap[B](f: A => UndefOr[B]): UndefOr[B] = + if (isEmpty) undefined else f(this.forceGet) + + def flatten[B](implicit ev: A <:< UndefOr[B]): UndefOr[B] = + if (isEmpty) undefined else ev(this.forceGet) + + /** Returns this $option if it is nonempty '''and''' applying the predicate $p to + * this $option's value returns true. Otherwise, return $none. + * + * @param p the predicate used for testing. + */ + @inline final def filter(p: A => Boolean): UndefOr[A] = + if (isEmpty || p(this.forceGet)) self else undefined + + /** Returns this $option if it is nonempty '''and''' applying the predicate $p to + * this $option's value returns false. Otherwise, return $none. + * + * @param p the predicate used for testing. + */ + @inline final def filterNot(p: A => Boolean): UndefOr[A] = + if (isEmpty || !p(this.forceGet)) self else undefined + + /** Returns false if the option is $none, true otherwise. + * @note Implemented here to avoid the implicit conversion to Iterable. + */ + final def nonEmpty = isDefined + + /** Necessary to keep $option from being implicitly converted to + * [[scala.collection.Iterable]] in `for` comprehensions. + */ + @inline final def withFilter(p: A => Boolean): WithFilter[A] = + new WithFilter(self, p) + + /** Returns true if this option is nonempty '''and''' the predicate + * $p returns true when applied to this $option's value. + * Otherwise, returns false. + * + * @param p the predicate to test + */ + @inline final def exists(p: A => Boolean): Boolean = + !isEmpty && p(this.forceGet) + + /** Returns true if this option is empty '''or''' the predicate + * $p returns true when applied to this $option's value. + * + * @param p the predicate to test + */ + @inline final def forall(p: A => Boolean): Boolean = + isEmpty || p(this.forceGet) + + /** Apply the given procedure $f to the option's value, + * if it is nonempty. Otherwise, do nothing. + * + * @param f the procedure to apply. + * @see map + * @see flatMap + */ + @inline final def foreach[U](f: A => U): Unit = + if (!isEmpty) f(this.forceGet) + + /** Returns a $some containing the result of + * applying `pf` to this $option's contained + * value, '''if''' this option is + * nonempty '''and''' `pf` is defined for that value. + * Returns $none otherwise. + * + * @param pf the partial function. + * @return the result of applying `pf` to this $option's + * value (if possible), or $none. + */ + @inline final def collect[B](pf: PartialFunction[A, B]): UndefOr[B] = + if (isEmpty) undefined + else pf.applyOrElse(this.forceGet, (_: A) => undefined).asInstanceOf[UndefOr[B]] + + /** Returns this $option if it is nonempty, + * otherwise return the result of evaluating `alternative`. + * @param alternative the alternative expression. + */ + @inline final def orElse[B >: A](alternative: => UndefOr[B]): UndefOr[B] = + if (isEmpty) alternative else self + + /** Returns a singleton iterator returning the $option's value + * if it is nonempty, or an empty iterator if the option is empty. + */ + def iterator: Iterator[A] = + if (isEmpty) scala.collection.Iterator.empty + else scala.collection.Iterator.single(this.forceGet) + + /** Returns a singleton list containing the $option's value + * if it is nonempty, or the empty list if the $option is empty. + */ + def toList: List[A] = + if (isEmpty) Nil else this.forceGet :: Nil + + /** Returns a [[scala.util.Left]] containing the given + * argument `left` if this $option is empty, or + * a [[scala.util.Right]] containing this $option's value if + * this is nonempty. + * + * @param left the expression to evaluate and return if this is empty + * @see toLeft + */ + @inline final def toRight[X](left: => X): Either[X, A] = + if (isEmpty) Left(left) else Right(this.forceGet) + + /** Returns a [[scala.util.Right]] containing the given + * argument `right` if this is empty, or + * a [[scala.util.Left]] containing this $option's value + * if this $option is nonempty. + * + * @param right the expression to evaluate and return if this is empty + * @see toRight + */ + @inline final def toLeft[X](right: => X): Either[A, X] = + if (isEmpty) Right(right) else Left(this.forceGet) + + /** Returns a [[scala.Some]] containing this $options's value + * if this $option is nonempty, [[scala.None]] otherwise. + */ + @inline final def toOption: Option[A] = + if (isEmpty) None else Some(this.forceGet) +} + +object UndefOrOps { + + /** We need a whole WithFilter class to honor the "doesn't create a new + * collection" contract even though it seems unlikely to matter much in a + * collection with max size 1. + */ + class WithFilter[A](self: UndefOr[A], p: A => Boolean) { + def map[B](f: A => B): UndefOr[B] = self filter p map f + def flatMap[B](f: A => UndefOr[B]): UndefOr[B] = self filter p flatMap f + def foreach[U](f: A => U): Unit = self filter p foreach f + def withFilter(q: A => Boolean): WithFilter[A] = + new WithFilter[A](self, x => p(x) && q(x)) + } +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedArray.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedArray.scala new file mode 100644 index 0000000..3626c15 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedArray.scala @@ -0,0 +1,92 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + +package scala.scalajs.js + +import scala.language.implicitConversions + +import scala.collection.mutable +import mutable.Builder + +import scala.collection.generic.{CanBuildFrom, GenericCompanion, SeqFactory} + +/** Equivalent of scm.WrappedArray for js.Array */ +@inline +final class WrappedArray[A](val array: Array[A]) + extends mutable.AbstractBuffer[A] + with scala.collection.generic.GenericTraversableTemplate[A, WrappedArray] + with mutable.IndexedSeq[A] + with mutable.BufferLike[A, WrappedArray[A]] + with mutable.ArrayLike[A, WrappedArray[A]] + with Builder[A, WrappedArray[A]] { + + /** Creates a new empty [[WrappedArray]]. */ + def this() = this(Array()) + + override def companion: GenericCompanion[WrappedArray] = WrappedArray + + // IndexedSeq interface + + @inline def update(index: Int, elem: A): Unit = array(index) = elem + @inline def apply(index: Int): A = array(index) + @inline def length: Int = array.length + + // Builder interface + + @inline def +=(elem: A): this.type = { + array.push(elem) + this + } + + @inline def clear(): Unit = + array.length = 0 + + @inline def result(): WrappedArray[A] = this + + // Rest of BufferLike interface + + @inline def +=:(elem: A): this.type = { + array.unshift(elem) + this + } + + @inline override def ++=:(xs: TraversableOnce[A]): this.type = { + array.unshift(xs.toSeq: _*) + this + } + + @inline def insertAll(n: Int, + elems: scala.collection.Traversable[A]): Unit = { + array.splice(n, 0, elems.toSeq: _*) + } + + @inline def remove(n: Int): A = + array.splice(n, 1)(0) + + @inline override def remove(n: Int, count: Int): Unit = + array.splice(n, count) + + @inline override def stringPrefix: String = "WrappedArray" + +} + +/** $factoryInfo + * @define coll wrapped array + * @define Coll `WrappedArray` + */ +object WrappedArray extends SeqFactory[WrappedArray] { + /** $genericCanBuildFromInfo */ + implicit def canBuildFrom[A]: CanBuildFrom[Coll, A, WrappedArray[A]] = + ReusableCBF.asInstanceOf[GenericCanBuildFrom[A]] + + def newBuilder[A]: Builder[A, WrappedArray[A]] = new WrappedArray[A] + + implicit def toJSArray[A](wrappedArray: WrappedArray[A]): Array[A] = + wrappedArray.array + +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedDictionary.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedDictionary.scala new file mode 100644 index 0000000..f215e6e --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/WrappedDictionary.scala @@ -0,0 +1,89 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + +package scala.scalajs.js + +import scala.collection.mutable +import mutable.Builder + +import scala.collection.generic.CanBuildFrom + +/** Wrapper to use a js.Dictionary as a scala.mutable.Map */ +@inline +class WrappedDictionary[A](val dict: Dictionary[A]) + extends mutable.AbstractMap[String, A] + with mutable.Map[String, A] + with mutable.MapLike[String, A, WrappedDictionary[A]] { + + def get(key: String): Option[A] = { + if (contains(key)) + Some(dict(key)) + else + None + } + + override def contains(key: String): Boolean = + dict.hasOwnProperty(key) + + def -=(key: String): this.type = { + dict.delete(key) + this + } + + def +=(kv: (String, A)): this.type = { + dict(kv._1) = kv._2 + this + } + + def iterator: Iterator[(String, A)] = new Iterator[(String, A)] { + private[this] val keys = Object.keys(dict) + private[this] var index: Int = 0 + def hasNext(): Boolean = index < keys.length + def next(): (String, A) = { + val key = keys(index) + index += 1 + (key, dict(key)) + } + } + + override def keys: Iterable[String] = + Object.keys(dict) + + override def empty: WrappedDictionary[A] = + new WrappedDictionary(Dictionary.empty) + +} + +object WrappedDictionary { + // Note: We can't extend MutableMapFactory[WrappedDictionary] since + // it requires support for any type of key + + def empty[A]: WrappedDictionary[A] = new WrappedDictionary(Dictionary.empty) + + type CBF[A] = CanBuildFrom[WrappedDictionary[_], (String, A), WrappedDictionary[A]] + implicit def canBuildFrom[A]: CBF[A] = new CBF[A] { + def apply(from: WrappedDictionary[_]): Builder[(String, A), WrappedDictionary[A]] = + new WrappedDictionaryBuilder[A] + def apply(): Builder[(String, A), WrappedDictionary[A]] = + new WrappedDictionaryBuilder[A] + } + + class WrappedDictionaryBuilder[A] + extends Builder[(String, A), WrappedDictionary[A]] { + private[this] var dict: Dictionary[A] = Dictionary.empty + def +=(elem: (String, A)): this.type = { + dict(elem._1) = elem._2 + this + } + def clear(): Unit = + dict = Dictionary.empty + def result(): WrappedDictionary[A] = + new WrappedDictionary(dict) + } + +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSBracketAccess.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSBracketAccess.scala new file mode 100644 index 0000000..596e327 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSBracketAccess.scala @@ -0,0 +1,17 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + + + +package scala.scalajs.js.annotation + +/** Marks the annotated method as representing bracket access in JavaScript. + * + * @see [[http://www.scala-js.org/doc/calling-javascript.html Calling JavaScript from Scala.js]] + */ +class JSBracketAccess extends scala.annotation.StaticAnnotation diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExport.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExport.scala new file mode 100644 index 0000000..0fa9a4e --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExport.scala @@ -0,0 +1,19 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + + + +package scala.scalajs.js.annotation + +/** Specifies that the given entity should be exported for use in raw JS. + * + * @see [[http://www.scala-js.org/doc/export-to-javascript.html Export Scala.js APIs to JavaScript]] + */ +class JSExport extends scala.annotation.StaticAnnotation { + def this(name: String) = this() +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExportAll.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExportAll.scala new file mode 100644 index 0000000..8174595 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExportAll.scala @@ -0,0 +1,21 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + + + +package scala.scalajs.js.annotation + +/** Exports all public members directly defined in a class / object. + * + * Strictly equivalent to putting [[JSExport]] on every public member. + * Note: You are allowed to export protected members, but you'll have to do + * this explicitly on each member. + * + * @see [[http://www.scala-js.org/doc/export-to-javascript.html Export Scala.js APIs to JavaScript]] + */ +class JSExportAll extends scala.annotation.StaticAnnotation diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExportDescendentClasses.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExportDescendentClasses.scala new file mode 100644 index 0000000..9f2be96 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExportDescendentClasses.scala @@ -0,0 +1,20 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + + +package scala.scalajs.js.annotation + +/** Specifies that all the concrete classes extending the annotated class or + * should have all their public constructors exported for use in raw JS. + * The constructors exported this way are exported under their fully + * qualified name. + * + * @see [[http://www.scala-js.org/doc/export-to-javascript.html Export Scala.js APIs to JavaScript]] + */ +class JSExportDescendentClasses extends scala.annotation.StaticAnnotation diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExportDescendentObjects.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExportDescendentObjects.scala new file mode 100644 index 0000000..c196b53 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExportDescendentObjects.scala @@ -0,0 +1,20 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + + +package scala.scalajs.js.annotation + +/** Specifies that all the objects extending the annotated class or trait + * should be exported for use in raw JS. + * Note that objects exported this way are exported under their fully + * qualified name. + * + * @see [[http://www.scala-js.org/doc/export-to-javascript.html Export Scala.js APIs to JavaScript]] + */ +class JSExportDescendentObjects extends scala.annotation.StaticAnnotation diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExportNamed.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExportNamed.scala new file mode 100644 index 0000000..718404a --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSExportNamed.scala @@ -0,0 +1,38 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + + + +package scala.scalajs.js.annotation + +/** Exports the given method to JavaScript with named parameters. + * + * It can then be called like this: + * {{{ + * obj.foo({ + * param1: value1 + * param2: value2 + * param7: value3 + * }); + * }}} + * + * Note that named exports don't support overloading. Therefore the + * following will fail: + * {{{ + * class A { + * @JSExportNamed + * def a(foo: Int) = foo + 1 + * @JSExportNamed + * def a(bar: String) = "Hello " + bar + * } + * }}} + * @see [[http://www.scala-js.org/doc/export-to-javascript.html Export Scala.js APIs to JavaScript]] + */ +class JSExportNamed extends scala.annotation.StaticAnnotation { + def this(name: String) = this() +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSName.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSName.scala new file mode 100644 index 0000000..5401749 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/JSName.scala @@ -0,0 +1,17 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + + + +package scala.scalajs.js.annotation + +/** Specifies the JavaScript name of an entity. + * + * @see [[http://www.scala-js.org/doc/calling-javascript.html Calling JavaScript from Scala.js]] + */ +class JSName(name: String) extends scala.annotation.StaticAnnotation diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/README.md b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/README.md new file mode 100644 index 0000000..9ce7ebf --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/README.md @@ -0,0 +1,3 @@ +**Attention**: Some files in here are also published in the Scala.js stubs JVM library (see the stubs project in the Scala.js build). + +If you add (or rename) a file, make sure the files in the stubs project are up to date. diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/RawJSType.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/RawJSType.scala new file mode 100644 index 0000000..a5bb771 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/annotation/RawJSType.scala @@ -0,0 +1,23 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003-2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + + + +package scala.scalajs.js.annotation + +/** Marks the annotated class, trait or object as a raw JavaScript type. + * + * This annotation is added automatically by the compiler to all classes, + * traits and objects inheriting directly or indirectly from + * [[scala.scalajs.js.Any]]. It marks the annotated entity as being a raw + * JavaScript type, i.e., one that represents type information for an entity + * defined in JavaScript code. + * + * Do not use this annotation yourself. + */ +class RawJSType extends scala.annotation.StaticAnnotation diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/package.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/package.scala new file mode 100644 index 0000000..4a17ba6 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/package.scala @@ -0,0 +1,161 @@ +/* __ *\ +** ________ ___ / / ___ __ ____ Scala.js API ** +** / __/ __// _ | / / / _ | __ / // __/ (c) 2013, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ |/_// /_\ \ http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | |__/ /____/ ** +** |/____/ ** +\* */ + + + +package scala.scalajs + +/** Contains primitive types for interoperability with JavaScript libraries. + * This package is only relevant to the Scala.js compiler, and should not be + * referenced by any project compiled to the JVM. + * + * All the values and methods in this package object are representatives of + * standard variables and functions available in the top-level scope, as + * standardized in ECMAScript 5.1. + * + * == Guide == + * + * General documentation on Scala.js is available at + * [[http://www.scala-js.org/doc/]]. + * + * == Overview == + * + * The trait [[js.Any]] is the super type of all JavaScript values. + * + * All class, trait and object definitions that inherit, directly or + * indirectly, from [[js.Any]] do not have actual implementations in Scala. + * They are only the manifestation of static types representing libraries + * written directly in JavaScript. It is not possible to implement yourself + * a subclass of [[js.Any]]: all the method definitions will be ignored when + * compiling to JavaScript. + * + * Implicit conversions to and from standard Scala types to their equivalent + * in JavaScript are provided. For example, from Scala arrays to JavaScript + * arrays and back. + * + * The most important subclasses of [[js.Any]] are: + * - [[js.Dynamic]], a dynamically typed interface to JavaScript APIs + * - [[js.Object]], the superclass of all statically typed JavaScript classes, + * which has subclasses for all the classes standardized in ECMAScript 5.1, + * among which: + * - [[js.Array]] + * - [[js.Function]] (and subtraits with specific number of parameters) + * - [[js.ThisFunction]] and its subtraits for functions that take the + * JavaScript `this` as an explicit parameters + * - [[js.Dictionary]] to access the properties of an object in a + * dictionary-like way + * - [[js.Date]] + * - [[js.RegExp]] + * + * The trait [[js.Dynamic]] is a special subtrait of [[js.Any]]. It can + * represent any JavaScript value in a dynamically-typed way. It is possible + * to call any method and read and write any field of a value of type + * [[js.Dynamic]]. + * + * The package [[scala.scalajs.js.prim]] gives definitions for the four + * primitive types of JavaScript as subtraits of [[js.Any]], but generally + * it is preferable to use the corresponding Scala type. + * - [[js.prim.Number]] corresponds to [[scala.Double]] + * - [[js.prim.Boolean]] corresponds to [[scala.Boolean]] + * - [[js.prim.String]] corresponds to [[java.lang.String]] + * - [[js.prim.Undefined]] corresponds to [[scala.Unit]] + * + * [[js.UndefOr]] gives a [[scala.Option]]-like interface where the JavaScript + * value `undefined` takes the role of `None`. + */ +package object js extends js.GlobalScope { + /** The type of JavaScript numbers, which is [[scala.Double]]. */ + type Number = scala.Double + /** The type of JavaScript booleans, which is [[scala.Boolean]]. */ + type Boolean = scala.Boolean + /** The type of JavaScript strings, which is [[java.lang.String]]. */ + type String = java.lang.String + /** The type of the JavaScript undefined value, which is [[scala.Unit]]. */ + type Undefined = scala.Unit + + /** The top-level `Number` JavaScript object. */ + val Number: js.prim.Number.type = native + /** The top-level `Boolean` JavaScript object. */ + val Boolean: js.prim.Boolean.type = native + /** The top-level `String` JavaScript object. */ + val String: js.prim.String.type = native + + /** The constant Not-a-Number. */ + val NaN: Double = native + /** The constant Positive Infinity. */ + val Infinity: Double = native + + /** The undefined value. */ + def undefined: js.prim.Undefined = sys.error("stub") + + /** Tests whether the given value is undefined. */ + def isUndefined(v: scala.Any): Boolean = sys.error("stub") + + /** Returns the type of `x` as identified by `typeof x` in JavaScript. */ + def typeOf(x: Any): String = sys.error("stub") + + /** Invokes any available debugging functionality. + * If no debugging functionality is available, this statement has no effect. + * + * MDN + * + * Browser support: + * - Has no effect in Rhino nor, apparently, in Firefox + * - In Chrome, it has no effect unless the developer tools are opened + * beforehand. + */ + def debugger(): Unit = sys.error("stub") + + /** Evaluates JavaScript code and returns the result. */ + def eval(x: String): Any = native + + /** Parses a string as an integer with a given radix. */ + def parseInt(s: String, radix: Int): js.Number = native + /** Parses a string as an integer with auto-detected radix. */ + def parseInt(s: String): js.Number = native + /** Parses a string as a floating point number. */ + def parseFloat(string: String): Double = native + + /** Tests whether the given value is Not-a-Number. */ + def isNaN(number: Double): Boolean = native + /** Tests whether the given value is a finite number. */ + def isFinite(number: Double): Boolean = native + + /** Decodes a Uniform Resource Identifier (URI). + * @see [[encodeURI]] + */ + def decodeURI(encodedURI: String): String = native + + /** Decodes a Uniform Resource Identifier (URI) component. + * @see [[encodeURIComponent]] + */ + def decodeURIComponent(encodedURIComponent: String): String = native + + /** Encodes a Uniform Resource Identifier (URI). + * @see [[decodeURI]] + */ + def encodeURI(uri: String): String = native + + /** Encodes a Uniform Resource Identifier (URI) component. + * @see [[decodeURIComponent]] + */ + def encodeURIComponent(uriComponent: String): String = native + + /** Denotes a method body as native JavaScript. For use in facade types: + * + * {{{ + * class MyJSClass extends js.Object { + * def myMethod(x: String): Int = js.native + * } + * }}} + */ + def native: Nothing = sys.error("A method defined in a JavaScript raw " + + "type of a Scala.js library has been called. This is most likely " + + "because you tried to run Scala.js binaries on the JVM. Make sure you " + + "are using the JVM version of the libraries.") +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/ArrayBuffer.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/ArrayBuffer.scala new file mode 100644 index 0000000..b8b8160 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/ArrayBuffer.scala @@ -0,0 +1,17 @@ +package scala.scalajs.js.typedarray + +import scala.scalajs.js + +class ArrayBuffer(length: Int) extends js.Object { + + /** Length of this buffer in bytes */ + val byteLength: Int = js.native + + /** Returns a copy of the given slice of this array buffer */ + def slice(begin: Int, end: Int = ???): ArrayBuffer = js.native + + // Note: Some specifications specify a static isView method on ArrayBuffer + // that checks whether a given object is an ArrayBufferView. We omit it here + // since neither Node.js nor PhantomJS support it at the time of writing. + +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/ArrayBufferInputStream.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/ArrayBufferInputStream.scala new file mode 100644 index 0000000..f3d2afb --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/ArrayBufferInputStream.scala @@ -0,0 +1,88 @@ +package scala.scalajs.js.typedarray + +import java.io.InputStream + +/** A java.io.InputStream wrapping a JavaScript ArrayBuffer + * + * This class is extremely similar to a ByteArrayInputStream, but + * uses ArrayBuffers as the underlying representation. Stream + * implementations may special case on this stream for better + * performance and access the underlying buffer directly. (They still + * need to make sure the internal pointers are properly aligned + * though). + * + * This stream has several public members (n.b. [[buffer]], [[offset]], + * [[length]] and [[pos]]) in order to allow JavaScript aware applications to + * special case on this kind of stream and access the underlying + * [[ArrayBuffer]] directly for efficiency. In this case it is the client's + * responsibility to synchronize [[pos]], as if the stream were read normally + * (if the context in which it is used requires this). + * + * @param buffer Underlying ArrayBuffer + * @param offset Offset in bytes in [[buffer]] + * @param length Length in bytes in [[buffer]] + */ +class ArrayBufferInputStream(val buffer: ArrayBuffer, val offset: Int, + val length: Int) extends InputStream { + + /** Convenience constructor. Strictly equivalent to + * {{new ArrayBufferInputStream(buffer, 0, buffer.byteLength)} + */ + def this(buffer: ArrayBuffer) = this(buffer, 0, buffer.byteLength) + + private val uintView = new Uint8Array(buffer, offset, length) + private val byteView = new Int8Array(buffer, offset, length) + + /** Used to persist [[pos]] when mark is called */ + protected var mark: Int = 0 + + /** Next byte to read in the buffer (after adding offset). + * + * Use [[skip]] to update (protects from overrun and moving backwards). + */ + @inline def pos: Int = _pos + @inline protected def pos_=(x: Int): Unit = _pos = x + private[this] var _pos: Int = 0 + + override def available(): Int = length - pos + override def mark(readlimit: Int): Unit = { mark = pos } + override def markSupported(): Boolean = true + def read(): Int = { + if (pos < length) { + val res = uintView(pos) + pos += 1 + res + } else -1 + } + + override def read(b: Array[Byte], off: Int, reqLen: Int): Int = { + if (off < 0 || reqLen < 0 || reqLen > b.length - off) + throw new IndexOutOfBoundsException + + val len = Math.min(reqLen, length - pos) + + if (reqLen == 0) + 0 // 0 requested, 0 returned + else if (len == 0) + -1 // nothing to read at all + else { + var i = 0 + while (i < len) { + b(i + off) = byteView(pos + i) + i += 1 + } + pos += len + len + } + } + + override def reset(): Unit = { pos = mark } + + /** Skips a given number of bytes. Always skips the maximum number possible */ + override def skip(n: Long): Long = { + val k = Math.max(0, Math.min(n, length - pos)).toInt + pos += k + k.toLong + } + +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/ArrayBufferView.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/ArrayBufferView.scala new file mode 100644 index 0000000..6b25bf5 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/ArrayBufferView.scala @@ -0,0 +1,14 @@ +package scala.scalajs.js.typedarray + +import scala.scalajs.js + +trait ArrayBufferView extends js.Object { + /** The underlying buffer of this ArrayBufferView */ + val buffer: ArrayBuffer = js.native + + /** The number of bytes of this ArrayBufferView */ + val byteLength: Int = js.native + + /** The offset of this ArrayBufferView in the underlying buffer */ + val byteOffset: Int = js.native +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/DataView.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/DataView.scala new file mode 100644 index 0000000..d97544c --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/DataView.scala @@ -0,0 +1,26 @@ +package scala.scalajs.js.typedarray + +import scala.scalajs.js + +class DataView(buffer: ArrayBuffer, byteOffset: Int = 0, + byteLength: Int = ???) extends ArrayBufferView { + + def getInt8(byteOffset: Int): Byte = js.native + def getUint8(byteOffset: Int): Short = js.native + def getInt16(byteOffset: Int, littleEndian: Boolean = false): Short = js.native + def getUint16(byteOffset: Int, littleEndian: Boolean = false): Int = js.native + def getInt32(byteOffset: Int, littleEndian: Boolean = false): Int = js.native + def getUint32(byteOffset: Int, littleEndian: Boolean = false): Double = js.native + def getFloat32(byteOffset: Int, littleEndian: Boolean = false): Float = js.native + def getFloat64(byteOffset: Int, littleEndian: Boolean = false): Double = js.native + + def setInt8(byteOffset: Int, value: Byte): Unit = js.native + def setUint8(byteOffset: Int, value: Short): Unit = js.native + def setInt16(byteOffset: Int, value: Short, littleEndian: Boolean = false): Unit = js.native + def setUint16(byteOffset: Int, value: Int, littleEndian: Boolean = false): Unit = js.native + def setInt32(byteOffset: Int, value: Int, littleEndian: Boolean = false): Unit = js.native + def setUint32(byteOffset: Int, value: Double, littleEndian: Boolean = false): Unit = js.native + def setFloat32(byteOffset: Int, value: Float, littleEndian: Boolean = false): Unit = js.native + def setFloat64(byteOffset: Int, value: Double, littleEndian: Boolean = false): Unit = js.native + +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Float32Array.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Float32Array.scala new file mode 100644 index 0000000..abb0dd9 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Float32Array.scala @@ -0,0 +1,24 @@ +package scala.scalajs.js.typedarray + +import scala.scalajs.js + +class Float32Array private extends TypedArray[Float, Float32Array] { + + /** Constructs a Float32Array with the given length. Initialized to all 0 */ + def this(length: Int) = this() + + /** Creates a new Float32Array with the same elements than the given TypedArray + * + * The elements are converted before being stored in the new Int8Array. + */ + def this(typedArray: TypedArray[_, _]) = this() + + /** Creates a new Float32Array with the elements in the given array */ + def this(array: js.Array[_]) = this() + + /** Creates a Float32Array view on the given ArrayBuffer */ + def this(buffer: ArrayBuffer, byteOffset: Int = 0, length: Int = ???) = this() + +} + +object Float32Array extends TypedArrayStatic diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Float64Array.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Float64Array.scala new file mode 100644 index 0000000..526b376 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Float64Array.scala @@ -0,0 +1,24 @@ +package scala.scalajs.js.typedarray + +import scala.scalajs.js + +class Float64Array private extends TypedArray[Double, Float64Array] { + + /** Constructs a Float64Array with the given length. Initialized to all 0 */ + def this(length: Int) = this() + + /** Creates a new Float64Array with the same elements than the given TypedArray + * + * The elements are converted before being stored in the new Int8Array. + */ + def this(typedArray: TypedArray[_, _]) = this() + + /** Creates a new Float64Array with the elements in the given array */ + def this(array: js.Array[_]) = this() + + /** Creates a Float64Array view on the given ArrayBuffer */ + def this(buffer: ArrayBuffer, byteOffset: Int = 0, length: Int = ???) = this() + +} + +object Float64Array extends TypedArrayStatic diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Int16Array.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Int16Array.scala new file mode 100644 index 0000000..c71f101 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Int16Array.scala @@ -0,0 +1,24 @@ +package scala.scalajs.js.typedarray + +import scala.scalajs.js + +class Int16Array private extends TypedArray[Short, Int16Array] { + + /** Constructs a Int16Array with the given length. Initialized to all 0 */ + def this(length: Int) = this() + + /** Creates a new Int16Array with the same elements than the given TypedArray + * + * The elements are converted before being stored in the new Int8Array. + */ + def this(typedArray: TypedArray[_, _]) = this() + + /** Creates a new Int16Array with the elements in the given array */ + def this(array: js.Array[_]) = this() + + /** Creates a Int16Array view on the given ArrayBuffer */ + def this(buffer: ArrayBuffer, byteOffset: Int = 0, length: Int = ???) = this() + +} + +object Int16Array extends TypedArrayStatic diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Int32Array.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Int32Array.scala new file mode 100644 index 0000000..37208e9 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Int32Array.scala @@ -0,0 +1,24 @@ +package scala.scalajs.js.typedarray + +import scala.scalajs.js + +class Int32Array private extends TypedArray[Int, Int32Array] { + + /** Constructs a Int32Array with the given length. Initialized to all 0 */ + def this(length: Int) = this() + + /** Creates a new Int32Array with the same elements than the given TypedArray + * + * The elements are converted before being stored in the new Int8Array. + */ + def this(typedArray: TypedArray[_, _]) = this() + + /** Creates a new Int32Array with the elements in the given array */ + def this(array: js.Array[_]) = this() + + /** Creates a Int32Array view on the given ArrayBuffer */ + def this(buffer: ArrayBuffer, byteOffset: Int = 0, length: Int = ???) = this() + +} + +object Int32Array extends TypedArrayStatic diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Int8Array.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Int8Array.scala new file mode 100644 index 0000000..690ff07 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Int8Array.scala @@ -0,0 +1,24 @@ +package scala.scalajs.js.typedarray + +import scala.scalajs.js + +class Int8Array private extends TypedArray[Byte, Int8Array] { + + /** Constructs a Int8Array with the given length. Initialized to all 0 */ + def this(length: Int) = this() + + /** Creates a new Int8Array with the same elements than the given TypedArray + * + * The elements are converted before being stored in the new Int8Array. + */ + def this(typedArray: TypedArray[_, _]) = this() + + /** Creates a new Int8Array with the elements in the given array */ + def this(array: js.Array[_]) = this() + + /** Creates a Int8Array view on the given ArrayBuffer */ + def this(buffer: ArrayBuffer, byteOffset: Int = 0, length: Int = ???) = this() + +} + +object Int8Array extends TypedArrayStatic diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/TypedArray.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/TypedArray.scala new file mode 100644 index 0000000..4e33b5d --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/TypedArray.scala @@ -0,0 +1,46 @@ +package scala.scalajs.js.typedarray + +import scala.scalajs.js +import scala.scalajs.js.annotation.JSBracketAccess + +trait TypedArray[T, Repr] extends ArrayBufferView { + + /** The number of elements in this TypedArray */ + val length: Int = js.native + + /** Retrieve element at index */ + @JSBracketAccess + def apply(index: Int): T = js.native + + /** Set element at index */ + @JSBracketAccess + def update(index: Int, value: T): Unit = js.native + + /** Retrieve element at index */ + @JSBracketAccess + def get(index: Int): T = js.native + + /** Set element at index */ + @JSBracketAccess + def set(index: Int, value: T): Unit = js.native + + /** Set the values of typedArray in this TypedArray */ + def set(typedArray: TypedArray[_, _]): Unit = js.native + + /** Set the values of typedArray in this TypedArray at given offset */ + def set(typedArray: TypedArray[_, _], offset: Int): Unit = js.native + + /** Set the values from array in this TypedArray */ + def set(array: js.Array[_]): Unit = js.native + + /** Set the values from array in this TypedArray at given offset */ + def set(array: js.Array[_], offset: Int): Unit = js.native + + /** Create a new TypedArray view of this TypedArray at given location */ + def subarray(begin: Int, end: Int = ???): Repr = js.native + +} + +trait TypedArrayStatic extends js.Object { + val BYTES_PER_ELEMENT: Int = js.native +} diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Uint16Array.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Uint16Array.scala new file mode 100644 index 0000000..82d2847 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Uint16Array.scala @@ -0,0 +1,24 @@ +package scala.scalajs.js.typedarray + +import scala.scalajs.js + +class Uint16Array private extends TypedArray[Int, Uint16Array] { + + /** Constructs a Uint16Array with the given length. Initialized to all 0 */ + def this(length: Int) = this() + + /** Creates a new Uint16Array with the same elements than the given TypedArray + * + * The elements are converted before being stored in the new Int8Array. + */ + def this(typedArray: TypedArray[_, _]) = this() + + /** Creates a new Uint16Array with the elements in the given array */ + def this(array: js.Array[_]) = this() + + /** Creates a Uint16Array view on the given ArrayBuffer */ + def this(buffer: ArrayBuffer, byteOffset: Int = 0, length: Int = ???) = this() + +} + +object Uint16Array extends TypedArrayStatic diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Uint32Array.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Uint32Array.scala new file mode 100644 index 0000000..9742e19 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Uint32Array.scala @@ -0,0 +1,24 @@ +package scala.scalajs.js.typedarray + +import scala.scalajs.js + +class Uint32Array private extends TypedArray[Double, Uint32Array] { + + /** Constructs a Uint32Array with the given length. Initialized to all 0 */ + def this(length: Int) = this() + + /** Creates a new Uint32Array with the same elements than the given TypedArray + * + * The elements are converted before being stored in the new Int8Array. + */ + def this(typedArray: TypedArray[_, _]) = this() + + /** Creates a new Uint32Array with the elements in the given array */ + def this(array: js.Array[_]) = this() + + /** Creates a Uint32Array view on the given ArrayBuffer */ + def this(buffer: ArrayBuffer, byteOffset: Int = 0, length: Int = ???) = this() + +} + +object Uint32Array extends TypedArrayStatic diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Uint8Array.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Uint8Array.scala new file mode 100644 index 0000000..f54904c --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Uint8Array.scala @@ -0,0 +1,24 @@ +package scala.scalajs.js.typedarray + +import scala.scalajs.js + +class Uint8Array private extends TypedArray[Short, Uint8Array] { + + /** Constructs a Uint8Array with the given length. Initialized to all 0 */ + def this(length: Int) = this() + + /** Creates a new Uint8Array with the same elements than the given TypedArray + * + * The elements are converted before being stored in the new Int8Array. + */ + def this(typedArray: TypedArray[_, _]) = this() + + /** Creates a new Uint8Array with the elements in the given array */ + def this(array: js.Array[_]) = this() + + /** Creates a Uint8Array view on the given ArrayBuffer */ + def this(buffer: ArrayBuffer, byteOffset: Int = 0, length: Int = ???) = this() + +} + +object Uint8Array extends TypedArrayStatic diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Uint8ClampedArray.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Uint8ClampedArray.scala new file mode 100644 index 0000000..601d65c --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/Uint8ClampedArray.scala @@ -0,0 +1,24 @@ +package scala.scalajs.js.typedarray + +import scala.scalajs.js + +class Uint8ClampedArray private extends TypedArray[Int, Uint8ClampedArray] { + + /** Constructs a Uint8ClampedArray with the given length. Initialized to all 0 */ + def this(length: Int) = this() + + /** Creates a new Uint8ClampedArray with the same elements than the given TypedArray + * + * The elements are converted before being stored in the new Int8Array. + */ + def this(typedArray: TypedArray[_, _]) = this() + + /** Creates a new Uint8ClampedArray with the elements in the given array */ + def this(array: js.Array[_]) = this() + + /** Creates a Uint8ClampedArray view on the given ArrayBuffer */ + def this(buffer: ArrayBuffer, byteOffset: Int = 0, length: Int = ???) = this() + +} + +object Uint8ClampedArray extends TypedArrayStatic diff --git a/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/package.scala b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/package.scala new file mode 100644 index 0000000..0ab5a05 --- /dev/null +++ b/examples/scala-js/library/src/main/scala/scala/scalajs/js/typedarray/package.scala @@ -0,0 +1,145 @@ +package scala.scalajs.js + +import JSConverters._ + +/** The typdearray package provides facade types for JavaScript + * ArrayBuffer, TypeArrays and DataView. Further, it provides + * conversions between primitive Scala arrays and TypedArrays + */ +package object typedarray { + + // Implicit classes scala.Array -> TypedArray + implicit class AB2TA(val array: scala.Array[Byte]) extends AnyVal { + def toTypedArray: Int8Array = byteArray2Int8Array(array) + } + + implicit class AS2TA(val array: scala.Array[Short]) extends AnyVal { + def toTypedArray: Int16Array = shortArray2Int16Array(array) + } + + implicit class AC2TA(val array: scala.Array[Char]) extends AnyVal { + def toTypedArray: Uint16Array = charArray2Uint16Array(array) + } + + implicit class AI2TA(val array: scala.Array[Int]) extends AnyVal { + def toTypedArray: Int32Array = intArray2Int32Array(array) + } + + implicit class AF2TA(val array: scala.Array[Float]) extends AnyVal { + def toTypedArray: Float32Array = floatArray2Float32Array(array) + } + + implicit class AD2TA(val array: scala.Array[Double]) extends AnyVal { + def toTypedArray: Float64Array = doubleArray2Float64Array(array) + } + + // Implicit classes TypedArray -> scala.Array + implicit class TA2AB(val array: Int8Array) extends AnyVal { + def toArray: scala.Array[Byte] = int8Array2ByteArray(array) + } + + implicit class TA2AS(val array: Int16Array) extends AnyVal { + def toArray: scala.Array[Short] = int16Array2ShortArray(array) + } + + implicit class TA2AC(val array: Uint16Array) extends AnyVal { + def toArray: scala.Array[Char] = uint16Array2CharArray(array) + } + + implicit class TA2AI(val array: Int32Array) extends AnyVal { + def toArray: scala.Array[Int] = int32Array2IntArray(array) + } + + implicit class TA2AF(val array: Float32Array) extends AnyVal { + def toArray: scala.Array[Float] = float32Array2FloatArray(array) + } + + implicit class TA2AD(val array: Float64Array) extends AnyVal { + def toArray: scala.Array[Double] = float64Array2DoubleArray(array) + } + + // scala.Array -> TypedArray + + def byteArray2Int8Array(array: scala.Array[Byte]): Int8Array = + array2typedArrayImpl(array, new Int8Array(array.length)) + + def shortArray2Int16Array(array: scala.Array[Short]): Int16Array = + array2typedArrayImpl(array, new Int16Array(array.length)) + + def charArray2Uint16Array(array: scala.Array[Char]): Uint16Array = { + // Can't use array2typedArrayImpl because Uint16Array contains Ints + val len = array.length + val dest = new Uint16Array(len) + var i = 0 + while (i < len) { + dest(i) = array(i).toInt + i += 1 + } + dest + } + + def intArray2Int32Array(array: scala.Array[Int]): Int32Array = + array2typedArrayImpl(array, new Int32Array(array.length)) + + def floatArray2Float32Array(array: scala.Array[Float]): Float32Array = + array2typedArrayImpl(array, new Float32Array(array.length)) + + def doubleArray2Float64Array(array: scala.Array[Double]): Float64Array = + array2typedArrayImpl(array, new Float64Array(array.length)) + + @inline private def array2typedArrayImpl[ + @specialized(Byte, Short, Int, Float, Double) T, + Repr <: TypedArray[T, Repr]]( + array: scala.Array[T], dest: Repr): Repr = { + val len = array.length + var i = 0 + while (i < len) { + dest(i) = array(i) + i += 1 + } + dest + } + + // TypedArray -> scala.Array + + def int8Array2ByteArray(array: Int8Array): scala.Array[Byte] = + typedArray2arrayImpl(array, new scala.Array(array.length)) + + def int16Array2ShortArray(array: Int16Array): scala.Array[Short] = + typedArray2arrayImpl(array, new scala.Array(array.length)) + + def uint16Array2CharArray(array: Uint16Array): scala.Array[Char] = { + // Can't use typedArray2arrayImpl because Uint16Array contains Ints + val len = array.length + val dest = new scala.Array[Char](len) + var i = 0 + while (i < len) { + dest(i) = array(i).toChar + i += 1 + } + dest + } + + def int32Array2IntArray(array: Int32Array): scala.Array[Int] = + typedArray2arrayImpl(array, new scala.Array(array.length)) + + def float32Array2FloatArray(array: Float32Array): scala.Array[Float] = + typedArray2arrayImpl(array, new scala.Array(array.length)) + + def float64Array2DoubleArray(array: Float64Array): scala.Array[Double] = + typedArray2arrayImpl(array, new scala.Array(array.length)) + + @inline private def typedArray2arrayImpl[ + @specialized(Byte, Short, Int, Float, Double) T, + Repr <: TypedArray[T, Repr]]( + array: Repr, dest: scala.Array[T]): scala.Array[T] = { + val len = dest.length + var i = 0 + while (i < len) { + dest(i) = array(i) + i += 1 + } + dest + } + +} |