Mercurial Hosting > luan
changeset 157:1de3e4a6e82d
remove LuanJavaFunction.RTN_NUMBER_ARRAY;
add request.java and response.java;
convert java Object[] members to List;
let LuanState.eval() take env;
improve web_run.luan;
rename LuanParser.JspExpressions to TemplateExpressions and add TemplateStmt;
git-svn-id: https://luan-java.googlecode.com/svn/trunk@158 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 18 Jun 2014 21:07:11 +0000 |
parents | 925ba2d59b9d |
children | cced1c4d3575 |
files | src/luan/LuanJavaFunction.java src/luan/LuanState.java src/luan/interp/LuanParser.java src/luan/lib/HttpLib.java src/luan/lib/JavaLib.java src/luan/tools/web_run.luan |
diffstat | 6 files changed, 62 insertions(+), 40 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/LuanJavaFunction.java Wed Jun 18 08:16:50 2014 +0000 +++ b/src/luan/LuanJavaFunction.java Wed Jun 18 21:07:11 2014 +0000 @@ -171,24 +171,10 @@ } }; - private static final RtnConverter RTN_NUMBER_ARRAY = new RtnConverter() { - @Override public Object convert(Object obj) { - if( obj == null ) - return null; - Object[] rtn = new Object[Array.getLength(obj)]; - for( int i=0; i<rtn.length; i++ ) { - rtn[i] = Array.get(obj,i); - } - return rtn; - } - }; - private static RtnConverter getRtnConverter(JavaMethod m) { Class<?> rtnType = m.getReturnType(); if( rtnType == Void.TYPE ) return RTN_NOTHING; - if( rtnType.isArray() && isNumber(rtnType.getComponentType()) ) - return RTN_NUMBER_ARRAY; return RTN_SAME; }
--- a/src/luan/LuanState.java Wed Jun 18 08:16:50 2014 +0000 +++ b/src/luan/LuanState.java Wed Jun 18 21:07:11 2014 +0000 @@ -151,8 +151,12 @@ } public final Object eval(String cmd) { + return eval(cmd,new LuanTable()); + } + + public final Object eval(String cmd,LuanTable env) { try { - LuanFunction fn = BasicLib.load(this,cmd,"eval",new LuanTable(),true); + LuanFunction fn = BasicLib.load(this,cmd,"eval",env,true); return call(fn); } catch(LuanException e) { throw new RuntimeException(e);
--- a/src/luan/interp/LuanParser.java Wed Jun 18 08:16:50 2014 +0000 +++ b/src/luan/interp/LuanParser.java Wed Jun 18 21:07:11 2014 +0000 @@ -76,19 +76,19 @@ static final In NOTHING = new In(false,false); final boolean parens; - final boolean jsp; + final boolean template; - private In(boolean parens,boolean jsp) { + private In(boolean parens,boolean template) { this.parens = parens; - this.jsp = jsp; + this.template = template; } In parens() { - return parens ? this : new In(true,jsp); + return parens ? this : new In(true,template); } - In jsp() { - return jsp ? this : new In(parens,true); + In template() { + return template ? this : new In(parens,true); } } @@ -225,7 +225,15 @@ if( parser.match( "--" ) ) { while( parser.noneOf("\r\n") ); } - return EndOfLine() ? parser.success() : parser.failure(); + if( EndOfLine() ) + return parser.success(); + parser.rollback(); + Stmt stmt = TemplateStmt(); + if( stmt != null ) { + stmts.add(stmt); + return parser.success(); + } + return parser.failure(); } private boolean EndOfLine() { @@ -255,22 +263,34 @@ } } - private Expressions JspExpressions(In in) throws ParseException { - if( in.jsp ) + private Stmt TemplateStmt() throws ParseException { + int start = parser.currentIndex(); + Expressions exp = TemplateExpressions(In.NOTHING); + if( exp == null ) + return null; + Expr fnExp = (Expr)nameVar(start,"Io").expr(); + fnExp = new IndexExpr( se(start), fnExp, new ConstExpr("stdout") ); + fnExp = new IndexExpr( se(start), fnExp, new ConstExpr("write") ); + FnCall fnCall = new FnCall( se(start), fnExp, exp ); + return new FnCallStmt(fnCall); + } + + private Expressions TemplateExpressions(In in) throws ParseException { + if( in.template ) return null; int start = parser.begin(); if( !parser.match( "%>" ) ) return parser.failure(null); EndOfLine(); - In inJsp = in.jsp(); + In inTemplate = in.template(); List<Expressions> builder = new ArrayList<Expressions>(); while(true) { if( parser.match( "<%=" ) ) { - Spaces(inJsp); - builder.add( RequiredExpr(inJsp) ); + Spaces(inTemplate); + builder.add( RequiredExpr(inTemplate) ); RequiredMatch( "%>" ); } else if( parser.match( "<%" ) ) { - Spaces(inJsp); + Spaces(inTemplate); return parser.success(ExpList.build(builder)); } else { int i = parser.currentIndex(); @@ -278,7 +298,7 @@ if( parser.match( "%>" ) ) throw parser.exception("'%>' unexpected"); if( !parser.anyChar() ) - throw parser.exception("Unclosed JSP expressions"); + throw parser.exception("Unclosed template expression"); } while( !parser.test( "<%" ) ); String match = parser.textFrom(i); builder.add( new ConstExpr(match) ); @@ -336,7 +356,7 @@ FnCall require = new FnCall( se, importExpr, new ConstExpr(modName) ); Settable settable; if( interactive ) { - settable = new SetTableEntry( se(start), env(), new ConstExpr(varName) ); + settable = nameVar(se,varName).settable(); } else { addSymbol( varName ); settable = new SetLocalVar(symbolsSize()-1); @@ -549,7 +569,7 @@ parser.begin(); Expressions exp; return (exp = VarArgs(in)) != null - || (exp = JspExpressions(in)) != null + || (exp = TemplateExpressions(in)) != null || (exp = OrExpr(in)) != null ? parser.success(exp) : parser.failure((Expressions)null) @@ -894,16 +914,20 @@ } private Var nameVar(final int start,final String name) { + return nameVar(se(start),name); + } + + private Var nameVar(final LuanSource.Element se,final String name) { return new Var() { public Expr expr() { int index = stackIndex(name); if( index != -1 ) - return new GetLocalVar(se(start),index); + return new GetLocalVar(se,index); index = upValueIndex(name); if( index != -1 ) - return new GetUpVar(se(start),index); - return new IndexExpr( se(start), env(), new ConstExpr(name) ); + return new GetUpVar(se,index); + return new IndexExpr( se, env(), new ConstExpr(name) ); } public Settable settable() { @@ -913,7 +937,7 @@ index = upValueIndex(name); if( index != -1 ) return new SetUpVar(index); - return new SetTableEntry( se(start), env(), new ConstExpr(name) ); + return new SetTableEntry( se, env(), new ConstExpr(name) ); } }; } @@ -971,7 +995,7 @@ builder.add( new ConstExpr(s) ); return true; } - Expressions exps = JspExpressions(in); + Expressions exps = TemplateExpressions(in); if( exps != null ) { builder.add(exps); return true;
--- a/src/luan/lib/HttpLib.java Wed Jun 18 08:16:50 2014 +0000 +++ b/src/luan/lib/HttpLib.java Wed Jun 18 21:07:11 2014 +0000 @@ -94,6 +94,7 @@ private LuanTable requestTable() throws NoSuchMethodException { LuanTable req = new LuanTable(); + req.put("java",request); req.put( "get_attribute", new LuanJavaFunction(HttpServletRequest.class.getMethod("getAttribute",String.class),request) ); req.put( "set_attribute", new LuanJavaFunction(HttpServletRequest.class.getMethod("setAttribute",String.class,Object.class),request) ); req.put( "get_parameter", new LuanJavaFunction(HttpServletRequest.class.getMethod("getParameter",String.class),request) ); @@ -112,6 +113,7 @@ private LuanTable responseTable() throws NoSuchMethodException { LuanTable resp = new LuanTable(); + resp.put("java",response); add( resp, "send_redirect", String.class ); add( resp, "send_error", Integer.TYPE, String.class ); resp.put( "contains_header", new LuanJavaFunction(HttpServletResponse.class.getMethod("containsHeader",String.class),response) );
--- a/src/luan/lib/JavaLib.java Wed Jun 18 08:16:50 2014 +0000 +++ b/src/luan/lib/JavaLib.java Wed Jun 18 21:07:11 2014 +0000 @@ -15,6 +15,7 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.Collections; +import java.util.Arrays; import luan.Luan; import luan.LuanState; import luan.LuanTable; @@ -146,7 +147,8 @@ return member; } else if( member instanceof Field ) { Field field = (Field)member; - return field.get(obj); + Object rtn = field.get(obj); + return rtn instanceof Object[] ? Arrays.asList((Object[])rtn) : rtn; } else { Method method = (Method)member; return new LuanJavaFunction(method,obj);
--- a/src/luan/tools/web_run.luan Wed Jun 18 08:16:50 2014 +0000 +++ b/src/luan/tools/web_run.luan Wed Jun 18 21:07:11 2014 +0000 @@ -17,17 +17,21 @@ end function page() + local content_type = Http.request.get_parameter("content_type") + if content_type ~= nil then + Http.response.set_content_type(content_type) + end Io.stdout = Http.response.text_writer() local code = Http.request.get_parameter "code" local env = { - write = Io.stdout.write; request = Http.request; response = Http.response; } try - local fn = load(code,"<web_run>",env) - fn() + local run = load(code,"<web_run>",env) + run() catch e do + Http.response.set_content_type "text/plain" print(e) print() print()