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; int i;
for ( i = this.upvals.size() - 1; i >= 0; --i ) { for ( i = this.upvals.size() - 1; i >= 0; --i ) {
up = (UpVal) this.upvals.elementAt( i ); up = (UpVal) this.upvals.elementAt( i );
if ( up.stack == this.stack && up.position == target ) { if ( up.state == this && up.position == target ) {
return up; return up;
} else if ( up.position < target ) { } else if ( up.position < target ) {
break; break;
} }
} }
up = new UpVal( upValName, this.stack, target ); up = new UpVal( upValName, this, target );
this.upvals.insertElementAt( up, i + 1 ); this.upvals.insertElementAt( up, i + 1 );
return up; return up;
} }

View File

@@ -25,12 +25,13 @@ package org.luaj.vm;
public class UpVal { public class UpVal {
private LString name; private LString name;
public LValue[] stack; LuaState state;
public int position; int position;
LValue value;
public UpVal( LString string, LValue[] stack, int i ) { public UpVal( LString string, LuaState state, int i ) {
this.name = string; this.name = string;
this.stack = stack; this.state = state;
this.position = i; this.position = i;
} }
@@ -39,18 +40,23 @@ public class UpVal {
} }
public LValue getValue() { public LValue getValue() {
return stack[ position ]; if ( state == null )
return value;
else
return state.stack[ position ];
} }
public void setValue( LValue value ) { public void setValue( LValue value ) {
stack[ position ] = value; if ( state == null )
this.value = value;
else
state.stack[ position ] = value;
} }
public boolean close( int limit ) { public boolean close( int limit ) {
if ( position >= limit ) { if ( position >= limit ) {
final LValue v = stack[ position ]; value = state.stack[ position ];
stack = new LValue[] { v }; state = null;
position = 0;
return true; return true;
} else { } else {
return false; return false;