The Ballerina Shell's core logic is handled by the Invoker component, which is responsible for executing code snippets provided by the user. The current implementation, ClassLoadInvoker, follows a specific lifecycle for each snippet:
- Information Derivation: The invoker extracts necessary metadata from the snippet to ensure syntactically correct source generation.
- File Generation: It generates a
.bal file by combining the snippet data with existing module-level declarations and variable details. - Execution: The generated Ballerina file is executed. A custom security manager is used to prevent
System.exit() calls from terminating the VM. - State Persistence: Variable values are persisted across execution phases using a memory class with
recall and memorize methods. At the start of execution, variables are loaded from memory; after execution, they are saved back. - Result Return: For expression evaluations, if the result is non-null, it is returned to the user.
public abstract class Invoker extends DiagnosticReporter {
/**
* Executes a snippet and returns the output lines.
* Snippets parameter should only include newly added snippets.
* Old snippets should be managed as necessary by the implementation.
*
* @param newSnippet New snippet to execute.
* @return Execution output result.
*/
public abstract Optional<Object> execute(Snippet newSnippet) throws InvokerException;
// ..
}