Skip to content

Commit

Permalink
REPL unblock event loop AND fix REPL setTimeout fire problems
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkassimo authored and ry committed Nov 28, 2018
1 parent 89096c9 commit 09aa9b9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
19 changes: 11 additions & 8 deletions js/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,19 @@ function startRepl(historyFile: string): number {
}

// @internal
export function readline(rid: number, prompt: string): string {
export async function readline(rid: number, prompt: string): Promise<string> {
const builder = flatbuffers.createBuilder();
const prompt_ = builder.createString(prompt);
msg.ReplReadline.startReplReadline(builder);
msg.ReplReadline.addRid(builder, rid);
msg.ReplReadline.addPrompt(builder, prompt_);
const inner = msg.ReplReadline.endReplReadline(builder);

// TODO use async?
const baseRes = dispatch.sendSync(builder, msg.Any.ReplReadline, inner);
const baseRes = await dispatch.sendAsync(
builder,
msg.Any.ReplReadline,
inner
);

assert(baseRes != null);
assert(msg.Any.ReplReadlineRes === baseRes!.innerType());
Expand All @@ -49,7 +52,7 @@ export function readline(rid: number, prompt: string): string {
}

// @internal
export function replLoop(): void {
export async function replLoop(): Promise<void> {
window.deno = deno; // FIXME use a new scope (rather than window).

const historyFile = "deno_history.txt";
Expand All @@ -58,7 +61,7 @@ export function replLoop(): void {
let code = "";
while (true) {
try {
code = readBlock(rid, "> ", " ");
code = await readBlock(rid, "> ", " ");
} catch (err) {
if (err.message === "EOF") {
break;
Expand Down Expand Up @@ -91,14 +94,14 @@ function evaluate(code: string): void {
}
}

function readBlock(
async function readBlock(
rid: number,
prompt: string,
continuedPrompt: string
): string {
): Promise<string> {
let code = "";
do {
code += readline(rid, prompt);
code += await readline(rid, prompt);
prompt = continuedPrompt;
} while (parenthesesAreOpen(code));
return code;
Expand Down
4 changes: 2 additions & 2 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ fn op_repl_readline(
// Ignore this clippy warning until this issue is addressed:
// https://github.com/rust-lang-nursery/rust-clippy/issues/1684
#[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 builder = &mut FlatBufferBuilder::new();
Expand All @@ -1141,7 +1141,7 @@ fn op_repl_readline(
..Default::default()
},
))
})()))
})
}

fn op_truncate(
Expand Down
13 changes: 13 additions & 0 deletions tools/repl_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ def test_multiline(self):
assertEqual(err, '')
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):
out, err, code = self.input(".exit", "'ignored'", exit=False)
assertEqual(out, '')
Expand Down

0 comments on commit 09aa9b9

Please sign in to comment.