@@ -3,8 +3,11 @@ export * as Runtime from "./runtime.js"
33import { type Context , Effect } from "effect"
44import { adapt } from "./work.js"
55import { type DurableFn , type Fn , Interrupted , type Namespace } from "../runtime/contract.js"
6+ import type { Diagnostic } from "../runtime/model.js"
67import {
78 checkMaxBytes ,
9+ Compiled ,
10+ compileProgram ,
811 finishExecution ,
912 makeProgram ,
1013 type Outcome ,
@@ -18,16 +21,23 @@ import {
1821import { type Globals , makeSetup , type Options , type ProgramOptions , type SuspendOptions } from "../runtime/options.js"
1922import { SnapshotError } from "../snapshot/snapshot.js"
2023
21- export { SuspendError }
24+ export { Compiled , SuspendError }
2225export type { Outcome , Result , Run , Suspended }
2326export type { Globals , Limits , Options , ProgramOptions , SuspendOptions , Translate } from "../runtime/options.js"
2427
2528/** A reusable confined runtime over host globals. */
2629export type Runtime < R = never > = {
27- /** Runs a program to its end. */
28- readonly execute : ( code : string , options ?: ProgramOptions ) => Effect . Effect < Result , never , R >
29- /** A program ready to run once; nothing runs until `program.run()`. */
30- readonly program : ( code : string , options ?: ProgramOptions ) => Program < R >
30+ /** Runs a program, from its source or compiled, to its end. */
31+ readonly execute : ( code : string | Compiled , options ?: ProgramOptions ) => Effect . Effect < Result , never , R >
32+ /** A program, from its source or compiled, ready to run once; nothing runs until `program.run()`. */
33+ readonly program : ( code : string | Compiled , options ?: ProgramOptions ) => Program < R >
34+ /**
35+ * Parses and compiles a program once, for `program` and `execute` to run many times without doing it again; or the
36+ * diagnostic a run of it would fail with first (an empty program, a syntax error).
37+ */
38+ readonly compile : (
39+ code : string ,
40+ ) => { readonly ok : true ; readonly compiled : Compiled } | { readonly ok : false ; readonly error : Diagnostic }
3141 /**
3242 * A suspended run (its `state`, or that state as bytes) as a program to continue in this runtime, which must be the
3343 * same build with the same host globals. The state is checked when it runs: `run()` fails with a `SnapshotError`
@@ -70,7 +80,7 @@ type ServicesOf<T, Depth extends ReadonlyArray<unknown>> = Depth["length"] exten
7080export const make = < const G extends Globals = { } > ( options : Options < G > = { } ) : Runtime < Services < G > > => {
7181 type R = Services < G >
7282 const setup = { ...makeSetup < R > ( options ) , adapt }
73- const program = ( code : string , programOptions ?: ProgramOptions ) : Program < R > =>
83+ const program = ( code : string | Compiled , programOptions ?: ProgramOptions ) : Program < R > =>
7484 wrap ( makeProgram ( ( ) => startProgram ( setup , code , programOptions ?. bindings , programOptions ?. runId ) ) )
7585 return {
7686 execute : ( code , programOptions ) =>
@@ -85,6 +95,7 @@ export const make = <const G extends Globals = {}>(options: Options<G> = {}): Ru
8595 ) ,
8696 ) as Effect . Effect < Result , never , R > ,
8797 program,
98+ compile : compileProgram ,
8899 resume : ( state ) => wrap ( makeProgram ( ( ) => resumeProgram ( setup , state ) ) ) as Program < R , SnapshotError > ,
89100 }
90101}
0 commit comments