changeset 4:24fd6381caca

add to interp git-svn-id: https://luan-java.googlecode.com/svn/trunk@5 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 18 Nov 2012 04:18:15 +0000
parents 7a2cdbc5767f
children 9ef0fd711101
files src/luan/LuaJavaFunction.java src/luan/interp/Block.java src/luan/interp/GetExpr.java src/luan/interp/IndexExpr.java src/luan/interp/SetStmt.java src/luan/interp/Stmt.java src/luan/interp/TableExpr.java src/luan/interp/ValuesStmt.java src/luan/lib/BasicLib.java
diffstat 9 files changed, 198 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/LuaJavaFunction.java	Fri Nov 16 05:23:12 2012 +0000
+++ b/src/luan/LuaJavaFunction.java	Sun Nov 18 04:18:15 2012 +0000
@@ -1,5 +1,6 @@
 package luan;
 
+import java.lang.reflect.Array;
 import java.lang.reflect.Method;
 import java.lang.reflect.InvocationTargetException;
 
@@ -9,12 +10,19 @@
 	private final Object obj;
 	private final RtnConverter rtnConverter;
 	private final ArgConverter[] argConverters;
+	private final Class<?> varArgCls;
 
 	public LuaJavaFunction(Method method,Object obj) {
 		this.method = method;
 		this.obj = obj;
-		rtnConverter = getRtnConverter(method);
-		argConverters = getArgConverters(method);
+		this.rtnConverter = getRtnConverter(method);
+		this.argConverters = getArgConverters(method);
+		if( method.isVarArgs() ) {
+			Class<?>[] paramTypes = method.getParameterTypes();
+			this.varArgCls = paramTypes[paramTypes.length-1].getComponentType();
+		} else {
+			this.varArgCls = null;
+		}
 	}
 
 	@Override public Object[] call(Object... args) {
@@ -31,15 +39,36 @@
 	}
 
 	private Object[] fixArgs(Object[] args) {
-		if( args.length != argConverters.length ) {
-			Object[] t = new Object[argConverters.length];
-			System.arraycopy(args,0,t,0,Math.min(args.length,t.length));
-			args = t;
+		if( varArgCls==null ) {
+			if( args.length != argConverters.length ) {
+				Object[] t = new Object[argConverters.length];
+				System.arraycopy(args,0,t,0,Math.min(args.length,t.length));
+				args = t;
+			}
+			for( int i=0; i<args.length; i++ ) {
+				args[i] = argConverters[i].convert(args[i]);
+			}
+			return args;
+		} else {  // varargs
+			Object[] rtn = new Object[argConverters.length];
+			int n = argConverters.length - 1;
+			if( args.length < argConverters.length ) {
+				System.arraycopy(args,0,rtn,0,args.length);
+				rtn[rtn.length-1] = Array.newInstance(varArgCls,0);
+			} else {
+				System.arraycopy(args,0,rtn,0,n);
+				Object[] varArgs = (Object[])Array.newInstance(varArgCls,args.length-n);
+				ArgConverter ac = argConverters[n];
+				for( int i=0; i<varArgs.length; i++ ) {
+					varArgs[i] = ac.convert(args[n+i]);
+				}
+				rtn[rtn.length-1] = varArgs;
+			}
+			for( int i=0; i<n; i++ ) {
+				rtn[i] = argConverters[i].convert(rtn[i]);
+			}
+			return rtn;
 		}
-		for( int i=0; i<args.length; i++ ) {
-			args[i] = argConverters[i].convert(args[i]);
-		}
-		return args;
 	}
 
 
@@ -184,10 +213,14 @@
 	};
 
 	private static ArgConverter[] getArgConverters(Method m) {
+		final boolean isVarArgs = m.isVarArgs();
 		Class<?>[] paramTypes = m.getParameterTypes();
 		ArgConverter[] a = new ArgConverter[paramTypes.length];
 		for( int i=0; i<a.length; i++ ) {
-			a[i] = getArgConverter(paramTypes[i]);
+			Class<?> paramType = paramTypes[i];
+			if( isVarArgs && i == a.length-1 )
+				paramType = paramType.getComponentType();
+			a[i] = getArgConverter(paramType);
 		}
 		return a;
 	}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/interp/Block.java	Sun Nov 18 04:18:15 2012 +0000
