Add last Lua type: userdata. Changed LInstance to inherit from LUserData,
and changed instanceof/casts to LInstance to use LUserData instead. This should enable various add-ons to be compatible without knowing much, or anything, about each other.
This commit is contained in:
@@ -11,6 +11,7 @@ import lua.value.LInteger;
|
||||
import lua.value.LNil;
|
||||
import lua.value.LNumber;
|
||||
import lua.value.LString;
|
||||
import lua.value.LUserData;
|
||||
import lua.value.LValue;
|
||||
|
||||
public class CoerceLuaToJava {
|
||||
@@ -69,15 +70,15 @@ public class CoerceLuaToJava {
|
||||
return value.luaAsString();
|
||||
}
|
||||
public int score(LValue value) {
|
||||
if ( value instanceof LInstance )
|
||||
if ( value instanceof LUserData )
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
Coercion objectCoercion = new Coercion() {
|
||||
public Object coerce(LValue value) {
|
||||
if ( value instanceof LInstance )
|
||||
return ((LInstance)value).instance;
|
||||
if ( value instanceof LUserData )
|
||||
return ((LUserData)value).m_instance;
|
||||
if ( value instanceof LString )
|
||||
return value.luaAsString();
|
||||
if ( value instanceof LInteger )
|
||||
@@ -116,8 +117,8 @@ public class CoerceLuaToJava {
|
||||
Coercion co = COERCIONS.get( type );
|
||||
if ( co != null )
|
||||
return co.coerce( v );
|
||||
if ( v instanceof LInstance )
|
||||
return ((LInstance) v).instance;
|
||||
if ( v instanceof LUserData )
|
||||
return ((LUserData) v).m_instance;
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -146,8 +147,8 @@ public class CoerceLuaToJava {
|
||||
Coercion co = COERCIONS.get( c );
|
||||
if ( co != null ) {
|
||||
score += co.score( v );
|
||||
} else if ( v instanceof LInstance ) {
|
||||
Object o = ((LInstance) v).instance;
|
||||
} else if ( v instanceof LUserData ) {
|
||||
Object o = ((LUserData) v).m_instance;
|
||||
if ( ! c.isAssignableFrom(o.getClass()) )
|
||||
score += 0x10000;
|
||||
} else {
|
||||
|
||||
@@ -14,6 +14,7 @@ import lua.GlobalState;
|
||||
import lua.value.LFunction;
|
||||
import lua.value.LString;
|
||||
import lua.value.LTable;
|
||||
import lua.value.LUserData;
|
||||
import lua.value.LValue;
|
||||
|
||||
public final class LuaJava extends LFunction {
|
||||
@@ -96,46 +97,39 @@ public final class LuaJava extends LFunction {
|
||||
call.adjustTop(base + nresults);
|
||||
}
|
||||
|
||||
public static class LInstance extends LValue {
|
||||
Object instance;
|
||||
public static class LInstance extends LUserData {
|
||||
private Class clazz;
|
||||
public LInstance(Object o, Class clazz) {
|
||||
this.instance = o;
|
||||
super(o);
|
||||
this.clazz = clazz;
|
||||
}
|
||||
public String luaAsString() {
|
||||
return instance.toString();
|
||||
}
|
||||
public void luaGetTable(CallFrame call, int base, LValue table, LValue key) {
|
||||
final String s = key.luaAsString();
|
||||
try {
|
||||
Field f = clazz.getField(s);
|
||||
Object o = f.get(instance);
|
||||
Object o = f.get(m_instance);
|
||||
LValue v = CoerceJavaToLua.coerce( o );
|
||||
call.stack[base] = v;
|
||||
call.top = base + 1;
|
||||
} catch (NoSuchFieldException nsfe) {
|
||||
call.stack[base] = new LMethod(instance,clazz,s);
|
||||
call.stack[base] = new LMethod(m_instance,clazz,s);
|
||||
call.top = base + 1;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
public void luaSetTable(CallFrame call, int base, LValue table, LValue key, LValue val) {
|
||||
Class c = instance.getClass();
|
||||
Class c = m_instance.getClass();
|
||||
String s = key.luaAsString();
|
||||
try {
|
||||
Field f = c.getField(s);
|
||||
Object v = CoerceLuaToJava.coerceArg(val, f.getType());
|
||||
f.set(instance,v);
|
||||
f.set(m_instance,v);
|
||||
call.top = base;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
public LString luaGetType() {
|
||||
return new LString("userdata");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
28
src/main/java/lua/value/LUserData.java
Normal file
28
src/main/java/lua/value/LUserData.java
Normal file
@@ -0,0 +1,28 @@
|
||||
package lua.value;
|
||||
|
||||
public class LUserData extends LValue {
|
||||
public static final LString TYPE_NAME = new LString("userdata");
|
||||
|
||||
public final Object m_instance;
|
||||
|
||||
public LUserData(Object obj) {
|
||||
m_instance = obj;
|
||||
}
|
||||
|
||||
public String luaAsString() {
|
||||
return m_instance.toString();
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
return (this == obj) ||
|
||||
(obj instanceof LUserData && this.m_instance == ((LUserData) obj).m_instance);
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return System.identityHashCode( m_instance );
|
||||
}
|
||||
|
||||
public LString luaGetType() {
|
||||
return TYPE_NAME;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user