changeset 59:5c9cadd0acce

move standard calls from CmdLine to LuanState git-svn-id: https://luan-java.googlecode.com/svn/trunk@60 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 07 Jan 2013 03:36:28 +0000
parents d6c454b7f58c
children a68ccb7aaa9c
files src/luan/LuanState.java src/luan/LuanTable.java src/luan/tools/CmdLine.java src/luan/tools/WebRun.java src/luan/tools/WebShell.java
diffstat 5 files changed, 37 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
diff -r d6c454b7f58c -r 5c9cadd0acce src/luan/LuanState.java
--- a/src/luan/LuanState.java	Mon Jan 07 00:18:35 2013 +0000
+++ b/src/luan/LuanState.java	Mon Jan 07 03:36:28 2013 +0000
@@ -4,9 +4,35 @@
 import java.io.PrintStream;
 import java.util.List;
 import java.util.ArrayList;
+import luan.interp.LuanCompiler;
+import luan.lib.BasicLib;
+import luan.lib.JavaLib;
+import luan.lib.MathLib;
+import luan.lib.StringLib;
+import luan.lib.TableLib;
+import luan.lib.HtmlLib;
 
 
 public abstract class LuanState {
+
+	public static LuanState newStandard() {
+		LuanState luan = LuanCompiler.newLuanState();
+		BasicLib.register(luan);
+		JavaLib.register(luan);
+		MathLib.register(luan);
+		StringLib.register(luan);
+		TableLib.register(luan);
+		HtmlLib.register(luan);
+		return luan;
+	}
+
+	public Object[] eval(String cmd,String sourceName) throws LuanException {
+		LuanFunction fn = BasicLib.load(this,cmd,sourceName);
+		return call(fn,null,null);
+	}
+
+
+
 	private final LuanTable global = new LuanTable();
 	private final List<MetatableGetter> mtGetters = new ArrayList<MetatableGetter>();
 	final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
diff -r d6c454b7f58c -r 5c9cadd0acce src/luan/LuanTable.java
--- a/src/luan/LuanTable.java	Mon Jan 07 00:18:35 2013 +0000
+++ b/src/luan/LuanTable.java	Mon Jan 07 03:36:28 2013 +0000
@@ -34,15 +34,15 @@
 		}
 	}
 
-	boolean isList() {
+	public boolean isList() {
 		return map==null || map.isEmpty();
 	}
 
-	List<Object> asList() {
+	public List<Object> asList() {
 		return list!=null ? list : Collections.emptyList();
 	}
 
-	Map<Object,Object> asMap() {
+	public Map<Object,Object> asMap() {
 		if( list == null || list.isEmpty() )
 			return map!=null ? map : Collections.emptyMap();
 		Map<Object,Object> rtn = map!=null ? new HashMap<Object,Object>(map) : new HashMap<Object,Object>();
@@ -53,7 +53,7 @@
 		return rtn;
 	}
 
-	boolean isSet() {
+	public boolean isSet() {
 		if( list != null ) {
 			for( Object obj : list ) {
 				if( obj!=null && !obj.equals(Boolean.TRUE) )
@@ -69,7 +69,7 @@
 		return true;
 	}
 
-	Set<Object> asSet() {
+	public Set<Object> asSet() {
 		if( list == null || list.isEmpty() )
 			return map!=null ? map.keySet() : Collections.emptySet();
 		Set<Object> rtn = map!=null ? new HashSet<Object>(map.keySet()) : new HashSet<Object>();
diff -r d6c454b7f58c -r 5c9cadd0acce src/luan/tools/CmdLine.java
--- a/src/luan/tools/CmdLine.java	Mon Jan 07 00:18:35 2013 +0000
+++ b/src/luan/tools/CmdLine.java	Mon Jan 07 03:36:28 2013 +0000
@@ -3,34 +3,17 @@
 import java.util.Arrays;
 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;
 import luan.Luan;
 import luan.LuanState;
 import luan.LuanFunction;
 import luan.LuanTable;
 import luan.LuanException;
-import luan.interp.LuanCompiler;
 
 
 public class CmdLine {
 
-	public static LuanState newStandardLuan() {
-		LuanState luan = LuanCompiler.newLuanState();
-		BasicLib.register(luan);
-		JavaLib.register(luan);
-		MathLib.register(luan);
-		StringLib.register(luan);
-		TableLib.register(luan);
-		HtmlLib.register(luan);
-		return luan;
-	}
-
 	public static void main(String[] args) {
-		LuanState luan = newStandardLuan();
+		LuanState luan = LuanState.newStandard();
 		BasicLib.make_standard(luan);
 		boolean interactive = false;
 		boolean showVersion = false;
@@ -115,7 +98,7 @@
 			System.out.print("> ");
 			String input = new Scanner(System.in).nextLine();
 			try {
-				Object[] rtn = eval(luan,input,"stdin");
+				Object[] rtn = luan.eval(input,"stdin");
 				if( rtn.length > 0 )
 					BasicLib.print(luan,rtn);
 			} catch(LuanException e) {
@@ -124,8 +107,4 @@
 		}
 	}
 
-	public static Object[] eval(LuanState luan,String cmd,String sourceName) throws LuanException {
-		LuanFunction fn = BasicLib.load(luan,cmd,sourceName);
-		return luan.call(fn,null,null);
-	}
 }
diff -r d6c454b7f58c -r 5c9cadd0acce src/luan/tools/WebRun.java
--- a/src/luan/tools/WebRun.java	Mon Jan 07 00:18:35 2013 +0000
+++ b/src/luan/tools/WebRun.java	Mon Jan 07 03:36:28 2013 +0000
@@ -18,7 +18,7 @@
 	private static final Logger logger = LoggerFactory.getLogger(WebRun.class);
 
 	protected LuanState newLuanState() throws LuanException {
-		return CmdLine.newStandardLuan();
+		return LuanState.newStandard();
 	}
 
 	protected void service(HttpServletRequest request,HttpServletResponse response)
@@ -35,7 +35,7 @@
 			luan.out = out;
 			luan.global().put("request",request);
 			luan.global().put("response",response);
-			CmdLine.eval(luan,code,"WebRun");
+			luan.eval(code,"WebRun");
 		} catch(LuanException e) {
 			logger.error(null,e);
 			response.reset();
diff -r d6c454b7f58c -r 5c9cadd0acce src/luan/tools/WebShell.java
--- a/src/luan/tools/WebShell.java	Mon Jan 07 00:18:35 2013 +0000
+++ b/src/luan/tools/WebShell.java	Mon Jan 07 03:36:28 2013 +0000
@@ -25,11 +25,11 @@
 	private static final Logger logger = LoggerFactory.getLogger(WebShell.class);
 
 	protected LuanState newLuanState() throws LuanException {
-		return CmdLine.newStandardLuan();
+		return LuanState.newStandard();
 	}
 
 	protected Object[] eval(LuanState luan,String cmd) throws LuanException {
-		return CmdLine.eval(luan,cmd,"WebShell");
+		return luan.eval(cmd,"WebShell");
 	}
 
 	protected void service(HttpServletRequest request,HttpServletResponse response)