2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2024-08-19 20:21:12 -04:00
|
|
|
use std::io;
|
|
|
|
use std::io::Write;
|
|
|
|
|
2024-01-31 22:15:22 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-04-27 10:05:20 -04:00
|
|
|
use crate::args::CliOptions;
|
2022-12-09 09:40:48 -05:00
|
|
|
use crate::args::Flags;
|
2022-12-07 14:21:18 -05:00
|
|
|
use crate::args::ReplFlags;
|
2023-12-18 05:43:02 -05:00
|
|
|
use crate::cdp;
|
2022-12-07 14:21:18 -05:00
|
|
|
use crate::colors;
|
2023-05-01 14:35:23 -04:00
|
|
|
use crate::factory::CliFactory;
|
2023-04-27 10:05:20 -04:00
|
|
|
use crate::file_fetcher::FileFetcher;
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2023-04-27 17:36:49 -04:00
|
|
|
use deno_core::futures::StreamExt;
|
2023-12-18 05:43:02 -05:00
|
|
|
use deno_core::serde_json;
|
2023-08-23 19:03:05 -04:00
|
|
|
use deno_core::unsync::spawn_blocking;
|
2024-04-24 15:45:49 -04:00
|
|
|
use deno_runtime::WorkerExecutionMode;
|
2020-10-01 19:14:55 -04:00
|
|
|
use rustyline::error::ReadlineError;
|
2021-11-25 14:05:12 -05:00
|
|
|
|
|
|
|
mod channel;
|
2021-12-17 20:18:17 -05:00
|
|
|
mod editor;
|
|
|
|
mod session;
|
2021-11-25 14:05:12 -05:00
|
|
|
|
|
|
|
use channel::rustyline_channel;
|
2021-12-17 20:18:17 -05:00
|
|
|
use channel::RustylineSyncMessage;
|
2021-11-25 14:05:12 -05:00
|
|
|
use channel::RustylineSyncMessageHandler;
|
2021-12-17 20:18:17 -05:00
|
|
|
use channel::RustylineSyncResponse;
|
|
|
|
use editor::EditorHelper;
|
|
|
|
use editor::ReplEditor;
|
2023-09-15 20:42:09 -04:00
|
|
|
pub use session::EvaluationOutput;
|
|
|
|
pub use session::ReplSession;
|
2024-07-02 18:37:54 -04:00
|
|
|
pub use session::TsEvaluateResponse;
|
2023-09-15 20:42:09 -04:00
|
|
|
pub use session::REPL_INTERNALS_NAME;
|
2020-10-14 08:34:05 -04:00
|
|
|
|
2024-02-23 13:11:15 -05:00
|
|
|
use super::test::create_single_test_event_channel;
|
2023-10-05 16:45:11 -04:00
|
|
|
|
2024-01-05 10:14:58 -05:00
|
|
|
struct Repl {
|
|
|
|
session: ReplSession,
|
|
|
|
editor: ReplEditor,
|
|
|
|
message_handler: RustylineSyncMessageHandler,
|
|
|
|
}
|
|
|
|
|
2024-05-08 22:45:06 -04:00
|
|
|
#[allow(clippy::print_stdout)]
|
2024-01-05 10:14:58 -05:00
|
|
|
impl Repl {
|
|
|
|
async fn run(&mut self) -> Result<(), AnyError> {
|
|
|
|
loop {
|
|
|
|
let line = read_line_and_poll(
|
|
|
|
&mut self.session,
|
|
|
|
&mut self.message_handler,
|
|
|
|
self.editor.clone(),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
match line {
|
|
|
|
Ok(line) => {
|
|
|
|
self.editor.set_should_exit_on_interrupt(false);
|
|
|
|
self.editor.update_history(line.clone());
|
|
|
|
let output = self.session.evaluate_line_and_get_output(&line).await;
|
|
|
|
|
|
|
|
// 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().
|
|
|
|
if self.session.closing().await? {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-05-08 22:45:06 -04:00
|
|
|
println!("{}", output);
|
2024-01-05 10:14:58 -05:00
|
|
|
}
|
|
|
|
Err(ReadlineError::Interrupted) => {
|
|
|
|
if self.editor.should_exit_on_interrupt() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
self.editor.set_should_exit_on_interrupt(true);
|
|
|
|
println!("press ctrl+c again to exit");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Err(ReadlineError::Eof) => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(err) => {
|
2024-05-08 22:45:06 -04:00
|
|
|
println!("Error: {:?}", err);
|
2024-01-05 10:14:58 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-08 22:45:06 -04:00
|
|
|
#[allow(clippy::print_stdout)]
|
2021-06-09 19:07:50 -04:00
|
|
|
async fn read_line_and_poll(
|
|
|
|
repl_session: &mut ReplSession,
|
2021-11-25 14:05:12 -05:00
|
|
|
message_handler: &mut RustylineSyncMessageHandler,
|
2021-06-09 19:07:50 -04:00
|
|
|
editor: ReplEditor,
|
|
|
|
) -> Result<String, ReadlineError> {
|
2023-05-14 17:40:01 -04:00
|
|
|
let mut line_fut = spawn_blocking(move || editor.readline());
|
2021-06-09 19:07:50 -04:00
|
|
|
let mut poll_worker = true;
|
2023-04-27 17:36:49 -04:00
|
|
|
let notifications_rc = repl_session.notifications.clone();
|
2024-01-05 10:14:58 -05:00
|
|
|
let mut notifications = notifications_rc.lock().await;
|
2021-06-09 19:07:50 -04:00
|
|
|
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
2021-06-29 14:39:28 -04:00
|
|
|
result = &mut line_fut => {
|
2021-06-09 19:07:50 -04:00
|
|
|
return result.unwrap();
|
|
|
|
}
|
2021-11-25 14:05:12 -05:00
|
|
|
result = message_handler.recv() => {
|
2021-12-15 13:23:43 -05:00
|
|
|
match result {
|
2022-02-07 11:05:49 -05:00
|
|
|
Some(RustylineSyncMessage::PostMessage { method, params }) => {
|
2021-12-15 13:23:43 -05: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 14:39:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
poll_worker = true;
|
2023-04-27 17:36:49 -04:00
|
|
|
}
|
|
|
|
message = notifications.next() => {
|
|
|
|
if let Some(message) = message {
|
2023-12-18 05:43:02 -05:00
|
|
|
let notification: cdp::Notification = serde_json::from_value(message).unwrap();
|
|
|
|
if notification.method == "Runtime.exceptionThrown" {
|
|
|
|
let exception_thrown: cdp::ExceptionThrown = serde_json::from_value(notification.params).unwrap();
|
|
|
|
let (message, description) = exception_thrown.exception_details.get_message_and_description();
|
|
|
|
println!("{} {}", message, description);
|
2023-04-27 17:36:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-09 19:07:50 -04:00
|
|
|
_ = repl_session.run_event_loop(), if poll_worker => {
|
|
|
|
poll_worker = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-18 10:19:47 -04:00
|
|
|
}
|
|
|
|
|
2022-04-20 08:16:37 -04:00
|
|
|
async fn read_eval_file(
|
2023-04-27 10:05:20 -04:00
|
|
|
cli_options: &CliOptions,
|
|
|
|
file_fetcher: &FileFetcher,
|
2022-04-20 08:16:37 -04:00
|
|
|
eval_file: &str,
|
2024-01-31 22:15:22 -05:00
|
|
|
) -> Result<Arc<str>, AnyError> {
|
2023-03-13 21:12:09 -04:00
|
|
|
let specifier =
|
2023-04-27 10:05:20 -04:00
|
|
|
deno_core::resolve_url_or_path(eval_file, cli_options.initial_cwd())?;
|
2022-04-20 08:16:37 -04:00
|
|
|
|
2024-09-16 16:39:37 -04:00
|
|
|
let file = file_fetcher.fetch_bypass_permissions(&specifier).await?;
|
2022-04-20 08:16:37 -04:00
|
|
|
|
2024-01-31 22:15:22 -05:00
|
|
|
Ok(file.into_text_decoded()?.source)
|
2022-04-20 08:16:37 -04:00
|
|
|
}
|
|
|
|
|
2024-05-08 22:45:06 -04:00
|
|
|
#[allow(clippy::print_stdout)]
|
2024-07-23 19:00:48 -04:00
|
|
|
pub async fn run(
|
|
|
|
flags: Arc<Flags>,
|
|
|
|
repl_flags: ReplFlags,
|
|
|
|
) -> Result<i32, AnyError> {
|
|
|
|
let factory = CliFactory::from_flags(flags);
|
|
|
|
let cli_options = factory.cli_options()?;
|
2023-05-01 14:35:23 -04:00
|
|
|
let main_module = cli_options.resolve_main_module()?;
|
2024-09-16 16:39:37 -04:00
|
|
|
let permissions = factory.create_permissions_container()?;
|
2023-05-01 14:35:23 -04:00
|
|
|
let npm_resolver = factory.npm_resolver().await?.clone();
|
|
|
|
let resolver = factory.resolver().await?.clone();
|
|
|
|
let file_fetcher = factory.file_fetcher()?;
|
|
|
|
let worker_factory = factory.create_cli_main_worker_factory().await?;
|
2023-05-25 14:27:45 -04:00
|
|
|
let history_file_path = factory
|
|
|
|
.deno_dir()
|
|
|
|
.ok()
|
|
|
|
.and_then(|dir| dir.repl_history_file_path());
|
2024-02-23 13:11:15 -05:00
|
|
|
let (worker, test_event_receiver) = create_single_test_event_channel();
|
|
|
|
let test_event_sender = worker.sender;
|
2023-04-27 10:05:20 -04:00
|
|
|
let mut worker = worker_factory
|
2023-10-05 16:45:11 -04:00
|
|
|
.create_custom_worker(
|
2024-04-24 15:45:49 -04:00
|
|
|
WorkerExecutionMode::Repl,
|
2023-10-05 16:45:11 -04:00
|
|
|
main_module.clone(),
|
|
|
|
permissions,
|
2024-04-16 14:54:50 -04:00
|
|
|
vec![crate::ops::testing::deno_test::init_ops(test_event_sender)],
|
2023-10-05 16:45:11 -04:00
|
|
|
Default::default(),
|
|
|
|
)
|
2023-04-27 10:05:20 -04:00
|
|
|
.await?;
|
2022-12-09 09:40:48 -05:00
|
|
|
worker.setup_repl().await?;
|
|
|
|
let worker = worker.into_main_worker();
|
2024-01-05 10:14:58 -05:00
|
|
|
let session = ReplSession::initialize(
|
2023-10-05 16:45:11 -04:00
|
|
|
cli_options,
|
|
|
|
npm_resolver,
|
|
|
|
resolver,
|
|
|
|
worker,
|
|
|
|
main_module,
|
|
|
|
test_event_receiver,
|
|
|
|
)
|
|
|
|
.await?;
|
2024-01-05 10:14:58 -05:00
|
|
|
let rustyline_channel = rustyline_channel();
|
2020-10-19 15:25:21 -04:00
|
|
|
|
2021-06-09 19:07:50 -04:00
|
|
|
let helper = EditorHelper {
|
2024-01-05 10:14:58 -05:00
|
|
|
context_id: session.context_id,
|
2021-11-25 14:05:12 -05:00
|
|
|
sync_sender: rustyline_channel.0,
|
2020-10-01 19:14:55 -04:00
|
|
|
};
|
|
|
|
|
2022-11-24 20:56:47 -05:00
|
|
|
let editor = ReplEditor::new(helper, history_file_path)?;
|
2020-10-01 19:14:55 -04:00
|
|
|
|
2024-01-05 10:14:58 -05:00
|
|
|
let mut repl = Repl {
|
|
|
|
session,
|
|
|
|
editor,
|
|
|
|
message_handler: rustyline_channel.1,
|
|
|
|
};
|
|
|
|
|
2022-12-07 14:21:18 -05:00
|
|
|
if let Some(eval_files) = repl_flags.eval_files {
|
2022-04-20 08:16:37 -04:00
|
|
|
for eval_file in eval_files {
|
2023-05-01 14:35:23 -04:00
|
|
|
match read_eval_file(cli_options, file_fetcher, &eval_file).await {
|
2022-04-20 08:16:37 -04:00
|
|
|
Ok(eval_source) => {
|
2024-01-05 10:14:58 -05:00
|
|
|
let output = repl
|
|
|
|
.session
|
2022-04-20 08:16:37 -04:00
|
|
|
.evaluate_line_and_get_output(&eval_source)
|
2022-12-16 11:11:10 -05:00
|
|
|
.await;
|
2022-04-20 08:16:37 -04:00
|
|
|
// only output errors
|
|
|
|
if let EvaluationOutput::Error(error_text) = output {
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("Error in --eval-file file \"{eval_file}\": {error_text}");
|
2022-04-20 08:16:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("Error in --eval-file file \"{eval_file}\": {e}");
|
2022-04-20 08:16:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 14:21:18 -05:00
|
|
|
if let Some(eval) = repl_flags.eval {
|
2024-01-05 10:14:58 -05:00
|
|
|
let output = repl.session.evaluate_line_and_get_output(&eval).await;
|
2021-08-06 17:30:28 -04:00
|
|
|
// only output errors
|
|
|
|
if let EvaluationOutput::Error(error_text) = output {
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("Error in --eval flag: {error_text}");
|
2021-08-06 17:30:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:25:20 -05:00
|
|
|
// Doing this manually, instead of using `log::info!` because these messages
|
|
|
|
// are supposed to go to stdout, not stderr.
|
2024-08-19 20:21:12 -04:00
|
|
|
// Using writeln, because println panics in certain cases
|
|
|
|
// (eg: broken pipes - https://github.com/denoland/deno/issues/21861)
|
2023-04-27 10:05:20 -04:00
|
|
|
if !cli_options.is_quiet() {
|
2024-08-19 20:21:12 -04:00
|
|
|
let mut handle = io::stdout().lock();
|
|
|
|
|
|
|
|
writeln!(handle, "Deno {}", crate::version::DENO_VERSION_INFO.deno)?;
|
|
|
|
writeln!(handle, "exit using ctrl+d, ctrl+c, or close()")?;
|
|
|
|
|
2022-12-07 14:21:18 -05:00
|
|
|
if repl_flags.is_default_command {
|
2024-08-19 20:21:12 -04:00
|
|
|
writeln!(
|
|
|
|
handle,
|
2022-12-07 14:21:18 -05:00
|
|
|
"{}",
|
|
|
|
colors::yellow("REPL is running with all permissions allowed.")
|
2024-08-19 20:21:12 -04:00
|
|
|
)?;
|
|
|
|
writeln!(
|
|
|
|
handle,
|
|
|
|
"To specify permissions, run `deno repl` with allow flags."
|
|
|
|
)?;
|
2022-12-07 14:21:18 -05:00
|
|
|
}
|
2022-11-30 17:25:20 -05:00
|
|
|
}
|
2020-10-01 19:14:55 -04:00
|
|
|
|
2024-01-05 10:14:58 -05:00
|
|
|
repl.run().await?;
|
2018-11-05 12:55:59 -05:00
|
|
|
|
2024-01-05 10:14:58 -05:00
|
|
|
Ok(repl.session.worker.exit_code())
|
2018-11-05 12:55:59 -05:00
|
|
|
}
|