Skip to content

Commit 645314b

Browse files
committed
Move readline.rs into the vm crate
1 parent 019036d commit 645314b

File tree

7 files changed

+62
-51
lines changed

7 files changed

+62
-51
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ rustpython-parser = {path = "parser", version = "0.1.1"}
2727
rustpython-vm = {path = "vm", version = "0.1.1"}
2828
dirs = "2.0"
2929
num-traits = "0.2.8"
30+
cfg-if = "0.1"
3031

3132
flame = { version = "0.2", optional = true }
3233
flamescope = { version = "0.1", optional = true }

src/shell.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
mod readline;
2-
#[cfg(not(target_os = "wasi"))]
3-
mod rustyline_helper;
1+
mod helper;
42

53
use rustpython_compiler::{compile, error::CompileError, error::CompileErrorType};
64
use rustpython_parser::error::ParseErrorType;
5+
use rustpython_vm::readline::{Readline, ReadlineResult};
76
use rustpython_vm::{
87
exceptions::{print_exception, PyBaseExceptionRef},
98
obj::objtype,
@@ -12,8 +11,6 @@ use rustpython_vm::{
1211
VirtualMachine,
1312
};
1413

15-
use readline::{Readline, ReadlineResult};
16-
1714
enum ShellExecResult {
1815
Ok,
1916
PyErr(PyBaseExceptionRef),
@@ -49,7 +46,7 @@ pub fn run_shell(vm: &VirtualMachine, scope: Scope) -> PyResult<()> {
4946
crate_version!()
5047
);
5148

52-
let mut repl = Readline::new(vm, scope.clone());
49+
let mut repl = Readline::new(helper::ShellHelper::new(vm, scope.clone()));
5350
let mut full_input = String::new();
5451

5552
// Retrieve a `history_path_str` dependent on the OS
Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ use rustpython_vm::obj::objstr::PyStringRef;
22
use rustpython_vm::pyobject::{PyIterable, PyResult, TryFromObject};
33
use rustpython_vm::scope::{NameProtocol, Scope};
44
use rustpython_vm::VirtualMachine;
5-
use rustyline::{
6-
completion::Completer, highlight::Highlighter, hint::Hinter, validate::Validator, Context,
7-
Helper,
8-
};
95

106
pub struct ShellHelper<'vm> {
117
vm: &'vm VirtualMachine,
@@ -146,24 +142,32 @@ impl<'vm> ShellHelper<'vm> {
146142
}
147143
}
148144

149-
impl Completer for ShellHelper<'_> {
150-
type Candidate = String;
145+
cfg_if::cfg_if! {
146+
if #[cfg(not(target_os = "wasi"))] {
147+
use rustyline::{
148+
completion::Completer, highlight::Highlighter, hint::Hinter, validate::Validator, Context,
149+
Helper,
150+
};
151+
impl Completer for ShellHelper<'_> {
152+
type Candidate = String;
153+
154+
fn complete(
155+
&self,
156+
line: &str,
157+
pos: usize,
158+
_ctx: &Context,
159+
) -> rustyline::Result<(usize, Vec<String>)> {
160+
Ok(self
161+
.complete_opt(&line[0..pos])
162+
// as far as I can tell, there's no better way to do both completion
163+
// and indentation (or even just indentation)
164+
.unwrap_or_else(|| (line.len(), vec!["\t".to_owned()])))
165+
}
166+
}
151167

152-
fn complete(
153-
&self,
154-
line: &str,
155-
pos: usize,
156-
_ctx: &Context,
157-
) -> rustyline::Result<(usize, Vec<String>)> {
158-
Ok(self
159-
.complete_opt(&line[0..pos])
160-
// as far as I can tell, there's no better way to do both completion
161-
// and indentation (or even just indentation)
162-
.unwrap_or_else(|| (line.len(), vec!["\t".to_owned()])))
168+
impl Hinter for ShellHelper<'_> {}
169+
impl Highlighter for ShellHelper<'_> {}
170+
impl Validator for ShellHelper<'_> {}
171+
impl Helper for ShellHelper<'_> {}
163172
}
164173
}
165-
166-
impl Hinter for ShellHelper<'_> {}
167-
impl Highlighter for ShellHelper<'_> {}
168-
impl Validator for ShellHelper<'_> {}
169-
impl Helper for ShellHelper<'_> {}

vm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ gethostname = "0.2.0"
8989
subprocess = "0.2.2"
9090
num_cpus = "1"
9191
socket2 = { version = "0.3", features = ["unix"] }
92+
rustyline = "6.0"
9293

