diff --git a/src/jse/org/luaj/vm2/luajc/JavaGen.java b/src/jse/org/luaj/vm2/luajc/JavaGen.java index 5fa61dc7..b2a7973c 100644 --- a/src/jse/org/luaj/vm2/luajc/JavaGen.java +++ b/src/jse/org/luaj/vm2/luajc/JavaGen.java @@ -317,6 +317,7 @@ public class JavaGen { builder.dup(); builder.dup(); builder.storeLocal(pc, a); +// builder.createUpvalues(pc, a+3, 1); builder.storeLocal(pc, a+3); builder.loadLocal(pc, a+1); // limit builder.loadLocal(pc, a+2); // step @@ -418,13 +419,6 @@ public class JavaGen { // let builder process branch instructions builder.onEndOfLuaInstruction( pc0 ); - - // for-loops have upvalue created at top of loop - switch ( o ) { - case Lua.OP_FORPREP: - builder.convertToUpvalue(pc+1, a+3); - break; - } } } diff --git a/src/jse/org/luaj/vm2/luajc/ProtoInfo.java b/src/jse/org/luaj/vm2/luajc/ProtoInfo.java index da70dd3d..d8f7bcdc 100644 --- a/src/jse/org/luaj/vm2/luajc/ProtoInfo.java +++ b/src/jse/org/luaj/vm2/luajc/ProtoInfo.java @@ -244,11 +244,12 @@ public class ProtoInfo { case Lua.OP_FORLOOP: /* A sBx R(A)+=R(A+2); if R(A) = 0 ) { - switch ( Lua.GET_OPCODE(prototype.code[pc]) ) { - case Lua.OP_CLOSURE: - case Lua.OP_CALL: - case Lua.OP_TFORLOOP: - pc -= 1; - break; - } - } + // special case when both refer and assign in same instruction + if ( pc >= 0 && vars[slot][pc] != null && vars[slot][pc].pc == pc ) + pc -= 1; VarInfo v = pc<0? params[slot]: vars[slot][pc]; // return v.upvalue != null && v.upvalue.rw; return v != null && v.upvalue != null; diff --git a/test/junit/org/luaj/vm2/FragmentsTest.java b/test/junit/org/luaj/vm2/FragmentsTest.java index 42437327..788be216 100644 --- a/test/junit/org/luaj/vm2/FragmentsTest.java +++ b/test/junit/org/luaj/vm2/FragmentsTest.java @@ -388,6 +388,18 @@ public class FragmentsTest extends TestSuite { "end\n"); } + public void testNumericForUpvalues2() { + runFragment( LuaValue.valueOf("222 222"), + "local t = {}\n"+ + "local template = [[123 456]]\n"+ + "for i = 1,2 do\n"+ + " t[i] = template:gsub('%d', function(s)\n"+ + " return i\n"+ + " end)\n"+ + "end\n" + + "return t[2]\n"); + } + public void testReturnUpvalue() { runFragment( LuaValue.varargsOf(new LuaValue[] { LuaValue.ONE, LuaValue.valueOf(5), }), "local a = 1\n"+