Mercurial Hosting > luan
changeset 55:9381b23ea9e1
various fixes
git-svn-id: https://luan-java.googlecode.com/svn/trunk@56 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 04 Jan 2013 04:50:18 +0000 |
parents | f5b27ef14d73 |
children | c84274b18f0c |
files | src/luan/LuanJavaFunction.java src/luan/interp/EqExpr.java src/luan/interp/LuanParser.java src/luan/lib/JavaLib.java src/luan/lib/MathLib.java src/luan/tools/CmdLine.java |
diffstat | 6 files changed, 55 insertions(+), 9 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/LuanJavaFunction.java Mon Dec 31 19:45:06 2012 +0000 +++ b/src/luan/LuanJavaFunction.java Fri Jan 04 04:50:18 2013 +0000 @@ -7,6 +7,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.Arrays; public final class LuanJavaFunction extends LuanFunction { @@ -44,13 +45,19 @@ } @Override public Object[] call(LuanState luan,Object[] args) throws LuanException { + try { + return rawCall(luan,args); + } catch(IllegalArgumentException e) { + checkArgs(luan,args); + throw e; + } + } + + public Object[] rawCall(LuanState luan,Object[] args) throws LuanException { args = fixArgs(luan,args); Object rtn; try { rtn = method.invoke(obj,args); - } catch(IllegalArgumentException e) { - checkArgs(luan,args); - throw e; } catch(IllegalAccessException e) { throw new RuntimeException(e); } catch(InvocationTargetException e) { @@ -324,6 +331,11 @@ Set<Object> set = (Set<Object>)obj; return new LuanTable(set); } + Class cls = obj.getClass(); + if( cls.isArray() && !cls.getComponentType().isPrimitive() ) { + Object[] a = (Object[])obj; + return new LuanTable(Arrays.asList(a)); + } return obj; } };
--- a/src/luan/interp/EqExpr.java Mon Dec 31 19:45:06 2012 +0000 +++ b/src/luan/interp/EqExpr.java Fri Jan 04 04:50:18 2013 +0000 @@ -23,7 +23,7 @@ Number n2 = (Number)o2; return n1.doubleValue() == n2.doubleValue(); } - if( !o1.getClass().equals(o2.getClass()) ) + if( o1==null || o2==null || !o1.getClass().equals(o2.getClass()) ) return false; LuanTable mt1 = luan.getMetatable(o1); LuanTable mt2 = luan.getMetatable(o2);
--- a/src/luan/interp/LuanParser.java Mon Dec 31 19:45:06 2012 +0000 +++ b/src/luan/interp/LuanParser.java Fri Jan 04 04:50:18 2013 +0000 @@ -1092,8 +1092,7 @@ FirstOf( AnyOf(" \t"), Comment(), - Sequence( '\\', EndOfLine() ), - Sequence( AnyOf("\r\n"), parens > 0 ) + Sequence( '\\', EndOfLine() ) ) ); }
--- a/src/luan/lib/JavaLib.java Mon Dec 31 19:45:06 2012 +0000 +++ b/src/luan/lib/JavaLib.java Fri Jan 04 04:50:18 2013 +0000 @@ -12,6 +12,7 @@ import java.util.List; import java.util.ArrayList; import java.util.Iterator; +import java.util.Collections; import luan.Luan; import luan.LuanState; import luan.LuanTable; @@ -110,7 +111,7 @@ } } } - throw new LuanException(luan,LuanElement.JAVA,"invalid index for java object: "+key); + throw new LuanException(luan,LuanElement.JAVA,"invalid member for java object: "+key); } private static Object member(Object obj,List<AccessibleObject> members) throws LuanException { @@ -166,7 +167,10 @@ } memberMap.put(cls,clsMap); } - return clsMap.get(name); + List<AccessibleObject> rtn = clsMap.get(name); + if( rtn==null ) + rtn = Collections.emptyList(); + return rtn; } private static synchronized List<AccessibleObject> getStaticMembers(Class cls,String name) { @@ -222,7 +226,7 @@ @Override public Object[] call(LuanState luan,Object[] args) throws LuanException { for( LuanJavaFunction fn : fnMap.get(args.length) ) { try { - return fn.call(luan,args); + return fn.rawCall(luan,args); } catch(IllegalArgumentException e) {} } throw new LuanException(luan,LuanElement.JAVA,"no method matched args");
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/lib/MathLib.java Fri Jan 04 04:50:18 2013 +0000 @@ -0,0 +1,29 @@ +package luan.lib; + +import luan.LuanState; +import luan.LuanTable; +import luan.LuanJavaFunction; + + +public final class MathLib { + + public static void register(LuanState luan) { + LuanTable module = new LuanTable(); + LuanTable global = luan.global(); + global.put("math",module); + try { + add( module, "floor", Double.TYPE ); + } catch(NoSuchMethodException e) { + throw new RuntimeException(e); + } + } + + private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { + t.put( method, new LuanJavaFunction(MathLib.class.getMethod(method,parameterTypes),null) ); + } + + public static double floor(double x) { + return Math.floor(x); + } + +}
--- a/src/luan/tools/CmdLine.java Mon Dec 31 19:45:06 2012 +0000 +++ b/src/luan/tools/CmdLine.java Fri Jan 04 04:50:18 2013 +0000 @@ -4,6 +4,7 @@ import java.util.Scanner; import luan.lib.BasicLib; import luan.lib.JavaLib; +import luan.lib.MathLib; import luan.lib.StringLib; import luan.lib.TableLib; import luan.lib.HtmlLib; @@ -21,6 +22,7 @@ LuanState luan = LuanCompiler.newLuanState(); BasicLib.register(luan); JavaLib.register(luan); + MathLib.register(luan); StringLib.register(luan); TableLib.register(luan); HtmlLib.register(luan);