newJsContext

suspend fun newJsContext(initializer: suspend JsContext.() -> Unit = {}): JsContext(source)

Creates a new JsContext, running initializer on it before returning.

The context's cancellation is automatically tied to whichever coroutine calls this function. If that coroutine is later cancelled (e.g. an enclosing withTimeout expires), any in-flight or subsequent JsContext.eval call on the returned context will throw CancellationException.

JsContext.maxExecutionTime / JsContext.maxInstructions can be changed from within initializer (via this.maxExecutionTime = ...) and will take effect, since the underlying interpreter isn't built until the context is first used.

Usage: val ctx = newJsContext { maxInstructions = 10_000 set("x", 1.0) eval("x + 1") }