How CLI REPL code evaluation works
mainWhen a user enters a command in the mongosh CLI REPL (e.g., db.coll.find({})) and presses enter, the code undergoes a multi-stage transformation and execution process involving several components:
- Async REPL: A customized
REPLServer.evalfunction intercepts the input to manage interrupts and error handling. - Mongosh Node REPL: Acts as the orchestrator, managing the Shell API abstraction and history, then forwarding input to the Shell Evaluator.
- Shell Evaluator: Determines if the input is a special shell command (like
show dbs) or JavaScript code.- Shell Commands: Executed immediately without further transformation.
- JavaScript Code: Passed to the Async Rewriter.
- Async Rewriter: Transforms synchronous-style JavaScript into asynchronous code by automatically inserting
awaitstatements for Shell API promises. - Final Evaluation: The rewritten code is evaluated in the Shell API context using the original
REPLServer.evalfunction.