-
Notifications
You must be signed in to change notification settings - Fork 79
feat(async-rewriter2): add uncatchable exception support #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/async-rewriter2/src/stages/uncatchable-exceptions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import * as babel from '@babel/core'; | ||
import * as BabelTypes from '@babel/types'; | ||
|
||
/** | ||
* In this step, we transform try/catch statements so that there are specific | ||
* types of exceptions that they cannot catch (marked by | ||
* Symbol.for('@@mongosh.uncatchable')) or run finally blocks for. | ||
*/ | ||
export default ({ types: t }: { types: typeof BabelTypes }): babel.PluginObj<{}> => { | ||
// We mark already-visited try/catch statements using these symbols. | ||
function asNodeKey(v: any): keyof babel.types.Node { return v; } | ||
const isGeneratedTryCatch = asNodeKey(Symbol('isGeneratedTryCatch')); | ||
const notUncatchableCheck = babel.template.expression(` | ||
(!ERR_IDENTIFIER || !ERR_IDENTIFIER[Symbol.for('@@mongosh.uncatchable')]) | ||
`); | ||
|
||
return { | ||
visitor: { | ||
TryStatement(path) { | ||
if (path.node[isGeneratedTryCatch]) return; | ||
const { block, finalizer } = path.node; | ||
let catchParam: babel.types.Identifier; | ||
let handler: babel.types.CatchClause; | ||
const fallbackCatchParam = path.scope.generateUidIdentifier('err'); | ||
|
||
if (path.node.handler) { | ||
if (path.node.handler.param?.type === 'Identifier') { | ||
// Classic catch(err) { ... }. We're good, no need to change anything. | ||
catchParam = path.node.handler.param; | ||
handler = path.node.handler; | ||
} else if (path.node.handler.param) { | ||
// Destructuring catch({ ... }) { ... body ... }. Transform to | ||
// catch(err) { let ... = err; ... body ... }. | ||
catchParam = fallbackCatchParam; | ||
handler = t.catchClause(catchParam, t.blockStatement([ | ||
t.variableDeclaration('let', [ | ||
t.variableDeclarator(path.node.handler.param, catchParam) | ||
]), | ||
path.node.handler.body | ||
])); | ||
} else { | ||
// | ||
catchParam = fallbackCatchParam; | ||
handler = path.node.handler; | ||
} | ||
} else { | ||
// try {} finally {} without 'catch' is valid -- if we encounter that, | ||
// pretend that there is a dummy catch (err) { throw err; } | ||
catchParam = fallbackCatchParam; | ||
handler = t.catchClause(catchParam, t.blockStatement([ | ||
t.throwStatement(catchParam) | ||
])); | ||
} | ||
|
||
if (!finalizer) { | ||
// No finalizer -> no need to keep track of state outside the catch {} | ||
// block itself. This is a bit simpler. | ||
path.replaceWith(Object.assign( | ||
t.tryStatement( | ||
block, | ||
t.catchClause( | ||
catchParam, | ||
t.blockStatement([ | ||
// if (!err[isUncatchableSymbol]) { ... } else throw err; | ||
t.ifStatement( | ||
notUncatchableCheck({ ERR_IDENTIFIER: catchParam }), | ||
handler.body, | ||
t.throwStatement(catchParam)) | ||
]) | ||
) | ||
), | ||
{ [isGeneratedTryCatch]: true })); | ||
} else { | ||
// finalizer -> need to store whether the exception was catchable | ||
// (i.e. whether the finalizer is allowed to run) outside of the | ||
// try/catch/finally block. | ||
const isCatchable = path.scope.generateUidIdentifier('_isCatchable'); | ||
path.replaceWithMultiple([ | ||
t.variableDeclaration('let', [t.variableDeclarator(isCatchable)]), | ||
Object.assign( | ||
t.tryStatement( | ||
block, | ||
t.catchClause( | ||
catchParam, | ||
t.blockStatement([ | ||
// isCatchable = !err[isUncatchableSymbol] | ||
t.expressionStatement( | ||
t.assignmentExpression('=', | ||
isCatchable, | ||
notUncatchableCheck({ ERR_IDENTIFIER: catchParam }))), | ||
// if (isCatchable) { ... } else throw err; | ||
t.ifStatement(isCatchable, handler.body, t.throwStatement(catchParam)) | ||
]), | ||
), | ||
t.blockStatement([ | ||
// if (isCatchable) { ... } | ||
t.ifStatement(isCatchable, finalizer) | ||
]) | ||
), | ||
{ [isGeneratedTryCatch]: true }) | ||
]); | ||
} | ||
} | ||
} | ||
}; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think for the sake of correctness there should be the throw in the else branch