|
| 1 | +# CLI REPL Code Evaluation |
| 2 | + |
| 3 | +This document describes the evaluation of user code in the [CLI REPL](../packages/cli-repl), |
| 4 | +i.e. what happens when the user types `db.coll.find({})` in the CLI REPL and hits enter. |
| 5 | + |
| 6 | +## Involved Components |
| 7 | + |
| 8 | +### Async REPL |
| 9 | +The [`async-repl`](../packages/cli-repl/src/async-repl.ts) is responsible for setting up the Node.js |
| 10 | +`REPLServer` that `mongosh` is using for the CLI. Most notabily we customize the `REPLServer.eval` function |
| 11 | +to disable certain default REPL behavior and set up customized interrupt and error handling. |
| 12 | + |
| 13 | +### Mongosh Node REPL |
| 14 | +The [`MongoshNodeRepl`](../packages/cli-repl/src/mongosh-repl.ts) is our main interface and orchestrator for the CLI. However it does not do any I/O |
| 15 | +on its own but relies on an external I/O provider. It initializes and manages the Shell API abstraction |
| 16 | +as well as handles things like access to the history or startup messages. |
| 17 | + |
| 18 | +### Shell Evaluator |
| 19 | +The [`ShellEvaluator`](../packages/shell-evaluator) is an intermediate package also used by non-CLI uses of the shell and is responsible |
| 20 | +for correctly transforming user input before evaluation. It relies on the `async-rewriter2` for input |
| 21 | +transformation. |
| 22 | +The Shell Evaluator also detects linux-commands like `show dbs` and triggers their execution without further code transformation or evaluation. |
| 23 | + |
| 24 | +### Async Rewriter |
| 25 | +The [`async-rewriter2`](../packages/async-rewriter2) transforms code written in a synchronous way into |
| 26 | +code with automatically inserted `await` statements to resolve promises returned by the Shell API. |
| 27 | + |
| 28 | +## Execution |
| 29 | + |
| 30 | +1. User enters `db.coll.find({})` and hits enter. |
| 31 | +2. The `REPLServer.eval` function is called which is customized in [`async-repl`](../packages/cli-repl/src/async-repl.ts). |
| 32 | +3. The customized `eval` function triggers [`MongoshNodeRepl.eval`](../packages/cli-repl/src/mongosh-repl.ts). |
| 33 | +4. `MongoshNodeRepl` forwards the user input to [`ShellEvaluator.customEval`](../packages/shell-evaluator/src/shell-evaluator.ts). |
| 34 | +5. `ShellEvaluator.customEval` calls `ShellEvaluator.innerEval`: |
| 35 | + 1. For linux-style commands, e.g. `show dbs`, processing is immediately triggered and the result is returned up the chain. |
| 36 | + 2. For JavaScript user input: |
| 37 | + 1. `ShellEvaluator` calls the `async-rewriter` to rewrite user input. |
| 38 | + 2. Rewritten user input is now evaluated in the current Shell API context using the _original, non-modified_ `REPLServer.eval` |
| 39 | + function that has been passed down. |
| 40 | + 3. The evaluation result is returned up the chain. |
| 41 | + |
| 42 | +See also the following diagram which additionally includes important events emitted during the execution. |
| 43 | + |
| 44 | + |
0 commit comments