Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
431:3ffe8ba5b297 | 432:d9df6d6cb927 |
---|---|
16 | 16 |
17 public final class TableLuan { | 17 public final class TableLuan { |
18 | 18 |
19 public static String concat(LuanState luan,LuanTable list,String sep,Integer i,Integer j) throws LuanException { | 19 public static String concat(LuanState luan,LuanTable list,String sep,Integer i,Integer j) throws LuanException { |
20 int first = i==null ? 1 : i; | 20 int first = i==null ? 1 : i; |
21 int last = j==null ? list.length() : j; | 21 int last = j==null ? list.length(luan) : j; |
22 StringBuilder buf = new StringBuilder(); | 22 StringBuilder buf = new StringBuilder(); |
23 for( int k=first; k<=last; k++ ) { | 23 for( int k=first; k<=last; k++ ) { |
24 Object val = list.rawGet(k); | 24 Object val = list.get(luan,k); |
25 if( val==null ) | 25 if( val==null ) |
26 break; | 26 break; |
27 if( sep!=null && k > first ) | 27 if( sep!=null && k > first ) |
28 buf.append(sep); | 28 buf.append(sep); |
29 String s = Luan.asString(val); | 29 String s = Luan.asString(val); |
32 buf.append(val); | 32 buf.append(val); |
33 } | 33 } |
34 return buf.toString(); | 34 return buf.toString(); |
35 } | 35 } |
36 | 36 |
37 public static void insert(LuanTable list,int pos,Object value){ | 37 public static void insert(LuanState luan,LuanTable list,int pos,Object value) throws LuanException { |
38 if( list.getMetatable() != null ) | |
39 throw luan.exception("can't insert into a table with a metatable"); | |
38 list.rawInsert(pos,value); | 40 list.rawInsert(pos,value); |
39 } | 41 } |
40 | 42 |
41 public static Object remove(LuanTable list,int pos) { | 43 public static Object remove(LuanState luan,LuanTable list,int pos) throws LuanException { |
44 if( list.getMetatable() != null ) | |
45 throw luan.exception("can't remove from a table with a metatable"); | |
42 return list.rawRemove(pos); | 46 return list.rawRemove(pos); |
43 } | 47 } |
44 | 48 |
45 private static interface LessThan { | 49 private static interface LessThan { |
46 public boolean isLessThan(Object o1,Object o2); | 50 public boolean isLessThan(Object o1,Object o2); |
47 } | 51 } |
48 | 52 |
49 public static void sort(final LuanState luan,LuanTable list,final LuanFunction comp) throws LuanException { | 53 public static void sort(final LuanState luan,LuanTable list,final LuanFunction comp) throws LuanException { |
54 if( list.getMetatable() != null ) | |
55 throw luan.exception("can't sort a table with a metatable"); | |
50 final LessThan lt; | 56 final LessThan lt; |
51 if( comp==null ) { | 57 if( comp==null ) { |
52 lt = new LessThan() { | 58 lt = new LessThan() { |
53 public boolean isLessThan(Object o1,Object o2) { | 59 public boolean isLessThan(Object o1,Object o2) { |
54 try { | 60 try { |
79 throw (LuanException)e.getCause(); | 85 throw (LuanException)e.getCause(); |
80 } | 86 } |
81 } | 87 } |
82 | 88 |
83 public static LuanTable pack(Object... args) { | 89 public static LuanTable pack(Object... args) { |
84 LuanTable tbl = new LuanTable(); | 90 LuanTable tbl = new LuanTable(Arrays.asList(args)); |
85 boolean hasNull = false; | |
86 for( int i=0; i<args.length; i++ ) { | |
87 Object v = args[i]; | |
88 if( v==null ) { | |
89 hasNull = true; | |
90 } else if( !hasNull ) { | |
91 tbl.rawAdd(v); | |
92 } else { | |
93 tbl.rawPut(i+1,v); | |
94 } | |
95 } | |
96 tbl.rawPut( "n", args.length ); | 91 tbl.rawPut( "n", args.length ); |
97 return tbl; | 92 return tbl; |
98 } | 93 } |
99 | 94 |
100 @LuanMethod public static Object[] unpack(LuanTable tbl,Integer iFrom,Integer iTo) { | 95 @LuanMethod public static Object[] unpack(LuanState luan,LuanTable tbl,Integer iFrom,Integer iTo) throws LuanException { |
101 int from = iFrom!=null ? iFrom : 1; | 96 int from = iFrom!=null ? iFrom : 1; |
102 int to = iTo!=null ? iTo : tbl.length(); | 97 int to = iTo!=null ? iTo : tbl.length(luan); |
103 List<Object> list = new ArrayList<Object>(); | 98 List<Object> list = new ArrayList<Object>(); |
104 for( int i=from; i<=to; i++ ) { | 99 for( int i=from; i<=to; i++ ) { |
105 list.add( tbl.rawGet(i) ); | 100 list.add( tbl.get(luan,i) ); |
106 } | 101 } |
107 return list.toArray(); | 102 return list.toArray(); |
108 } | 103 } |
109 | 104 |
110 public static LuanTable sub_list(LuanTable list,int from,int to) { | 105 public static LuanTable sub_list(LuanState luan,LuanTable list,int from,int to) throws LuanException { |
106 if( list.getMetatable() != null ) | |
107 throw luan.exception("can't sub_list a table with a metatable"); | |
111 return list.rawSubList(from,to); | 108 return list.rawSubList(from,to); |
112 } | 109 } |
113 | 110 |
114 public static LuanTable clone(LuanTable tbl) { | 111 public static LuanTable clone(LuanTable tbl) { |
115 return new LuanTable(tbl); | 112 return new LuanTable(tbl); |