Mercurial Hosting > luan
changeset 52:0c0f2b107099
add WebRun
git-svn-id: https://luan-java.googlecode.com/svn/trunk@53 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Mon, 31 Dec 2012 03:15:20 +0000 |
parents | 28dfd91a816c |
children | 54a61d1dc070 |
files | src/luan/tools/CmdLine.java src/luan/tools/WebRun.java src/luan/tools/WebShell.java |
diffstat | 3 files changed, 98 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/tools/CmdLine.java Fri Dec 28 20:43:06 2012 +0000 +++ b/src/luan/tools/CmdLine.java Mon Dec 31 03:15:20 2012 +0000 @@ -6,6 +6,7 @@ import luan.lib.JavaLib; import luan.lib.StringLib; import luan.lib.TableLib; +import luan.lib.HtmlLib; import luan.Luan; import luan.LuanState; import luan.LuanFunction; @@ -16,12 +17,18 @@ public class CmdLine { - public static void main(String[] args) { + public static LuanState newStandardLuan() { LuanState luan = LuanCompiler.newLuanState(); BasicLib.register(luan); JavaLib.register(luan); StringLib.register(luan); TableLib.register(luan); + HtmlLib.register(luan); + return luan; + } + + public static void main(String[] args) { + LuanState luan = newStandardLuan(); BasicLib.make_standard(luan); boolean interactive = false; boolean showVersion = false; @@ -106,8 +113,7 @@ System.out.print("> "); String input = new Scanner(System.in).nextLine(); try { - LuanFunction fn = BasicLib.load(luan,input,"stdin"); - Object[] rtn = luan.call(fn,null,null); + Object[] rtn = eval(luan,input,"stdin"); if( rtn.length > 0 ) BasicLib.print(luan,rtn); } catch(LuanException e) { @@ -115,4 +121,9 @@ } } } + + 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); + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/tools/WebRun.java Mon Dec 31 03:15:20 2012 +0000 @@ -0,0 +1,82 @@ +package luan.tools; + +import java.io.IOException; +import java.io.PrintStream; +import javax.servlet.ServletException; +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import luan.LuanState; +import luan.LuanException; +import luan.lib.HtmlLib; + + +public class WebRun extends HttpServlet { + private static final Logger logger = LoggerFactory.getLogger(WebRun.class); + + protected LuanState newLuanState() throws LuanException { + return CmdLine.newStandardLuan(); + } + + protected void service(HttpServletRequest request,HttpServletResponse response) + throws ServletException, IOException + { + ServletOutputStream sout = response.getOutputStream(); + PrintStream out = new PrintStream(sout); + String contentType = request.getParameter("content_type"); + if( contentType != null ) + response.setContentType("text/plain"); + String code = request.getParameter("code"); + try { + LuanState luan = newLuanState(); + luan.out = out; + luan.global().put("request",request); + luan.global().put("response",response); + CmdLine.eval(luan,code,"WebRun"); + } catch(LuanException e) { + logger.error(null,e); + response.reset(); + response.setHeader("Content-Type","text/html"); + out.println( "<html>" ); + out.println( "<body>" ); + out.println( "<pre>" ); + out.println( e ); + out.println(); + out.println( addLineNumbers(HtmlLib.encode(code)) ); + out.println( "</pre>" ); + out.println( "</body>" ); + out.println( "</html>" ); + } + } + + public static String addLineNumbers(String s) { + StringBuilder buf = new StringBuilder(); + int line = 1; + int i = 0; + while(true) { + buf.append( fmt(line++,3) ); + buf.append(" "); + int i2 = s.indexOf('\n',i); + if( i2 == -1 ) { + buf.append( s.substring(i) ); + break; + } + buf.append( s.substring(i,i2+1) ); + i = i2 + 1; + } + return buf.toString(); + } + + private static String fmt(int i,int w) { + StringBuilder buf = new StringBuilder(); + buf.append(i); + while( buf.length() < w ) { + buf.insert(0,' '); + } + return buf.toString(); + } + +}
--- a/src/luan/tools/WebShell.java Fri Dec 28 20:43:06 2012 +0000 +++ b/src/luan/tools/WebShell.java Mon Dec 31 03:15:20 2012 +0000 @@ -18,9 +18,6 @@ import luan.LuanException; import luan.interp.LuanCompiler; import luan.lib.BasicLib; -import luan.lib.JavaLib; -import luan.lib.StringLib; -import luan.lib.TableLib; import luan.lib.HtmlLib; @@ -28,18 +25,11 @@ private static final Logger logger = LoggerFactory.getLogger(WebShell.class); protected LuanState newLuanState() throws LuanException { - LuanState luan = LuanCompiler.newLuanState(); - BasicLib.register(luan); - JavaLib.register(luan); - StringLib.register(luan); - TableLib.register(luan); - HtmlLib.register(luan); - return luan; + return CmdLine.newStandardLuan(); } protected Object[] eval(LuanState luan,String cmd) throws LuanException { - LuanFunction fn = BasicLib.load(luan,cmd,"WebShell"); - return luan.call(fn,null,null); + return CmdLine.eval(luan,cmd,"WebShell"); } protected void service(HttpServletRequest request,HttpServletResponse response)