2023-01-02 21:00:42 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 00:34:02 +00:00
|
|
|
|
2023-04-27 14:05:20 +00:00
|
|
|
use crate::args::CliOptions;
|
2022-12-09 14:40:48 +00:00
|
|
|
use crate::args::Flags;
|
2022-12-07 19:21:18 +00:00
|
|
|
use crate::args::ReplFlags;
|
|
|
|
use crate::colors;
|
2023-05-01 18:35:23 +00:00
|
|
|
use crate::factory::CliFactory;
|
2023-04-27 14:05:20 +00:00
|
|
|
use crate::file_fetcher::FileFetcher;
|
2020-09-14 16:48:57 +00:00
|
|
|
use deno_core::error::AnyError;
|
2023-04-27 21:36:49 +00:00
|
|
|
use deno_core::futures::StreamExt;
|
2023-05-14 21:40:01 +00:00
|
|
|
use deno_core::task::spawn_blocking;
|
2022-04-20 12:16:37 +00:00
|
|
|
use deno_runtime::permissions::Permissions;
|
2023-01-07 16:25:34 +00:00
|
|
|
use deno_runtime::permissions::PermissionsContainer;
|
2020-10-01 23:14:55 +00:00
|
|
|
use rustyline::error::ReadlineError;
|
2021-11-25 19:05:12 +00:00
|
|
|
|
2022-11-25 23:38:08 +00:00
|
|
|
mod cdp;
|
2021-11-25 19:05:12 +00:00
|
|
|
mod channel;
|
2021-12-18 01:18:17 +00:00
|
|
|
mod editor;
|
|
|
|
mod session;
|
2021-11-25 19:05:12 +00:00
|
|
|
|
|
|
|
use channel::rustyline_channel;
|
2021-12-18 01:18:17 +00:00
|
|
|
use channel::RustylineSyncMessage;
|
2021-11-25 19:05:12 +00:00
|
|
|
use channel::RustylineSyncMessageHandler;
|
2021-12-18 01:18:17 +00:00
|
|
|
use channel::RustylineSyncResponse;
|
|
|
|
use editor::EditorHelper;
|
|
|
|
use editor::ReplEditor;
|
|
|
|
use session::EvaluationOutput;
|
|
|
|
use session::ReplSession;
|
2020-10-14 12:34:05 +00:00
|
|
|
|
2021-06-09 23:07:50 +00:00
|
|
|
async fn read_line_and_poll(
|
|
|
|
repl_session: &mut ReplSession,
|
2021-11-25 19:05:12 +00:00
|
|
|
message_handler: &mut RustylineSyncMessageHandler,
|
2021-06-09 23:07:50 +00:00
|
|
|
editor: ReplEditor,
|
|
|
|
) -> Result<String, ReadlineError> {
|
2023-04-27 21:36:49 +00:00
|
|
|
#![allow(clippy::await_holding_refcell_ref)]
|
2023-05-14 21:40:01 +00:00
|
|
|
let mut line_fut = spawn_blocking(move || editor.readline());
|
2021-06-09 23:07:50 +00:00
|
|
|
let mut poll_worker = true;
|
2023-04-27 21:36:49 +00:00
|
|
|
let notifications_rc = repl_session.notifications.clone();
|
|
|
|
let mut notifications = notifications_rc.borrow_mut();
|
2021-06-09 23:07:50 +00:00
|
|
|
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
2021-06-29 18:39:28 +00:00
|
|
|
result = &mut line_fut => {
|
2021-06-09 23:07:50 +00:00
|
|
|
return result.unwrap();
|
|
|
|
}
|
2021-11-25 19:05:12 +00:00
|
|
|
result = message_handler.recv() => {
|
2021-12-15 18:23:43 +00:00
|
|
|
match result {
|
2022-02-07 16:05:49 +00:00
|
|
|
Some(RustylineSyncMessage::PostMessage { method, params }) => {
|
2021-12-15 18:23:43 +00:00
|
|
|
let result = repl_session
|
|
|
|
.post_message_with_event_loop(&method, params)
|
|
|
|
.await;
|
|
|
|
message_handler.send(RustylineSyncResponse::PostMessage(result)).unwrap();
|
|
|
|
},
|
|
|
|
Some(RustylineSyncMessage::LspCompletions {
|
|
|
|
line_text,
|
|
|
|
position,
|
|
|
|
}) => {
|
|
|
|
let result = repl_session.language_server.completions(&line_text, position).await;
|
|
|
|
message_handler.send(RustylineSyncResponse::LspCompletions(result)).unwrap();
|
|
|
|
}
|
|
|
|
None => {}, // channel closed
|
2021-06-29 18:39:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
poll_worker = true;
|
2023-04-27 21:36:49 +00:00
|
|
|
}
|
|
|
|
message = notifications.next() => {
|
|
|
|
if let Some(message) = message {
|
|
|
|
let method = message.get("method").unwrap().as_str().unwrap();
|
|
|
|
if method == "Runtime.exceptionThrown" {
|
|
|
|
let params = message.get("params").unwrap().as_object().unwrap();
|
|
|
|
let exception_details = params.get("exceptionDetails").unwrap().as_object().unwrap();
|
|
|
|
let text = exception_details.get("text").unwrap().as_str().unwrap();
|
|
|
|
let exception = exception_details.get("exception").unwrap().as_object().unwrap();
|
2023-04-28 13:21:55 +00:00
|
|
|
let description = exception.get("description").and_then(|d| d.as_str()).unwrap_or("undefined");
|
2023-04-27 21:36:49 +00:00
|
|
|
println!("{text} {description}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 23:07:50 +00:00
|
|
|
_ = repl_session.run_event_loop(), if poll_worker => {
|
|
|
|
poll_worker = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-18 14:19:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 12:16:37 +00:00
|
|
|
async fn read_eval_file(
|
2023-04-27 14:05:20 +00:00
|
|
|
cli_options: &CliOptions,
|
|
|
|
file_fetcher: &FileFetcher,
|
2022-04-20 12:16:37 +00:00
|
|
|
eval_file: &str,
|
|
|
|
) -> Result<String, AnyError> {
|
2023-03-14 01:12:09 +00:00
|
|
|
let specifier =
|
2023-04-27 14:05:20 +00:00
|
|
|
deno_core::resolve_url_or_path(eval_file, cli_options.initial_cwd())?;
|
2022-04-20 12:16:37 +00:00
|
|
|
|
2023-04-27 14:05:20 +00:00
|
|
|
let file = file_fetcher
|
2023-01-07 16:25:34 +00:00
|
|
|
.fetch(&specifier, PermissionsContainer::allow_all())
|
2022-04-20 12:16:37 +00:00
|
|
|
.await?;
|
|
|
|
|
2022-05-20 20:40:55 +00:00
|
|
|
Ok((*file.source).to_string())
|
2022-04-20 12:16:37 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 14:40:48 +00:00
|
|
|
pub async fn run(flags: Flags, repl_flags: ReplFlags) -> Result<i32, AnyError> {
|
2023-05-01 18:35:23 +00:00
|
|
|
let factory = CliFactory::from_flags(flags).await?;
|
|
|
|
let cli_options = factory.cli_options();
|
|
|
|
let main_module = cli_options.resolve_main_module()?;
|
2023-04-27 14:05:20 +00:00
|
|
|
let permissions = PermissionsContainer::new(Permissions::from_options(
|
2023-05-01 18:35:23 +00:00
|
|
|
&cli_options.permissions_options(),
|
2023-04-27 14:05:20 +00:00
|
|
|
)?);
|
2023-05-01 18:35:23 +00:00
|
|
|
let npm_resolver = factory.npm_resolver().await?.clone();
|
|
|
|
let resolver = factory.resolver().await?.clone();
|
|
|
|
let dir = factory.deno_dir()?;
|
|
|
|
let file_fetcher = factory.file_fetcher()?;
|
|
|
|
let worker_factory = factory.create_cli_main_worker_factory().await?;
|
2023-04-27 14:05:20 +00:00
|
|
|
|
|
|
|
let mut worker = worker_factory
|
|
|
|
.create_main_worker(main_module, permissions)
|
|
|
|
.await?;
|
2022-12-09 14:40:48 +00:00
|
|
|
worker.setup_repl().await?;
|
|
|
|
let worker = worker.into_main_worker();
|
2023-04-27 14:05:20 +00:00
|
|
|
let mut repl_session =
|
2023-05-01 18:35:23 +00:00
|
|
|
ReplSession::initialize(cli_options, npm_resolver, resolver, worker)
|
2023-04-27 14:05:20 +00:00
|
|
|
.await?;
|
2021-11-25 19:05:12 +00:00
|
|
|
let mut rustyline_channel = rustyline_channel();
|
2020-10-19 19:25:21 +00:00
|
|
|
|
2021-06-09 23:07:50 +00:00
|
|
|
let helper = EditorHelper {
|
|
|
|
context_id: repl_session.context_id,
|
2021-11-25 19:05:12 +00:00
|
|
|
sync_sender: rustyline_channel.0,
|
2020-10-01 23:14:55 +00:00
|
|
|
};
|
|
|
|
|
2023-04-27 14:05:20 +00:00
|
|
|
let history_file_path = dir.repl_history_file_path();
|
2022-11-25 01:56:47 +00:00
|
|
|
let editor = ReplEditor::new(helper, history_file_path)?;
|
2020-10-01 23:14:55 +00:00
|
|
|
|
2022-12-07 19:21:18 +00:00
|
|
|
if let Some(eval_files) = repl_flags.eval_files {
|
2022-04-20 12:16:37 +00:00
|
|
|
for eval_file in eval_files {
|
2023-05-01 18:35:23 +00:00
|
|
|
match read_eval_file(cli_options, file_fetcher, &eval_file).await {
|
2022-04-20 12:16:37 +00:00
|
|
|
Ok(eval_source) => {
|
|
|
|
let output = repl_session
|
|
|
|
.evaluate_line_and_get_output(&eval_source)
|
2022-12-16 16:11:10 +00:00
|
|
|
.await;
|
2022-04-20 12:16:37 +00:00
|
|
|
// only output errors
|
|
|
|
if let EvaluationOutput::Error(error_text) = output {
|
2023-01-27 15:43:16 +00:00
|
|
|
println!("Error in --eval-file file \"{eval_file}\": {error_text}");
|
2022-04-20 12:16:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2023-01-27 15:43:16 +00:00
|
|
|
println!("Error in --eval-file file \"{eval_file}\": {e}");
|
2022-04-20 12:16:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 19:21:18 +00:00
|
|
|
if let Some(eval) = repl_flags.eval {
|
2022-12-16 16:11:10 +00:00
|
|
|
let output = repl_session.evaluate_line_and_get_output(&eval).await;
|
2021-08-06 21:30:28 +00:00
|
|
|
// only output errors
|
|
|
|
if let EvaluationOutput::Error(error_text) = output {
|
2023-01-27 15:43:16 +00:00
|
|
|
println!("Error in --eval flag: {error_text}");
|
2021-08-06 21:30:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 22:25:20 +00:00
|
|
|
// Doing this manually, instead of using `log::info!` because these messages
|
|
|
|
// are supposed to go to stdout, not stderr.
|
2023-04-27 14:05:20 +00:00
|
|
|
if !cli_options.is_quiet() {
|
2022-11-30 22:25:20 +00:00
|
|
|
println!("Deno {}", crate::version::deno());
|
|
|
|
println!("exit using ctrl+d, ctrl+c, or close()");
|
2022-12-07 19:21:18 +00:00
|
|
|
if repl_flags.is_default_command {
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
colors::yellow("REPL is running with all permissions allowed.")
|
|
|
|
);
|
|
|
|
println!("To specify permissions, run `deno repl` with allow flags.")
|
|
|
|
}
|
2022-11-30 22:25:20 +00:00
|
|
|
}
|
2020-10-01 23:14:55 +00:00
|
|
|
|
2020-12-01 13:13:30 +00:00
|
|
|
loop {
|
2020-10-19 19:25:21 +00:00
|
|
|
let line = read_line_and_poll(
|
2021-06-09 23:07:50 +00:00
|
|
|
&mut repl_session,
|
2021-11-25 19:05:12 +00:00
|
|
|
&mut rustyline_channel.1,
|
2020-10-19 19:25:21 +00:00
|
|
|
editor.clone(),
|
|
|
|
)
|
|
|
|
.await;
|
2020-10-01 23:14:55 +00:00
|
|
|
match line {
|
|
|
|
Ok(line) => {
|
2022-12-16 23:39:52 +00:00
|
|
|
editor.set_should_exit_on_interrupt(false);
|
2022-11-25 01:56:47 +00:00
|
|
|
editor.update_history(line.clone());
|
2022-12-16 16:11:10 +00:00
|
|
|
let output = repl_session.evaluate_line_and_get_output(&line).await;
|
2020-10-01 23:14:55 +00:00
|
|
|
|
2020-12-01 13:13:30 +00:00
|
|
|
// We check for close and break here instead of making it a loop condition to get
|
|
|
|
// consistent behavior in when the user evaluates a call to close().
|
2022-05-12 23:12:55 +00:00
|
|
|
if repl_session.closing().await? {
|
2020-12-01 13:13:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-27 15:43:16 +00:00
|
|
|
println!("{output}");
|
2020-10-01 23:14:55 +00:00
|
|
|
}
|
|
|
|
Err(ReadlineError::Interrupted) => {
|
2022-12-16 23:39:52 +00:00
|
|
|
if editor.should_exit_on_interrupt() {
|
2022-09-22 08:42:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-12-16 23:39:52 +00:00
|
|
|
editor.set_should_exit_on_interrupt(true);
|
2022-09-22 08:42:09 +00:00
|
|
|
println!("press ctrl+c again to exit");
|
2020-10-19 12:06:21 +00:00
|
|
|
continue;
|
2020-10-01 23:14:55 +00:00
|
|
|
}
|
|
|
|
Err(ReadlineError::Eof) => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(err) => {
|
2023-01-27 15:43:16 +00:00
|
|
|
println!("Error: {err:?}");
|
2020-10-01 23:14:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-11-05 17:55:59 +00:00
|
|
|
}
|
|
|
|
|
2023-01-05 19:29:50 +00:00
|
|
|
Ok(repl_session.worker.exit_code())
|
2018-11-05 17:55:59 +00:00
|
|
|
}
|