summaryrefslogtreecommitdiff
path: root/examples/scala-js/library/src/main/scala/scala/scalajs/js/package.scala
blob: 4a17ba630a36eb8b3b3a0bba8de9c72a39f4fd8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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.")
}