1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-18 03:44:05 -05:00

REPL unblock event loop AND fix REPL setTimeout fire problems

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2018-11-28 01:07:22 -08:00 committed by Ryan Dahl
parent 89096c9210
commit 09aa9b9698
3 changed files with 26 additions and 10 deletions

View file

@ -28,7 +28,7 @@ function startRepl(historyFile: string): number {
} }
// @internal // @internal
export function readline(rid: number, prompt: string): string { export async function readline(rid: number, prompt: string): Promise<string> {
const builder = flatbuffers.createBuilder(); const builder = flatbuffers.createBuilder();
const prompt_ = builder.createString(prompt); const prompt_ = builder.createString(prompt);
msg.ReplReadline.startReplReadline(builder); msg.ReplReadline.startReplReadline(builder);
@ -36,8 +36,11 @@ export function readline(rid: number, prompt: string): string {
msg.ReplReadline.addPrompt(builder, prompt_); msg.ReplReadline.addPrompt(builder, prompt_);
const inner = msg.ReplReadline.endReplReadline(builder); const inner = msg.ReplReadline.endReplReadline(builder);
// TODO use async? const baseRes = await dispatch.sendAsync(
const baseRes = dispatch.sendSync(builder, msg.Any.ReplReadline, inner); builder,
msg.Any.ReplReadline,
inner
);
assert(baseRes != null); assert(baseRes != null);
assert(msg.Any.ReplReadlineRes === baseRes!.innerType()); assert(msg.Any.ReplReadlineRes === baseRes!.innerType());
@ -49,7 +52,7 @@ export function readline(rid: number, prompt: string): string {
} }
// @internal // @internal
export function replLoop(): void { export async function replLoop(): Promise<void> {
window.deno = deno; // FIXME use a new scope (rather than window). window.deno = deno; // FIXME use a new scope (rather than window).
const historyFile = "deno_history.txt"; const historyFile = "deno_history.txt";
@ -58,7 +61,7 @@ export function replLoop(): void {
let code = ""; let code = "";
while (true) { while (true) {
try { try {
code = readBlock(rid, "> ", " "); code = await readBlock(rid, "> ", " ");
} catch (err) { } catch (err) {
if (err.message === "EOF") { if (err.message === "EOF") {
break; break;
@ -91,14 +94,14 @@ function evaluate(code: string): void {
} }
} }
function readBlock( async function readBlock(
rid: number, rid: number,
prompt: string, prompt: string,
continuedPrompt: string continuedPrompt: string
): string { ): Promise<string> {
let code = ""; let code = "";
do { do {
code += readline(rid, prompt); code += await readline(rid, prompt);
prompt = continuedPrompt; prompt = continuedPrompt;
} while (parenthesesAreOpen(code)); } while (parenthesesAreOpen(code));
return code; return code;

View file

@ -1121,7 +1121,7 @@ fn op_repl_readline(
// Ignore this clippy warning until this issue is addressed: // Ignore this clippy warning until this issue is addressed:
// https://github.com/rust-lang-nursery/rust-clippy/issues/1684 // https://github.com/rust-lang-nursery/rust-clippy/issues/1684
#[cfg_attr(feature = "cargo-clippy", allow(redundant_closure_call))] #[cfg_attr(feature = "cargo-clippy", allow(redundant_closure_call))]
Box::new(futures::future::result((move || { blocking!(base.sync(), || -> OpResult {
let line = resources::readline(rid, &prompt)?; let line = resources::readline(rid, &prompt)?;
let builder = &mut FlatBufferBuilder::new(); let builder = &mut FlatBufferBuilder::new();
@ -1141,7 +1141,7 @@ fn op_repl_readline(
..Default::default() ..Default::default()
}, },
)) ))
})())) })
} }
fn op_truncate( fn op_truncate(

View file

@ -86,6 +86,19 @@ class Repl(object):
assertEqual(err, '') assertEqual(err, '')
assertEqual(code, 0) assertEqual(code, 0)
def test_set_timeout(self):
# Special treatment
p = Popen([self.deno_exe], stdout=PIPE, stderr=PIPE, stdin=PIPE)
# Print after 0.1 second
p.stdin.write(
"setTimeout(() => console.log('HI'), 100)\n".encode("utf-8"))
sleep(0.2) # Wait 0.2 second before proceed
out, err = p.communicate()
code = p.poll()
assertEqual(out.replace('\r\n', '\n'), '1\nHI\n')
assertEqual(err.replace('\r\n', '\n'), '')
assertEqual(code, 0)
def test_exit_command(self): def test_exit_command(self):
out, err, code = self.input(".exit", "'ignored'", exit=False) out, err, code = self.input(".exit", "'ignored'", exit=False)
assertEqual(out, '') assertEqual(out, '')