changeset 302:2f8938fc518c

fix modules Binary and Html git-svn-id: https://luan-java.googlecode.com/svn/trunk@303 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 18 Dec 2014 04:56:30 +0000
parents a6bf8ff720f8
children fdb4bd391c28
files core/src/luan/modules/Binary.luan core/src/luan/modules/BinaryLuan.java core/src/luan/modules/Html.luan core/src/luan/modules/HtmlLuan.java core/src/luan/modules/String.luan core/src/luan/modules/StringLuan.java
diffstat 6 files changed, 27 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
diff -r a6bf8ff720f8 -r 2f8938fc518c core/src/luan/modules/Binary.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/src/luan/modules/Binary.luan	Thu Dec 18 04:56:30 2014 +0000
@@ -0,0 +1,5 @@
+java()
+import "java:luan.modules.BinaryLuan"
+
+pack = BinaryLuan.pack
+unpack = BinaryLuan.unpack
diff -r a6bf8ff720f8 -r 2f8938fc518c core/src/luan/modules/BinaryLuan.java
--- a/core/src/luan/modules/BinaryLuan.java	Wed Dec 17 12:35:57 2014 +0000
+++ b/core/src/luan/modules/BinaryLuan.java	Thu Dec 18 04:56:30 2014 +0000
@@ -4,30 +4,22 @@
 import luan.LuanState;
 import luan.LuanTable;
 import luan.LuanFunction;
-import luan.LuanJavaFunction;
 import luan.LuanException;
+import luan.LuanMethod;
 
 
 public final class BinaryLuan {
 
-	public static final LuanFunction LOADER = new LuanFunction() {
-		@Override public Object call(LuanState luan,Object[] args) {
-			LuanTable module = Luan.newTable();
-			try {
-				add( module, "to_string", new byte[0].getClass() );
-			} catch(NoSuchMethodException e) {
-				throw new RuntimeException(e);
-			}
-			return module;
-		}
-	};
-
-	private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
-		t.put( method, new LuanJavaFunction(BinaryLuan.class.getMethod(method,parameterTypes),null) );
+	@LuanMethod public static byte[] pack(byte... bytes) {
+		return bytes;
 	}
 
-	public static String to_string(byte[] bytes) {
-		return new String(bytes);
+	@LuanMethod public static Byte[] unpack(byte[] binary) {
+		Byte[] bytes = new Byte[binary.length];
+		for( int i=0; i<binary.length; i++ ) {
+			bytes[i] = binary[i];
+		}
+		return bytes;
 	}
 
 }
diff -r a6bf8ff720f8 -r 2f8938fc518c core/src/luan/modules/Html.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core/src/luan/modules/Html.luan	Thu Dec 18 04:56:30 2014 +0000
@@ -0,0 +1,4 @@
+java()
+import "java:luan.modules.HtmlLuan"
+
+encode = HtmlLuan.encode
diff -r a6bf8ff720f8 -r 2f8938fc518c core/src/luan/modules/HtmlLuan.java
--- a/core/src/luan/modules/HtmlLuan.java	Wed Dec 17 12:35:57 2014 +0000
+++ b/core/src/luan/modules/HtmlLuan.java	Thu Dec 18 04:56:30 2014 +0000
@@ -4,28 +4,11 @@
 import luan.LuanState;
 import luan.LuanTable;
 import luan.LuanFunction;
-import luan.LuanJavaFunction;
 import luan.LuanException;
 
 
 public final class HtmlLuan {
 
-	public static final LuanFunction LOADER = new LuanFunction() {
-		@Override public Object call(LuanState luan,Object[] args) {
-			LuanTable module = Luan.newTable();
-			try {
-				add( module, "encode", String.class );
-			} catch(NoSuchMethodException e) {
-				throw new RuntimeException(e);
-			}
-			return module;
-		}
-	};
-
-	private static void add(LuanTable t,String method,Class<?>... parameterTypes) throws NoSuchMethodException {
-		t.put( method, new LuanJavaFunction(HtmlLuan.class.getMethod(method,parameterTypes),null) );
-	}
-
 	public static String encode(String s) {
 		char[] a = s.toCharArray();
 		StringBuilder buf = new StringBuilder();
diff -r a6bf8ff720f8 -r 2f8938fc518c core/src/luan/modules/String.luan
--- a/core/src/luan/modules/String.luan	Wed Dec 17 12:35:57 2014 +0000
+++ b/core/src/luan/modules/String.luan	Thu Dec 18 04:56:30 2014 +0000
@@ -1,7 +1,6 @@
 java()
 import "java:luan.modules.StringLuan"
 
-to_binary = StringLuan.to_binary
 byte = StringLuan.byte_
 char = StringLuan.char_
 concat = StringLuan.concat
diff -r a6bf8ff720f8 -r 2f8938fc518c core/src/luan/modules/StringLuan.java
--- a/core/src/luan/modules/StringLuan.java	Wed Dec 17 12:35:57 2014 +0000
+++ b/core/src/luan/modules/StringLuan.java	Thu Dec 18 04:56:30 2014 +0000
@@ -51,15 +51,16 @@
 		return i==null ? dflt : end(s,i);
 	}
 
-	public static byte[] to_binary(String s) {
-		return s.getBytes();
-	}
-
-	public static int[] byte_(String s) {
+	@LuanMethod public static Integer[] byte_(String s,Integer i,Integer j) {
+		if( i== null )
+			i = 1;
+		if( j==null )
+			j = i;
+		s = s.substring(i-1,j);
 		char[] a = s.toCharArray();
-		int[] chars = new int[a.length];
-		for( int i=0; i<a.length; i++ ) {
-			chars[i] = a[i];
+		Integer[] chars = new Integer[a.length];
+		for( int k=0; k<a.length; k++ ) {
+			chars[k] = (int)a[k];
 		}
 		return chars;
 	}