Skip to content

Commit

Permalink
Merge pull request #487 from XmiliaH/fix-485
Browse files Browse the repository at this point in the history
Fix 485
  • Loading branch information
XmiliaH committed Nov 22, 2022
2 parents 9624295 + 12790ca commit 442feea
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lib/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ function transformer(args, body, isAsync, isGenerator, filename) {
const TO_RIGHT = 100;

let internStateValiable = undefined;
let tmpname = 'VM2_INTERNAL_TMPNAME';

acornWalkFull(ast, (node, state, type) => {
if (type === 'Function') {
Expand All @@ -112,15 +113,30 @@ function transformer(args, body, isAsync, isGenerator, filename) {
if (nodeType === 'CatchClause') {
const param = node.param;
if (param) {
const name = assertType(param, 'Identifier').name;
const cBody = assertType(node.body, 'BlockStatement');
if (cBody.body.length > 0) {
if (param.type === 'ObjectPattern') {
insertions.push({
__proto__: null,
pos: cBody.body[0].start,
pos: node.start,
order: TO_RIGHT,
code: `catch($tmpname){try{throw ${INTERNAL_STATE_NAME}.handleException($tmpname);}`
});
insertions.push({
__proto__: null,
pos: node.body.end,
order: TO_LEFT,
code: `${name}=${INTERNAL_STATE_NAME}.handleException(${name});`
code: `}`
});
} else {
const name = assertType(param, 'Identifier').name;
const cBody = assertType(node.body, 'BlockStatement');
if (cBody.body.length > 0) {
insertions.push({
__proto__: null,
pos: cBody.body[0].start,
order: TO_LEFT,
code: `${name}=${INTERNAL_STATE_NAME}.handleException(${name});`
});
}
}
}
} else if (nodeType === 'WithStatement') {
Expand All @@ -141,6 +157,8 @@ function transformer(args, body, isAsync, isGenerator, filename) {
if (internStateValiable === undefined || internStateValiable.start > node.start) {
internStateValiable = node;
}
} else if (node.name.startsWith(tmpname)) {
tmpname = node.name + '_UNIQUE';
}
} else if (nodeType === 'ImportExpression') {
insertions.push({
Expand Down Expand Up @@ -168,7 +186,7 @@ function transformer(args, body, isAsync, isGenerator, filename) {
let curr = 0;
for (let i = 0; i < insertions.length; i++) {
const change = insertions[i];
ncode += code.substring(curr, change.pos) + change.code;
ncode += code.substring(curr, change.pos) + change.code.replace(/\$tmpname/g, tmpname);
curr = change.pos;
}
ncode += code.substring(curr);
Expand Down

0 comments on commit 442feea

Please sign in to comment.