diff --git a/js/compiler.ts b/js/compiler.ts index 0462334429b9fe..56dec9dd663478 100644 --- a/js/compiler.ts +++ b/js/compiler.ts @@ -469,6 +469,31 @@ export class DenoCompiler return moduleMetaData.outputCode; } + /** + * TODO: + * Incremental compilation of code used in REPL. + */ + incrementalCompile( + sourceCode: SourceCode, + previousOutput?: OutputCode + ): { diagnostics: ts.Diagnostic[], outputCode: OutputCode, additionalCode: OutputCode } { + + console.log('incrementalCompile', sourceCode, previousOutput); + + // const service = this._service; + + // TODO: + // 1. transpile code to JS + // 2. diff code with 'previousOutput' + // 3. return new lines and diagnostics + + return { + diagnostics: [], + outputCode: sourceCode, + additionalCode: '', + }; + } + /** Given a fileName, return what was generated by the compiler. */ getGeneratedContents = (fileName: string): string | RawSourceMap => { this._log("compiler.getGeneratedContents", fileName); diff --git a/js/main.ts b/js/main.ts index 176f098c760de6..d945f41518cf26 100644 --- a/js/main.ts +++ b/js/main.ts @@ -96,6 +96,6 @@ export default function denoMain() { if (inputFn) { compiler.run(inputFn, `${cwd}/`); } else { - replLoop(); + replLoop(`${cwd}/`); } } diff --git a/js/repl.ts b/js/repl.ts index 3139330f6bf696..bf4b74263d9d6f 100644 --- a/js/repl.ts +++ b/js/repl.ts @@ -7,6 +7,7 @@ import { close } from "./files"; import * as dispatch from "./dispatch"; import { exit } from "./os"; import { window } from "./globals"; +import { DenoCompiler } from "./compiler"; function startRepl(historyFile: string): number { const builder = flatbuffers.createBuilder(); @@ -46,6 +47,21 @@ export function readline(rid: number, prompt: string): string { return line || ""; } + +interface ReplContext { + lines: string[]; + previousOutput: string; +} + +/** + * Eval helpers. + */ +// const EVAL_FILENAME = `[eval].ts` +const REPL_CONTEXT: ReplContext = { + lines: [], + previousOutput: '', +}; + // @internal export function replLoop(): void { window.deno = deno; // FIXME use a new scope (rather than window). @@ -78,8 +94,17 @@ export function replLoop(): void { function evaluate(code: string): void { try { - const result = eval.call(window, code); // FIXME use a new scope. + // TODO: + // 1. preserve context between calls + // 2. run only incremental lines, not full output + // 3. output diagnostics + REPL_CONTEXT.lines.push(code); + const compiledCode = compileReplCode(REPL_CONTEXT); + console.log('compiledCode', compiledCode); + + const result = eval.call(window, compiledCode.outputCode); // FIXME use a new scope. console.log(result); + REPL_CONTEXT.previousOutput = compiledCode.outputCode; } catch (err) { if (err instanceof Error) { console.error(`${err.constructor.name}: ${err.message}`); @@ -89,6 +114,13 @@ function evaluate(code: string): void { } } +const compiler = DenoCompiler.instance(); + +function compileReplCode(context: ReplContext) { + const sourceCode = context.lines.join('\n'); + return compiler.incrementalCompile(sourceCode, context.previousOutput); +} + function readBlock( rid: number, prompt: string,