@@ -273,23 +273,33 @@ private static boolean appendStraightLineMethodBody(StringBuilder out, ByteCodeC
273273 if (!isStraightLineEligible (method , instructions )) {
274274 return false ;
275275 }
276+ StringBuilder setup = new StringBuilder ();
277+ StringBuilder instructionBody = new StringBuilder ();
276278 StringBuilder body = new StringBuilder ();
277279 StraightLineContext ctx = new StraightLineContext (method .getMaxLocals (), method .getMaxStack ());
278280 if (!method .isStatic ()) {
279- body .append (" let l0 = __cn1ThisObject;\n " );
281+ setup .append (" let l0 = __cn1ThisObject;\n " );
280282 ctx .localsInitialized [0 ] = true ;
283+ ctx .localsUsed [0 ] = true ;
281284 }
282285 List <ByteCodeMethodArg > arguments = method .getArguments ();
283286 int localIndex = method .isStatic () ? 0 : 1 ;
284287 for (int i = 0 ; i < arguments .size (); i ++) {
285- body .append (" let l" ).append (localIndex ).append (" = __cn1Arg" ).append (i + 1 ).append (";\n " );
288+ setup .append (" let l" ).append (localIndex ).append (" = __cn1Arg" ).append (i + 1 ).append (";\n " );
286289 ctx .localsInitialized [localIndex ] = true ;
287290 ctx .localsUsed [localIndex ] = true ;
288291 localIndex ++;
289292 if (arguments .get (i ).isDoubleOrLong ()) {
290293 localIndex ++;
291294 }
292295 }
296+ for (int i = 0 ; i < instructions .size (); i ++) {
297+ Instruction instruction = instructions .get (i );
298+ if (!appendStraightLineInstruction (instructionBody , method , instruction , ctx )) {
299+ return false ;
300+ }
301+ }
302+ body .append (setup );
293303 for (int i = 0 ; i < method .getMaxLocals (); i ++) {
294304 if (!ctx .localsInitialized [i ] && ctx .localsUsed [i ]) {
295305 body .append (" let l" ).append (i ).append (" = null;\n " );
@@ -303,12 +313,7 @@ private static boolean appendStraightLineMethodBody(StringBuilder out, ByteCodeC
303313 body .append (" jvm.monitorEnter(jvm.currentThread, __cn1Monitor);\n " );
304314 body .append (" try {\n " );
305315 }
306- for (int i = 0 ; i < instructions .size (); i ++) {
307- Instruction instruction = instructions .get (i );
308- if (!appendStraightLineInstruction (body , method , instruction , ctx )) {
309- return false ;
310- }
311- }
316+ body .append (instructionBody );
312317 if (method .isSynchronizedMethod ()) {
313318 body .append (" } finally {\n " );
314319 body .append (" jvm.monitorExit(jvm.currentThread, __cn1Monitor);\n " );
@@ -1568,10 +1573,7 @@ private static void appendInvokeInstruction(StringBuilder out, Invoke invoke, in
15681573
15691574 if (invoke .getOpcode () == Opcodes .INVOKEVIRTUAL || invoke .getOpcode () == Opcodes .INVOKEINTERFACE ) {
15701575 out .append (" {\n " );
1571- out .append (" const __args = [];\n " );
1572- for (int i = argCount - 1 ; i >= 0 ; i --) {
1573- out .append (" __args.unshift(stack.pop());\n " );
1574- }
1576+ appendInvocationArgumentBindings (out , argCount , " " , "stack.pop()" );
15751577 out .append (" const __target = stack.pop();\n " );
15761578 out .append (" const __class = jvm.classes[__target.__class];\n " );
15771579 out .append (" const __method = (__class && __class.methods && __class.methods[\" " ).append (methodId )
@@ -1592,10 +1594,7 @@ private static void appendInvokeInstruction(StringBuilder out, Invoke invoke, in
15921594 }
15931595
15941596 out .append (" {\n " );
1595- out .append (" const __args = [];\n " );
1596- for (int i = argCount - 1 ; i >= 0 ; i --) {
1597- out .append (" __args.unshift(stack.pop());\n " );
1598- }
1597+ appendInvocationArgumentBindings (out , argCount , " " , "stack.pop()" );
15991598 if (invoke .getOpcode () != Opcodes .INVOKESTATIC ) {
16001599 out .append (" const __target = stack.pop();\n " );
16011600 }
@@ -1624,7 +1623,13 @@ private static void appendInvocationArguments(StringBuilder out, boolean include
16241623 out .append (", " );
16251624 }
16261625 first = false ;
1627- out .append ("__args[" ).append (i ).append ("]" );
1626+ out .append ("__arg" ).append (i );
1627+ }
1628+ }
1629+
1630+ private static void appendInvocationArgumentBindings (StringBuilder out , int argCount , String indent , String sourceExpression ) {
1631+ for (int i = argCount - 1 ; i >= 0 ; i --) {
1632+ out .append (indent ).append ("const __arg" ).append (i ).append (" = " ).append (sourceExpression ).append (";\n " );
16281633 }
16291634 }
16301635}
0 commit comments