JsValue

value class JsValue(val raw: Any?)(source)

One value from an engine. It costs nothing at runtime: the wrapper disappears and only the value itself is passed around.

A number, a string, a boolean, a BigInt, null or undefined is a copy, the same whichever engine it came from, and works anywhere, even after its engine is closed. A symbol, an object, an array or a function is a handle into the engine that made it, and works only while that engine is open and only on its thread.

The asX readers coerce the way JavaScript does, so asString() on the number 5 gives "5", the same answer String(5) gives in a script. Use type first when you need the exact kind.

Constructors

Link copied to clipboard
constructor(raw: Any?)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
Link copied to clipboard

True for null and undefined, the two values optional chaining stops at.

Link copied to clipboard

True when this value is thenable, which is what await looks for: an object, an array or a function whose then is callable. Reading then can run a getter, so for those this needs the value's engine open and on this thread, and a getter that throws reaches you as a JsError. Every other value answers false without touching an engine.

Link copied to clipboard
Link copied to clipboard
val raw: Any?

The value itself: JsUndefined, null, a Boolean, a Double, a String, a KBigInt, a JsSymbol or a JsObject. Engines build and read these; nothing else should.

Link copied to clipboard

Which kind of value this is.

Link copied to clipboard

What the typeof operator answers for this value.

Functions

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Throws unless this really is a BigInt. There is no coercion from a number, as in a script.

Link copied to clipboard

ToBoolean: false for undefined, null, false, zero, NaN, "" and 0n, true for the rest.

Link copied to clipboard

ToNumber. An object is turned into a primitive first, which can run its script.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun asInt(): Int

ToInt32: the number wrapped into a 32-bit integer, as the bitwise operators see it.

Link copied to clipboard
fun asLong(): Long

An exact signed 64-bit integer. A BigInt is checked directly; other values undergo numeric primitive conversion first. Fractions, NaN, infinities and values outside the Long range throw a JsError named RangeError. Negative zero becomes zero. Unlike asInt, this never wraps, truncates or saturates. A Number already rounded by JavaScript keeps that represented value; use BigInt when the original integer must retain all its digits.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

ToString. An object is turned into a primitive first, which can run its script.

Link copied to clipboard
Link copied to clipboard
inline fun <reified T> JsValue.convertTo(): T

Reads a JsValue as T, coercing the way JavaScript would where that makes sense.

Link copied to clipboard
fun toKotlin(): Any?

A plain Kotlin value, all the way down: Map for an object, List for an array, ByteArray for an ArrayBuffer, a typed array or a DataView, Double, String, Boolean, KBigInt, or null. A function stays a JsFunction and a symbol a JsSymbol, since they have no Kotlin twin, and an object that closes a cycle stays the JsObject it is.

Link copied to clipboard
open override fun toString(): String

What String(value) gives, without ever throwing: a symbol prints as Symbol(description), and an object its engine cannot run here, or whose own toString fails, prints its class.