-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
13 changed files
with
505 additions
and
13 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
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,89 @@ | ||
// Copyright 2018 the Deno authors. All rights reserved. MIT license. | ||
import * as msg from "gen/msg_generated"; | ||
import * as flatbuffers from "./flatbuffers"; | ||
import { assert } from "./util"; | ||
import * as deno from "./deno"; | ||
import { close } from "./files"; | ||
import * as dispatch from "./dispatch"; | ||
import { exit } from "./os"; | ||
import { window } from "./globals"; | ||
|
||
function startRepl(historyFile: string): number { | ||
const builder = flatbuffers.createBuilder(); | ||
const historyFile_ = builder.createString(historyFile); | ||
|
||
msg.ReplStart.startReplStart(builder); | ||
msg.ReplStart.addHistoryFile(builder, historyFile_); | ||
const inner = msg.ReplStart.endReplStart(builder); | ||
|
||
const baseRes = dispatch.sendSync(builder, msg.Any.ReplStart, inner); | ||
assert(baseRes != null); | ||
assert(msg.Any.ReplStartRes === baseRes!.innerType()); | ||
const innerRes = new msg.ReplStartRes(); | ||
assert(baseRes!.inner(innerRes) != null); | ||
const rid = innerRes.rid(); | ||
return rid; | ||
} | ||
|
||
// @internal | ||
export function readline(rid: number, prompt: string): 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); | ||
|
||
assert(baseRes != null); | ||
assert(msg.Any.ReplReadlineRes === baseRes!.innerType()); | ||
const innerRes = new msg.ReplReadlineRes(); | ||
assert(baseRes!.inner(innerRes) != null); | ||
const line = innerRes.line(); | ||
assert(line !== null); | ||
return line || ""; | ||
} | ||
|
||
// @internal | ||
export function replLoop(): void { | ||
window.deno = deno; // FIXME use a new scope (rather than window). | ||
|
||
const historyFile = "deno_history.txt"; | ||
const prompt = "> "; | ||
|
||
const rid = startRepl(historyFile); | ||
|
||
let line = ""; | ||
while (true) { | ||
try { | ||
line = readline(rid, prompt); | ||
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); | ||
} | ||
} | ||
} | ||
|
||
close(rid); | ||
} |
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
Oops, something went wrong.