changeset 304:03e9cda4748d

add JavaLuan.Static.luan_proxy() git-svn-id: https://luan-java.googlecode.com/svn/trunk@305 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 18 Dec 2014 12:06:56 +0000
parents fdb4bd391c28
children 5e7450ac27f2
files core/src/luan/modules/JavaLuan.java
diffstat 1 files changed, 36 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/modules/JavaLuan.java	Thu Dec 18 07:51:29 2014 +0000
+++ b/core/src/luan/modules/JavaLuan.java	Thu Dec 18 12:06:56 2014 +0000
@@ -74,8 +74,12 @@
 							return new AmbiguousJavaFunction(fns);
 						}
 					}
+/*
 				} else if( "assert".equals(name) ) {
 					return new LuanJavaFunction(assertClass,new AssertClass(cls));
+*/
+				} else if( "luan_proxy".equals(name) ) {
+					return new LuanJavaFunction(luan_proxyMethod,st);
 				} else {
 					List<Member> members = getStaticMembers(cls,name);
 					if( !members.isEmpty() ) {
@@ -308,6 +312,36 @@
 		@Override public boolean isSynthetic() {
 			return cls.isSynthetic();
 		}
+
+		public Object luan_proxy(final LuanState luan,final LuanTable t,final Object base) throws LuanException {
+			return Proxy.newProxyInstance(
+				cls.getClassLoader(),
+				new Class[]{cls},
+				new InvocationHandler() {
+					public Object invoke(Object proxy,Method method, Object[] args)
+						throws Throwable
+					{
+						if( args==null )
+							args = new Object[0];
+						String name = method.getName();
+						Object fnObj = t.get(name);
+						if( fnObj==null && base!=null )
+							return method.invoke(base,args);
+						LuanFunction fn = luan.checkFunction(fnObj);
+						return Luan.first(luan.call(fn,name,args));
+					}
+				}
+			);
+		}
+	}
+	private static final Method luan_proxyMethod;
+	static {
+		try {
+			luan_proxyMethod = Static.class.getMethod("luan_proxy",LuanState.class,LuanTable.class,Object.class);
+			luan_proxyMethod.setAccessible(true);
+		} catch(NoSuchMethodException e) {
+			throw new RuntimeException(e);
+		}
 	}
 
 	public static Static load(LuanState luan,String name) throws LuanException {
@@ -371,7 +405,7 @@
 		}
 	}
 
-
+/*
 	private static class AssertClass {
 		private final Class cls;
 
@@ -419,6 +453,7 @@
 			}
 		);
 	}
+*/