diff core/src/luan/modules/TableLuan.java @ 432:d9df6d6cb927

finish fixing LuanTable to use metatables
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 02 May 2015 23:41:59 -0600
parents 3ffe8ba5b297
children 93e6e67768d7
line wrap: on
line diff
--- a/core/src/luan/modules/TableLuan.java	Sat May 02 21:12:48 2015 -0600
+++ b/core/src/luan/modules/TableLuan.java	Sat May 02 23:41:59 2015 -0600
@@ -18,10 +18,10 @@
 
 	public static String concat(LuanState luan,LuanTable list,String sep,Integer i,Integer j) throws LuanException {
 		int first = i==null ? 1 : i;
-		int last = j==null ? list.length() : j;
+		int last = j==null ? list.length(luan) : j;
 		StringBuilder buf = new StringBuilder();
 		for( int k=first; k<=last; k++ ) {
-			Object val = list.rawGet(k);
+			Object val = list.get(luan,k);
 			if( val==null )
 				break;
 			if( sep!=null && k > first )
@@ -34,11 +34,15 @@
 		return buf.toString();
 	}
 
-	public static void insert(LuanTable list,int pos,Object value){
+	public static void insert(LuanState luan,LuanTable list,int pos,Object value) throws LuanException {
+		if( list.getMetatable() != null )
+			throw luan.exception("can't insert into a table with a metatable");
 		list.rawInsert(pos,value);
 	}
 
-	public static Object remove(LuanTable list,int pos) {
+	public static Object remove(LuanState luan,LuanTable list,int pos) throws LuanException {
+		if( list.getMetatable() != null )
+			throw luan.exception("can't remove from a table with a metatable");
 		return list.rawRemove(pos);
 	}
 
@@ -47,6 +51,8 @@
 	}
 
 	public static void sort(final LuanState luan,LuanTable list,final LuanFunction comp) throws LuanException {
+		if( list.getMetatable() != null )
+			throw luan.exception("can't sort a table with a metatable");
 		final LessThan lt;
 		if( comp==null ) {
 			lt = new LessThan() {
@@ -81,33 +87,24 @@
 	}
 
 	public static LuanTable pack(Object... args) {
-		LuanTable tbl = new LuanTable();
-		boolean hasNull = false;
-		for( int i=0; i<args.length; i++ ) {
-			Object v = args[i];
-			if( v==null ) {
-				hasNull = true;
-			} else if( !hasNull ) {
-				tbl.rawAdd(v);
-			} else {
-				tbl.rawPut(i+1,v);
-			}
-		}
+		LuanTable tbl = new LuanTable(Arrays.asList(args));
 		tbl.rawPut( "n", args.length );
 		return tbl;
 	}
 
-	@LuanMethod public static Object[] unpack(LuanTable tbl,Integer iFrom,Integer iTo) {
+	@LuanMethod public static Object[] unpack(LuanState luan,LuanTable tbl,Integer iFrom,Integer iTo) throws LuanException {
 		int from = iFrom!=null ? iFrom : 1;
-		int to = iTo!=null ? iTo : tbl.length();
+		int to = iTo!=null ? iTo : tbl.length(luan);
 		List<Object> list = new ArrayList<Object>();
 		for( int i=from; i<=to; i++ ) {
-			list.add( tbl.rawGet(i) );
+			list.add( tbl.get(luan,i) );
 		}
 		return list.toArray();
 	}
 
-	public static LuanTable sub_list(LuanTable list,int from,int to) {
+	public static LuanTable sub_list(LuanState luan,LuanTable list,int from,int to) throws LuanException {
+		if( list.getMetatable() != null )
+			throw luan.exception("can't sub_list a table with a metatable");
 		return list.rawSubList(from,to);
 	}