Scala standard library implementation for Scala.js, providing core library functionality for Scala code compiled to JavaScript
—
Low-level runtime support classes and utilities that provide the foundation for Scala.js execution, handling JavaScript-specific concerns while maintaining Scala semantics.
Core runtime environment support for Scala.js execution context and JavaScript interoperability.
/**
* Runtime environment utilities and system integration
*/
object ScalaRunTime {
/** Array utilities for runtime support */
def array_apply(xs: AnyRef, idx: Int): Any
def array_update(xs: AnyRef, idx: Int, value: Any): Unit
def array_length(xs: AnyRef): Int
/** String representation utilities */
def stringOf(x: Any): String
def replStringOf(x: Any, maxElements: Int): String
/** Hash code computation */
def hash(x: Any): Int
/** Tuple utilities */
def tuple2ToList[A, B](t: (A, B)): List[Any] = List(t._1, t._2)
def tuple3ToList[A, B, C](t: (A, B, C)): List[Any] = List(t._1, t._2, t._3)
def tuple4ToList[A, B, C, D](t: (A, B, C, D)): List[Any] = List(t._1, t._2, t._3, t._4)
def tuple5ToList[A, B, C, D, E](t: (A, B, C, D, E)): List[Any] = List(t._1, t._2, t._3, t._4, t._5)
def tuple6ToList[A, B, C, D, E, F](t: (A, B, C, D, E, F)): List[Any] = List(t._1, t._2, t._3, t._4, t._5, t._6)
def tuple7ToList[A, B, C, D, E, F, G](t: (A, B, C, D, E, F, G)): List[Any] = List(t._1, t._2, t._3, t._4, t._5, t._6, t._7)
def tuple8ToList[A, B, C, D, E, F, G, H](t: (A, B, C, D, E, F, G, H)): List[Any] = List(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8)
def tuple9ToList[A, B, C, D, E, F, G, H, I](t: (A, B, C, D, E, F, G, H, I)): List[Any] = List(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9)
def tuple10ToList[A, B, C, D, E, F, G, H, I, J](t: (A, B, C, D, E, F, G, H, I, J)): List[Any] = List(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8, t._9, t._10)
// Additional tuple methods up to tuple22ToList available for higher arities
/** Type testing utilities */
def isArray(x: Any): Boolean
def isArray(x: Any, atLevel: Int): Boolean
}
/**
* Boxed primitive value utilities
*/
object BoxesRunTime {
/** Boxing operations for primitive types */
def boxToBoolean(b: Boolean): java.lang.Boolean
def boxToCharacter(c: Char): java.lang.Character
def boxToByte(b: Byte): java.lang.Byte
def boxToShort(s: Short): java.lang.Short
def boxToInteger(i: Int): java.lang.Integer
def boxToLong(l: Long): java.lang.Long
def boxToFloat(f: Float): java.lang.Float
def boxToDouble(d: Double): java.lang.Double
/** Unboxing operations */
def unboxToBoolean(o: Any): Boolean
def unboxToChar(o: Any): Char
def unboxToByte(o: Any): Byte
def unboxToShort(o: Any): Short
def unboxToInt(o: Any): Int
def unboxToLong(o: Any): Long
def unboxToFloat(o: Any): Float
def unboxToDouble(o: Any): Double
/** Equality operations that handle boxing */
def equals(x: Any, y: Any): Boolean
def equals2(x: Any, y: Any): Boolean
}JavaScript-specific runtime utilities for compilation and execution support.
/**
* Scala.js runtime utilities for JavaScript integration
*/
object js {
/** Global JavaScript environment access */
val global: js.Dynamic = js.Dynamic.global
/** Undefined value representation */
val undefined: js.UndefOr[Nothing] = js.native
/** Type testing utilities */
def typeOf(x: Any): String = js.native
def isUndefined(x: Any): Boolean = x.asInstanceOf[js.UndefOr[Any]].isEmpty
/** Dynamic property access */
def selectDynamic(receiver: js.Dynamic, name: String): js.Dynamic = js.native
def applyDynamic(receiver: js.Dynamic, name: String)(args: Any*): js.Dynamic = js.native
/** JavaScript object creation */
def obj(fields: (String, Any)*): js.Dynamic = js.native
def array(elements: Any*): js.Array[Any] = js.native
/** Function utilities */
def functionNToType[F](f: js.Function): F = js.native
def typeToFunctionN[F](f: F): js.Function = js.native
/** Promise integration */
def fromPromise[T](promise: js.Promise[T]): Future[T] = js.native
def toPromise[T](future: Future[T]): js.Promise[T] = js.native
/**
* Dynamic type for JavaScript interoperability
*/
trait Dynamic extends Any {
/** Dynamic method calls */
def applyDynamic(name: String)(args: Any*): js.Dynamic = js.native
def applyDynamicNamed(name: String)(args: (String, Any)*): js.Dynamic = js.native
/** Dynamic property access */
def selectDynamic(name: String): js.Dynamic = js.native
def updateDynamic(name: String)(value: Any): Unit = js.native
/** Array-like access */
def apply(index: Any): js.Dynamic = js.native
def update(index: Any, value: Any): Unit = js.native
}
/**
* JavaScript Array type
*/
@js.native
trait Array[+A] extends js.Object {
val length: Int = js.native
def apply(index: Int): A = js.native
def update[A1 >: A](index: Int, value: A1): Unit = js.native
def push[A1 >: A](items: A1*): Int = js.native
def pop(): js.UndefOr[A] = js.native
def shift(): js.UndefOr[A] = js.native
def unshift[A1 >: A](items: A1*): Int = js.native
def slice(start: Int = 0, end: Int = Int.MaxValue): js.Array[A] = js.native
def splice(start: Int, deleteCount: Int, items: A*): js.Array[A] = js.native
def indexOf[A1 >: A](searchElement: A1, fromIndex: Int = 0): Int = js.native
def lastIndexOf[A1 >: A](searchElement: A1, fromIndex: Int = Int.MaxValue): Int = js.native
def join(separator: String = ","): String = js.native
def reverse(): js.Array[A] = js.native
def sort(compareFn: js.Function2[A, A, Int] = js.native): js.Array[A] = js.native
}
}Limited reflection capabilities compatible with JavaScript compilation and runtime constraints.
/**
* Scala.js compatible reflection utilities
* Provides basic type information without full JVM reflection
*/
object reflect {
/**
* Class tag for preserving type information at runtime
*/
trait ClassTag[T] {
/** Runtime class representation */
def runtimeClass: Class[_]
/** Create new array of this type */
def newArray(len: Int): Array[T]
/** Wrap array as this type */
def wrap: ClassTag[Array[T]]
/** String representation of type */
override def toString: String = s"ClassTag(${runtimeClass.getName})"
}
object ClassTag {
/** Create ClassTag for given type */
def apply[T](implicit ct: ClassTag[T]): ClassTag[T] = ct
/** Predefined ClassTags for common types */
val Unit: ClassTag[Unit] = ClassTag[Unit]
val Boolean: ClassTag[Boolean] = ClassTag[Boolean]
val Byte: ClassTag[Byte] = ClassTag[Byte]
val Short: ClassTag[Short] = ClassTag[Short]
val Char: ClassTag[Char] = ClassTag[Char]
val Int: ClassTag[Int] = ClassTag[Int]
val Long: ClassTag[Long] = ClassTag[Long]
val Float: ClassTag[Float] = ClassTag[Float]
val Double: ClassTag[Double] = ClassTag[Double]
val AnyVal: ClassTag[AnyVal] = ClassTag[AnyVal]
val AnyRef: ClassTag[AnyRef] = ClassTag[AnyRef]
val Any: ClassTag[Any] = ClassTag[Any]
val Nothing: ClassTag[Nothing] = ClassTag[Nothing]
val Null: ClassTag[Null] = ClassTag[Null]
}
/**
* Basic class representation for Scala.js
*/
abstract class Class[T] {
/** Simple class name */
def getSimpleName: String
/** Full class name */
def getName: String
/** Test if instance of this class */
def isInstance(obj: Any): Boolean
/** String representation */
override def toString: String = s"class $getName"
}
/**
* Manifest for preserving generic type information
* Limited implementation for JavaScript compatibility
*/
trait Manifest[T] extends ClassTag[T] {
/** Type arguments (limited support) */
def typeArguments: List[Manifest[_]]
/** Array manifest */
def arrayManifest: Manifest[Array[T]]
}
object Manifest {
/** Create manifest for type */
def apply[T](implicit m: Manifest[T]): Manifest[T] = m
/** Predefined manifests */
val Unit: Manifest[Unit] = Manifest[Unit]
val Boolean: Manifest[Boolean] = Manifest[Boolean]
val Byte: Manifest[Byte] = Manifest[Byte]
val Short: Manifest[Short] = Manifest[Short]
val Char: Manifest[Char] = Manifest[Char]
val Int: Manifest[Int] = Manifest[Int]
val Long: Manifest[Long] = Manifest[Long]
val Float: Manifest[Float] = Manifest[Float]
val Double: Manifest[Double] = Manifest[Double]
val AnyVal: Manifest[AnyVal] = Manifest[AnyVal]
val AnyRef: Manifest[AnyRef] = Manifest[AnyRef]
val Any: Manifest[Any] = Manifest[Any]
val Nothing: Manifest[Nothing] = Manifest[Nothing]
val Null: Manifest[Null] = Manifest[Null]
}
}Usage Examples:
import scala.scalajs.js
import scala.reflect.ClassTag
// JavaScript interoperability
val jsObject = js.Dynamic.literal(
name = "John",
age = 30,
active = true
)
println(jsObject.name.asInstanceOf[String]) // "John"
println(jsObject.age.asInstanceOf[Int]) // 30
// Dynamic property access
jsObject.updateDynamic("email")("john@example.com")
val email = jsObject.selectDynamic("email").asInstanceOf[String]
println(s"Email: $email")
// JavaScript Array usage
val jsArray = js.Array(1, 2, 3, 4, 5)
jsArray.push(6)
val popped = jsArray.pop()
println(s"Array: ${jsArray.toList}, Popped: $popped")
// Type checking and conversion
def processValue(value: Any): String = {
js.typeOf(value) match {
case "string" => s"String: $value"
case "number" => s"Number: $value"
case "boolean" => s"Boolean: $value"
case "undefined" => "Undefined value"
case _ => s"Object: $value"
}
}
println(processValue("hello")) // "String: hello"
println(processValue(42)) // "Number: 42"
println(processValue(js.undefined)) // "Undefined value"
// ClassTag usage for generic arrays
def createArray[T: ClassTag](size: Int, default: T): Array[T] = {
val ct = implicitly[ClassTag[T]]
val array = ct.newArray(size)
for (i <- array.indices) {
array(i) = default
}
array
}
val intArray = createArray(5, 0)
val stringArray = createArray(3, "default")
println(s"Int array: ${intArray.toList}")
println(s"String array: ${stringArray.toList}")
// Runtime type information
def getTypeInfo[T: ClassTag](value: T): String = {
val ct = implicitly[ClassTag[T]]
s"Value: $value, Type: ${ct.runtimeClass.getSimpleName}"
}
println(getTypeInfo(42))
println(getTypeInfo("hello"))
println(getTypeInfo(List(1, 2, 3)))
// Boxing/unboxing utilities (typically handled automatically)
import scala.runtime.BoxesRunTime
val boxedInt = BoxesRunTime.boxToInteger(42)
val unboxedInt = BoxesRunTime.unboxToInt(boxedInt)
println(s"Boxed: $boxedInt, Unboxed: $unboxedInt")
// Safe equality with boxing consideration
val result1 = BoxesRunTime.equals(42, 42.0) // false (different types)
val result2 = BoxesRunTime.equals(42, 42) // true (same type and value)
println(s"42 == 42.0: $result1")
println(s"42 == 42: $result2")
// Global JavaScript environment access
val navigator = js.Dynamic.global.navigator
if (!js.isUndefined(navigator)) {
val userAgent = navigator.userAgent.asInstanceOf[String]
println(s"User Agent: $userAgent")
} else {
println("Running in Node.js environment")
}
// Creating JavaScript objects
val config = js.obj(
"timeout" -> 5000,
"retries" -> 3,
"debug" -> true
)
// JavaScript function creation and invocation
val jsFunc: js.Function1[Int, Int] = (x: Int) => x * 2
val result = jsFunc(21)
println(s"JavaScript function result: $result")
// Working with JavaScript Promises (if available)
val jsPromise = js.Promise.resolve("Hello from Promise")
val scalaFuture = js.fromPromise(jsPromise)
scalaFuture.foreach { value =>
println(s"Promise result: $value")
}
// Array-like operations with js.Array
val fruits = js.Array("apple", "banana", "cherry")
fruits.push("date")
val sorted = fruits.sort()
val joined = fruits.join(" | ")
println(s"Fruits: $joined")
// Runtime class information
def analyzeObject(obj: Any): Unit = {
val clazz = obj.getClass
println(s"Object: $obj")
println(s"Class: ${clazz.getName}")
println(s"Simple name: ${clazz.getSimpleName}")
println(s"Is String: ${clazz.isInstance("test")}")
println()
}
analyzeObject(42)
analyzeObject("hello")
analyzeObject(List(1, 2, 3))Install with Tessl CLI
npx tessl i tessl/maven-org-scala-js--scalajs-scalalib-2-13