///|
pub fn run(
  source : String,
  annex_b? : Bool = false,
) -> (Array[String], String) raise Error {
  let interp = @interpreter.new_interpreter(annex_b~)
  let prog = @parser.parse(source)
  let result = interp.run(prog.stmts)
  // Drain microtask queue after script execution
  // This implements the event loop checkpoint per WHATWG spec
  interp.run_microtasks()
  // Process timer task queue with microtask checkpoints between each
  interp.run_timers()
  (interp.host.output, result.to_string())
}

///|
/// Run JavaScript source through the opt-in closure-conversion prototype.
///
/// The normal `run` facade remains the default interpreter path. This entry
/// point parses once, compiles the supported script subset to executable
/// closures, then runs it with the same event-loop drain behavior as `run`.
pub fn run_compiled(
  source : String,
  annex_b? : Bool = false,
) -> (Array[String], String) raise Error {
  let interp = @interpreter.new_interpreter(annex_b~)
  let prog = @parser.parse(source)
  let compiled = @compiler.compile_script_to_closure(prog.stmts)
  let result = compiled(interp)
  interp.run_microtasks()
  interp.run_timers()
  (interp.host.output, result.to_string())
}

///|
/// Run JavaScript source with event loop support
/// This version allows the host to control microtask and timer execution timing
pub fn run_with_event_loop(
  source : String,
  annex_b? : Bool = false,
) -> (@interpreter.Interpreter, @interpreter.Value) raise Error {
  let interp = @interpreter.new_interpreter(annex_b~)
  let prog = @parser.parse(source)
  let result = interp.run(prog.stmts)
  // Return interpreter to allow host to manage event loop
  (interp, result)
}

///|
/// Check if there are pending microtasks in the queue
pub fn has_pending_microtasks(interp : @interpreter.Interpreter) -> Bool {
  interp.host.microtask_queue.length() > 0
}

///|
/// Check if there are pending timers in the queue
pub fn has_pending_timers(interp : @interpreter.Interpreter) -> Bool {
  interp.host.timer_queue.length() > 0
}

///|
/// Run a single microtask checkpoint
/// Returns true if there are more microtasks to process
pub fn run_microtask_checkpoint(
  interp : @interpreter.Interpreter,
) -> Bool raise Error {
  interp.run_microtasks()
  interp.host.microtask_queue.length() > 0
}

///|
/// Run all pending timers with microtask draining between each
pub fn run_timer_checkpoint(
  interp : @interpreter.Interpreter,
) -> Unit raise Error {
  interp.run_timers()
}

///|
/// Run a JavaScript module source and return its exports
/// The module is executed in strict mode and its exports are collected
pub fn run_module(
  source : String,
  annex_b? : Bool = false,
) -> (Array[String], Map[String, @interpreter.Value]) raise Error {
  let interp = @interpreter.new_interpreter(annex_b~)
  let prog = @parser.parse(source)
  let exports = interp.run_module(prog.stmts)
  interp.run_microtasks()
  interp.run_timers()
  (interp.host.output, exports)
}

///|
/// Run multiple modules with dependency resolution.
/// The runtime graph runner pre-registers every module specifier before
/// instantiation/evaluation, so callers do not need to order modules by
/// dependency. Returns the exports of the last module.
pub fn run_modules(
  modules : Array[(String, String)],
  annex_b? : Bool = false,
) -> (Array[String], Map[String, @interpreter.Value]) raise Error {
  let interp = @interpreter.new_interpreter(annex_b~)
  let last_exports = interp.run_modules(modules)
  interp.run_microtasks()
  interp.run_timers()
  (interp.host.output, last_exports)
}