-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Running repl from js side. Add tests for repl behavior. Handle ctrl-C and ctrl-D.
- Loading branch information
Showing
12 changed files
with
269 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2018 the Deno authors. All rights reserved. MIT license. | ||
import * as msg from "gen/msg_generated"; | ||
import { flatbuffers } from "flatbuffers"; | ||
import { assert } from "./util"; | ||
import * as deno from "./deno"; | ||
import * as dispatch from "./dispatch"; | ||
import { exit } from "./os"; | ||
import { window } from "./globals"; | ||
|
||
// @internal | ||
export async function readline(prompt: string): Promise<string> { | ||
return res(await dispatch.sendAsync(...req(prompt))); | ||
} | ||
|
||
function req( | ||
prompt: string | ||
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] { | ||
const builder = new flatbuffers.Builder(); | ||
const prompt_ = builder.createString(prompt); | ||
msg.Repl.startRepl(builder); | ||
msg.Repl.addPrompt(builder, prompt_); | ||
const inner = msg.Repl.endRepl(builder); | ||
return [builder, msg.Any.Repl, inner]; | ||
} | ||
|
||
function res(baseRes: null | msg.Base): string { | ||
assert(baseRes != null); | ||
assert(msg.Any.ReplRes === baseRes!.innerType()); | ||
const inner = new msg.ReplRes(); | ||
assert(baseRes!.inner(inner) != null); | ||
const line = inner.line(); | ||
assert(line !== null); | ||
return line || ""; | ||
} | ||
|
||
// @internal | ||
export async function replLoop(): Promise<void> { | ||
window.deno = deno; // FIXME use a new scope (rather than window). | ||
let line = ""; | ||
while(true){ | ||
try { | ||
line = await readline(">> "); | ||
line = line.trim(); | ||
} catch(err) { | ||
if (err.message === "EOF") { break; } | ||
console.error(err); | ||
exit(1); | ||
} | ||
if (!line) { continue; } | ||
if (line === ".exit") { break; } | ||
try { | ||
const result = eval.call(window, line); // FIXME use a new scope. | ||
console.log(result); | ||
} catch (err) { | ||
if (err instanceof Error) { | ||
console.error(`${err.constructor.name}: ${err.message}`); | ||
} else { | ||
console.error("Thrown:", err); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2018 the Deno authors. All rights reserved. MIT license. | ||
extern crate rustyline; | ||
use rustyline::Editor; | ||
use rustyline::error::ReadlineError::Interrupted; | ||
|
||
use std::error::Error; | ||
use msg::ErrorKind; | ||
|
||
use errors::DenoResult; | ||
use errors::new as deno_error; | ||
use std::process::exit; | ||
|
||
const HISTORY_FILE: &str = "history.txt"; | ||
|
||
pub fn readline(prompt: &String) -> DenoResult<String> { | ||
// TODO instantiate the editor once only (for the session). | ||
let mut editor = start_repl(); | ||
editor | ||
.readline(prompt) | ||
.map(|line| { | ||
editor.add_history_entry(line.as_ref()); | ||
// TODO We'd rather save the history only upon close, | ||
// but atm we're instantiating a new editor each readline. | ||
editor.save_history(HISTORY_FILE).unwrap(); | ||
line | ||
}) | ||
.map_err(|err| | ||
match err { | ||
Interrupted => exit(1), | ||
err => err | ||
}) | ||
.map_err(|err| deno_error(ErrorKind::Other, err.description().to_string())) | ||
} | ||
|
||
fn start_repl() -> Editor<()> { | ||
let mut editor = Editor::<()>::new(); | ||
if editor.load_history(HISTORY_FILE).is_err() { | ||
eprintln!("No repl history found, creating new file: {}", HISTORY_FILE); | ||
} | ||
editor | ||
} |
Submodule third_party
updated
207 files
Oops, something went wrong.