Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
hayd committed Oct 28, 2018
1 parent cce3b72 commit e7e1676
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
6 changes: 3 additions & 3 deletions js/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ function req(name: string): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {

function res(baseRes: null | msg.Base): string {
assert(baseRes != null);
assert(msg.Any.ReplRes === baseRes!.innerType());
const inner = new msg.ReplRes();
assert(msg.Any.ReplReadlineRes === baseRes!.innerType());
const inner = new msg.ReplReadlineRes();
assert(baseRes!.inner(inner) != null);
const line = inner.line();
assert(line !== null);
Expand All @@ -69,7 +69,7 @@ function res(baseRes: null | msg.Base): string {
export async function replLoop(): Promise<void> {
window.deno = deno; // FIXME use a new scope (rather than window).

const replName = "repl";
const replName = "deno";
const prompt = ">> ";

startRepl(replName, prompt);
Expand Down
4 changes: 2 additions & 2 deletions src/msg.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ union Any {
ReplStart,
ReplExit,
ReplReadline,
ReplRes,
ReplReadlineRes,
Symlink,
Stat,
StatRes,
Expand Down Expand Up @@ -283,7 +283,7 @@ table ReplReadline {
name: string;
}

table ReplRes {
table ReplReadlineRes {
line: string;
}

Expand Down
14 changes: 7 additions & 7 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ pub fn dispatch(
msg::Any::Read => op_read,
msg::Any::Remove => op_remove,
msg::Any::Rename => op_rename,
msg::Any::ReplStart => op_repl_start,
msg::Any::ReplExit => op_repl_exit,
msg::Any::ReplReadline => op_repl,
msg::Any::ReplReadline => op_repl_readline,
msg::Any::ReplStart => op_repl_start,
msg::Any::SetEnv => op_set_env,
msg::Any::Shutdown => op_shutdown,
msg::Any::Start => op_start,
Expand Down Expand Up @@ -1152,7 +1152,7 @@ fn op_repl_exit(
ok_future(empty_buf())
}

fn op_repl(
fn op_repl_readline(
state: Arc<IsolateState>,
base: &msg::Base,
data: &'static mut [u8],
Expand All @@ -1163,17 +1163,17 @@ fn op_repl(
let name = inner.name().unwrap().to_owned();

blocking!(base.sync(), || -> OpResult {
debug!("op_repl {}", name);
debug!("op_repl_readline {}", name);

let mut repls = state.repls.lock().unwrap();
let repl = repls.get_mut(&name).unwrap();

let line = repl.readline()?;
let builder = &mut FlatBufferBuilder::new();
let line_off = builder.create_string(&line);
let inner = msg::ReplRes::create(
let inner = msg::ReplReadlineRes::create(
builder,
&msg::ReplResArgs {
&msg::ReplReadlineResArgs {
line: Some(line_off),
..Default::default()
},
Expand All @@ -1183,7 +1183,7 @@ fn op_repl(
builder,
msg::BaseArgs {
inner: Some(inner.as_union_value()),
inner_type: msg::Any::ReplRes,
inner_type: msg::Any::ReplReadlineRes,
..Default::default()
},
))
Expand Down
38 changes: 15 additions & 23 deletions src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,23 @@ impl DenoRepl {

fn load_history(&mut self) -> () {
debug!("Loading history file: {:?}", self.history_file);
if self
self
.editor
.load_history(&self.history_file.to_str().unwrap())
.is_err()
{}
.map_err(|e| debug!("Unable to load history file: {:?} {}", self.history_file, e))
// ignore this error (e.g. it occurs on first load)
.unwrap_or(())
}

fn save_history(&mut self) -> () {
match self
fn save_history(&mut self) -> DenoResult<()> {
self
.editor
.save_history(&self.history_file.to_str().unwrap())
{
Ok(_val) => debug!(
"Saved history file to: {:?}",
self.history_file
),
Err(e) => eprintln!(
"Unable to save history file: {:?} {}",
self.history_file,
e
),
};
.map(|_| debug!("Saved history file to: {:?}", self.history_file))
.map_err(|e| {
eprintln!("Unable to save history file: {:?} {}", self.history_file, e);
deno_error(ErrorKind::Other, e.description().to_string())
})
}

pub fn readline(&mut self) -> DenoResult<String> {
Expand All @@ -67,19 +62,16 @@ impl DenoRepl {
self.editor.add_history_entry(line.as_ref());
line
})
.map_err(|err| match err {
Interrupted => { self.save_history(); exit(1) },
err => err,
})
.map_err(|err| {
deno_error(ErrorKind::Other, err.description().to_string())
.map_err(|e| match e {
Interrupted => { self.save_history().unwrap(); exit(1) },
e => deno_error(ErrorKind::Other, e.description().to_string()),
})
}
}

impl Drop for DenoRepl {
fn drop(&mut self) {
self.save_history()
self.save_history().unwrap();
}
}

Expand Down
4 changes: 2 additions & 2 deletions tools/repl_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ def __init__(self, deno_exe):
self.deno_exe = deno_exe

def run(self, *lines, **kwargs):
exit = kwargs.pop("exit", True)
exit_ = kwargs.pop("exit", True)
p = Popen([self.deno_exe], stdout=PIPE, stderr=PIPE, stdin=PIPE)
try:
for line in lines:
p.stdin.write(line.encode("utf-8") + b'\n')
if exit:
if exit_:
p.stdin.write(b'deno.exit(0)\n')
else:
sleep(1) # wait to be killed by js
Expand Down

0 comments on commit e7e1676

Please sign in to comment.