Allow metatable operations on values of all types, and use the metatable
on strings as the string package, so that string functions can be called with self (:) syntax. Autoload test case now inexplicably fails more, but it was already broken.
This commit is contained in:
@@ -6,6 +6,12 @@ import lua.VM;
|
||||
abstract
|
||||
public class LValue {
|
||||
|
||||
/** Metatable tag for intercepting table gets */
|
||||
public static final LString TM_INDEX = new LString("__index");
|
||||
|
||||
/** Metatable tag for intercepting table sets */
|
||||
public static final LString TM_NEWINDEX = new LString("__newindex");
|
||||
|
||||
protected static LValue luaUnsupportedOperation() {
|
||||
throw new java.lang.RuntimeException( "not supported" );
|
||||
}
|
||||
@@ -79,22 +85,39 @@ public class LValue {
|
||||
}
|
||||
|
||||
/** set a value in a table
|
||||
* For non-tables, goes straight to the meta-table.
|
||||
* @param vm the calling vm
|
||||
* @param table the table to operate on
|
||||
* @param the key to set
|
||||
* @param the value to set
|
||||
*/
|
||||
public void luaSetTable(VM vm, LValue table, LValue key, LValue val) {
|
||||
luaUnsupportedOperation();
|
||||
LTable mt = luaGetMetatable();
|
||||
if ( mt != null ) {
|
||||
LValue event = mt.get( TM_NEWINDEX );
|
||||
if ( event != null && event != LNil.NIL ) {
|
||||
event.luaSetTable( vm, table, key, val );
|
||||
return;
|
||||
}
|
||||
}
|
||||
vm.push( LNil.NIL );
|
||||
}
|
||||
|
||||
|
||||
/** Get a value from a table
|
||||
* @param vm the calling vm
|
||||
* @param table the table from which to get the value
|
||||
* @param key the key to look up
|
||||
*/
|
||||
public void luaGetTable(VM vm, LValue table, LValue key) {
|
||||
luaUnsupportedOperation();
|
||||
LTable mt = luaGetMetatable();
|
||||
if ( mt != null ) {
|
||||
LValue event = mt.get( TM_INDEX );
|
||||
if ( event != null && event != LNil.NIL ) {
|
||||
event.luaGetTable( vm, table, key );
|
||||
return;
|
||||
}
|
||||
}
|
||||
vm.push(LNil.NIL);
|
||||
}
|
||||
|
||||
/** Get the value as a String
|
||||
@@ -135,9 +158,18 @@ public class LValue {
|
||||
return luaUnsupportedOperation();
|
||||
}
|
||||
|
||||
/** Valid for tables */
|
||||
public LValue luaGetMetatable() {
|
||||
return luaUnsupportedOperation();
|
||||
/**
|
||||
* Valid for all types: get a metatable. Only tables and userdata can have a
|
||||
* different metatable per instance, though, other types are restricted to
|
||||
* one metatable per type.
|
||||
*
|
||||
* Since metatables on non-tables can only be set through Java and not Lua,
|
||||
* this function should be overridden for each value type as necessary.
|
||||
*
|
||||
* @return null if there is no meta-table
|
||||
*/
|
||||
public LTable luaGetMetatable() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Valid for tables */
|
||||
@@ -147,4 +179,5 @@ public class LValue {
|
||||
|
||||
/** Valid for all types: return the type of this value as an LString */
|
||||
public abstract LString luaGetType();
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user