summaryrefslogtreecommitdiff
path: root/js/repl.ts
blob: 6676721fc4e1f468b159229d89eb328b2d49ba63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2018-2019 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 { globalEval } from "./global_eval";
import { libdeno } from "./libdeno";
import { formatError } from "./format_error";

const window = globalEval("this");

const helpMsg = [
  "exit    Exit the REPL",
  "help    Print this help message"
].join("\n");

const replCommands = {
  exit: {
    get() {
      exit(0);
    }
  },
  help: {
    get() {
      return helpMsg;
    }
  }
};

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 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);

  const baseRes = await dispatch.sendAsync(
    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 async function replLoop(): Promise<void> {
  window.deno = deno; // FIXME use a new scope (rather than window).
  Object.defineProperties(window, replCommands);

  const historyFile = "deno_history.txt";
  const rid = startRepl(historyFile);

  let code = "";
  while (true) {
    try {
      code = await readBlock(rid, "> ", "  ");
    } catch (err) {
      if (err.message === "EOF") {
        break;
      }
      console.error(err);
      exit(1);
    }

    evaluate(code);
  }

  close(rid);
}

function evaluate(code: string): void {
  if (code.trim() === "") {
    return;
  }
  const [result, errInfo] = libdeno.evalContext(code);
  if (!errInfo) {
    console.log(result);
  } else {
    if (errInfo.isNativeError) {
      const formattedError = formatError(
        libdeno.errorToJSON(errInfo.thrown as Error));
      console.error(formattedError);
    } else {
      console.error("Thrown:", errInfo.thrown);
    }
  }
}

async function readBlock(
  rid: number,
  prompt: string,
  continuedPrompt: string
): Promise<string> {
  let code = "";
  do {
    code += await readline(rid, prompt);
    prompt = continuedPrompt;
  } while (parenthesesAreOpen(code));
  return code;
}

// modified from
// https://codereview.stackexchange.com/a/46039/148556
function parenthesesAreOpen(code: string): boolean {
  const parentheses = "[]{}()";
  const stack = [];

  for (const ch of code) {
    const bracePosition = parentheses.indexOf(ch);

    if (bracePosition === -1) {
      // not a paren
      continue;
    }

    if (bracePosition % 2 === 0) {
      stack.push(bracePosition + 1); // push next expected brace position
    } else {
      if (stack.pop() !== bracePosition) {
        return false;
      }
    }
  }
  return stack.length > 0;
}