asyncKiteJs

suspend fun <C : KiteJsConfig> asyncKiteJs(engine: JsEngine<C>, dispatcher: CoroutineDispatcher = EngineThread(), configure: C.() -> Unit = {}): AsyncKiteJs(source)

Builds an engine on engine, on a thread of its own. The engine is loaded first, which for QuickJS in a browser compiles its WebAssembly; see JsEngine.load.

The thread that opens an engine holds it, so every call to the engine has to run on that thread. By default the engine gets a new thread, which AsyncKiteJs.close ends. A dispatcher you pass instead must run everything on one thread, such as one from newSingleThreadContext, and you close it yourself. A pool view such as Dispatchers.Default.limitedParallelism(1) does not work, because it moves calls between the threads of the pool.

Cancelling the coroutine that called into the engine stops the running script: the engine asks the hook between instructions, and a cancelled job answers "stop". That needs an instruction budget to be set, so one is set for you when you do not name one.