From 43ac8960d85d5a1c44cedb621fe17ec62ab681d4 Mon Sep 17 00:00:00 2001 From: Andy Hayden Date: Mon, 22 Oct 2018 22:42:34 -0700 Subject: [PATCH] clean up --- js/repl.ts | 6 +++--- src/msg.fbs | 4 ++-- src/ops.rs | 14 +++++++------- src/repl.rs | 38 +++++++++++++++----------------------- tools/repl_test.py | 4 ++-- 5 files changed, 29 insertions(+), 37 deletions(-) diff --git a/js/repl.ts b/js/repl.ts index 9b6c3dfba45832..04f5bdcdc79b74 100644 --- a/js/repl.ts +++ b/js/repl.ts @@ -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); @@ -69,7 +69,7 @@ function res(baseRes: null | msg.Base): string { export async function replLoop(): Promise { window.deno = deno; // FIXME use a new scope (rather than window). - const replName = "repl"; + const replName = "deno"; const prompt = ">> "; startRepl(replName, prompt); diff --git a/src/msg.fbs b/src/msg.fbs index 15024f5cacfdca..f51a4e43cb8bed 100644 --- a/src/msg.fbs +++ b/src/msg.fbs @@ -26,7 +26,7 @@ union Any { ReplStart, ReplExit, ReplReadline, - ReplRes, + ReplReadlineRes, Symlink, Stat, StatRes, @@ -268,7 +268,7 @@ table ReplReadline { name: string; } -table ReplRes { +table ReplReadlineRes { line: string; } diff --git a/src/ops.rs b/src/ops.rs index f545c8456d9206..d1f5afa9dcbaa1 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -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, @@ -1117,7 +1117,7 @@ fn op_repl_exit( ok_future(empty_buf()) } -fn op_repl( +fn op_repl_readline( state: Arc, base: &msg::Base, data: &'static mut [u8], @@ -1128,7 +1128,7 @@ 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(); @@ -1136,9 +1136,9 @@ fn op_repl( 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() }, @@ -1148,7 +1148,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() }, )) diff --git a/src/repl.rs b/src/repl.rs index 4debd255206d16..a029873851f5bd 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -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 { @@ -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(); } } diff --git a/tools/repl_test.py b/tools/repl_test.py index 2e3424ac6d92ae..2444ad0847137f 100644 --- a/tools/repl_test.py +++ b/tools/repl_test.py @@ -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