diff src/luan/modules/IoLuan.java @ 1280:781ec0a92bb5

add Boot.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 20 Dec 2018 13:38:16 -0700
parents d83f6cc558de
children ca742d51b31f
line wrap: on
line diff
--- a/src/luan/modules/IoLuan.java	Tue Dec 18 12:54:55 2018 -0700
+++ b/src/luan/modules/IoLuan.java	Thu Dec 20 13:38:16 2018 -0700
@@ -16,37 +16,26 @@
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.io.ByteArrayInputStream;
-import java.io.DataInputStream;
-import java.io.DataOutputStream;
 import java.io.StringWriter;
 import java.io.IOException;
 import java.io.FileNotFoundException;
 import java.net.URL;
-import java.net.Socket;
-import java.net.ServerSocket;
 import java.net.InetAddress;
 import java.net.Inet4Address;
 import java.net.NetworkInterface;
 import java.net.MalformedURLException;
 import java.net.UnknownHostException;
 import java.util.Enumeration;
-import java.util.List;
 import java.util.Map;
-import luan.Luan;
 import luan.LuanState;
 import luan.LuanTable;
 import luan.LuanFunction;
-import luan.LuanJavaFunction;
 import luan.LuanException;
 import luan.modules.url.LuanUrl;
 
 
 public final class IoLuan {
 
-	private static void add(LuanTable t,String method,Class... parameterTypes) throws NoSuchMethodException {
-		t.rawPut( method, new LuanJavaFunction(IoLuan.class.getMethod(method,parameterTypes),null) );
-	}
-
 	public static String read_console_line(String prompt) throws IOException {
 		if( prompt==null )
 			prompt = "> ";
@@ -60,7 +49,7 @@
 		public void close() throws IOException;
 	}
 
-	private static LuanWriter luanWriter(final PrintStream out) {
+	public static LuanWriter luanWriter(final PrintStream out) {
 		return new LuanWriter() {
 
 			public Object out() {
@@ -79,11 +68,7 @@
 		};
 	}
 
-	public static LuanTable textWriter(LuanState luan,final PrintStream out) {
-		return writer(luan,luanWriter(out));
-	}
-
-	private static LuanWriter luanWriter(final Writer out) {
+	public static LuanWriter luanWriter(final Writer out) {
 		return new LuanWriter() {
 
 			public Object out() {
@@ -102,43 +87,6 @@
 		};
 	}
 
-	public static LuanTable textWriter(LuanState luan,final Writer out) {
-		return writer(luan,luanWriter(out));
-	}
-
-	private static LuanTable writer(LuanState luan,LuanWriter luanWriter) {
-		LuanTable writer = new LuanTable(luan);
-		writer.rawPut( "java", luanWriter.out() );
-		try {
-			writer.rawPut( "write", new LuanJavaFunction(
-				LuanWriter.class.getMethod( "write", LuanState.class, new Object[0].getClass() ), luanWriter
-			) );
-			writer.rawPut( "close", new LuanJavaFunction(
-				LuanWriter.class.getMethod( "close" ), luanWriter
-			) );
-		} catch(NoSuchMethodException e) {
-			throw new RuntimeException(e);
-		}
-		return writer;
-	}
-
-
-	public static LuanTable binaryWriter(LuanState luan,final OutputStream out) {
-		LuanTable writer = new LuanTable(luan);
-		writer.rawPut( "java", out );
-		try {
-			writer.rawPut( "write", new LuanJavaFunction(
-				OutputStream.class.getMethod( "write", new byte[0].getClass() ), out
-			) );
-			writer.rawPut( "close", new LuanJavaFunction(
-				OutputStream.class.getMethod( "close" ), out
-			) );
-		} catch(NoSuchMethodException e) {
-			throw new RuntimeException(e);
-		}
-		return writer;
-	}
-
 	static LuanFunction lines(final BufferedReader in) {
 		return new LuanFunction() {
 			@Override public Object call(LuanState luan,Object[] args) throws LuanException {
@@ -265,46 +213,6 @@
 		public void set_charset(String charset) {
 			this.charset = charset;
 		}
-
-		public LuanTable table(LuanState luan) {
-			LuanTable tbl = new LuanTable(luan);
-			try {
-				tbl.rawPut( "java", this );
-				tbl.rawPut( "to_string", new LuanJavaFunction(
-					LuanIn.class.getMethod( "to_string" ), this
-				) );
-				tbl.rawPut( "to_uri_string", new LuanJavaFunction(
-					LuanIn.class.getMethod( "to_uri_string" ), this
-				) );
-				tbl.rawPut( "read_text", new LuanJavaFunction(
-					LuanIn.class.getMethod( "read_text", LuanState.class ), this
-				) );
-				tbl.rawPut( "read_binary", new LuanJavaFunction(
-					LuanIn.class.getMethod( "read_binary", LuanState.class ), this
-				) );
-				tbl.rawPut( "read_lines", new LuanJavaFunction(
-					LuanIn.class.getMethod( "read_lines", LuanState.class ), this
-				) );
-				tbl.rawPut( "read_blocks", new LuanJavaFunction(
-					LuanIn.class.getMethod( "read_blocks", LuanState.class, Integer.class ), this
-				) );
-				tbl.rawPut( "exists", new LuanJavaFunction(
-					LuanIn.class.getMethod( "exists", LuanState.class ), this
-				) );
-				tbl.rawPut( "checksum", new LuanJavaFunction(
-					LuanIn.class.getMethod( "checksum", LuanState.class ), this
-				) );
-				tbl.rawPut( "charset", new LuanJavaFunction(
-					LuanIn.class.getMethod( "charset" ), this
-				) );
-				tbl.rawPut( "set_charset", new LuanJavaFunction(
-					LuanIn.class.getMethod( "set_charset", String.class ), this
-				) );
-			} catch(NoSuchMethodException e) {
-				throw new RuntimeException(e);
-			}
-			return tbl;
-		}
 	}
 
 	public static final LuanIn defaultStdin = new LuanIn() {
@@ -373,12 +281,12 @@
 			throw new LuanException( "bad argument #1 to 'write' (string or binary or Io.uri expected)" );
 		}
 
-		public LuanTable text_writer(LuanState luan) throws IOException {
-			return textWriter(luan,new BufferedWriter(writer()));
+		public LuanWriter text_writer() throws IOException {
+			return luanWriter(new BufferedWriter(writer()));
 		}
 
-		public LuanTable binary_writer(LuanState luan) throws IOException {
-			return binaryWriter(luan,new BufferedOutputStream(outputStream()));
+		public OutputStream binary_writer() throws IOException {
+			return new BufferedOutputStream(outputStream());
 		}
 
 		public void write_text(LuanState luan,Object... args) throws LuanException, IOException {
@@ -386,30 +294,9 @@
 			luanWriter.write(luan,args);
 			luanWriter.close();
 		}
-
-		@Override public LuanTable table(LuanState luan) {
-			LuanTable tbl = super.table(luan);
-			try {
-				tbl.rawPut( "write", new LuanJavaFunction(
-					LuanIO.class.getMethod( "write", LuanState.class, Object.class ), this
-				) );
-				tbl.rawPut( "text_writer", new LuanJavaFunction(
-					LuanIO.class.getMethod( "text_writer", LuanState.class ), this
-				) );
-				tbl.rawPut( "binary_writer", new LuanJavaFunction(
-					LuanIO.class.getMethod( "binary_writer", LuanState.class ), this
-				) );
-				tbl.rawPut( "write_text", new LuanJavaFunction(
-					LuanIO.class.getMethod( "write_text", LuanState.class, new Object[0].getClass() ), this
-				) );
-			} catch(NoSuchMethodException e) {
-				throw new RuntimeException(e);
-			}
-			return tbl;
-		}
 	}
 
-	private static final LuanIO nullIO = new LuanIO() {
+	public static final LuanIO nullIO = new LuanIO() {
 		private final InputStream in = new InputStream() {
 			@Override public int read() {
 				return -1;
@@ -440,7 +327,8 @@
 	public static final class LuanString extends LuanIO {
 		private String s;
 
-		private LuanString(String s) {
+		public LuanString(String s) throws LuanException {
+			Utils.checkNotNull(s);
 			this.s = s;
 		}
 
@@ -472,8 +360,8 @@
 			return true;
 		}
 
-		@Override public LuanTable text_writer(LuanState luan) throws IOException {
-			LuanWriter luanWriter = new LuanWriter() {
+		@Override public LuanWriter text_writer() {
+			return new LuanWriter() {
 				private final Writer out = new StringWriter();
 
 				public Object out() {
@@ -490,13 +378,16 @@
 					s = out.toString();
 				}
 			};
-			return writer(luan,luanWriter);
 		}
 	}
 
 	public static final class LuanFile extends LuanIO {
 		public final File file;
 
+		public LuanFile(LuanState luan,String path) throws LuanException {
+			this(luan,new File(path));
+		}
+
 		private LuanFile(LuanState luan,File file) throws LuanException {
 			this(file);
 			check(luan,"file:"+file.toString());
@@ -522,8 +413,8 @@
 			return "file:" + file.toString();
 		}
 
-		public LuanTable child(LuanState luan,String name) throws LuanException {
-			return new LuanFile(luan,new File(file,name)).table(luan);
+		public LuanFile child(LuanState luan,String name) throws LuanException {
+			return new LuanFile(luan,new File(file,name));
 		}
 
 		public LuanTable children(LuanState luan) throws LuanException {
@@ -532,16 +423,16 @@
 				return null;
 			LuanTable list = new LuanTable(luan);
 			for( File f : files ) {
-				list.rawPut(list.rawLength()+1,new LuanFile(luan,f).table(luan));
+				list.rawPut(list.rawLength()+1,new LuanFile(luan,f));
 			}
 			return list;
 		}
 
-		public LuanTable parent(LuanState luan) throws LuanException, IOException {
+		public LuanFile parent(LuanState luan) throws LuanException, IOException {
 			File parent = file.getParentFile();
 			if( parent==null )
 				parent = file.getCanonicalFile().getParentFile();
-			return new LuanFile(luan,parent).table(luan);
+			return new LuanFile(luan,parent);
 		}
 
 		@Override public boolean exists(LuanState luan) {
@@ -556,13 +447,13 @@
 				throw new LuanException("couldn't rename file "+file+" to "+dest);
 		}
 
-		public LuanTable canonical(LuanState luan) throws LuanException, IOException {
-			return new LuanFile(luan,file.getCanonicalFile()).table(luan);
+		public LuanFile canonical(LuanState luan) throws LuanException, IOException {
+			return new LuanFile(luan,file.getCanonicalFile());
 		}
 
-		public LuanTable create_temp_file(LuanState luan,String prefix,String suffix) throws LuanException, IOException {
+		public LuanFile create_temp_file(LuanState luan,String prefix,String suffix) throws LuanException, IOException {
 			File tmp = File.createTempFile(prefix,suffix,file);
-			return new LuanFile(luan,tmp).table(luan);
+			return new LuanFile(luan,tmp);
 		}
 
 		public void delete() throws LuanException {
@@ -592,77 +483,9 @@
 			if( !file.setLastModified(time) )
 				throw new LuanException("couldn't set_last_modified on "+file);
 		}
-
-		@Override public LuanTable table(LuanState luan) {
-			LuanTable tbl = super.table(luan);
-			try {
-				tbl.rawPut( "name", new LuanJavaFunction(
-					File.class.getMethod( "getName" ), file
-				) );
-				tbl.rawPut( "is_directory", new LuanJavaFunction(
-					File.class.getMethod( "isDirectory" ), file
-				) );
-				tbl.rawPut( "is_file", new LuanJavaFunction(
-					File.class.getMethod( "isFile" ), file
-				) );
-				tbl.rawPut( "delete", new LuanJavaFunction(
-					LuanFile.class.getMethod( "delete" ), this
-				) );
-				tbl.rawPut( "delete_on_exit", new LuanJavaFunction(
-					File.class.getMethod( "deleteOnExit" ), file
-				) );
-				tbl.rawPut( "mkdir", new LuanJavaFunction(
-					LuanFile.class.getMethod( "mkdir" ), this
-				) );
-				tbl.rawPut( "last_modified", new LuanJavaFunction(
-					File.class.getMethod( "lastModified" ), file
-				) );
-				tbl.rawPut( "set_last_modified", new LuanJavaFunction(
-					LuanFile.class.getMethod( "set_last_modified", Long.TYPE ), this
-				) );
-				tbl.rawPut( "length", new LuanJavaFunction(
-					File.class.getMethod( "length" ), file
-				) );
-				tbl.rawPut( "child", new LuanJavaFunction(
-					LuanFile.class.getMethod( "child", LuanState.class, String.class ), this
-				) );
-				tbl.rawPut( "children", new LuanJavaFunction(
-					LuanFile.class.getMethod( "children", LuanState.class ), this
-				) );
-				tbl.rawPut( "parent", new LuanJavaFunction(
-					LuanFile.class.getMethod( "parent", LuanState.class ), this
-				) );
-				tbl.rawPut( "rename_to", new LuanJavaFunction(
-					LuanFile.class.getMethod( "rename_to", Object.class ), this
-				) );
-				tbl.rawPut( "canonical", new LuanJavaFunction(
-					LuanFile.class.getMethod( "canonical", LuanState.class ), this
-				) );
-				tbl.rawPut( "create_temp_file", new LuanJavaFunction(
-					LuanFile.class.getMethod( "create_temp_file", LuanState.class, String.class, String.class ), this
-				) );
-			} catch(NoSuchMethodException e) {
-				throw new RuntimeException(e);
-			}
-			return tbl;
-		}
 	}
 
-	public static LuanTable null_(LuanState luan,String ignore) {
-		return nullIO.table(luan);
-	}
-
-	public static LuanTable string(LuanState luan,String s) throws LuanException {
-		Utils.checkNotNull(s);
-		return new LuanString(s).table(luan);
-	}
-
-	public static LuanTable file(LuanState luan,String name) throws LuanException {
-		File file = new File(name);
-		return new LuanFile(luan,file).table(luan);
-	}
-
-	public static LuanTable classpath(LuanState luan,String name) throws LuanException {
+	public static LuanUrl classpath(LuanState luan,String name) throws LuanException {
 		if( name.contains("//") )
 			return null;
 		String path = name;
@@ -688,136 +511,11 @@
 			}
 		}
 		if( url != null )
-			return new LuanUrl(url,null).table(luan);
+			return new LuanUrl(url,null);
 
 		return null;
 	}
 
-	private static LuanTable url(LuanState luan,String url,LuanTable options) throws IOException, LuanException {
-		return new LuanUrl(new URL(url),options).table(luan);
-	}
-
-	public static LuanTable http(LuanState luan,String path,LuanTable options) throws IOException, LuanException {
-		return url(luan,"http:"+path,options);
-	}
-
-	public static LuanTable https(LuanState luan,String path,LuanTable options) throws IOException, LuanException {
-		return url(luan,"https:"+path,options);
-	}
-
-	public static LuanTable luan(LuanState luan,String path) throws LuanException {
-		return classpath( luan, "luan/modules/" + path );
-	}
-
-	public static LuanTable stdin(LuanState luan) throws LuanException {
-		LuanTable io = (LuanTable)PackageLuan.require(luan,"luan:Io.luan");
-		return (LuanTable)io.get("stdin");
-	}
-
-	public static LuanTable newSchemes(LuanState luan) {
-		LuanTable schemes = new LuanTable(luan);
-		try {
-			schemes.rawPut( "null", new LuanJavaFunction(IoLuan.class.getMethod("null_",LuanState.class,String.class),null) );
-			add( schemes, "string", LuanState.class, String.class );
-			add( schemes, "file", LuanState.class, String.class );
-			add( schemes, "classpath", LuanState.class, String.class );
-//			add( schemes, "socket", String.class );
-			add( schemes, "http", LuanState.class, String.class, LuanTable.class );
-			add( schemes, "https", LuanState.class, String.class, LuanTable.class );
-			add( schemes, "luan", LuanState.class, String.class );
-			add( schemes, "stdin", LuanState.class );
-			add( schemes, "os", LuanState.class, String.class, LuanTable.class );
-			add( schemes, "bash", LuanState.class, String.class, LuanTable.class );
-		} catch(NoSuchMethodException e) {
-			throw new RuntimeException(e);
-		}
-		return schemes;
-	}
-
-	private static LuanTable schemes(LuanState luan) throws LuanException {
-		LuanTable t = (LuanTable)PackageLuan.loaded(luan).rawGet("luan:Io.luan");
-		if( t == null )
-			return newSchemes(luan);
-		t = (LuanTable)t.get("schemes");
-		if( t == null )
-			return newSchemes(luan);
-		return t;
-	}
-
-	public static LuanTable uri(LuanState luan,String name,LuanTable options) throws LuanException {
-		int i = name.indexOf(':');
-		if( i == -1 )
-			throw new LuanException( "invalid Io.uri name '"+name+"', missing scheme" );
-		String scheme = name.substring(0,i);
-		String location = name.substring(i+1);
-		LuanTable schemes = schemes(luan);
-		LuanFunction opener = (LuanFunction)schemes.get(scheme);
-		if( opener == null )
-			throw new LuanException( "invalid scheme '"+scheme+"' in '"+name+"'" );
-		return (LuanTable)Luan.first(opener.call(luan,new Object[]{location,options}));
-	}
-/*
-	public static final class LuanSocket extends LuanIO {
-		public final Socket socket;
-
-		private LuanSocket(String host,int port) throws LuanException {
-			try {
-				this.socket = new Socket(host,port);
-			} catch(IOException e) {
-				throw new LuanException(e.toString());
-			}
-		}
-
-		private LuanSocket(Socket socket) {
-			this.socket = socket;
-		}
-
-		@Override public InputStream inputStream() throws IOException {
-			return socket.getInputStream();
-		}
-
-		@Override OutputStream outputStream() throws IOException {
-			return socket.getOutputStream();
-		}
-
-		@Override public String to_string() {
-			return socket.toString();
-		}
-
-		@Override public String to_uri_string() {
-			throw new UnsupportedOperationException();
-		}
-	}
-
-	public static LuanTable socket(String name) throws LuanException, IOException {
-		int i = name.indexOf(':');
-		if( i == -1 )
-			throw new LuanException( "invalid socket '"+name+"', format is: <host>:<port>" );
-		String host = name.substring(0,i);
-		String portStr = name.substring(i+1);
-		int port = Integer.parseInt(portStr);
-		return new LuanSocket(host,port).table();
-	}
-
-	public static LuanFunction socket_server(int port) throws IOException {
-		final ServerSocket ss = new ServerSocket(port);
-		return new LuanFunction() {
-			@Override public Object call(LuanState luan,Object[] args) throws LuanException {
-				try {
-					if( args.length > 0 ) {
-						if( args.length > 1 || !"close".equals(args[0]) )
-							throw new LuanException( "the only argument allowed is 'close'" );
-						ss.close();
-						return null;
-					}
-					return new LuanSocket(ss.accept()).table();
-				} catch(IOException e) {
-					throw new LuanException(e);
-				}
-			}
-		};
-	}
-*/
 
 	public static class BaseOs extends LuanIO {
 		private final String cmd;
@@ -881,45 +579,24 @@
 			wait_for();
 			return s;
 		}
-
-		@Override public LuanTable table(LuanState luan) {
-			LuanTable tbl = super.table(luan);
-			try {
-				tbl.rawPut( "wait_for", new LuanJavaFunction(
-					BaseOs.class.getMethod( "wait_for" ), this
-				) );
-			} catch(NoSuchMethodException e) {
-				throw new RuntimeException(e);
-			}
-			return tbl;
-		}
 	}
 
 	public static final class LuanOs extends BaseOs {
-		private LuanOs(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
+		public LuanOs(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
 			super(cmd,options);
 			check(luan,"os:"+cmd);
 			this.proc = Runtime.getRuntime().exec(cmd,null,dir);
 		}
 	}
 
-	public static LuanTable os(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
-		return new LuanOs(luan,cmd,options).table(luan);
-	}
-
 	public static final class LuanBash extends BaseOs {
-		private LuanBash(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
+		public LuanBash(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
 			super(cmd,options);
 			check(luan,"bash:"+cmd);
 			this.proc = Runtime.getRuntime().exec(new String[]{"bash","-c",cmd},null,dir);
 		}
 	}
 
-	public static LuanTable bash(LuanState luan,String cmd,LuanTable options) throws IOException, LuanException {
-		return new LuanBash(luan,cmd,options).table(luan);
-	}
-
-
 
 	public static class LuanInput extends LuanIn {
 		private final InputStream in;
@@ -967,36 +644,6 @@
 		return tbl;
 	}
 
-/*
-	// files maps zip name to uri
-	public static void zip(LuanState luan,String zipUri,LuanTable files) throws LuanException, IOException {
-		Object obj = uri(luan,zipUri,null).rawGet("java");
-		if( !(obj instanceof LuanIO) )
-			throw new LuanException("invalid uri for zip");
-		LuanIO zipIo = (LuanIO)obj;
-		ZipOutputStream out = new ZipOutputStream(zipIo.outputStream());
-		for( Map.Entry<Object,Object> entry : files.iterable(luan) ) {
-			obj = entry.getKey();
-			if( !(obj instanceof String) )
-				throw new LuanException("zip file table keys must be strings");
-			String fileName = (String)obj;
-			obj = entry.getValue();
-			if( !(obj instanceof String) )
-				throw new LuanException("zip file table values must be strings");
-			String uriStr = (String)obj;
-			out.putNextEntry(new ZipEntry(fileName));
-			obj = uri(luan,uriStr,null).rawGet("java");
-			if( !(obj instanceof LuanIn) )
-				throw new LuanException("invalid uri for zip");
-			LuanIn zipIn = (LuanIn)obj;
-			InputStream in = zipIn.inputStream();
-			Utils.copyAll(in,out);
-			in.close();
-			out.closeEntry();
-		}
-		out.close();
-	}
-*/
 
 	// security