comparison src/luan/lib/BasicLib.java @ 37:8a57ebfdfd78

add JavaLib git-svn-id: https://luan-java.googlecode.com/svn/trunk@38 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 20 Dec 2012 02:36:07 +0000
parents 2a35154aec14
children e5bcb1eeafc1
comparison
equal deleted inserted replaced
36:2a35154aec14 37:8a57ebfdfd78
16 import luan.LuaJavaFunction; 16 import luan.LuaJavaFunction;
17 import luan.LuaException; 17 import luan.LuaException;
18 import luan.interp.LuaCompiler; 18 import luan.interp.LuaCompiler;
19 19
20 20
21 public class BasicLib { 21 public final class BasicLib {
22 22
23 public static void register(LuaState lua) { 23 public static void register(LuaState lua) {
24 LuaTable t = lua.global(); 24 LuaTable global = lua.global();
25 t.put( "_G", t ); 25 global.put( "_G", global );
26 add( t, "getmetatable", Object.class ); 26 add( global, "getmetatable", LuaState.class, Object.class );
27 add( t, "ipairs", LuaTable.class ); 27 add( global, "ipairs", LuaTable.class );
28 add( t, "load", LuaState.class, String.class ); 28 add( global, "load", LuaState.class, String.class );
29 add( t, "loadfile", LuaState.class, String.class ); 29 add( global, "loadfile", LuaState.class, String.class );
30 add( t, "pairs", LuaTable.class ); 30 add( global, "pairs", LuaTable.class );
31 add( t, "print", LuaState.class, new Object[0].getClass() ); 31 add( global, "print", LuaState.class, new Object[0].getClass() );
32 add( t, "rawequal", Object.class, Object.class ); 32 add( global, "rawequal", Object.class, Object.class );
33 add( t, "rawget", LuaTable.class, Object.class ); 33 add( global, "rawget", LuaTable.class, Object.class );
34 add( t, "rawlen", Object.class ); 34 add( global, "rawlen", Object.class );
35 add( t, "rawset", LuaTable.class, Object.class, Object.class ); 35 add( global, "rawset", LuaTable.class, Object.class, Object.class );
36 add( t, "setmetatable", LuaTable.class, LuaTable.class ); 36 add( global, "setmetatable", LuaTable.class, LuaTable.class );
37 add( t, "tonumber", Object.class, Integer.class ); 37 add( global, "tonumber", Object.class, Integer.class );
38 add( t, "tostring", LuaState.class, Object.class ); 38 add( global, "tostring", LuaState.class, Object.class );
39 add( t, "type", Object.class ); 39 add( global, "type", Object.class );
40 t.put( "_VERSION", Lua.version ); 40 global.put( "_VERSION", Lua.version );
41 } 41 }
42 42
43 private static void add(LuaTable t,String method,Class<?>... parameterTypes) { 43 private static void add(LuaTable t,String method,Class<?>... parameterTypes) {
44 try { 44 try {
45 t.put( method, new LuaJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) ); 45 t.put( method, new LuaJavaFunction(BasicLib.class.getMethod(method,parameterTypes),null) );
97 } 97 }
98 98
99 private static class TableIter { 99 private static class TableIter {
100 private final Iterator<Map.Entry<Object,Object>> iter; 100 private final Iterator<Map.Entry<Object,Object>> iter;
101 101
102 TableIter(LuaTable t) { 102 TableIter(Iterator<Map.Entry<Object,Object>> iter) {
103 this.iter = t.iterator(); 103 this.iter = iter;
104 } 104 }
105 105
106 public Object[] next() { 106 public Object[] next() {
107 if( !iter.hasNext() ) 107 if( !iter.hasNext() )
108 return LuaFunction.EMPTY_RTN; 108 return LuaFunction.EMPTY_RTN;
109 Map.Entry<Object,Object> entry = iter.next(); 109 Map.Entry<Object,Object> entry = iter.next();
110 return new Object[]{entry.getKey(),entry.getValue()}; 110 return new Object[]{entry.getKey(),entry.getValue()};
111 } 111 }
112 } 112 }
113 113 private static final Method nextTableIter;
114 public static LuaFunction pairs(LuaTable t) { 114 static {
115 try { 115 try {
116 TableIter ti = new TableIter(t); 116 nextTableIter = TableIter.class.getMethod("next");
117 Method m = TableIter.class.getMethod("next"); 117 nextTableIter.setAccessible(true);
118 m.setAccessible(true);
119 return new LuaJavaFunction(m,ti);
120 } catch(NoSuchMethodException e) { 118 } catch(NoSuchMethodException e) {
121 throw new RuntimeException(e); 119 throw new RuntimeException(e);
122 } 120 }
121 }
122
123 static LuaFunction pairs(Iterator<Map.Entry<Object,Object>> iter) {
124 TableIter ti = new TableIter(iter);
125 return new LuaJavaFunction(nextTableIter,ti);
126 }
127
128 public static LuaFunction pairs(LuaTable t) {
129 return pairs(t.iterator());
123 } 130 }
124 131
125 private static class ArrayIter { 132 private static class ArrayIter {
126 private final LuaTable t; 133 private final LuaTable t;
127 private double i = 0.0; 134 private double i = 0.0;
134 LuaNumber n = new LuaNumber(++i); 141 LuaNumber n = new LuaNumber(++i);
135 Object val = t.get(n); 142 Object val = t.get(n);
136 return val==null ? LuaFunction.EMPTY_RTN : new Object[]{n,val}; 143 return val==null ? LuaFunction.EMPTY_RTN : new Object[]{n,val};
137 } 144 }
138 } 145 }
139 146 private static final Method nextArrayIter;
140 public static LuaFunction ipairs(LuaTable t) { 147 static {
141 try { 148 try {
142 ArrayIter ai = new ArrayIter(t); 149 nextArrayIter = ArrayIter.class.getMethod("next");
143 Method m = ArrayIter.class.getMethod("next"); 150 nextArrayIter.setAccessible(true);
144 m.setAccessible(true);
145 return new LuaJavaFunction(m,ai);
146 } catch(NoSuchMethodException e) { 151 } catch(NoSuchMethodException e) {
147 throw new RuntimeException(e); 152 throw new RuntimeException(e);
148 } 153 }
149 } 154 }
150 155
151 public static LuaTable getmetatable(Object obj) { 156 public static LuaFunction ipairs(LuaTable t) {
152 return Lua.getMetatable(obj); 157 ArrayIter ai = new ArrayIter(t);
158 return new LuaJavaFunction(nextArrayIter,ai);
159 }
160
161 public static LuaTable getmetatable(LuaState lua,Object obj) {
162 return lua.getMetatable(obj);
153 } 163 }
154 164
155 public static LuaTable setmetatable(LuaTable table,LuaTable metatable) { 165 public static LuaTable setmetatable(LuaTable table,LuaTable metatable) {
156 table.setMetatable(metatable); 166 table.setMetatable(metatable);
157 return table; 167 return table;