eval

suspend fun <R> eval(block: T.() -> R): R

Suspends until a script instance can be borrowed, runs block on it, and returns the instance to the pool afterwards, even if block throws.

Only the wait for an instance is cancellable. block runs on the calling thread, so once it starts, neither withTimeout nor cancelling the caller can stop it: it runs to completion, holding its instance. Use the overload that takes a timeout for scripts that might not finish.

Return

the result of block

Parameters

block

the work to do with the borrowed instance

Throws

kotlinx.coroutines.channels.ClosedReceiveChannelException

if the pool has been closed


suspend fun <R> eval(timeout: Duration, block: T.() -> R): R

Like eval, but runs block on a thread of its own and waits at most timeout for it; the wait for an instance does not count toward timeout. If block does not finish in time, its thread is interrupted, the instance is taken out of the pool for good and a fresh one takes its place, and a ScriptTimeoutException is thrown. Cancelling the caller while block runs stops it the same way.

An engine that ignores the interrupt keeps the thread running until the script returns, if it ever does; the instance is closed then.

Return

the result of block

Parameters

timeout

how long block may run; must be positive

block

the work to do with the borrowed instance

Throws

if block does not finish within timeout

kotlinx.coroutines.channels.ClosedReceiveChannelException

if the pool has been closed