Mercurial Hosting > luan
diff core/src/luan/Luan.java @ 647:8e8c30b72e9b
move methods from LuanState to Luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 29 Mar 2016 20:39:14 -0600 |
parents | 6cc2f047019b |
children | d3e5414bdf4c |
line wrap: on
line diff
--- a/core/src/luan/Luan.java Tue Mar 29 19:58:39 2016 -0600 +++ b/core/src/luan/Luan.java Tue Mar 29 20:39:14 2016 -0600 @@ -85,5 +85,61 @@ return s; } + + // from LuanState + + public static Boolean checkBoolean(Object obj) throws LuanException { + if( obj instanceof Boolean ) + return (Boolean)obj; + throw new LuanException("attempt to use a " + Luan.type(obj) + " value as a boolean" ); + } + + public static String checkString(Object obj) throws LuanException { + if( obj instanceof String ) + return (String)obj; + throw new LuanException("attempt to use a " + Luan.type(obj) + " value as a string" ); + } + + public static LuanFunction checkFunction(Object obj) throws LuanException { + if( obj instanceof LuanFunction ) + return (LuanFunction)obj; + throw new LuanException("attempt to call a " + Luan.type(obj) + " value" ); + } + + public static boolean isLessThan(LuanState luan,Object o1,Object o2) throws LuanException { + if( o1 instanceof Number && o2 instanceof Number ) { + Number n1 = (Number)o1; + Number n2 = (Number)o2; + return n1.doubleValue() < n2.doubleValue(); + } + if( o1 instanceof String && o2 instanceof String ) { + String s1 = (String)o1; + String s2 = (String)o2; + return s1.compareTo(s2) < 0; + } + LuanFunction fn = getBinHandler("__lt",o1,o2); + if( fn != null ) + return checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) ); + throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) ); + } + + public static LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException { + if( o1 instanceof LuanTable ) { + LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1); + if( f1 != null ) + return f1; + } + return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null; + } + + public static LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException { + Object f = t.getHandler(op); + if( f == null ) + return null; + return checkFunction(f); + } + + + private Luan() {} // never }