@@ -0,0 +1,20 @@
+package luan.interp;
+
+import luan.LuaState;
+import luan.LuaException;
+
+
+final class Block extends Stmt {
+	private final Stmt[] stmts;
+
+	Block(Stmt[] stmts) {
+		this.stmts = stmts;
+	}
+
+	@Override void eval(LuaState lua) throws LuaException {
+		for( Stmt stmt : stmts ) {
+			stmt.eval(lua);
+		}
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/interp/GetExpr.java	Sun Nov 18 04:18:15 2012 +0000
@@ -0,0 +1,24 @@
+package luan.interp;
+
+import luan.Lua;
+import luan.LuaException;
+import luan.LuaState;
+import luan.LuaTable;
+
+
+final class GetExpr extends BinaryOpExpr {
+
+	GetExpr(Expr op1,Expr op2) {
+		super(op1,op2);
+	}
+
+	@Override Object eval(LuaState lua) throws LuaException {
+		Object t = op1.eval(lua);
+		if( t instanceof LuaTable ) {
+			LuaTable tbl = (LuaTable)t;
+			Object key = op2.eval(lua);
+			return tbl.get(key);
+		}
+		throw new LuaException( "attempt to index a " + Lua.type(t) + " value" );
+	}
+}
--- a/src/luan/interp/IndexExpr.java	Fri Nov 16 05:23:12 2012 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-package luan.interp;
-
-import luan.Lua;
-import luan.LuaException;
-import luan.LuaState;
-import luan.LuaTable;
-
-
-final class IndexExpr extends BinaryOpExpr {
-
-	IndexExpr(Expr op1,Expr op2) {
-		super(op1,op2);
-	}
-
-	@Override Object eval(LuaState lua) throws LuaException {
-		Object t = op1.eval(lua);
-		if( t instanceof LuaTable ) {
-			LuaTable tbl = (LuaTable)t;
-			Object key = op2.eval(lua);
-			return tbl.get(key);
-		}
-		throw new LuaException( "attempt to index a " + Lua.type(t) + " value" );
-	}
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/interp/SetStmt.java	Sun Nov 18 04:18:15 2012 +0000
@@ -0,0 +1,45 @@
+package luan.interp;
+
+import java.util.List;
+import luan.Lua;
+import luan.LuaState;
+import luan.LuaException;
+import luan.LuaTable;
+
+
+final class SetStmt extends Stmt {
+
+	static class Var {
+		final Expr table;
+		final Expr key;
+
+		Var(Expr table,Expr key) {
+			this.table = table;
+			this.key = key;
+		}
+	}
+
+	private final Var[] vars;
+	private final Values values;
+
+	SetStmt(Var[] vars,Values values) {
+		this.vars = vars;
+		this.values = values;
+	}
+
+	@Override void eval(LuaState lua) throws LuaException {
+		List vals = values.eval(lua);
+		int n = vals.size();
+		for( int i=0; i<vars.length; i++ ) {
+			Var var = vars[i];
+			Object t = var.table.eval(lua);
+			if( !(t instanceof LuaTable) )
+				throw new LuaException( "attempt to index a " + Lua.type(t) + " value" );
+			LuaTable tbl = (LuaTable)t;
+			Object key = var.key.eval(lua);
+			Object val = i < n ? vals.get(i) : null;
+			tbl.set(key,val);
+		}
+	}
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/interp/Stmt.java	Sun Nov 18 04:18:15 2012 +0000
@@ -0,0 +1,9 @@
+package luan.interp;
+
+import luan.LuaState;
+import luan.LuaException;
+
+
+abstract class Stmt {
+	abstract void eval(LuaState lua) throws LuaException;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/interp/TableExpr.java	Sun Nov 18 04:18:15 2012 +0000
@@ -0,0 +1,33 @@
+package luan.interp;
+
+import luan.LuaException;
+import luan.LuaState;
+import luan.LuaTable;
+
+
+final class TableExpr extends Expr {
+
+	static class Field {
+		final Expr key;
+		final Expr value;
+
+		Field(Expr key,Expr value) {
+			this.key = key;
+			this.value = value;
+		}
+	}
+
+	private final Field[] fields;
+
+	TableExpr(Field[] fields) {
+		this.fields = fields;
+	}
+
+	@Override Object eval(LuaState lua) throws LuaException {
+		LuaTable table = new LuaTable();
+		for( Field field : fields ) {
+			table.set( field.key.eval(lua), field.value.eval(lua) );
+		}
+		return table;
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/interp/ValuesStmt.java	Sun Nov 18 04:18:15 2012 +0000
@@ -0,0 +1,18 @@
+package luan.interp;
+
+import luan.LuaState;
+import luan.LuaException;
+
+
+final class ValuesStmt extends Stmt {
+	private final Values values;
+
+	ValuesStmt(Values values) {
+		this.values = values;
+	}
+
+	@Override void eval(LuaState lua) throws LuaException {
+		values.eval(lua);
+	}
+
+}
--- a/src/luan/lib/BasicLib.java	Fri Nov 16 05:23:12 2012 +0000
+++ b/src/luan/lib/BasicLib.java	Sun Nov 18 04:18:15 2012 +0000
@@ -9,6 +9,7 @@
 
 	public static void register(LuaTable t) {
 		add( t, "print", new Object[0].getClass() );
+		add( t, "type", Object.class );
 	}
 
 	private static void add(LuaTable t,String method,Class<?>... parameterTypes) {
@@ -28,4 +29,8 @@
 		System.out.println();
 	}
 
+	public static String type(Object obj) {
+		return Lua.type(obj);
+	}
+
 }