Class BytecodeExecutor

java.lang.Object
org.ek9lang.compiler.backend.jvm.BytecodeExecutor

public final class BytecodeExecutor extends Object
Executes generated EK9 bytecode within the current JVM. This avoids spawning external JVM processes for each execution, significantly improving performance.

The executor uses a URLClassLoader to load generated .class files from the compilation output directory. The ek9-lang runtime classes are loaded from the classpath (parent-first delegation).

Output capture is achieved using a thread-local PrintStream. Each thread captures its own output independently, enabling parallel-safe execution. EK9 programs use Stdout which writes to System.out, but we redirect System.out to a thread-local delegating stream.

Used by REPL and bytecode tests.

  • Method Details

    • execute

      public static String execute(Path bytecodeDir, String programClassName, String... args) throws Exception
      Executes a generated EK9 program and returns its captured stdout output.

      This method is thread-safe and can be called from parallel threads. Each thread captures its own output independently.

      Parameters:
      bytecodeDir - Directory containing generated .class files
      programClassName - Fully qualified class name (e.g., "bytecode.test.MyProgram")
      args - String arguments to pass to _main() method
      Returns:
      The captured stdout output from the program
      Throws:
      Exception - if execution fails