From 933a4fcc3655a2e317a33e7eaa44e61f76bacd4a Mon Sep 17 00:00:00 2001 From: James Roseborough Date: Fri, 30 Oct 2009 05:27:36 +0000 Subject: [PATCH] Replace upvalue holder when upvalues are closed. --- .../luaj/vm2/luajc/JavaBytecodeGenerator.java | 54 ++++++++++++++----- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/src/jse/org/luaj/vm2/luajc/JavaBytecodeGenerator.java b/src/jse/org/luaj/vm2/luajc/JavaBytecodeGenerator.java index 6ec958d9..6d37ab0e 100644 --- a/src/jse/org/luaj/vm2/luajc/JavaBytecodeGenerator.java +++ b/src/jse/org/luaj/vm2/luajc/JavaBytecodeGenerator.java @@ -285,22 +285,14 @@ public class JavaBytecodeGenerator { int nl = p.maxstacksize; LocalVariableGen[] locals = new LocalVariableGen[nl]; - // find upvalues - boolean isup[] = new boolean[nl]; - for (pc = 0; pc < nc; pc++) { - if (OP(code[pc]) == Lua.OP_CLOSURE) { - b = Bx(code[pc]); - Prototype newp = p.p[b]; - for (int j = 0, nup = newp.nups; j < nup; ++j) { - i = code[++pc]; - if ((i & 4) == 0) - isup[B(i)] = true; - } - } - } // initialize locals LocalVariableGen nil = null; +// LocalVariableGen reg[] = new LocalVariableGen[nl]; +// LocalVariableGen regup[] = new LocalVariableGen[nl]; + boolean isup[] = new boolean[nl]; + markups(p, isup, code, 0, 0); + for (int j = 0; j < nl; j++) { String name = j < p.locvars.length && p.locvars[j].varname != null ? @@ -912,6 +904,14 @@ public class JavaBytecodeGenerator { break; case Lua.OP_CLOSE: /* A close all variables in the stack up to (>=) R(A)*/ + for ( int j=nl; --j>=a; ) { + if ( isup[j] ) { + il.append(new PUSH(cp, 1)); + il.append(new ANEWARRAY(cp.addClass(STR_LUAVALUE))); + il.append(new ASTORE(locals[j].getIndex())); + } + } + // markups( p, isup, code, pc+1, a ); break; case Lua.OP_CLOSURE: /* A Bx R(A):= closure(KPROTO[Bx], R(A), ... ,R(A+n)) */ @@ -1012,6 +1012,34 @@ public class JavaBytecodeGenerator { } + // find the upvalues implied by the subsequent instructions + private void markups(Prototype p, boolean[] isup, int[] code, int startpc, int startregister) { + int last = isup.length; + for ( int j=startregister; j