Skip to content

Commit

Permalink
Revert "refactor(repl): use an inspector session (denoland#7763)"
Browse files Browse the repository at this point in the history
This reverts commit 4c779b5.
  • Loading branch information
ry committed Oct 2, 2020
1 parent c0ee07a commit 3dc0e45
Show file tree
Hide file tree
Showing 13 changed files with 349 additions and 282 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ 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: 0 additions & 4 deletions cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ 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 @@ -448,7 +447,6 @@ 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 @@ -2144,7 +2142,6 @@ mod tests {
assert_eq!(
r.unwrap(),
Flags {
repl: true,
subcommand: DenoSubcommand::Repl,
allow_net: true,
allow_env: true,
Expand All @@ -2165,7 +2162,6 @@ 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: 0 additions & 1 deletion cli/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,6 @@ 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: 2 additions & 20 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ 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 @@ -429,26 +428,9 @@ 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.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);

let mut worker = MainWorker::new(&global_state, main_module);
loop {
tokio::select! {
result = &mut repl => {
return result;
}
_ = &mut *worker => {}
}
(&mut *worker).await?;
}
}

Expand Down
1 change: 1 addition & 0 deletions cli/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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: 78 additions & 0 deletions cli/ops/repl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.

use crate::repl;
use crate::repl::Repl;
use deno_core::error::bad_resource_id;
use deno_core::error::AnyError;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::serde_json::Value;
use deno_core::BufVec;
use deno_core::OpState;
use deno_core::ZeroCopyBuf;
use serde::Deserialize;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::Mutex;

pub fn init(rt: &mut deno_core::JsRuntime) {
super::reg_json_sync(rt, "op_repl_start", op_repl_start);
super::reg_json_async(rt, "op_repl_readline", op_repl_readline);
}

struct ReplResource(Arc<Mutex<Repl>>);

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct ReplStartArgs {
history_file: String,
}

fn op_repl_start(
state: &mut OpState,
args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
let args: ReplStartArgs = serde_json::from_value(args)?;
debug!("op_repl_start {}", args.history_file);
let history_path = {
let cli_state = super::global_state(state);
repl::history_path(&cli_state.dir, &args.history_file)
};
let repl = repl::Repl::new(history_path);
let resource = ReplResource(Arc::new(Mutex::new(repl)));
let rid = state.resource_table.add("repl", Box::new(resource));
Ok(json!(rid))
}

#[derive(Deserialize)]
struct ReplReadlineArgs {
rid: i32,
prompt: String,
}

async fn op_repl_readline(
state: Rc<RefCell<OpState>>,
args: Value,
_zero_copy: BufVec,
) -> Result<Value, AnyError> {
let args: ReplReadlineArgs = serde_json::from_value(args)?;
let rid = args.rid as u32;
let prompt = args.prompt;
debug!("op_repl_readline {} {}", rid, prompt);
let repl = {
let state = state.borrow();
let resource = state
.resource_table
.get::<ReplResource>(rid)
.ok_or_else(bad_resource_id)?;
resource.0.clone()
};
tokio::task::spawn_blocking(move || {
let line = repl.lock().unwrap().readline(&prompt)?;
Ok(json!(line))
})
.await
.unwrap()
}
2 changes: 2 additions & 0 deletions cli/ops/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 @@ -40,6 +41,7 @@ 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 3dc0e45

Please sign in to comment.