diff --git a/packages/transform-metering/src/transform.js b/packages/transform-metering/src/transform.js index a3ca1ad6cc1..eef8451d170 100644 --- a/packages/transform-metering/src/transform.js +++ b/packages/transform-metering/src/transform.js @@ -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. diff --git a/packages/transform-metering/test/test-transform.js b/packages/transform-metering/test/test-transform.js index 4d8758df796..46b4287efa6 100644 --- a/packages/transform-metering/test/test-transform.js +++ b/packages/transform-metering/test/test-transform.js @@ -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'); diff --git a/packages/transform-metering/testdata/try-blocks/rewrite.js b/packages/transform-metering/testdata/try-blocks/rewrite.js new file mode 100644 index 00000000000..2092e8103d0 --- /dev/null +++ b/packages/transform-metering/testdata/try-blocks/rewrite.js @@ -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();} diff --git a/packages/transform-metering/testdata/try-blocks/source.js b/packages/transform-metering/testdata/try-blocks/source.js new file mode 100644 index 00000000000..d9bbdacd803 --- /dev/null +++ b/packages/transform-metering/testdata/try-blocks/source.js @@ -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(); +} \ No newline at end of file