Refactor weak tables including proper weak key semantics and improved userdata handling.

This commit is contained in:
James Roseborough
2010-06-21 01:31:40 +00:00
parent 03cebfbf82
commit dc84bc9e8d
8 changed files with 523 additions and 414 deletions

View File

@@ -21,6 +21,8 @@
******************************************************************************/
package org.luaj.vm2;
import java.util.Vector;
public class LuaTable extends LuaValue {
private static final int MIN_HASH_CAPACITY = 2;
private static final LuaString N = valueOf("n");
@@ -29,28 +31,28 @@ public class LuaTable extends LuaValue {
protected LuaValue[] hashKeys;
protected LuaValue[] hashValues;
private int hashEntries;
private LuaValue m_metatable;
protected LuaValue m_metatable;
public LuaTable() {
array = NOVALS;
hashKeys = NOVALS;
hashValues = NOVALS;
}
public LuaTable(int narray, int nhash) {
presize(narray, nhash);
}
public LuaTable(LuaValue[] named, LuaValue[] unnamed, Varargs lastarg) {
int nn = (named!=null? named.length: 0);
int nu = (unnamed!=null? unnamed.length: 0);
int nl = (lastarg!=null? lastarg.narg(): 0);
presize(nu+nl, nn-(nn>>1));
for ( int i=0; i<nu; i++ )
array[i] = unnamed[i].optvalue(null);
rawset(i+1,unnamed[i].optvalue(null));
if ( lastarg != null )
for ( int i=0,n=lastarg.narg(); i<n; ++i )
array[nu+i] = lastarg.arg(i+1).optvalue(null);
for ( int i=1,n=lastarg.narg(); i<=n; ++i )
rawset(nu+i,lastarg.arg(i).optvalue(null));
for ( int i=0; i<nn; i+=2 )
if (!named[i+1].isnil())
rawset(named[i], named[i+1]);
@@ -68,15 +70,6 @@ public class LuaTable extends LuaValue {
for ( int i=1; i<=n; i++ )
set(i, varargs.arg(i+nskip));
}
private void presize(int narray, int nhash) {
if ( nhash > 0 && nhash < MIN_HASH_CAPACITY )
nhash = MIN_HASH_CAPACITY;
array = (narray>0? new LuaValue[narray]: NOVALS);
hashKeys = (nhash>0? new LuaValue[nhash]: NOVALS);
hashValues = (nhash>0? new LuaValue[nhash]: NOVALS);
hashEntries = 0;
}
public int type() {
return LuaValue.TTABLE;
@@ -98,9 +91,18 @@ public class LuaTable extends LuaValue {
return this;
}
public void presize( int i ) {
if ( i > array.length )
array = resize( array, i );
public void presize( int narray ) {
if ( narray > array.length )
array = resize( array, narray );
}
public void presize(int narray, int nhash) {
if ( nhash > 0 && nhash < MIN_HASH_CAPACITY )
nhash = MIN_HASH_CAPACITY;
array = (narray>0? new LuaValue[narray]: NOVALS);
hashKeys = (nhash>0? new LuaValue[nhash]: NOVALS);
hashValues = (nhash>0? new LuaValue[nhash]: NOVALS);
hashEntries = 0;
}
private static LuaValue[] resize( LuaValue[] old, int n ) {
@@ -109,6 +111,14 @@ public class LuaTable extends LuaValue {
return v;
}
protected int getArrayLength() {
return array.length;
}
protected int getHashLength() {
return hashValues.length;
}
public LuaValue getmetatable() {
if ( m_metatable!=null )
return m_metatable.rawget(METATABLE).optvalue(m_metatable);
@@ -130,25 +140,11 @@ public class LuaTable extends LuaValue {
}
protected LuaTable changemode(boolean weakkeys, boolean weakvalues) {
if ( weakkeys || weakvalues ) {
return recreateas(weakkeys, weakvalues);
}
if ( weakkeys || weakvalues )
return new WeakTable(weakkeys, weakvalues, this);
return this;
}
protected LuaTable recreateas(boolean weakkeys, boolean weakvalues) {
LuaTable t = weakkeys||weakvalues?
new WeakTable(weakkeys, weakvalues):
new LuaTable();
t.presize(array.length,hashKeys.length);
Varargs n;
LuaValue k = NIL;
while ( !(k = ((n = next(k)).arg1())).isnil() )
t.rawset(k, n.arg(2));
t.m_metatable = m_metatable;
return t;
}
public LuaValue get( int key ) {
LuaValue v = rawget(key);
return v.isnil() && m_metatable!=null? gettable(this,valueOf(key)): v;
@@ -173,7 +169,7 @@ public class LuaTable extends LuaValue {
}
return hashget( key );
}
private LuaValue hashget(LuaValue key) {
if ( hashEntries > 0 ) {
LuaValue v = hashValues[hashFindSlot(key)];
@@ -181,7 +177,7 @@ public class LuaTable extends LuaValue {
}
return NIL;
}
public void set( int key, LuaValue value ) {
if ( m_metatable==null || ! rawget(key).isnil() || ! settable(this,LuaInteger.valueOf(key),value) )
rawset(key, value);
@@ -234,8 +230,6 @@ public class LuaTable extends LuaValue {
public LuaValue remove(int pos) {
if ( pos == 0 )
pos = length();
if ( pos < 1 || pos > array.length )
return NONE;
LuaValue v = rawget(pos);
for ( LuaValue r=v; !r.isnil(); ) {
r = rawget(pos+1);
@@ -267,9 +261,9 @@ public class LuaTable extends LuaValue {
}
public LuaValue getn() {
for ( int n=array.length; --n>0; )
if ( array[n]!=null )
return LuaInteger.valueOf(n+1);
for ( int n=getArrayLength(); n>0; --n )
if ( !rawget(n).isnil() )
return LuaInteger.valueOf(n);
return ZERO;
}
@@ -277,10 +271,11 @@ public class LuaTable extends LuaValue {
* Get the length of this table, as lua defines it.
*/
public int length() {
int n=array.length+1,m=0;
int a = getArrayLength();
int n = a+1,m=0;
while ( !rawget(n).isnil() ) {
m = n;
n += array.length+hashEntries+1;
n += a+getHashLength()+1;
}
while ( n > m+1 ) {
int k = (n+m) / 2;
@@ -312,7 +307,10 @@ public class LuaTable extends LuaValue {
return n;
}
/**
* Get the next element after a particular key in the table
* @return key,value or nil
*/
/**
* Get the next element after a particular key in the table
* @return key,value or nil
@@ -352,7 +350,7 @@ public class LuaTable extends LuaValue {
// nothing found, push nil, return nil.
return NIL;
}
/**
* Get the next element after a particular key in the
* contiguous array part of a table
@@ -371,16 +369,13 @@ public class LuaTable extends LuaValue {
* @param func function to call
*/
public LuaValue foreach(LuaValue func) {
LuaValue v = NIL;
for ( int i=0; i<array.length; i++ )
if ( array[i] != null )
if ( !(v = func.call(LuaInteger.valueOf(i+1), array[i])).isnil() )
return v;
for ( int i=0; i<hashKeys.length; i++ )
if ( hashKeys[i] != null )
if ( !(v = func.call(hashKeys[i], hashValues[i])).isnil() )
return v;
return v;
Varargs n;
LuaValue k = NIL;
LuaValue v;
while ( !(k = ((n = next(k)).arg1())).isnil() )
if ( ! (v = func.call(k, n.arg(2))).isnil() )
return v;
return NIL;
}
/**
@@ -390,46 +385,15 @@ public class LuaTable extends LuaValue {
* @param func
*/
public LuaValue foreachi(LuaValue func) {
LuaValue v = NIL;
for ( int i=0; i<array.length && array[i]!=null; i++ )
if ( !(v = func.call(LuaInteger.valueOf(i+1), array[i])).isnil() )
Varargs n;
LuaValue k = NIL;
LuaValue v;
while ( !(k = ((n = inext(k)).arg1())).isnil() )
if ( ! (v = func.call(k, n.arg(2))).isnil() )
return v;
return v;
return NIL;
}
// ======================= test hooks =================
/** Value used in testing to provide the capacity of the array part */
int arrayCapacity() {
return array.length;
}
/** Value used in testing to provide the capacity of the hash part */
int hashCapacity() {
return hashKeys.length;
}
/** Value used in testing to provide the total count of elements */
int keyCount() {
int n = 0;
for ( int i=0; i<array.length; i++ )
if ( array[i] != null )
++n;
return n + hashEntries;
}
/** Value used in testing to enumerate the keys */
public LuaValue[] keys() {
LuaValue[] vals = new LuaValue[keyCount()];
int n = 0;
for ( int i=0; i<array.length; i++ )
if ( array[i] != null )
vals[n++] = LuaInteger.valueOf(i+1);
for ( int i=0; i<hashKeys.length; i++ )
if ( hashKeys[i] != null )
vals[n++] = hashKeys[i];
return vals;
}
// ======================= hashset =================
@@ -592,4 +556,21 @@ public class LuaTable extends LuaValue {
array[j] = a;
}
/** @deprecate - count via iteration instead */
public int keyCount() {
return keys().length;
}
/** @deprecate - use next() instead */
public LuaValue[] keys() {
Vector l = new Vector();
LuaValue k = LuaValue.NIL;
while ( true ) {
Varargs n = next(k);
if ( (k = n.arg1()).isnil() )
break;
l.add( k );
}
return (LuaValue[]) l.toArray(new LuaValue[l.size()]);
}
}

View File

@@ -167,6 +167,7 @@ public class LuaValue extends Varargs {
public static LuaValue argerror(int iarg,String msg) { throw new LuaError("bad argument #"+iarg+": "+msg); }
protected LuaValue typerror(String expected) { throw new LuaError(expected+" expected, got "+typename()); }
protected LuaValue unimplemented(String fun) { throw new LuaError("'"+fun+"' not implemented for "+typename()); }
protected LuaValue illegal(String op,String typename) { throw new LuaError("illegal operation '"+op+"' for "+typename); }
protected LuaValue callerror() { throw new LuaError("attempt to call "+typename()); }
protected LuaValue lenerror() { throw new LuaError("attempt to get length of "+typename()); }
protected LuaValue aritherror() { throw new LuaError("attempt to perform arithmetic on "+typename()); }
@@ -316,6 +317,7 @@ public class LuaValue extends Varargs {
// lua number/string conversion
public LuaString strvalue() { typerror("strValue"); return null; }
public LuaValue strongkey() { return this; }
public LuaValue strongvalue() { return this; }
// conversion from java values

View File

@@ -23,170 +23,269 @@ package org.luaj.vm2;
import java.lang.ref.WeakReference;
public class WeakTable extends LuaTable {
import org.luaj.vm2.lib.TwoArgFunction;
private final boolean weakKeys,weakValues;
public class WeakTable extends LuaTable {
private LuaTable backing;
private boolean weakkeys,weakvalues;
WeakTable( boolean weakKeys, boolean weakValues ) {
this.weakKeys = weakKeys;
this.weakValues = weakValues;
public WeakTable(boolean weakkeys, boolean weakvalues) {
this(weakkeys, weakvalues, 0, 0);
}
protected WeakTable(boolean weakkeys, boolean weakvalues, int narray, int nhash) {
this.backing = new LuaTable(narray, nhash);
this.weakkeys = weakkeys;
this.weakvalues = weakvalues;
}
protected WeakTable(boolean weakkeys, boolean weakvalues, LuaTable source) {
this(weakkeys, weakvalues, source.getArrayLength(), source.getHashLength());
Varargs n;
LuaValue k = NIL;
while ( !(k = ((n = source.next(k)).arg1())).isnil() )
rawset(k, n.arg(2));
m_metatable = source.m_metatable;
}
public void presize( int narray ) {
backing.presize(narray);
}
private static class WeakValue extends LuaValue {
private final WeakReference ref;
public WeakValue(LuaValue val) {
ref = new WeakReference(val);
public void presize(int narray, int nhash) {
backing.presize(narray, nhash);
}
protected int getArrayLength() {
return backing.getArrayLength();
}
protected int getHashLength() {
return backing.getHashLength();
}
protected WeakTable changemode(boolean weakkeys, boolean weakvalues) {
this.weakkeys = weakkeys;
this.weakvalues = weakvalues;
return this;
}
LuaValue weaken( LuaValue value ) {
switch ( value.type() ) {
case LuaValue.TFUNCTION:
case LuaValue.TTHREAD:
case LuaValue.TTABLE:
return new WeakValue(value);
case LuaValue.TUSERDATA:
return new WeakUserdata(value);
default:
return value;
}
}
public void rawset( int key, LuaValue value ) {
if ( weakvalues )
value = weaken( value );
backing.set(key, value);
}
/** caller must ensure key is not nil */
public void rawset( LuaValue key, LuaValue value ) {
if ( weakvalues )
value = weaken( value );
if ( weakkeys ) {
switch ( key.type() ) {
case LuaValue.TFUNCTION:
case LuaValue.TTHREAD:
case LuaValue.TTABLE:
case LuaValue.TUSERDATA:
key = value = new WeakEntry(this, key, value);
break;
default:
break;
}
}
backing.set(key, value);
}
public LuaValue rawget( int key ) {
return rawget(valueOf(key));
}
public LuaValue rawget( LuaValue key ) {
LuaValue v = backing.rawget(key);
if ( v.isnil() )
return NIL;
v = v.strongvalue();
if ( v.isnil() )
backing.rawset(key, NIL);
return v;
}
public int maxn() {
return backing.maxn();
}
/**
* Get the next element after a particular key in the table
* @return key,value or nil
*/
public Varargs next( LuaValue key ) {
while ( true ) {
Varargs n = backing.next(key);
LuaValue k = n.arg1();
if ( k.isnil() )
return NIL;
LuaValue ks = k.strongkey();
LuaValue vs = n.arg(2).strongvalue();
if ( ks.isnil() || vs.isnil() ) {
backing.rawset(ks, NIL);
} else {
return varargsOf(ks,vs);
}
}
}
/**
* Get the next element after a particular key in the
* contiguous array part of a table
* @return key,value or nil
*/
public Varargs inext(LuaValue key) {
int k = key.optint(0)+1;
LuaValue v = this.rawget(k);
return v.isnil()? NIL: varargsOf(valueOf(k),v);
}
// ----------------- sort support -----------------------------
public void sort(final LuaValue comparator) {
backing.sort( new TwoArgFunction() {
public LuaValue call(LuaValue arg1, LuaValue arg2) {
return comparator.call( arg1.strongvalue(), arg2.strongvalue() );
}
} );
}
static class WeakValue extends LuaValue {
final WeakReference ref;
protected WeakValue(LuaValue value) {
ref = new WeakReference(value);
}
public int type() {
return strongvalue().type();
illegal("type","weak value");
return 0;
}
public String typename() {
return "weakvalue";
illegal("typename","weak value");
return null;
}
public String toString() {
return "weak<"+ref.get()+">";
}
public LuaValue strongkey() {
Object o = ref.get();
return o!=null? (LuaValue)o: NIL;
}
public LuaValue strongvalue() {
Object o = ref.get();
return o!=null? (LuaValue)o: NIL;
}
public String tojstring() {
return strongvalue().tojstring();
}
}
private static class WeakUserdata extends LuaValue {
private WeakReference ref;
private WeakReference mt;
public WeakUserdata(Object val, LuaValue metatable) {
this.ref = new WeakReference(val);
this.mt = new WeakReference(metatable);
}
public int type() {
return TVALUE;
}
public String typename() {
return "weakuserdata";
}
public LuaValue strongvalue() {
if ( ref != null ) {
Object o = ref.get();
if ( o != null )
return userdataOf( o, (LuaValue) mt.get() );
}
ref = mt = null;
return NIL;
}
}
private static class WeakEntry extends LuaValue {
private LuaValue key;
private LuaValue val;
private WeakEntry(LuaValue key, LuaValue val) {
this.key = key;
this.val = val;
}
public int type() {
return LuaValue.TNIL;
}
public String typename() {
return "weakentry";
}
public LuaValue strongkey() {
LuaValue k = key.strongvalue();
LuaValue v = val.strongvalue();
if ( k.isnil() || v.isnil() )
return key = val = NIL;
return k;
}
public LuaValue strongvalue() {
LuaValue k = key.strongvalue();
LuaValue v = val.strongvalue();
if ( k.isnil() || v.isnil() )
return key = val = NIL;
return v;
}
public boolean eq_b(LuaValue rhs) {
return strongkey().eq_b(rhs);
}
public int hashCode() {
return strongkey().hashCode();
Object o = ref.get();
return o!=null && rhs.eq_b((LuaValue)o);
}
}
private boolean shouldWeaken( LuaValue value ) {
switch ( value.type() ) {
case LuaValue.TFUNCTION:
case LuaValue.TTHREAD:
case LuaValue.TTABLE:
case LuaValue.TUSERDATA:
static final class WeakUserdata extends WeakValue {
private final WeakReference ob;
private final WeakReference mt;
private WeakUserdata(LuaValue value) {
super(value);
ob = new WeakReference(value.touserdata());
LuaValue udmt = value.getmetatable();
mt = udmt!=null? new WeakReference(udmt): null;
}
public LuaValue strongvalue() {
Object u = ref.get();
if ( u != null )
return (LuaValue) u;
Object o = ob.get();
Object m = mt!=null? mt.get(): null;
return o!=null? m!=null? userdataOf(o,(LuaValue)m): userdataOf(o): NIL;
}
public boolean eq_b(LuaValue rhs) {
return rhs.isuserdata() && (rhs.touserdata() == ob.get());
}
public boolean isuserdata() {
return true;
}
return false;
}
private LuaValue toWeak( LuaValue value ) {
switch ( value.type() ) {
case LuaValue.TFUNCTION:
case LuaValue.TTHREAD:
case LuaValue.TTABLE: return new WeakValue( value );
case LuaValue.TUSERDATA: return new WeakUserdata( value.checkuserdata(), value.getmetatable() );
default: return value;
public Object touserdata() {
return ob.get();
}
}
public LuaValue rawget(int key) {
LuaValue v = super.rawget(key);
if ( v.isnil() )
return NIL;
v = v.strongvalue();
if ( v.isnil() ) {
// TODO: mark table for culling?
super.rawset(key, NIL);
}
return v;
}
static final class WeakEntry extends LuaValue {
final WeakTable table;
final LuaValue weakkey;
final int keyhash;
public LuaValue rawget(LuaValue key) {
LuaValue v = super.rawget(key);
if ( v.isnil() )
return NIL;
v = v.strongvalue();
if ( v.isnil() ) {
// TODO: mark table for culling?
super.rawset(key, NIL);
}
return v;
}
public void rawset(int key, LuaValue val) {
if ( val.isnil() || !weakValues || !shouldWeaken(val) ) {
super.rawset(key, val);
} else {
super.rawset(key, toWeak(val));
}
}
private WeakEntry(WeakTable table, LuaValue key, LuaValue weakvalue) {
this.table = table;
this.weakkey = table.weaken(key);
this.keyhash = key.hashCode();
public void rawset(LuaValue key, LuaValue val) {
if ( val.isnil() ) {
super.rawset(key, val);
} else {
boolean weakenKey = weakKeys && shouldWeaken(key);
boolean weakenVal = weakValues && shouldWeaken(val);
if ( weakenKey ) {
WeakEntry e = new WeakEntry( toWeak(key), weakenVal? toWeak(val): val);
super.rawset(e, e);
} else if ( weakenVal ) {
super.rawset(key, toWeak(val));
} else {
super.rawset(key, val);
}
// store an association from table to value in the key's metatable
LuaValue mt = key.getmetatable();
if ( mt == null )
key.setmetatable(mt=new LuaTable(0,1));
mt.set(table, weakvalue);
}
// when looking up the value, look in the keys metatable
public LuaValue strongvalue() {
LuaValue key = weakkey.strongkey();
if ( key.isnil() )
return NIL;
LuaValue mt = key.getmetatable();
if ( mt == null )
return NIL;
LuaValue weakvalue = mt.get(table);
return weakvalue.strongvalue();
}
}
protected LuaTable changemode(boolean k, boolean v) {
if ( k!=this.weakKeys || v!=weakValues )
return recreateas(k,v);
return this;
}
public int type() {
illegal("type","weak entry");
return 0;
}
public String typename() {
illegal("typename","weak entry");
return null;
}
public String toString() {
return "weak<"+strongkey()+","+strongvalue()+">";
}
public int hashCode() {
return keyhash;
}
public boolean eq_b(LuaValue rhs) {
return rhs.eq_b(weakkey.strongkey());
}
}
}