summaryrefslogtreecommitdiff
path: root/cli/repl.rs
diff options
context:
space:
mode:
authorCasper Beyer <caspervonb@pm.me>2020-10-14 20:34:05 +0800
committerGitHub <noreply@github.com>2020-10-14 14:34:05 +0200
commite2a1a7c937b67d050e7e3287d7e5dbc6ffd9b168 (patch)
tree97e6d642987bb17645bb3bc14fc3056a555f36c0 /cli/repl.rs
parent135053486c4bd112ebd7d0b25c94a8dd346472e6 (diff)
refactor(cli/repl): clean up prelude injection (#7967)
This extracts prelude injection into a helper function and moves the prelude string literal into a top level static string to help trim some of the fat out of the run function.
Diffstat (limited to 'cli/repl.rs')
-rw-r--r--cli/repl.rs92
1 files changed, 51 insertions, 41 deletions
diff --git a/cli/repl.rs b/cli/repl.rs
index 71b7fc649..506a79f0d 100644
--- a/cli/repl.rs
+++ b/cli/repl.rs
@@ -167,6 +167,56 @@ async fn read_line_and_poll(
}
}
+static PRELUDE: &str = r#"
+Object.defineProperty(globalThis, "_", {
+ configurable: true,
+ get: () => Deno[Deno.internal].lastEvalResult,
+ set: (value) => {
+ Object.defineProperty(globalThis, "_", {
+ value: value,
+ writable: true,
+ enumerable: true,
+ configurable: true,
+ });
+ console.log("Last evaluation result is no longer saved to _.");
+ },
+});
+
+Object.defineProperty(globalThis, "_error", {
+ configurable: true,
+ get: () => Deno[Deno.internal].lastThrownError,
+ set: (value) => {
+ Object.defineProperty(globalThis, "_error", {
+ value: value,
+ writable: true,
+ enumerable: true,
+ configurable: true,
+ });
+
+ console.log("Last thrown error is no longer saved to _error.");
+ },
+});
+"#;
+
+async fn inject_prelude(
+ worker: &mut MainWorker,
+ session: &mut InspectorSession,
+ context_id: u64,
+) -> Result<(), AnyError> {
+ post_message_and_poll(
+ worker,
+ session,
+ "Runtime.evaluate",
+ Some(json!({
+ "expression": PRELUDE,
+ "contextId": context_id,
+ })),
+ )
+ .await?;
+
+ Ok(())
+}
+
pub async fn run(
program_state: &ProgramState,
mut worker: MainWorker,
@@ -215,47 +265,7 @@ pub async fn run(
println!("Deno {}", crate::version::DENO);
println!("exit using ctrl+d or close()");
- let prelude = r#"
- Object.defineProperty(globalThis, "_", {
- configurable: true,
- get: () => Deno[Deno.internal].lastEvalResult,
- set: (value) => {
- Object.defineProperty(globalThis, "_", {
- value: value,
- writable: true,
- enumerable: true,
- configurable: true,
- });
- console.log("Last evaluation result is no longer saved to _.");
- },
- });
-
- Object.defineProperty(globalThis, "_error", {
- configurable: true,
- get: () => Deno[Deno.internal].lastThrownError,
- set: (value) => {
- Object.defineProperty(globalThis, "_error", {
- value: value,
- writable: true,
- enumerable: true,
- configurable: true,
- });
-
- console.log("Last thrown error is no longer saved to _error.");
- },
- });
- "#;
-
- post_message_and_poll(
- &mut *worker,
- &mut session,
- "Runtime.evaluate",
- Some(json!({
- "expression": prelude,
- "contextId": context_id,
- })),
- )
- .await?;
+ inject_prelude(&mut worker, &mut session, context_id).await?;
loop {
let line = read_line_and_poll(&mut *worker, editor.clone()).await;