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.LNil;
|
||||||
import lua.value.LNumber;
|
import lua.value.LNumber;
|
||||||
import lua.value.LString;
|
import lua.value.LString;
|
||||||
|
import lua.value.LUserData;
|
||||||
import lua.value.LValue;
|
import lua.value.LValue;
|
||||||
|
|
||||||
public class CoerceLuaToJava {
|
public class CoerceLuaToJava {
|
||||||
@@ -69,15 +70,15 @@ public class CoerceLuaToJava {
|
|||||||
return value.luaAsString();
|
return value.luaAsString();
|
||||||
}
|
}
|
||||||
public int score(LValue value) {
|
public int score(LValue value) {
|
||||||
if ( value instanceof LInstance )
|
if ( value instanceof LUserData )
|
||||||
return 0;
|
return 0;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Coercion objectCoercion = new Coercion() {
|
Coercion objectCoercion = new Coercion() {
|
||||||
public Object coerce(LValue value) {
|
public Object coerce(LValue value) {
|
||||||
if ( value instanceof LInstance )
|
if ( value instanceof LUserData )
|
||||||
return ((LInstance)value).instance;
|
return ((LUserData)value).m_instance;
|
||||||
if ( value instanceof LString )
|
if ( value instanceof LString )
|
||||||
return value.luaAsString();
|
return value.luaAsString();
|
||||||
if ( value instanceof LInteger )
|
if ( value instanceof LInteger )
|
||||||
@@ -116,8 +117,8 @@ public class CoerceLuaToJava {
|
|||||||
Coercion co = COERCIONS.get( type );
|
Coercion co = COERCIONS.get( type );
|
||||||
if ( co != null )
|
if ( co != null )
|
||||||
return co.coerce( v );
|
return co.coerce( v );
|
||||||
if ( v instanceof LInstance )
|
if ( v instanceof LUserData )
|
||||||
return ((LInstance) v).instance;
|
return ((LUserData) v).m_instance;
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,8 +147,8 @@ public class CoerceLuaToJava {
|
|||||||
Coercion co = COERCIONS.get( c );
|
Coercion co = COERCIONS.get( c );
|
||||||
if ( co != null ) {
|
if ( co != null ) {
|
||||||
score += co.score( v );
|
score += co.score( v );
|
||||||
} else if ( v instanceof LInstance ) {
|
} else if ( v instanceof LUserData ) {
|
||||||
Object o = ((LInstance) v).instance;
|
Object o = ((LUserData) v).m_instance;
|
||||||
if ( ! c.isAssignableFrom(o.getClass()) )
|
if ( ! c.isAssignableFrom(o.getClass()) )
|
||||||
score += 0x10000;
|
score += 0x10000;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import lua.GlobalState;
|
|||||||
import lua.value.LFunction;
|
import lua.value.LFunction;
|
||||||
import lua.value.LString;
|
import lua.value.LString;
|
||||||
import lua.value.LTable;
|
import lua.value.LTable;
|
||||||
|
import lua.value.LUserData;
|
||||||
import lua.value.LValue;
|
import lua.value.LValue;
|
||||||
|
|
||||||
public final class LuaJava extends LFunction {
|
public final class LuaJava extends LFunction {
|
||||||
@@ -96,46 +97,39 @@ public final class LuaJava extends LFunction {
|
|||||||
call.adjustTop(base + nresults);
|
call.adjustTop(base + nresults);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class LInstance extends LValue {
|
public static class LInstance extends LUserData {
|
||||||
Object instance;
|
|
||||||
private Class clazz;
|
private Class clazz;
|
||||||
public LInstance(Object o, Class clazz) {
|
public LInstance(Object o, Class clazz) {
|
||||||
this.instance = o;
|
super(o);
|
||||||
this.clazz = clazz;
|
this.clazz = clazz;
|
||||||
}
|
}
|
||||||
public String luaAsString() {
|
|
||||||
return instance.toString();
|
|
||||||
}
|
|
||||||
public void luaGetTable(CallFrame call, int base, LValue table, LValue key) {
|
public void luaGetTable(CallFrame call, int base, LValue table, LValue key) {
|
||||||
final String s = key.luaAsString();
|
final String s = key.luaAsString();
|
||||||
try {
|
try {
|
||||||
Field f = clazz.getField(s);
|
Field f = clazz.getField(s);
|
||||||
Object o = f.get(instance);
|
Object o = f.get(m_instance);
|
||||||
LValue v = CoerceJavaToLua.coerce( o );
|
LValue v = CoerceJavaToLua.coerce( o );
|
||||||
call.stack[base] = v;
|
call.stack[base] = v;
|
||||||
call.top = base + 1;
|
call.top = base + 1;
|
||||||
} catch (NoSuchFieldException nsfe) {
|
} catch (NoSuchFieldException nsfe) {
|
||||||
call.stack[base] = new LMethod(instance,clazz,s);
|
call.stack[base] = new LMethod(m_instance,clazz,s);
|
||||||
call.top = base + 1;
|
call.top = base + 1;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void luaSetTable(CallFrame call, int base, LValue table, LValue key, LValue val) {
|
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();
|
String s = key.luaAsString();
|
||||||
try {
|
try {
|
||||||
Field f = c.getField(s);
|
Field f = c.getField(s);
|
||||||
Object v = CoerceLuaToJava.coerceArg(val, f.getType());
|
Object v = CoerceLuaToJava.coerceArg(val, f.getType());
|
||||||
f.set(instance,v);
|
f.set(m_instance,v);
|
||||||
call.top = base;
|
call.top = base;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(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