changeset 421:b31d614343e8

add Io.LuanString.text_writer(); improve java related errors; fix to_string;
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 01 May 2015 15:13:14 -0600
parents e9d4d5854e54
children af82b266fe89
files core/src/luan/LuanBit.java core/src/luan/cmd_line.luan core/src/luan/impl/IndexExpr.java core/src/luan/impl/SetTableEntry.java core/src/luan/modules/IoLuan.java core/src/luan/modules/JavaLuan.java
diffstat 6 files changed, 44 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/LuanBit.java	Fri May 01 14:23:17 2015 -0600
+++ b/core/src/luan/LuanBit.java	Fri May 01 15:13:14 2015 -0600
@@ -95,9 +95,11 @@
 				LuanMeta meta = (LuanMeta)h;
 				return meta.__tostring(luan,tbl);
 			}
-			LuanFunction fn = checkFunction(h);
-			if( fn != null )
-				return checkString( Luan.first( call(fn,"__tostring",new Object[]{obj}) ) );
+			if( h != null ) {
+				LuanFunction fn = checkFunction(h);
+				if( fn != null )
+					return checkString( Luan.first( call(fn,"__tostring",new Object[]{obj}) ) );
+			}
 		}
 		return Luan.toString(obj);
 	}
--- a/core/src/luan/cmd_line.luan	Fri May 01 14:23:17 2015 -0600
+++ b/core/src/luan/cmd_line.luan	Fri May 01 15:13:14 2015 -0600
@@ -79,6 +79,7 @@
 			main_file( Table.unpack(Luan.arg) )
 		end;
 		catch = function(e)
+--			java(); e.printStackTrace(); return
 			Io.print_to(Io.stderr, e )
 		end;
 	}
--- a/core/src/luan/impl/IndexExpr.java	Fri May 01 14:23:17 2015 -0600
+++ b/core/src/luan/impl/IndexExpr.java	Fri May 01 15:13:14 2015 -0600
@@ -44,11 +44,9 @@
 			return StringLuan.__index(luan,(String)obj,key);
 		if( obj instanceof byte[] )
 			return BinaryLuan.__index(luan,(byte[])obj,key);
-		if( obj != null ) {
-			Object value = JavaLuan.__index(luan,obj,key);
-			if( value != null )
-				return value;
-		}
-		throw luan.bit(op1.se()).exception( "attempt to index '"+op1.se().text()+"' (a " + Luan.type(obj) + " value)" );
+		if( obj != null && luan.currentEnvironment().hasJava() )
+			return JavaLuan.__index(luan,obj,key);
+		else
+			throw luan.bit(op1.se()).exception( "attempt to index '"+op1.se().text()+"' (a " + Luan.type(obj) + " value)" );
 	}
 }
--- a/core/src/luan/impl/SetTableEntry.java	Fri May 01 14:23:17 2015 -0600
+++ b/core/src/luan/impl/SetTableEntry.java	Fri May 01 15:13:14 2015 -0600
@@ -49,7 +49,9 @@
 			}
 			newindex(luan,h,key,value);
 		}
-		if( !JavaLuan.__newindex(luan,t,key,value) )
+		if( t != null && luan.currentEnvironment().hasJava() )
+			JavaLuan.__newindex(luan,t,key,value);
+		else
 			throw luan.bit(se).exception( "attempt to index '"+tableExpr.se().text()+"' (a " + Luan.type(t) + " value)" );
 	}
 
--- a/core/src/luan/modules/IoLuan.java	Fri May 01 14:23:17 2015 -0600
+++ b/core/src/luan/modules/IoLuan.java	Fri May 01 15:13:14 2015 -0600
@@ -18,6 +18,7 @@
 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;
@@ -301,14 +302,18 @@
 		}
 	}
 
-	public static final class LuanString extends LuanIn {
-		private final String s;
+	public static final class LuanString extends LuanIO {
+		private String s;
 
 		private LuanString(String s) {
 			this.s = s;
 		}
 
-		@Override InputStream inputStream() throws IOException {
+		@Override InputStream inputStream() {
+			throw new UnsupportedOperationException();
+		}
+
+		@Override OutputStream outputStream() {
 			throw new UnsupportedOperationException();
 		}
 
@@ -327,6 +332,23 @@
 		@Override public boolean exists() {
 			return true;
 		}
+
+		@Override public LuanTable text_writer() throws IOException {
+			LuanWriter luanWriter = new LuanWriter() {
+				private final Writer out = new StringWriter();
+	
+				public void write(LuanState luan,Object... args) throws LuanException, IOException {
+					for( Object obj : args ) {
+						out.write( luan.toString(obj) );
+					}
+				}
+	
+				public void close() throws IOException {
+					s = out.toString();
+				}
+			};
+			return writer(luanWriter);
+		}
 	}
 
 	public static final class LuanUrl extends LuanIn {
--- a/core/src/luan/modules/JavaLuan.java	Fri May 01 14:23:17 2015 -0600
+++ b/core/src/luan/modules/JavaLuan.java	Fri May 01 15:13:14 2015 -0600
@@ -52,8 +52,7 @@
 	}
 
 	public static Object __index(LuanState luan,Object obj,Object key) throws LuanException {
-		if( !luan.currentEnvironment().hasJava() )
-			return null;
+		checkJava(luan);
 		if( obj instanceof Static ) {
 			if( key instanceof String ) {
 				String name = (String)key;
@@ -143,9 +142,8 @@
 		}
 	}
 
-	public static boolean __newindex(LuanState luan,Object obj,Object key,Object value) throws LuanException {
-		if( !luan.currentEnvironment().hasJava() )
-			return false;
+	public static void __newindex(LuanState luan,Object obj,Object key,Object value) throws LuanException {
+		checkJava(luan);
 		if( obj instanceof Static ) {
 			if( key instanceof String ) {
 				String name = (String)key;
@@ -156,7 +154,7 @@
 					if( members.size() != 1 )
 						throw new RuntimeException("not field '"+name+"' of "+obj);
 					setMember(obj,members,value);
-					return true;
+					return;
 				}
 			}
 			throw luan.exception("invalid member '"+key+"' for: "+obj);
@@ -166,7 +164,7 @@
 			Integer i = Luan.asInteger(key);
 			if( i != null ) {
 				Array.set(obj,i,value);
-				return true;
+				return;
 			}
 			throw luan.exception("invalid member '"+key+"' for java array: "+obj);
 		}
@@ -177,7 +175,7 @@
 				if( members.size() != 1 )
 					throw new RuntimeException("not field '"+name+"' of "+obj);
 				setMember(obj,members,value);
-				return true;
+				return;
 			}
 		}
 		throw luan.exception("invalid member '"+key+"' for java object: "+obj);