-
Notifications
You must be signed in to change notification settings - Fork 12
/
compiler.ts
119 lines (107 loc) · 3.96 KB
/
compiler.ts
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
/**
* This is the entrypoint for stopify-full.bundle.js. A page that includes
* this entrypoint can compile programs in the browser.
*/
import * as babylon from 'babylon';
import { RawSourceMap } from 'source-map';
import { CompilerOpts, RuntimeOpts, AsyncRun, AsyncEval, Error } from '../types';
import { Runtime, Result } from 'stopify-continuations';
import { AbstractRunner, EventProcessingMode } from '../runtime/abstractRunner';
import { compileFromAst } from '../compiler/compiler';
import { checkAndFillCompilerOpts } from 'stopify-continuations-compiler/dist/src/compiler/check-compiler-opts';
import { checkAndFillRuntimeOpts } from '../runtime/check-runtime-opts';
import { getSourceMap } from 'stopify-continuations-compiler';
import * as t from 'babel-types';
// We need to provide these for stopify-continuations
export * from 'stopify-continuations/dist/runtime/runtime';
export * from 'stopify-continuations-compiler/dist/src/runtime/implicitApps';
export { knownBuiltIns } from 'stopify-continuations-compiler/dist/src/common/cannotCapture';
export { Result } from 'stopify-continuations';
export { AsyncRun, AsyncEval, Error, CompilerOpts, RuntimeOpts };
import * as compiler from '../stopify/compileFunction';
export { compiler };
let runner : Runner | undefined;
function copyCompilerOpts(compileOpts: CompilerOpts): CompilerOpts {
return {
compileFunction: compileOpts.compileFunction,
getters: compileOpts.getters,
debug: compileOpts.debug,
captureMethod: compileOpts.captureMethod,
newMethod: compileOpts.newMethod,
eval: compileOpts.eval,
es: compileOpts.es,
hofs: compileOpts.hofs,
jsArgs: compileOpts.jsArgs,
requireRuntime: compileOpts.requireRuntime,
sourceMap: compileOpts.sourceMap,
onDone: compileOpts.onDone,
eval2: compileOpts.eval2,
compileMode: compileOpts.compileMode
};
}
class Runner extends AbstractRunner {
private evalOpts: CompilerOpts;
constructor(private code: string,
compilerOpts: CompilerOpts,
runtimeOpts: RuntimeOpts) {
super(runtimeOpts);
this.evalOpts = copyCompilerOpts(compilerOpts);
this.evalOpts.eval2 = true;
}
run(onDone: (result: Result) => void,
onYield?: () => void,
onBreakpoint?: (line: number) => void) {
this.runInit(onDone, onYield, onBreakpoint);
eval.call(global, this.code);
}
evalAsyncFromAst(ast: t.Program, onDone: (result: Result) => void): void {
const stopifiedCode = compileFromAst(ast, this.evalOpts);
this.eventMode = EventProcessingMode.Running;
this.continuationsRTS.runtime(eval(stopifiedCode), onDone);
}
evalAsync(src: string, onDone: (result: Result) => void): void {
this.evalAsyncFromAst(babylon.parse(src).program, onDone);
}
}
/**
* Called by the stopified program to get suspend() and other functions.
*/
export function init(rts: Runtime): AsyncRun {
if (runner === undefined) {
throw new Error('stopify not called');
}
return runner.init(rts);
}
export function stopifyLocallyFromAst(
src: t.Program,
sourceMap?: RawSourceMap,
optionalCompileOpts?: Partial<CompilerOpts>,
optionalRuntimeOpts?: Partial<RuntimeOpts>): (AsyncRun & AsyncEval) | Error {
try {
const compileOpts = checkAndFillCompilerOpts(optionalCompileOpts || {},
sourceMap);
const runtimeOpts = checkAndFillRuntimeOpts(optionalRuntimeOpts || {});
const stopifiedCode = compileFromAst(src, compileOpts);
runner = new Runner(stopifiedCode, compileOpts, runtimeOpts);
return runner;
}
catch (exn) {
return { kind: 'error', exception: exn };
}
}
/**
* Control the execution of a pre-compiled program.
*
* @param url URL of a pre-compiled program
* @param opts runtime settings
*/
export function stopifyLocally(
src: string,
optionalCompileOpts?: Partial<CompilerOpts>,
optionalRuntimeOpts?: Partial<RuntimeOpts>): (AsyncRun & AsyncEval) | Error {
return stopifyLocallyFromAst(
babylon.parse(src).program,
getSourceMap(src),
optionalCompileOpts,
optionalRuntimeOpts);
}