Change upvalues to point to the LuaState instead of directly to the stack.

This change has two benefits:
 * Reference to LuaState's stack does not need to be adjusted when
   the stack needs to grow.
 * One less heap object for a closed upvalue.
This commit is contained in:
Ian Farmer
2007-11-21 02:21:35 +00:00
parent 9f7b675220
commit 62cda2bc49
2 changed files with 17 additions and 11 deletions

View File

@@ -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;
}

View File

@@ -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;