1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use crate::proc_state::ProcState;
use deno_core::error::AnyError;
use deno_runtime::permissions::Permissions;
use deno_runtime::worker::MainWorker;
use rustyline::error::ReadlineError;
mod channel;
mod editor;
mod session;
use channel::rustyline_channel;
use channel::RustylineSyncMessage;
use channel::RustylineSyncMessageHandler;
use channel::RustylineSyncResponse;
use editor::EditorHelper;
use editor::ReplEditor;
use session::EvaluationOutput;
use session::ReplSession;
async fn read_line_and_poll(
repl_session: &mut ReplSession,
message_handler: &mut RustylineSyncMessageHandler,
editor: ReplEditor,
) -> Result<String, ReadlineError> {
let mut line_fut = tokio::task::spawn_blocking(move || editor.readline());
let mut poll_worker = true;
loop {
tokio::select! {
result = &mut line_fut => {
return result.unwrap();
}
result = message_handler.recv() => {
match result {
Some(RustylineSyncMessage::PostMessage { method, params }) => {
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
}
poll_worker = true;
},
_ = repl_session.run_event_loop(), if poll_worker => {
poll_worker = false;
}
}
}
}
async fn read_eval_file(
ps: &ProcState,
eval_file: &str,
) -> Result<String, AnyError> {
let specifier = deno_core::resolve_url_or_path(eval_file)?;
let file = ps
.file_fetcher
.fetch(&specifier, &mut Permissions::allow_all())
.await?;
Ok((*file.source).clone())
}
pub async fn run(
ps: &ProcState,
worker: MainWorker,
maybe_eval_files: Option<Vec<String>>,
maybe_eval: Option<String>,
) -> Result<i32, AnyError> {
let mut repl_session = ReplSession::initialize(worker).await?;
let mut rustyline_channel = rustyline_channel();
let helper = EditorHelper {
context_id: repl_session.context_id,
sync_sender: rustyline_channel.0,
};
let history_file_path = ps.dir.root.join("deno_history.txt");
let editor = ReplEditor::new(helper, history_file_path);
if let Some(eval_files) = maybe_eval_files {
for eval_file in eval_files {
match read_eval_file(ps, &eval_file).await {
Ok(eval_source) => {
let output = repl_session
.evaluate_line_and_get_output(&eval_source)
.await?;
// only output errors
if let EvaluationOutput::Error(error_text) = output {
println!("error in --eval-file file {}. {}", eval_file, error_text);
}
}
Err(e) => {
println!("error in --eval-file file {}. {}", eval_file, e);
}
}
}
}
if let Some(eval) = maybe_eval {
let output = repl_session.evaluate_line_and_get_output(&eval).await?;
// only output errors
if let EvaluationOutput::Error(error_text) = output {
println!("error in --eval flag. {}", error_text);
}
}
println!("Deno {}", crate::version::deno());
println!("exit using ctrl+d or close()");
loop {
let line = read_line_and_poll(
&mut repl_session,
&mut rustyline_channel.1,
editor.clone(),
)
.await;
match line {
Ok(line) => {
let output = repl_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 repl_session.closing().await? {
break;
}
println!("{}", output);
editor.add_history_entry(line);
}
Err(ReadlineError::Interrupted) => {
println!("exit using ctrl+d or close()");
continue;
}
Err(ReadlineError::Eof) => {
break;
}
Err(err) => {
println!("Error: {:?}", err);
break;
}
}
}
editor.save_history()?;
Ok(repl_session.worker.get_exit_code())
}
|