Mercurial Hosting > luan
changeset 265:454a486d9c19
allow IO on files that don't exist
git-svn-id: https://luan-java.googlecode.com/svn/trunk@266 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 29 Oct 2014 16:26:42 +0000 |
parents | 9e0d4452e649 |
children | 4dca283b9b74 |
files | core/src/luan/modules/BasicLuan.java core/src/luan/modules/IoLuan.java core/src/luan/modules/PackageLuan.java |
diffstat | 3 files changed, 41 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/core/src/luan/modules/BasicLuan.java Wed Oct 29 03:50:59 2014 +0000 +++ b/core/src/luan/modules/BasicLuan.java Wed Oct 29 16:26:42 2014 +0000 @@ -74,11 +74,12 @@ } public static LuanFunction load_file(LuanState luan,String fileName) throws LuanException { - LuanTable t = fileName==null ? IoLuan.stdin.table() : IoLuan.get(luan,fileName,false); - if( t == null ) + if( fileName == null ) + fileName = "stdin:"; + LuanFunction fn = PackageLuan.loader(luan,fileName,false); + if( fn == null ) throw luan.exception( "file '"+fileName+"' not found" ); - LuanFunction loader = (LuanFunction)t.get("loader"); - return (LuanFunction)Luan.first(luan.call(loader,new Object[]{fileName})); + return fn; } public static Object do_file(LuanState luan,String fileName) throws LuanException {
--- a/core/src/luan/modules/IoLuan.java Wed Oct 29 03:50:59 2014 +0000 +++ b/core/src/luan/modules/IoLuan.java Wed Oct 29 16:26:42 2014 +0000 @@ -18,6 +18,7 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.io.FileNotFoundException; import java.net.URL; import java.net.Socket; import java.net.ServerSocket; @@ -210,11 +211,22 @@ try { String src = read_text(); return BasicLuan.load(luan,src,name,null,false); + } catch(FileNotFoundException e) { + return null; } catch(IOException e) { throw luan.exception(e); } } + public boolean exists() throws IOException { + try { + inputStream().close(); + return true; + } catch(FileNotFoundException e) { + return false; + } + } + LuanTable table() { LuanTable tbl = Luan.newTable(); try { @@ -236,6 +248,9 @@ tbl.put( "loader", new LuanJavaFunction( LuanIn.class.getMethod( "loader", LuanState.class, String.class ), this ) ); + tbl.put( "exists", new LuanJavaFunction( + LuanIn.class.getMethod( "exists" ), this + ) ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); } @@ -260,6 +275,10 @@ @Override public byte[] read_binary() throws IOException { return Utils.readAll(System.in); } + + @Override public boolean exists() { + return true; + } }; public static abstract class LuanIO extends LuanIn { @@ -365,7 +384,7 @@ return list; } - public boolean exists() { + @Override public boolean exists() { return file.exists(); } @@ -375,9 +394,6 @@ tbl.put( "name", new LuanJavaFunction( File.class.getMethod( "getName" ), file ) ); - tbl.put( "exists", new LuanJavaFunction( - LuanFile.class.getMethod( "exists" ), this - ) ); tbl.put( "is_directory", new LuanJavaFunction( File.class.getMethod( "isDirectory" ), file ) ); @@ -413,8 +429,6 @@ if( Boolean.TRUE.equals(loading) ) name += ".luan"; File file = new File(name); - if( !file.exists() ) - return null; return new LuanFile(file).table(); } @@ -504,6 +518,11 @@ return classpath( luan, "luan/modules/" + path, loading ); } + public static LuanTable stdin(LuanState luan) throws LuanException { + LuanTable io = (LuanTable)PackageLuan.loaded(luan).get("luan:Io"); + return (LuanTable)io.get("stdin"); + } + private static LuanTable newProtocols() { LuanTable protocols = Luan.newTable(); try { @@ -516,6 +535,7 @@ IoLuan.class.getMethod( "_class", LuanState.class, String.class, Boolean.class ), null ) ); add( protocols, "luan", LuanState.class, String.class, Boolean.class ); + add( protocols, "stdin", LuanState.class ); } catch(NoSuchMethodException e) { throw new RuntimeException(e); }
--- a/core/src/luan/modules/PackageLuan.java Wed Oct 29 03:50:59 2014 +0000 +++ b/core/src/luan/modules/PackageLuan.java Wed Oct 29 16:26:42 2014 +0000 @@ -87,13 +87,19 @@ return mod; } - public static Object[] search(LuanState luan,String modName) throws LuanException { - LuanTable t = IoLuan.get(luan,modName,true); + static LuanFunction loader(LuanState luan,String name,boolean loading) throws LuanException { + LuanTable t = IoLuan.get(luan,name,loading); if( t == null ) return null; LuanFunction loader = (LuanFunction)t.get("loader"); - LuanFunction fn = (LuanFunction)Luan.first(luan.call(loader,new Object[]{modName})); - return new Object[]{fn,modName}; + if( loader == null ) + return null; + return (LuanFunction)Luan.first(luan.call(loader,new Object[]{name})); + } + + public static Object[] search(LuanState luan,String modName) throws LuanException { + LuanFunction fn = loader(luan,modName,true); + return fn==null ? null : new Object[]{fn,modName}; }