9394
[target.'cfg(not(any(target_arch = "wasm32", target_os = "redox")))'.dependencies]
9495
dns-lookup = "1.0"

vm/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub mod obj;
7070
pub mod py_serde;
7171
mod pyhash;
7272
pub mod pyobject;
73+
pub mod readline;
7374
pub mod scope;
7475
mod sequence;
7576
pub mod slots;
Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::io;
22
use std::path::Path;
33

4-
use rustpython_vm::{scope::Scope, VirtualMachine};
5-
64
type OtherError = Box<dyn std::error::Error>;
75
type OtherResult<T> = Result<T, OtherError>;
86

@@ -19,13 +17,16 @@ pub enum ReadlineResult {
1917
mod basic_readline {
2018
use super::*;
2119

22-
pub struct BasicReadline<'vm> {
23-
vm: &'vm VirtualMachine,
20+
pub trait Helper {}
21+
impl<T> Helper for T {}
22+
23+
pub struct Readline<H: Helper> {
24+
helper: H,
2425
}
2526

26-
impl<'vm> BasicReadline<'vm> {
27-
pub fn new(vm: &'vm VirtualMachine, _scope: Scope) -> Self {
28-
BasicReadline { vm }
27+
impl<H: Helper> Readline<H> {
28+
pub fn new(helper: H) -> Self {
29+
Readline { helper }
2930
}
3031

3132
pub fn load_history(&mut self, _path: &Path) -> OtherResult<()> {
@@ -60,16 +61,19 @@ mod basic_readline {
6061
}
6162
}
6263

63-
#[cfg(not(target_os = "wasi"))]
64+
#[cfg(not(target_arch = "wasm32"))]
6465
mod rustyline_readline {
65-
use super::{super::rustyline_helper::ShellHelper, *};
66+
use super::*;
67+
68+
pub trait Helper: rustyline::Helper {}
69+
impl<T: rustyline::Helper> Helper for T {}
6670

67-
pub struct RustylineReadline<'vm> {
68-
repl: rustyline::Editor<ShellHelper<'vm>>,
71+
pub struct Readline<H: Helper> {
72+
repl: rustyline::Editor<H>,
6973
}
7074

71-
impl<'vm> RustylineReadline<'vm> {
72-
pub fn new(vm: &'vm VirtualMachine, scope: Scope) -> Self {
75+
impl<H: Helper> Readline<H> {
76+
pub fn new(helper: H) -> Self {
7377
use rustyline::{At, Cmd, CompletionType, Config, Editor, KeyPress, Movement, Word};
7478
let mut repl = Editor::with_config(
7579
Config::builder()
@@ -85,8 +89,8 @@ mod rustyline_readline {
8589
KeyPress::ControlRight,
8690
Cmd::Move(Movement::ForwardWord(1, At::AfterEnd, Word::Vi)),
8791
);
88-
repl.set_helper(Some(ShellHelper::new(vm, scope)));
89-
RustylineReadline { repl }
92+
repl.set_helper(Some(helper));
93+
Readline { repl }
9094
}
9195

9296
pub fn load_history(&mut self, path: &Path) -> OtherResult<()> {
@@ -126,17 +130,18 @@ mod rustyline_readline {
126130
}
127131
}
128132

129-
#[cfg(target_os = "wasi")]
130-
type ReadlineInner<'vm> = basic_readline::BasicReadline<'vm>;
133+
#[cfg(target_arch = "wasm32")]
134+
use basic_readline as readline_inner;
135+
#[cfg(not(target_arch = "wasm32"))]
136+
use rustyline_readline as readline_inner;
131137

132-
#[cfg(not(target_os = "wasi"))]
133-
type ReadlineInner<'vm> = rustyline_readline::RustylineReadline<'vm>;
138+
pub use readline_inner::Helper;
134139

135-
pub struct Readline<'vm>(ReadlineInner<'vm>);
140+
pub struct Readline<H: Helper>(readline_inner::Readline<H>);
136141

137-
impl<'vm> Readline<'vm> {
138-
pub fn new(vm: &'vm VirtualMachine, scope: Scope) -> Self {
139-
Readline(ReadlineInner::new(vm, scope))
142+
impl<H: Helper> Readline<H> {
143+
pub fn new(helper: H) -> Self {
144+
Readline(readline_inner::Readline::new(helper))
140145
}
141146
pub fn load_history(&mut self, path: &Path) -> OtherResult<()> {
142147
self.0.load_history(path)

0 commit comments

Comments
 (0)