comparison src/luan/lib/JavaLib.java @ 48:64ecb7a3aad7

rename Lua to Luan git-svn-id: https://luan-java.googlecode.com/svn/trunk@49 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Fri, 28 Dec 2012 03:29:12 +0000
parents a443637829c1
children 8ede219cd111
comparison
equal deleted inserted replaced
47:659c7139e903 48:64ecb7a3aad7
9 import java.util.Map; 9 import java.util.Map;
10 import java.util.HashMap; 10 import java.util.HashMap;
11 import java.util.List; 11 import java.util.List;
12 import java.util.ArrayList; 12 import java.util.ArrayList;
13 import java.util.Iterator; 13 import java.util.Iterator;
14 import luan.Lua; 14 import luan.Luan;
15 import luan.LuaState; 15 import luan.LuanState;
16 import luan.LuaTable; 16 import luan.LuanTable;
17 import luan.MetatableGetter; 17 import luan.MetatableGetter;
18 import luan.LuaException; 18 import luan.LuanException;
19 import luan.LuaFunction; 19 import luan.LuanFunction;
20 import luan.LuaJavaFunction; 20 import luan.LuanJavaFunction;
21 import luan.LuaElement; 21 import luan.LuanElement;
22 22
23 23
24 public final class JavaLib { 24 public final class JavaLib {
25 25
26 public static void register(LuaState lua) { 26 public static void register(LuanState lua) {
27 lua.addMetatableGetter(mg); 27 lua.addMetatableGetter(mg);
28 LuaTable module = new LuaTable(); 28 LuanTable module = new LuanTable();
29 LuaTable global = lua.global(); 29 LuanTable global = lua.global();
30 global.put("java",module); 30 global.put("java",module);
31 try { 31 try {
32 global.put( "import", new LuaJavaFunction(JavaLib.class.getMethod("importClass",LuaState.class,String.class),null) ); 32 global.put( "import", new LuanJavaFunction(JavaLib.class.getMethod("importClass",LuanState.class,String.class),null) );
33 module.put( "class", new LuaJavaFunction(JavaLib.class.getMethod("getClass",LuaState.class,String.class),null) ); 33 module.put( "class", new LuanJavaFunction(JavaLib.class.getMethod("getClass",LuanState.class,String.class),null) );
34 } catch(NoSuchMethodException e) { 34 } catch(NoSuchMethodException e) {
35 throw new RuntimeException(e); 35 throw new RuntimeException(e);
36 } 36 }
37 } 37 }
38 38
39 private static final LuaTable mt = new LuaTable(); 39 private static final LuanTable mt = new LuanTable();
40 static { 40 static {
41 add( mt, "__index", LuaState.class, Object.class, Object.class ); 41 add( mt, "__index", LuanState.class, Object.class, Object.class );
42 } 42 }
43 43
44 private static void add(LuaTable t,String method,Class<?>... parameterTypes) { 44 private static void add(LuanTable t,String method,Class<?>... parameterTypes) {
45 try { 45 try {
46 t.put( method, new LuaJavaFunction(JavaLib.class.getMethod(method,parameterTypes),null) ); 46 t.put( method, new LuanJavaFunction(JavaLib.class.getMethod(method,parameterTypes),null) );
47 } catch(NoSuchMethodException e) { 47 } catch(NoSuchMethodException e) {
48 throw new RuntimeException(e); 48 throw new RuntimeException(e);
49 } 49 }
50 } 50 }
51 51
52 private static final MetatableGetter mg = new MetatableGetter() { 52 private static final MetatableGetter mg = new MetatableGetter() {
53 public LuaTable getMetatable(Object obj) { 53 public LuanTable getMetatable(Object obj) {
54 if( obj==null ) 54 if( obj==null )
55 return null; 55 return null;
56 return mt; 56 return mt;
57 } 57 }
58 }; 58 };
59 59
60 public static Object __index(LuaState lua,Object obj,Object key) throws LuaException { 60 public static Object __index(LuanState lua,Object obj,Object key) throws LuanException {
61 if( obj instanceof Static ) { 61 if( obj instanceof Static ) {
62 if( key instanceof String ) { 62 if( key instanceof String ) {
63 String name = (String)key; 63 String name = (String)key;
64 Static st = (Static)obj; 64 Static st = (Static)obj;
65 Class cls = st.cls; 65 Class cls = st.cls;
67 return cls; 67 return cls;
68 } else if( "new".equals(name) ) { 68 } else if( "new".equals(name) ) {
69 Constructor<?>[] constructors = cls.getConstructors(); 69 Constructor<?>[] constructors = cls.getConstructors();
70 if( constructors.length > 0 ) { 70 if( constructors.length > 0 ) {
71 if( constructors.length==1 ) { 71 if( constructors.length==1 ) {
72 return new LuaJavaFunction(constructors[0],null); 72 return new LuanJavaFunction(constructors[0],null);
73 } else { 73 } else {
74 List<LuaJavaFunction> fns = new ArrayList<LuaJavaFunction>(); 74 List<LuanJavaFunction> fns = new ArrayList<LuanJavaFunction>();
75 for( Constructor constructor : constructors ) { 75 for( Constructor constructor : constructors ) {
76 fns.add(new LuaJavaFunction(constructor,null)); 76 fns.add(new LuanJavaFunction(constructor,null));
77 } 77 }
78 return new AmbiguousJavaFunction(fns); 78 return new AmbiguousJavaFunction(fns);
79 } 79 }
80 } 80 }
81 } else { 81 } else {
83 if( !members.isEmpty() ) { 83 if( !members.isEmpty() ) {
84 return member(null,members); 84 return member(null,members);
85 } 85 }
86 } 86 }
87 } 87 }
88 throw new LuaException(lua,LuaElement.JAVA,"invalid index for java class: "+key); 88 throw new LuanException(lua,LuanElement.JAVA,"invalid index for java class: "+key);
89 } 89 }
90 Class cls = obj.getClass(); 90 Class cls = obj.getClass();
91 if( cls.isArray() ) { 91 if( cls.isArray() ) {
92 if( "length".equals(key) ) { 92 if( "length".equals(key) ) {
93 return Array.getLength(obj); 93 return Array.getLength(obj);
94 } 94 }
95 Integer i = Lua.asInteger(key); 95 Integer i = Luan.asInteger(key);
96 if( i != null ) { 96 if( i != null ) {
97 return Array.get(obj,i); 97 return Array.get(obj,i);
98 } 98 }
99 throw new LuaException(lua,LuaElement.JAVA,"invalid index for java array: "+key); 99 throw new LuanException(lua,LuanElement.JAVA,"invalid index for java array: "+key);
100 } 100 }
101 if( key instanceof String ) { 101 if( key instanceof String ) {
102 String name = (String)key; 102 String name = (String)key;
103 if( "instanceof".equals(name) ) { 103 if( "instanceof".equals(name) ) {
104 return new LuaJavaFunction(instanceOf,new InstanceOf(obj)); 104 return new LuanJavaFunction(instanceOf,new InstanceOf(obj));
105 } else { 105 } else {
106 List<Member> members = getMembers(cls,name); 106 List<Member> members = getMembers(cls,name);
107 if( !members.isEmpty() ) { 107 if( !members.isEmpty() ) {
108 return member(obj,members); 108 return member(obj,members);
109 } 109 }
110 } 110 }
111 } 111 }
112 throw new LuaException(lua,LuaElement.JAVA,"invalid index for java object: "+key); 112 throw new LuanException(lua,LuanElement.JAVA,"invalid index for java object: "+key);
113 } 113 }
114 114
115 private static Object member(Object obj,List<Member> members) throws LuaException { 115 private static Object member(Object obj,List<Member> members) throws LuanException {
116 try { 116 try {
117 if( members.size()==1 ) { 117 if( members.size()==1 ) {
118 Member member = members.get(0); 118 Member member = members.get(0);
119 if( member instanceof Field ) { 119 if( member instanceof Field ) {
120 Field field = (Field)member; 120 Field field = (Field)member;
121 return field.get(obj); 121 return field.get(obj);
122 } else { 122 } else {
123 Method method = (Method)member; 123 Method method = (Method)member;
124 return new LuaJavaFunction(method,obj); 124 return new LuanJavaFunction(method,obj);
125 } 125 }
126 } else { 126 } else {
127 List<LuaJavaFunction> fns = new ArrayList<LuaJavaFunction>(); 127 List<LuanJavaFunction> fns = new ArrayList<LuanJavaFunction>();
128 for( Member member : members ) { 128 for( Member member : members ) {
129 Method method = (Method)member; 129 Method method = (Method)member;
130 fns.add(new LuaJavaFunction(method,obj)); 130 fns.add(new LuanJavaFunction(method,obj));
131 } 131 }
132 return new AmbiguousJavaFunction(fns); 132 return new AmbiguousJavaFunction(fns);
133 } 133 }
134 } catch(IllegalAccessException e) { 134 } catch(IllegalAccessException e) {
135 throw new RuntimeException(e); 135 throw new RuntimeException(e);
180 Static(Class cls) { 180 Static(Class cls) {
181 this.cls = cls; 181 this.cls = cls;
182 } 182 }
183 } 183 }
184 184
185 public static Static getClass(LuaState lua,String name) throws LuaException { 185 public static Static getClass(LuanState lua,String name) throws LuanException {
186 try { 186 try {
187 return new Static( Class.forName(name) ); 187 return new Static( Class.forName(name) );
188 } catch(ClassNotFoundException e) { 188 } catch(ClassNotFoundException e) {
189 throw new LuaException(lua,LuaElement.JAVA,e); 189 throw new LuanException(lua,LuanElement.JAVA,e);
190 } 190 }
191 } 191 }
192 192
193 public static void importClass(LuaState lua,String name) throws LuaException { 193 public static void importClass(LuanState lua,String name) throws LuanException {
194 lua.global().put( name.substring(name.lastIndexOf('.')+1), getClass(lua,name) ); 194 lua.global().put( name.substring(name.lastIndexOf('.')+1), getClass(lua,name) );
195 } 195 }
196 196
197 static class AmbiguousJavaFunction extends LuaFunction { 197 static class AmbiguousJavaFunction extends LuanFunction {
198 private final Map<Integer,List<LuaJavaFunction>> fnMap = new HashMap<Integer,List<LuaJavaFunction>>(); 198 private final Map<Integer,List<LuanJavaFunction>> fnMap = new HashMap<Integer,List<LuanJavaFunction>>();
199 199
200 AmbiguousJavaFunction(List<LuaJavaFunction> fns) { 200 AmbiguousJavaFunction(List<LuanJavaFunction> fns) {
201 for( LuaJavaFunction fn : fns ) { 201 for( LuanJavaFunction fn : fns ) {
202 Integer n = fn.getParameterTypes().length; 202 Integer n = fn.getParameterTypes().length;
203 List<LuaJavaFunction> list = fnMap.get(n); 203 List<LuanJavaFunction> list = fnMap.get(n);
204 if( list==null ) { 204 if( list==null ) {
205 list = new ArrayList<LuaJavaFunction>(); 205 list = new ArrayList<LuanJavaFunction>();
206 fnMap.put(n,list); 206 fnMap.put(n,list);
207 } 207 }
208 list.add(fn); 208 list.add(fn);
209 } 209 }
210 } 210 }
211 211
212 @Override public Object[] call(LuaState lua,Object[] args) throws LuaException { 212 @Override public Object[] call(LuanState lua,Object[] args) throws LuanException {
213 for( LuaJavaFunction fn : fnMap.get(args.length) ) { 213 for( LuanJavaFunction fn : fnMap.get(args.length) ) {
214 try { 214 try {
215 return fn.call(lua,args); 215 return fn.call(lua,args);
216 } catch(IllegalArgumentException e) {} 216 } catch(IllegalArgumentException e) {}
217 } 217 }
218 throw new LuaException(lua,LuaElement.JAVA,"no method matched args"); 218 throw new LuanException(lua,LuanElement.JAVA,"no method matched args");
219 } 219 }
220 } 220 }
221 221
222 private static class InstanceOf { 222 private static class InstanceOf {
223 private final Object obj; 223 private final Object obj;