@@ -103,6 +103,7 @@ function transformer(args, body, isAsync, isGenerator, filename) {
103
103
const TO_RIGHT = 100 ;
104
104
105
105
let internStateValiable = undefined ;
106
+ let tmpname = 'VM2_INTERNAL_TMPNAME' ;
106
107
107
108
acornWalkFull ( ast , ( node , state , type ) => {
108
109
if ( type === 'Function' ) {
@@ -112,15 +113,30 @@ function transformer(args, body, isAsync, isGenerator, filename) {
112
113
if ( nodeType === 'CatchClause' ) {
113
114
const param = node . param ;
114
115
if ( param ) {
115
- const name = assertType ( param , 'Identifier' ) . name ;
116
- const cBody = assertType ( node . body , 'BlockStatement' ) ;
117
- if ( cBody . body . length > 0 ) {
116
+ if ( param . type === 'ObjectPattern' ) {
118
117
insertions . push ( {
119
118
__proto__ : null ,
120
- pos : cBody . body [ 0 ] . start ,
119
+ pos : node . start ,
120
+ order : TO_RIGHT ,
121
+ code : `catch($tmpname){try{throw ${ INTERNAL_STATE_NAME } .handleException($tmpname);}`
122
+ } ) ;
123
+ insertions . push ( {
124
+ __proto__ : null ,
125
+ pos : node . body . end ,
121
126
order : TO_LEFT ,
122
- code : `${ name } = ${ INTERNAL_STATE_NAME } .handleException( ${ name } ); `
127
+ code : `} `
123
128
} ) ;
129
+ } else {
130
+ const name = assertType ( param , 'Identifier' ) . name ;
131
+ const cBody = assertType ( node . body , 'BlockStatement' ) ;
132
+ if ( cBody . body . length > 0 ) {
133
+ insertions . push ( {
134
+ __proto__ : null ,
135
+ pos : cBody . body [ 0 ] . start ,
136
+ order : TO_LEFT ,
137
+ code : `${ name } =${ INTERNAL_STATE_NAME } .handleException(${ name } );`
138
+ } ) ;
139
+ }
124
140
}
125
141
}
126
142
} else if ( nodeType === 'WithStatement' ) {
@@ -141,6 +157,8 @@ function transformer(args, body, isAsync, isGenerator, filename) {
141
157
if ( internStateValiable === undefined || internStateValiable . start > node . start ) {
142
158
internStateValiable = node ;
143
159
}
160
+ } else if ( node . name . startsWith ( tmpname ) ) {
161
+ tmpname = node . name + '_UNIQUE' ;
144
162
}
145
163
} else if ( nodeType === 'ImportExpression' ) {
146
164
insertions . push ( {
@@ -168,7 +186,7 @@ function transformer(args, body, isAsync, isGenerator, filename) {
168
186
let curr = 0 ;
169
187
for ( let i = 0 ; i < insertions . length ; i ++ ) {
170
188
const change = insertions [ i ] ;
171
- ncode += code . substring ( curr , change . pos ) + change . code ;
189
+ ncode += code . substring ( curr , change . pos ) + change . code . replace ( / \$ t m p n a m e / g , tmpname ) ;
172
190
curr = change . pos ;
173
191
}
174
192
ncode += code . substring ( curr ) ;
0 commit comments