diff src/luan/LuanState.java @ 88:6ca02b188dba

add LuanBit to clean up code; add repr(); git-svn-id: https://luan-java.googlecode.com/svn/trunk@89 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 27 Feb 2013 23:50:32 +0000
parents 6db8f286fa6c
children 1f8b6edc2b08
line wrap: on
line diff
--- a/src/luan/LuanState.java	Wed Feb 27 19:42:09 2013 +0000
+++ b/src/luan/LuanState.java	Wed Feb 27 23:50:32 2013 +0000
@@ -19,6 +19,9 @@
 public abstract class LuanState implements DeepCloneable<LuanState> {
 	public static final String _G = "_G";
 
+	public final LuanBit JAVA = bit(LuanElement.JAVA);
+	public final LuanBit COMPILER = bit(LuanElement.COMPILER);
+
 	private LuanTable loaded;
 	private LuanTable preload;
 	private final List<String> defaultMods;
@@ -124,7 +127,7 @@
 
 	public final Object[] eval(String cmd,String sourceName,LuanTable env) throws LuanException {
 		LuanFunction fn = BasicLib.load(this,cmd,sourceName,env);
-		return call(fn,null,null);
+		return JAVA.call(fn,null);
 	}
 
 
@@ -145,47 +148,8 @@
 		mtGetters.add(mg);
 	}
 
-	public final Object[] call(LuanFunction fn,LuanElement calledFrom,String fnName,Object... args) throws LuanException {
-		stackTrace.add( new StackTraceElement(calledFrom,fnName) );
-		try {
-			return fn.call(this,args);
-		} finally {
-			stackTrace.remove(stackTrace.size()-1);
-		}
-	}
-
-	public final String checkString(LuanElement el,Object obj) throws LuanException {
-		String s = Luan.asString(obj);
-		if( s == null )
-			throw new LuanException( this, el, "attempt to use a " + Luan.type(obj) + " as a string" );
-		return s;
-	}
-
-	public final Number checkNumber(LuanElement el,Object obj) throws LuanException {
-		Number n = Luan.toNumber(obj);
-		if( n == null )
-			throw new LuanException( this, el, "attempt to perform arithmetic on a " + Luan.type(obj) + " value" );
-		return n;
-	}
-
-	public final LuanFunction checkFunction(LuanElement el,Object obj) throws LuanException {
-		if( obj instanceof LuanFunction )
-			return (LuanFunction)obj;
-		throw new LuanException( this, el, "attempt to call a " + Luan.type(obj) + " value" );
-	}
-
-	public final String toString(LuanElement el,Object obj) throws LuanException {
-		LuanFunction fn = getHandlerFunction(el,"__tostring",obj);
-		if( fn != null )
-			return checkString( el, Luan.first( call(fn,el,"__tostring",obj) ) );
-		return Luan.toString(obj);
-	}
-
-	public final LuanFunction getHandlerFunction(LuanElement el,String op,Object obj) throws LuanException {
-		Object f = getHandler(op,obj);
-		if( f == null )
-			return null;
-		return checkFunction(el,f);
+	public final LuanBit bit(LuanElement el) {
+		return new LuanBit(this,el);
 	}
 
 	public final Object getHandler(String op,Object obj) {
@@ -193,28 +157,4 @@
 		return t==null ? null : t.get(op);
 	}
 
-
-	public final LuanFunction getBinHandler(LuanElement el,String op,Object o1,Object o2) throws LuanException {
-		LuanFunction f1 = getHandlerFunction(el,op,o1);
-		if( f1 != null )
-			return f1;
-		return getHandlerFunction(el,op,o2);
-	}
-
-	public final boolean isLessThan(LuanElement el,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(el,"__lt",o1,o2);
-		if( fn != null )
-			return Luan.toBoolean( Luan.first(call(fn,el,"__lt",o1,o2)) );
-		throw new LuanException( this, el, "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
-	}
 }