Skip to content

Commit

Permalink
refactor(repl): use an inspector session (denoland#7763)
Browse files Browse the repository at this point in the history
This ports the REPL over to Rust and makes use of an inspector session to run a REPL on top of any isolate which lets make full use of rustylines various things like validators and completors without having to introduce a bunch of hard to test internal ops and glue code.

An accidental but good side effect of this is that the multiple line input we previously had is now an editable multi-line input prompt that is correctly stored in the history as a single entry.
  • Loading branch information
caspervonb authored Oct 1, 2020
1 parent 5590b97 commit 4c779b5
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 349 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ rand = "0.7.3"
regex = "1.3.9"
ring = "0.16.15"
rustyline = { version = "6.3.0", default-features = false }
rustyline-derive = "0.3.1"
serde = { version = "1.0.116", features = ["derive"] }
sys-info = "0.7.0"
sourcemap = "6.0.1"
Expand Down
4 changes: 4 additions & 0 deletions cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ pub struct Flags {
pub no_remote: bool,
pub read_allowlist: Vec<PathBuf>,
pub reload: bool,
pub repl: bool,
pub seed: Option<u64>,
pub unstable: bool,
pub v8_flags: Option<Vec<String>>,
Expand Down Expand Up @@ -447,6 +448,7 @@ fn completions_parse(flags: &mut Flags, matches: &clap::ArgMatches) {

fn repl_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
runtime_args_parse(flags, matches, false);
flags.repl = true;
flags.subcommand = DenoSubcommand::Repl;
flags.allow_net = true;
flags.allow_env = true;
Expand Down Expand Up @@ -2142,6 +2144,7 @@ mod tests {
assert_eq!(
r.unwrap(),
Flags {
repl: true,
subcommand: DenoSubcommand::Repl,
allow_net: true,
allow_env: true,
Expand All @@ -2162,6 +2165,7 @@ mod tests {
assert_eq!(
r.unwrap(),
Flags {
repl: true,
subcommand: DenoSubcommand::Repl,
unstable: true,
import_map_path: Some("import_map.json".to_string()),
Expand Down
1 change: 1 addition & 0 deletions cli/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,7 @@ impl v8::inspector::ChannelImpl for InspectorSession {
) {
let raw_message = message.unwrap().string().to_string();
let message = serde_json::from_str(&raw_message).unwrap();

self
.response_tx_map
.remove(&call_id)
Expand Down
22 changes: 20 additions & 2 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ use crate::file_fetcher::SourceFileFetcher;
use crate::file_fetcher::TextDocument;
use crate::fs as deno_fs;
use crate::global_state::GlobalState;
use crate::inspector::InspectorSession;
use crate::media_type::MediaType;
use crate::permissions::Permissions;
use crate::worker::MainWorker;
Expand Down Expand Up @@ -428,9 +429,26 @@ async fn run_repl(flags: Flags) -> Result<(), AnyError> {
let main_module =
ModuleSpecifier::resolve_url_or_path("./$deno$repl.ts").unwrap();
let global_state = GlobalState::new(flags)?;
let mut worker = MainWorker::new(&global_state, main_module);
let mut worker = MainWorker::new(&global_state, main_module.clone());
(&mut *worker).await?;

let inspector = worker
.inspector
.as_mut()
.expect("Inspector is not created.");

let inspector_session = InspectorSession::new(&mut **inspector);
let repl = repl::run(&global_state, inspector_session);

tokio::pin!(repl);

loop {
(&mut *worker).await?;
tokio::select! {
result = &mut repl => {
return result;
}
_ = &mut *worker => {}
}
}
}

Expand Down
1 change: 0 additions & 1 deletion cli/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub mod permissions;
pub mod plugin;
pub mod process;
pub mod random;
pub mod repl;
pub mod runtime;
pub mod runtime_compiler;
pub mod signal;
Expand Down
78 changes: 0 additions & 78 deletions cli/ops/repl.rs

This file was deleted.

2 changes: 0 additions & 2 deletions cli/ops/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::colors;
use crate::metrics::Metrics;
use crate::permissions::Permissions;
use crate::version;
use crate::DenoSubcommand;
use deno_core::error::AnyError;
use deno_core::serde_json;
use deno_core::serde_json::json;
Expand Down Expand Up @@ -41,7 +40,6 @@ fn op_start(
"noColor": !colors::use_color(),
"pid": std::process::id(),
"ppid": ppid(),
"repl": gs.flags.subcommand == DenoSubcommand::Repl,
"target": env!("TARGET"),
"tsVersion": version::TYPESCRIPT,
"unstableFlag": gs.flags.unstable,
Expand Down
Loading

0 comments on commit 4c779b5

Please sign in to comment.