diff --git a/src/core/org/luaj/vm/LuaState.java b/src/core/org/luaj/vm/LuaState.java index 641125bb..49aa0ed0 100644 --- a/src/core/org/luaj/vm/LuaState.java +++ b/src/core/org/luaj/vm/LuaState.java @@ -875,14 +875,14 @@ public class LuaState extends Lua { int i; for ( i = this.upvals.size() - 1; i >= 0; --i ) { up = (UpVal) this.upvals.elementAt( i ); - if ( up.stack == this.stack && up.position == target ) { + if ( up.state == this && up.position == target ) { return up; } else if ( up.position < target ) { break; } } - up = new UpVal( upValName, this.stack, target ); + up = new UpVal( upValName, this, target ); this.upvals.insertElementAt( up, i + 1 ); return up; } diff --git a/src/core/org/luaj/vm/UpVal.java b/src/core/org/luaj/vm/UpVal.java index 42b6cc27..cb15a315 100644 --- a/src/core/org/luaj/vm/UpVal.java +++ b/src/core/org/luaj/vm/UpVal.java @@ -25,12 +25,13 @@ package org.luaj.vm; public class UpVal { private LString name; - public LValue[] stack; - public int position; + LuaState state; + int position; + LValue value; - public UpVal( LString string, LValue[] stack, int i ) { + public UpVal( LString string, LuaState state, int i ) { this.name = string; - this.stack = stack; + this.state = state; this.position = i; } @@ -39,18 +40,23 @@ public class UpVal { } public LValue getValue() { - return stack[ position ]; + if ( state == null ) + return value; + else + return state.stack[ position ]; } public void setValue( LValue value ) { - stack[ position ] = value; + if ( state == null ) + this.value = value; + else + state.stack[ position ] = value; } public boolean close( int limit ) { if ( position >= limit ) { - final LValue v = stack[ position ]; - stack = new LValue[] { v }; - position = 0; + value = state.stack[ position ]; + state = null; return true; } else { return false;