Skip to content

Commit

Permalink
fix(metering): properly transform try/catch/finally
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Mar 2, 2020
1 parent a42cfec commit 6fd28ae
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
11 changes: 6 additions & 5 deletions packages/transform-metering/src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,15 @@ const ${reid}=RegExp(${JSON.stringify(pattern)},${JSON.stringify(flags)});`);
path.node.body = wrapWithComputeMeter(blockify(path.node.body));
},
// To prevent interception after exhaustion, wrap catch and finally.
CatchClause(path) {
path.node.body = wrapWithComputeMeter(path.node.body);
},
TryStatement(path) {
if (path.node.handler) {
path.node.handler.body = wrapWithComputeMeter(path.node.handler.body);
if (path.node.handler && !t.isCatchClause(path.node.handler)) {
path.node.handler = wrapWithComputeMeter(path.node.handler);
}
if (path.node.finalizer && !path.node.finalizer[METER_GENERATED]) {
path.node.finalizer.body = wrapWithComputeMeter(
path.node.finalizer.body,
);
path.node.finalizer = wrapWithComputeMeter(path.node.finalizer);
}
},
// Function definitions need a stack meter, too.
Expand Down
17 changes: 8 additions & 9 deletions packages/transform-metering/test/test-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@ test('meter transform', async t => {
`${base}/${testDir}/source.js`,
'utf8',
);
const rewritten = await fs.promises.readFile(
`${base}/${testDir}/rewrite.js`,
'utf8',
);
t.equals(
rewrite(src.trimRight(), testDir),
rewritten.trimRight(),
`rewrite ${testDir}`,
);
const rewritten = await fs.promises
.readFile(`${base}/${testDir}/rewrite.js`, 'utf8')
.catch(_ => undefined);
const transformed = rewrite(src.trimRight(), testDir);
if (rewritten === undefined) {
console.log(transformed);
}
t.equals(transformed, rewritten.trimRight(), `rewrite ${testDir}`);
}
} catch (e) {
t.isNot(e, e, 'unexpected exception');
Expand Down
34 changes: 34 additions & 0 deletions packages/transform-metering/testdata/try-blocks/rewrite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const $m=getMeter();$m&&$m.e();try{try {
doit();
} catch (e) {$m && $m.c();
foo(e);
}

try {
doit();
} finally {$m && $m.c();
bar();
}

try {
doit();
} catch {$m && $m.c();
bar();
}

try {
doit();
} catch (e) {$m && $m.c();
foo(e);
} finally {$m && $m.c();
bar();
}

try {
doit();
} catch {$m && $m.c();
foo();
} finally {$m && $m.c();
bar();
}
}finally{$m && $m.l();}
33 changes: 33 additions & 0 deletions packages/transform-metering/testdata/try-blocks/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
try {
doit();
} catch (e) {
foo(e);
}

try {
doit();
} finally {
bar();
}

try {
doit();
} catch {
bar();
}

try {
doit();
} catch (e) {
foo(e);
} finally {
bar();
}

try {
doit();
} catch {
foo();
} finally {
bar();
}

0 comments on commit 6fd28ae

Please sign in to comment.