summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2018-11-28 01:07:22 -0800
committerRyan Dahl <ry@tinyclouds.org>2018-11-28 15:25:30 -0800
commit09aa9b9698dd7646029551b8e7cd4f5b67a81b31 (patch)
tree94df088853ce58e885f82f40aadf2fc4be3d360e
parent89096c92104ab89b1fd8ba273cbbf441dcfe5a84 (diff)
REPL unblock event loop AND fix REPL setTimeout fire problems
-rw-r--r--js/repl.ts19
-rw-r--r--src/ops.rs4
-rw-r--r--tools/repl_test.py13
3 files changed, 26 insertions, 10 deletions
diff --git a/js/repl.ts b/js/repl.ts
index ae457e8e4..075affc10 100644
--- a/js/repl.ts
+++ b/js/repl.ts
@@ -28,7 +28,7 @@ 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);
@@ -36,8 +36,11 @@ export function readline(rid: number, prompt: string): string {
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());
@@ -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";
@@ -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;
@@ -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;
diff --git a/src/ops.rs b/src/ops.rs
index e08e67705..f8e3c9dc1 100644
--- a/src/ops.rs
+++ b/src/ops.rs
@@ -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();
@@ -1141,7 +1141,7 @@ fn op_repl_readline(
..Default::default()
},
))
- })()))
+ })
}
fn op_truncate(
diff --git a/tools/repl_test.py b/tools/repl_test.py
index db51ec511..9969e9804 100644
--- a/tools/repl_test.py
+++ b/tools/repl_test.py
@@ -86,6 +86,19 @@ class Repl(object):
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, '')