Mercurial Hosting > luan
changeset 60:a68ccb7aaa9c
implement setting java field values
git-svn-id: https://luan-java.googlecode.com/svn/trunk@61 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Mon, 07 Jan 2013 05:41:59 +0000 |
parents | 5c9cadd0acce |
children | 183d470be7e8 |
files | src/luan/LuanJavaFunction.java src/luan/interp/LuanParser.java src/luan/interp/SetTableEntry.java src/luan/lib/JavaLib.java |
diffstat | 4 files changed, 55 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/LuanJavaFunction.java Mon Jan 07 03:36:28 2013 +0000 +++ b/src/luan/LuanJavaFunction.java Mon Jan 07 05:41:59 2013 +0000 @@ -40,6 +40,10 @@ } } + @Override public String toString() { + return "java-function: " + method; + } + public Class<?>[] getParameterTypes() { return method.getParameterTypes(); }
--- a/src/luan/interp/LuanParser.java Mon Jan 07 03:36:28 2013 +0000 +++ b/src/luan/interp/LuanParser.java Mon Jan 07 05:41:59 2013 +0000 @@ -1092,8 +1092,8 @@ FirstOf( AnyOf(" \t"), Comment(), - Sequence( '\\', EndOfLine() ), - Sequence( AnyOf("\r\n"), parens > 0 ) + Sequence( '\\', EndOfLine() )//, +// Sequence( AnyOf("\r\n"), parens > 0 ) ) ); }
--- a/src/luan/interp/SetTableEntry.java Mon Jan 07 03:36:28 2013 +0000 +++ b/src/luan/interp/SetTableEntry.java Mon Jan 07 05:41:59 2013 +0000 @@ -40,6 +40,7 @@ if( h instanceof LuanFunction ) { LuanFunction fn = (LuanFunction)h; luan.call(fn,se,"__newindex",t,key,value); + return; } newindex(luan,h,key,value); }
--- a/src/luan/lib/JavaLib.java Mon Jan 07 03:36:28 2013 +0000 +++ b/src/luan/lib/JavaLib.java Mon Jan 07 05:41:59 2013 +0000 @@ -41,6 +41,7 @@ private static final LuanTable mt = new LuanTable(); static { add( mt, "__index", LuanState.class, Object.class, Object.class ); + add( mt, "__newindex", LuanState.class, Object.class, Object.class, Object.class ); } private static void add(LuanTable t,String method,Class<?>... parameterTypes) { @@ -144,6 +145,53 @@ } } + public static void __newindex(LuanState luan,Object obj,Object key,Object value) throws LuanException { + if( obj instanceof Static ) { + if( key instanceof String ) { + String name = (String)key; + Static st = (Static)obj; + Class cls = st.cls; + List<Member> members = getStaticMembers(cls,name); + if( !members.isEmpty() ) { + if( members.size() != 1 ) + throw new RuntimeException("not field '"+name+"' of "+obj); + setMember(obj,members,value); + return; + } + } + throw new LuanException(luan,LuanElement.JAVA,"invalid member '"+key+"' for: "+obj); + } + Class cls = obj.getClass(); + if( cls.isArray() ) { + Integer i = Luan.asInteger(key); + if( i != null ) { + Array.set(obj,i,value); + return; + } + throw new LuanException(luan,LuanElement.JAVA,"invalid member '"+key+"' for java array: "+obj); + } + if( key instanceof String ) { + String name = (String)key; + List<Member> members = getMembers(cls,name); + if( !members.isEmpty() ) { + if( members.size() != 1 ) + throw new RuntimeException("not field '"+name+"' of "+obj); + setMember(obj,members,value); + return; + } + } + throw new LuanException(luan,LuanElement.JAVA,"invalid member '"+key+"' for java object: "+obj); + } + + private static void setMember(Object obj,List<Member> members,Object value) { + Field field = (Field)members.get(0); + try { + field.set(obj,value); + } catch(IllegalAccessException e) { + throw new RuntimeException(e); + } + } + private static Map<Class,Map<String,List<Member>>> memberMap = new HashMap<Class,Map<String,List<Member>>>(); private static synchronized List<Member> getMembers(Class cls,String name) {