Skip to content

Commit

Permalink
Catch parse errors from babylon gracefully (#486)
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunguha authored Apr 17, 2021
1 parent 3ac8225 commit 10d6aa2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
17 changes: 17 additions & 0 deletions stopify/js/api.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require("../dist/stopify-full.bundle.js");

test('strict mode parse error from stopifyLocally', () => {
// The use strict makes the Babylon parser crash.
var runner = window.stopify.stopifyLocally(`"use strict"; function F(x,x) { }`);
expect(runner.kind).toBe('error');
});

test('strict mode parse error from evalAsync', (done) => {
// The use strict makes the Babylon parser crash.
var runner = window.stopify.stopifyLocally(``);
expect(runner.kind).toBe('ok');
runner.evalAsync(`"use strict"; function F(x,x) { }`, (result) => {
expect(result.type).toBe('exception');
done();
});
});
4 changes: 2 additions & 2 deletions stopify/js/stopify-webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const runtimeOnly = {
output: {
filename: 'dist/stopify.bundle.js',
library: 'stopify',
libraryTarget: 'var'
libraryTarget: 'window'
},
devtool: 'source-map',
node: {
Expand All @@ -18,7 +18,7 @@ const compilerAndRuntime = {
output: {
filename: 'dist/stopify-full.bundle.js',
library: 'stopify',
libraryTarget: 'var'
libraryTarget: 'window'
},
devtool: 'source-map',
node: {
Expand Down
3 changes: 2 additions & 1 deletion stopify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@
},
"jest": {
"roots": [
"<rootDir>/dist/test"
"<rootDir>/dist/test",
"<rootDir>/js"
]
}
}
24 changes: 21 additions & 3 deletions stopify/src/entrypoints/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from '@stopify/continuations/dist/src/runtime/implicitApps';
export { knownBuiltIns } from '@stopify/continuations/dist/src/common/cannotCapture';
export { Result } from '@stopify/continuations-runtime';
export { AsyncRun, AsyncEval, Error, CompilerOpts, RuntimeOpts };
import * as util from '@stopify/util';

import * as compiler from '../stopify/compileFunction';
export { compiler };
Expand Down Expand Up @@ -59,7 +60,7 @@ class Runner extends AbstractRunner {
this.runInit(onDone, onYield, onBreakpoint);
eval.call(global, this.code);
}

compile(src: string): string {
const ast = babylon.parse(src).program;
return compileFromAst(ast, this.evalOpts);
Expand All @@ -80,7 +81,11 @@ class Runner extends AbstractRunner {
}

evalAsync(src: string, onDone: (result: Result) => void): void {
this.evalAsyncFromAst(babylon.parse(src).program, onDone);
let ast = parseOrError(src);
if (ast.kind == 'error') {
return onDone({ type: 'exception', value: ast.exception, stack: [] });
}
this.evalAsyncFromAst(ast.value, onDone);
}
}

Expand Down Expand Up @@ -112,6 +117,15 @@ export function stopifyLocallyFromAst(
}
}

function parseOrError(src: string): { kind: 'program', value: t.Program } | Error {
try {
return { kind: 'program', value: babylon.parse(src).program };
}
catch (exn) {
return { kind: 'error', exception: exn };
}
}

/**
* Control the execution of a pre-compiled program.
*
Expand All @@ -122,8 +136,12 @@ export function stopifyLocally(
src: string,
optionalCompileOpts?: Partial<CompilerOpts>,
optionalRuntimeOpts?: Partial<RuntimeOpts>): (AsyncRun & AsyncEval) | Error {
let ast = parseOrError(src);
if (ast.kind === 'error') {
return ast;
}
return stopifyLocallyFromAst(
babylon.parse(src).program,
ast.value,
getSourceMap(src),
optionalCompileOpts,
optionalRuntimeOpts);
Expand Down

0 comments on commit 10d6aa2

Please sign in to comment.