Mercurial Hosting > luan
changeset 115:eacf6ce1b47d
add IoLib
git-svn-id: https://luan-java.googlecode.com/svn/trunk@116 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 29 May 2014 09:26:44 +0000 |
parents | c599206448b9 |
children | 1ff1c32417eb |
files | src/luan/LuanJavaFunction.java src/luan/LuanState.java src/luan/lib/BasicLib.java src/luan/lib/HttpLib.java src/luan/lib/IoLib.java src/luan/lib/Utils.java src/luan/tools/WebRun.java src/luan/tools/WebShell.java |
diffstat | 8 files changed, 312 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/LuanJavaFunction.java Mon May 26 05:39:54 2014 +0000 +++ b/src/luan/LuanJavaFunction.java Thu May 29 09:26:44 2014 +0000 @@ -74,11 +74,9 @@ Throwable cause = e.getCause(); if( cause instanceof Error ) throw (Error)cause; - if( cause instanceof RuntimeException ) - throw luan.JAVA.exception(cause); if( cause instanceof LuanException ) throw (LuanException)cause; - throw new RuntimeException(e); + throw luan.JAVA.exception(cause); } catch(InstantiationException e) { throw new RuntimeException(e); }
--- a/src/luan/LuanState.java Mon May 26 05:39:54 2014 +0000 +++ b/src/luan/LuanState.java Thu May 29 09:26:44 2014 +0000 @@ -15,6 +15,7 @@ import luan.lib.TableLib; import luan.lib.HtmlLib; import luan.lib.BinaryLib; +import luan.lib.IoLib; public abstract class LuanState implements DeepCloneable<LuanState> { @@ -25,10 +26,6 @@ private LuanTable preload; private LuanTable searchers; - public InputStream in = System.in; - public PrintStream out = System.out; - public PrintStream err = System.err; - private final List<MetatableGetter> mtGetters; final List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>(); @@ -117,6 +114,7 @@ luan.load(TableLib.NAME,TableLib.LOADER); luan.load(HtmlLib.NAME,HtmlLib.LOADER); luan.load(BinaryLib.NAME,BinaryLib.LOADER); + luan.load(IoLib.NAME,IoLib.LOADER); return luan; } catch(LuanException e) { throw new RuntimeException(e);
--- a/src/luan/lib/BasicLib.java Mon May 26 05:39:54 2014 +0000 +++ b/src/luan/lib/BasicLib.java Thu May 29 09:26:44 2014 +0000 @@ -6,6 +6,8 @@ import java.lang.reflect.Method; import java.util.Iterator; import java.util.Map; +import java.util.List; +import java.util.ArrayList; import luan.Luan; import luan.LuanState; import luan.LuanTable; @@ -63,12 +65,15 @@ } public static void print(LuanState luan,Object... args) throws LuanException { + LuanFunction write = (LuanFunction)luan.get("Io.stdout.write"); + List list = new ArrayList(); for( int i=0; i<args.length; i++ ) { if( i > 0 ) - luan.out.print('\t'); - luan.out.print( luan.JAVA.toString(args[i]) ); + list.add("\t"); + list.add(args[i]); } - luan.out.println(); + list.add("\n"); + write.call(luan,list.toArray()); } public static String type(Object obj) { @@ -83,16 +88,12 @@ } - public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException { - try { - String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName)); - return load(luan,src,fileName,false); - } catch(IOException e) { - throw luan.JAVA.exception(e); - } + public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException, IOException { + String src = fileName==null ? Utils.readAll(new InputStreamReader(System.in)) : Utils.read(new File(fileName)); + return load(luan,src,fileName,false); } - public static Object do_file(LuanState luan,String fileName) throws LuanException { + public static Object do_file(LuanState luan,String fileName) throws LuanException, IOException { LuanFunction fn = load_file(luan,fileName); return luan.JAVA.call(fn,null); }
--- a/src/luan/lib/HttpLib.java Mon May 26 05:39:54 2014 +0000 +++ b/src/luan/lib/HttpLib.java Thu May 29 09:26:44 2014 +0000 @@ -36,7 +36,7 @@ { LuanFunction fn = (LuanFunction)luan.get(FN_NAME); ServletOutputStream sout = response.getOutputStream(); - luan.out = new PrintStream(sout); + luan.set( "Io.stdout", IoLib.writer(new PrintStream(sout)) ); LuanTable module = (LuanTable)luan.loaded().get(NAME);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/lib/IoLib.java Thu May 29 09:26:44 2014 +0000 @@ -0,0 +1,226 @@ +package luan.lib; + +import java.io.OutputStream; +import java.io.PrintStream; +import java.io.Reader; +import java.io.Writer; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import luan.LuanState; +import luan.LuanTable; +import luan.LuanFunction; +import luan.LuanJavaFunction; +import luan.LuanException; + + +public final class IoLib { + + public static final String NAME = "Io"; + + public static final LuanFunction LOADER = new LuanFunction() { + @Override public Object call(LuanState luan,Object[] args) { + LuanTable module = new LuanTable(); + try { + add( module, "file", String.class ); + + LuanTable stdin = new LuanTable(); + stdin.put( "read_text", new LuanJavaFunction( + IoLib.class.getMethod( "stdin_read_text" ), null + ) ); + stdin.put( "read_binary", new LuanJavaFunction( + IoLib.class.getMethod( "stdin_read_binary" ), null + ) ); + stdin.put( "read_lines", new LuanJavaFunction( + IoLib.class.getMethod( "stdin_read_lines" ), null + ) ); + module.put( "stdin", stdin ); + } catch(NoSuchMethodException e) { + throw new RuntimeException(e); + } + module.put( "stdout", writer(System.out) ); + module.put( "stderr", writer(System.err) ); + return module; + } + }; + + private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException { + t.put( method, new LuanJavaFunction(IoLib.class.getMethod(method,parameterTypes),null) ); + } + + public static final class LuanFile { + private final File file; + + private LuanFile(String name) { + this.file = new File(name); + } + + public String read_text() throws IOException { + return Utils.read(file); + } + + public byte[] read_binary() throws IOException { + return Utils.readAll(file); + } + + public void write(LuanState luan,Object obj) throws LuanException, IOException { + if( obj instanceof String ) { + String s = (String)obj; + Utils.write(file,s); + return; + } + if( obj instanceof byte[] ) { + byte[] a = (byte[])obj; + Utils.writeAll(file,a); + return; + } + throw luan.JAVA.exception( "bad argument #1 to 'write' (string or binary expected)" ); + } + + public LuanTable text_writer() throws IOException { + return writer(new FileWriter(file)); + } + + public LuanTable binary_writer() throws IOException { + return binaryWriter(new FileOutputStream(file)); + } + + public LuanFunction read_lines() throws IOException { + return lines(new BufferedReader(new FileReader(file))); + } + } + + public static LuanTable file(String name) { + LuanTable tbl = new LuanTable(); + LuanFile file = new LuanFile(name); + try { + tbl.put( "read_text", new LuanJavaFunction( + LuanFile.class.getMethod( "read_text" ), file + ) ); + tbl.put( "read_binary", new LuanJavaFunction( + LuanFile.class.getMethod( "read_binary" ), file + ) ); + tbl.put( "write", new LuanJavaFunction( + LuanFile.class.getMethod( "write", LuanState.class, Object.class ), file + ) ); + tbl.put( "text_writer", new LuanJavaFunction( + LuanFile.class.getMethod( "text_writer" ), file + ) ); + tbl.put( "binary_writer", new LuanJavaFunction( + LuanFile.class.getMethod( "binary_writer" ), file + ) ); + tbl.put( "read_lines", new LuanJavaFunction( + LuanFile.class.getMethod( "read_lines" ), file + ) ); + } catch(NoSuchMethodException e) { + throw new RuntimeException(e); + } + return tbl; + } + + + public String stdin_read_text() throws IOException { + return Utils.readAll(new InputStreamReader(System.in)); + } + + public byte[] stdin_read_binary() throws IOException { + return Utils.readAll(System.in); + } + + public LuanFunction stdin_read_lines() throws IOException { + return lines(new BufferedReader(new InputStreamReader(System.in))); + } + + + public interface LuanWriter { + public void write(LuanState luan,Object... args) throws LuanException, IOException; + public void close() throws IOException; + } + + public static LuanTable writer(final PrintStream out) { + LuanWriter luanWriter = new LuanWriter() { + + public void write(LuanState luan,Object... args) throws LuanException { + for( Object obj : args ) { + out.print( luan.JAVA.toString(obj) ); + } + } + + public void close() { + out.close(); + } + }; + return writer(luanWriter); + } + + public static LuanTable writer(final Writer out) { + LuanWriter luanWriter = new LuanWriter() { + + public void write(LuanState luan,Object... args) throws LuanException, IOException { + for( Object obj : args ) { + out.write( luan.JAVA.toString(obj) ); + } + } + + public void close() throws IOException { + out.close(); + } + }; + return writer(luanWriter); + } + + private static LuanTable writer(LuanWriter luanWriter) { + LuanTable writer = new LuanTable(); + try { + writer.put( "write", new LuanJavaFunction( + LuanWriter.class.getMethod( "write", LuanState.class, new Object[0].getClass() ), luanWriter + ) ); + writer.put( "close", new LuanJavaFunction( + LuanWriter.class.getMethod( "close" ), luanWriter + ) ); + } catch(NoSuchMethodException e) { + throw new RuntimeException(e); + } + return writer; + } + + + public static LuanTable binaryWriter(final OutputStream out) { + LuanTable writer = new LuanTable(); + try { + writer.put( "write", new LuanJavaFunction( + OutputStream.class.getMethod( "write", new byte[0].getClass() ), out + ) ); + writer.put( "close", new LuanJavaFunction( + OutputStream.class.getMethod( "close" ), out + ) ); + } catch(NoSuchMethodException e) { + throw new RuntimeException(e); + } + return writer; + } + + public static LuanFunction lines(final BufferedReader in) { + return new LuanFunction() { + public Object call(LuanState luan,Object[] args) throws LuanException { + try { + if( args.length==1 && "close".equals(args[0]) ) { + in.close(); + return null; + } + String rtn = in.readLine(); + if( rtn==null ) + in.close(); + return rtn; + } catch(IOException e) { + throw luan.JAVA.exception(e); + } + } + }; + } + +}
--- a/src/luan/lib/Utils.java Mon May 26 05:39:54 2014 +0000 +++ b/src/luan/lib/Utils.java Thu May 29 09:26:44 2014 +0000 @@ -2,9 +2,17 @@ import java.io.File; import java.io.FileReader; +import java.io.FileWriter; import java.io.InputStreamReader; import java.io.Reader; +import java.io.Writer; import java.io.IOException; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; import java.net.URL; import luan.LuanState; import luan.LuanException; @@ -14,6 +22,8 @@ public final class Utils { private Utils() {} // never + private static final int bufSize = 8192; + public static void checkNotNull(LuanState luan,Object v,String expected) throws LuanException { if( v == null ) throw luan.JAVA.exception("bad argument #1 ("+expected+" expected, got nil)"); @@ -22,7 +32,7 @@ public static String readAll(Reader in) throws IOException { - char[] a = new char[8192]; + char[] a = new char[bufSize]; StringBuilder buf = new StringBuilder(); int n; while( (n=in.read(a)) != -1 ) { @@ -49,4 +59,59 @@ return s; } + public static void copyAll(InputStream in,OutputStream out) + throws IOException + { + byte[] a = new byte[bufSize]; + int n; + while( (n=in.read(a)) != -1 ) { + out.write(a,0,n); + } + } + + public static byte[] readAll(File file) + throws IOException + { + int len = (int)file.length(); + ByteArrayOutputStream out = new ByteArrayOutputStream(len) { + public byte[] toByteArray() { + return buf; + } + }; + FileInputStream in = new FileInputStream(file); + copyAll(in,out); + in.close(); + return out.toByteArray(); + } + + public static void write(File file,String s) + throws IOException + { + Writer out = new FileWriter(file); + out.write(s); + out.close(); + } + + public static void writeAll(byte[] a,OutputStream out) + throws IOException + { + copyAll(new ByteArrayInputStream(a),out); + } + + public static void writeAll(File file,byte[] a) + throws IOException + { + FileOutputStream fos = new FileOutputStream(file); + writeAll(a,fos); + fos.close(); + } + + public static byte[] readAll(InputStream in) + throws IOException + { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + copyAll(in,out); + return out.toByteArray(); + } + }
--- a/src/luan/tools/WebRun.java Mon May 26 05:39:54 2014 +0000 +++ b/src/luan/tools/WebRun.java Thu May 29 09:26:44 2014 +0000 @@ -13,6 +13,7 @@ import luan.LuanTable; import luan.LuanException; import luan.lib.HtmlLib; +import luan.lib.IoLib; public class WebRun extends HttpServlet { @@ -33,7 +34,7 @@ String code = request.getParameter("code"); try { LuanState luan = newLuanState(); - luan.out = out; + luan.set( "Io.stdout", IoLib.writer(out) ); LuanTable env = luan.global(); env.put("request",request); env.put("response",response);
--- a/src/luan/tools/WebShell.java Mon May 26 05:39:54 2014 +0000 +++ b/src/luan/tools/WebShell.java Thu May 29 09:26:44 2014 +0000 @@ -20,6 +20,7 @@ import luan.LuanException; import luan.lib.BasicLib; import luan.lib.HtmlLib; +import luan.lib.IoLib; public class WebShell extends HttpServlet { @@ -59,7 +60,7 @@ luan = newLuanState(); session.putValue("luan",luan); } - luan.out = new PrintStream(history); + luan.set( "Io.stdout", IoLib.writer(new PrintStream(history)) ); LuanTable env = luan.global(); env.put("request",request); env.put("response",response);