changeset 1257:e38f5869e9df

don't reset in send_redirect and other improvements
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 20 Sep 2018 21:04:41 -0600
parents c147e2e877e3
children e4d7a3114fa8
files src/luan/modules/BasicLuan.java src/luan/modules/BinaryLuan.java src/luan/modules/IoLuan.java src/luan/modules/http/Http.luan src/luan/modules/http/LuanHandler.java website/src/manual.html.luan
diffstat 6 files changed, 38 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
diff -r c147e2e877e3 -r e38f5869e9df src/luan/modules/BasicLuan.java
--- a/src/luan/modules/BasicLuan.java	Wed Sep 19 20:15:16 2018 -0600
+++ b/src/luan/modules/BasicLuan.java	Thu Sep 20 21:04:41 2018 -0600
@@ -115,19 +115,19 @@
 		return tbl;
 	}
 
-	public static int assert_integer(int v) throws LuanException {
+	public static int assert_integer(int v) {
 		return v;
 	}
 
-	public static long assert_long(long v) throws LuanException {
+	public static long assert_long(long v) {
 		return v;
 	}
 
-	public static double assert_double(double v) throws LuanException {
+	public static double assert_double(double v) {
 		return v;
 	}
 
-	public static float assert_float(float v) throws LuanException {
+	public static float assert_float(float v) {
 		return v;
 	}
 
diff -r c147e2e877e3 -r e38f5869e9df src/luan/modules/BinaryLuan.java
--- a/src/luan/modules/BinaryLuan.java	Wed Sep 19 20:15:16 2018 -0600
+++ b/src/luan/modules/BinaryLuan.java	Thu Sep 20 21:04:41 2018 -0600
@@ -1,5 +1,6 @@
 package luan.modules;
 
+import java.io.UnsupportedEncodingException;
 import luan.Luan;
 import luan.LuanState;
 import luan.LuanTable;
@@ -43,8 +44,17 @@
 		return bytes;
 	}
 
-	public static String to_string(byte[] binary) {
-		return new String(binary);
+	private static String toString(byte[] a) {
+		char[] ac = new char[a.length];
+		for( int i=0; i<a.length; i++ ) {
+			ac[i] = (char)a[i];
+		}
+		return new String(ac);
+	}
+
+	public static String to_string(byte[] binary,String charsetName) throws LuanException, UnsupportedEncodingException {
+		Utils.checkNotNull(binary);
+		return charsetName!=null ? new String(binary,charsetName) : toString(binary);
 	}
 
 }
diff -r c147e2e877e3 -r e38f5869e9df src/luan/modules/IoLuan.java
--- a/src/luan/modules/IoLuan.java	Wed Sep 19 20:15:16 2018 -0600
+++ b/src/luan/modules/IoLuan.java	Thu Sep 20 21:04:41 2018 -0600
@@ -55,6 +55,7 @@
 
 
 	public interface LuanWriter {
+		public Object out();
 		public void write(LuanState luan,Object... args) throws LuanException, IOException;
 		public void close() throws IOException;
 	}
@@ -62,6 +63,10 @@
 	private static LuanWriter luanWriter(final PrintStream out) {
 		return new LuanWriter() {
 
+			public Object out() {
+				return out;
+			}
+
 			public void write(LuanState luan,Object... args) throws LuanException {
 				for( Object obj : args ) {
 					out.print( luan.toString(obj) );
@@ -81,6 +86,10 @@
 	private static LuanWriter luanWriter(final Writer out) {
 		return new LuanWriter() {
 
+			public Object out() {
+				return out;
+			}
+
 			public void write(LuanState luan,Object... args) throws LuanException, IOException {
 				for( Object obj : args ) {
 					out.write( luan.toString(obj) );
@@ -99,6 +108,7 @@
 
 	private static LuanTable writer(LuanWriter luanWriter) {
 		LuanTable writer = new LuanTable();
+		writer.rawPut( "java", luanWriter.out() );
 		try {
 			writer.rawPut( "write", new LuanJavaFunction(
 				LuanWriter.class.getMethod( "write", LuanState.class, new Object[0].getClass() ), luanWriter
@@ -115,6 +125,7 @@
 
 	public static LuanTable binaryWriter(final OutputStream out) {
 		LuanTable writer = new LuanTable();
+		writer.rawPut( "java", out );
 		try {
 			writer.rawPut( "write", new LuanJavaFunction(
 				OutputStream.class.getMethod( "write", new byte[0].getClass() ), out
@@ -442,6 +453,10 @@
 		@Override public LuanTable text_writer() throws IOException {
 			LuanWriter luanWriter = new LuanWriter() {
 				private final Writer out = new StringWriter();
+
+				public Object out() {
+					return out;
+				}
 	
 				public void write(LuanState luan,Object... args) throws LuanException, IOException {
 					for( Object obj : args ) {
diff -r c147e2e877e3 -r e38f5869e9df src/luan/modules/http/Http.luan
--- a/src/luan/modules/http/Http.luan	Wed Sep 19 20:15:16 2018 -0600
+++ b/src/luan/modules/http/Http.luan	Thu Sep 20 21:04:41 2018 -0600
@@ -74,7 +74,6 @@
 	this.reset()
 
 	function this.send_redirect(location)
-		this.reset()
 		this.status = STATUS.FOUND
 		this.headers["location"] = location
 	end
diff -r c147e2e877e3 -r e38f5869e9df src/luan/modules/http/LuanHandler.java
--- a/src/luan/modules/http/LuanHandler.java	Wed Sep 19 20:15:16 2018 -0600
+++ b/src/luan/modules/http/LuanHandler.java	Thu Sep 20 21:04:41 2018 -0600
@@ -57,6 +57,10 @@
 		luanInit.onClose(this);
 	}
 
+	public LuanState getLuan() {
+		return luan;
+	}
+
 	private void setLuan() {
 		LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
 		luan = (LuanState)cloner.clone(luanInit);
diff -r c147e2e877e3 -r e38f5869e9df website/src/manual.html.luan
--- a/website/src/manual.html.luan	Wed Sep 19 20:15:16 2018 -0600
+++ b/website/src/manual.html.luan	Thu Sep 20 21:04:41 2018 -0600
@@ -2624,9 +2624,9 @@
 following the same rules of function <a href="#String.sub"><code>String.sub</code></a>.
 
 
-<h4 heading><a name="Binary.to_string" href="#Binary.to_string"><code>Binary.to_string (b)</code></a></h4>
-<p>
-Converts the binary <code>b</code> to a string using the Java <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte[])">String constructor</a>.
+<h4 heading><a name="Binary.to_string" href="#Binary.to_string"><code>Binary.to_string (b [,charset])</code></a></h4>
+<p>
+If <code>charset</code> is not nil then converts the binary <code>b</code> to a string using the Java <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte[],%20java.lang.String)">String constructor</a>, else makes each byte a char.