AsyncKiteJs

An engine that lives on one dispatcher, so suspending code can use it from anywhere.

The engine underneath is single-threaded, as JavaScript is. Every call here hops onto the engine's own dispatcher, which runs one thing at a time, so two callers never overlap.

val js = asyncKiteJs()
js.use { println(js.evaluate("1 + 1").asDouble()) }
js.close()

Functions

Link copied to clipboard
suspend fun await(value: JsValue): JsValue

Waits for a JavaScript promise. A rejection comes back as the JsError it carries.

Link copied to clipboard
open override fun close()

Releases the engine.

Link copied to clipboard
suspend fun compile(source: String, fileName: String = "<script>"): JsScript

Parses source once, for running more than once.

Link copied to clipboard
suspend fun <T> deferredToPromise(deferred: Deferred<T>, scope: CoroutineScope): JsObject

Hands the script a promise that settles when deferred does.

Link copied to clipboard
suspend fun evaluate(source: String, fileName: String = "<eval>"): JsValue

Parses and runs source on the engine's dispatcher.

Link copied to clipboard
suspend fun evaluateAwaiting(source: String, fileName: String = "<eval>"): JsValue

Runs source and, if it answers a promise, waits for that promise to settle.

Link copied to clipboard
suspend fun <T> onEngine(block: (KiteJs) -> T): T

Runs block on the engine's dispatcher with the engine in hand.

Link copied to clipboard
suspend fun suspendFunction(target: JsObject, name: String, arity: Int = 0, scope: CoroutineScope, body: suspend (List<JsValue>) -> Any?): JsFunction

Binds a host function that suspends. The script sees a normal function returning a promise; body runs on scope and settles it.