فهرست منبع

[java/cs] initial haxe.io.* API. C# has all of them working (needs testing); Java still only has haxe.io.Bytes implemented.

Caue Waneck 13 سال پیش
والد
کامیت
4c36f6d730
9فایلهای تغییر یافته به همراه206 افزوده شده و 3 حذف شده
  1. 10 0
      gencs.ml
  2. 10 0
      genjava.ml
  3. 1 1
      std/cs/NativeArray.hx
  4. 40 2
      std/haxe/io/Bytes.hx
  5. 14 0
      std/haxe/io/BytesBuffer.hx
  6. 4 0
      std/haxe/io/BytesData.hx
  7. 88 0
      std/haxe/io/Input.hx
  8. 36 0
      std/haxe/io/Output.hx
  9. 3 0
      std/java/_std/String.hx

+ 10 - 0
gencs.ml

@@ -858,6 +858,16 @@ let configure gen =
                 loop (times - 1)
           in
           check_t_s (TInst(cl, params)) 0 
+        | TNew ({ cl_path = ([], "String") } as cl, [], el) ->
+          write w "new ";
+          write w (t_s (TInst(cl, [])));
+          write w "(";
+          ignore (List.fold_left (fun acc e ->
+            (if acc <> 0 then write w ", ");
+            expr_s w e;
+            acc + 1
+          ) 0 el);
+          write w ")"
         | TNew (cl, params, el) -> 
           write w "new ";
           write w (path_param_s (TClassDecl cl) cl.cl_path params);

+ 10 - 0
genjava.ml

@@ -1015,6 +1015,16 @@ let configure gen =
                 loop (times - 1)
           in
           check_t_s (TInst(cl, params)) 0 
+        | TNew ({ cl_path = ([], "String") } as cl, [], el) ->
+          write w "new ";
+          write w (t_s (TInst(cl, [])));
+          write w "(";
+          ignore (List.fold_left (fun acc e ->
+            (if acc <> 0 then write w ", ");
+            expr_s w e;
+            acc + 1
+          ) 0 el);
+          write w ")"
         | TNew (cl, params, el) -> 
           write w "new ";
           write w (path_param_s (TClassDecl cl) cl.cl_path params);

+ 1 - 1
std/cs/NativeArray.hx

@@ -9,5 +9,5 @@ extern class NativeArray<T> extends system.Array, implements ArrayAccess<T>
 	@:overload(function(arr:system.Array, destIndex:Int64):Void {} )
 	public function CopyTo(arr:system.Array, destIndex:Int):Void;
 	
-	
+	static function Reverse(arr:system.Array):Void;
 }

+ 40 - 2
std/haxe/io/Bytes.hx

@@ -43,6 +43,8 @@ class Bytes {
 		return untyped __call__("ord", b[pos]);
 		#elseif cpp
 		return untyped b[pos];
+		#elseif java
+		return b[pos] & 0xFF;
 		#else
 		return b[pos];
 		#end
@@ -57,6 +59,10 @@ class Bytes {
 		b[pos] = untyped __call__("chr", v);
 		#elseif cpp
 		untyped b[pos] = v;
+		#elseif java
+		b[pos] = v;
+		#elseif cs
+		b[pos] = v;
 		#else
 		b[pos] = v & 0xFF;
 		#end
@@ -74,6 +80,10 @@ class Bytes {
 		#elseif flash9
 		b.position = pos;
 		if( len > 0 ) b.writeBytes(src.b,srcpos,len);
+		#elseif java
+		java.lang.System.arraycopy(b, pos, src.b, srcpos, len);
+		#elseif cs
+		system.Array.Copy(b, pos, src.b, srcpos, len);
 		#else
 		var b1 = b;
 		var b2 = src.b;
@@ -104,6 +114,14 @@ class Bytes {
 		#elseif php
 		// TODO: test me
 		return new Bytes(len, untyped __call__("substr", b, pos, len));
+		#elseif java
+		var newarr = new java.NativeArray(len);
+		java.lang.System.arraycopy(b, pos, newarr, 0, len);
+		return new Bytes(len, newarr);
+		#elseif cs
+		var newarr = new cs.NativeArray(len);
+		system.Array.Copy(b, pos, newarr, 0, len);
+		return new Bytes(len, newarr);
 		#else
 		return new Bytes(len,b.slice(pos,pos+len));
 		#end
@@ -130,6 +148,8 @@ class Bytes {
 		return length - other.length;
 		#elseif php
 		return untyped __php__("$this->b < $other->b ? -1 : ($this->b == $other->b ? 0 : 1)");
+		//#elseif cs
+		//TODO: memcmp if unsafe flag is on
 		#else
 		var b1 = b;
 		var b2 = other.b;
@@ -162,6 +182,10 @@ class Bytes {
 		var result:String="";
 		untyped __global__.__hxcpp_string_of_bytes(b,result,pos,len);
 		return result;
+		#elseif cs
+		return system.text.Encoding.UTF8.GetString(b, pos, len);
+		#elseif java
+		return new String(b, pos, len);
 		#else
 		var s = "";
 		var b = b;
@@ -199,6 +223,10 @@ class Bytes {
 		// TODO: test me
 		return cast b;
 //		return untyped __call__("call_user_func_array", "pack", __call__("array_merge", __call__("array", "C*"), b.»a));
+		#elseif cs
+		return system.text.Encoding.UTF8.GetString(b, 0, length);
+		#elseif java
+		return new String(b, 0, length);
 		#else
 		return readString(0,length);
 		#end
@@ -241,7 +269,11 @@ class Bytes {
 		#elseif cpp
 		var a = new BytesData();
 		if (length>0) a[length-1] = untyped 0;
-		return new Bytes(length,a);
+		return new Bytes(length, a);
+		#elseif cs
+		return new Bytes(length, new cs.NativeArray(length));
+		#elseif java
+		return new Bytes(length, new java.NativeArray(length));
 		#else
 		var a = new Array();
 		for( i in 0...length )
@@ -263,7 +295,11 @@ class Bytes {
 		#elseif cpp
 		var a = new BytesData();
 		untyped __global__.__hxcpp_bytes_of_string(a,s);
-		return new Bytes(a.length,a);
+		return new Bytes(a.length, a);
+		#elseif cs
+		return new Bytes(s.length, system.text.Encoding.UTF8.GetBytes(s));
+		#elseif java
+		return new Bytes(s.length, untyped s.getBytes());
 		#else
 		var a = new Array();
 		// utf8-decode
@@ -296,6 +332,8 @@ class Bytes {
 		return new Bytes(untyped __dollar__ssize(b),b);
 		#elseif php
 		return new Bytes(untyped __call__("strlen", b), b);
+		#elseif cs
+		return new Bytes(b.Length,b);
 		#else
 		return new Bytes(b.length,b);
 		#end

+ 14 - 0
std/haxe/io/BytesBuffer.hx

@@ -34,6 +34,9 @@ class BytesBuffer {
 	var b : String;
 	#elseif cpp
 	var b : BytesData;
+	#elseif cs
+	var b : system.io.MemoryStream;
+	//java - ByteArrayOutputStream
 	#else
 	var b : Array<Int>;
 	#end
@@ -47,6 +50,8 @@ class BytesBuffer {
 		b = "";
 		#elseif cpp
 		b = new BytesData();
+		#elseif cs
+		b = new system.io.MemoryStream();
 		#else
 		b = new Array();
 		#end
@@ -61,6 +66,8 @@ class BytesBuffer {
 		b += untyped __call__("chr", byte);
 		#elseif cpp
 		b.push(untyped byte);
+		#elseif cs
+		b.WriteByte(byte);
 		#else
 		b.push(byte);
 		#end
@@ -73,6 +80,8 @@ class BytesBuffer {
 		b.writeBytes(src.getData());
 		#elseif php
 		b += cast src.getData();
+		#elseif cs
+		b.Write(src.getData(), 0, src.length);
 		#else
 		var b1 = b;
 		var b2 = src.getData();
@@ -91,6 +100,8 @@ class BytesBuffer {
 		b.writeBytes(src.getData(),pos,len);
 		#elseif php
 		b += untyped __call__("substr", src.b, pos, len);
+		#elseif cs
+		b.Write(src.getData(), pos, len);
 		#else
 		var b1 = b;
 		var b2 = src.getData();
@@ -112,6 +123,9 @@ class BytesBuffer {
 		b.position = 0;
 		#elseif php
 		var bytes = new Bytes(b.length, cast b);
+		#elseif cs
+		var buf = b.GetBuffer();
+		var bytes = new Bytes(buf.Length, buf);
 		#else
 		var bytes = new Bytes(b.length,b);
 		#end

+ 4 - 0
std/haxe/io/BytesData.hx

@@ -33,6 +33,10 @@ package haxe.io;
 #elseif cpp
 	extern class Unsigned_char__ { }
 	typedef BytesData = Array<Unsigned_char__>;
+#elseif java
+	typedef BytesData = java.NativeArray<java.StdTypes.Int8>;
+#elseif cs
+	typedef BytesData = cs.NativeArray<cs.StdTypes.UInt8>;
 #else
 	typedef BytesData = Array<Int>;
 #end

+ 88 - 0
std/haxe/io/Input.hx

@@ -31,6 +31,9 @@ package haxe.io;
 class Input {
 
 	public var bigEndian(default,setEndian) : Bool;
+	#if cs
+	private var helper:BytesData;
+	#end
 
 	public function readByte() : Int {
 	#if cpp
@@ -79,6 +82,46 @@ class Input {
 		#else
 			bufsize = (1 << 14); // 16 Ko
 		#end
+		
+		#if (cs || java)
+		var buf = Bytes.alloc(bufsize);
+		var total = [];
+		var tlen = 0;
+		try
+		{
+			while (true)
+			{
+				var len = readBytes(buf, 0, bufsize);
+				tlen += len;
+				if (len == 0)
+					throw Error.Blocked;
+				total.push(buf);
+			}
+		} catch (e:Eof) {
+		}
+			#if cs
+			var ret = new cs.NativeArray(tlen);
+			var idx = 0;
+			for (buf in total)
+			{
+				var len = buf.getData().Length;
+				system.Array.Copy(buf.getData(), 0, ret, idx, len);
+				idx += buf.getData().Length;
+			}
+			return Bytes.ofData(ret);
+			#else
+			var ret = new java.NativeArray(tlen);
+			var idx = 0;
+			for (buf in total)
+			{
+				var len = buf.getData().length;
+				java.lang.System.arraycopy(buf.getData(), 0, ret, idx, len);
+				idx += buf.getData().length;
+			}
+			return Bytes.ofData(ret);
+			#end
+		#else
+		
 		var buf = Bytes.alloc(bufsize);
 		var total = new haxe.io.BytesBuffer();
 		try {
@@ -91,6 +134,7 @@ class Input {
 		} catch( e : Eof ) {
 		}
 		return total.getBytes();
+		#end
 	}
 
 	public function readFullBytes( s : Bytes, pos : Int, len : Int ) {
@@ -146,6 +190,24 @@ class Input {
 		#elseif php
 			var a = untyped __call__('unpack', 'f', readString(4));
 			return a[1];
+		#elseif cs
+			if (helper == null) helper = new cs.NativeArray(8);
+			
+			var helper = helper;
+			if (bigEndian == !system.BitConverter.IsLittleEndian)
+			{
+				helper[0] = readByte();
+				helper[1] = readByte();
+				helper[2] = readByte();
+				helper[3] = readByte();
+			} else {
+				helper[3] = readByte();
+				helper[2] = readByte();
+				helper[1] = readByte();
+				helper[0] = readByte();
+			}
+			
+			return system.BitConverter.ToSingle(helper, 0);
 		#else
 			var bytes = [];
 			bytes.push(readByte());
@@ -190,6 +252,32 @@ class Input {
 		if (sig == 0 && exp == -1023)
 			return 0.0;
 		return sign * (1.0 + Math.pow(2, -52) * sig) * Math.pow(2, exp);
+		#elseif cs
+		if (helper == null) helper = new cs.NativeArray(8);
+		
+		var helper = helper;
+		if (bigEndian == !system.BitConverter.IsLittleEndian)
+		{
+			helper[0] = readByte();
+			helper[1] = readByte();
+			helper[2] = readByte();
+			helper[3] = readByte();
+			helper[4] = readByte();
+			helper[5] = readByte();
+			helper[6] = readByte();
+			helper[7] = readByte();
+		} else {
+			helper[7] = readByte();
+			helper[6] = readByte();
+			helper[5] = readByte();
+			helper[4] = readByte();
+			helper[3] = readByte();
+			helper[2] = readByte();
+			helper[1] = readByte();
+			helper[0] = readByte();
+		}
+		
+		return system.BitConverter.ToDouble(helper, 0);
 		#else
 		return throw "not implemented";
 		#end

+ 36 - 0
std/haxe/io/Output.hx

@@ -100,6 +100,20 @@ class Output {
 		write(Bytes.ofData(_float_bytes(x,bigEndian)));
 		#elseif php
 		write(untyped Bytes.ofString(__call__('pack', 'f', x)));
+		#elseif cs
+		var bytes = system.BitConverter.GetBytes(cast(x, Single));
+		if (bigEndian == system.BitConverter.IsLittleEndian)
+		{
+			writeByte(bytes[3]);
+			writeByte(bytes[2]);
+			writeByte(bytes[1]);
+			writeByte(bytes[0]);
+		} else {
+			writeByte(bytes[0]);
+			writeByte(bytes[1]);
+			writeByte(bytes[2]);
+			writeByte(bytes[3]);
+		}
 		#else
 		if (x == 0.0)
 		{
@@ -130,6 +144,28 @@ class Output {
 		write(Bytes.ofData(_double_bytes(x,bigEndian)));
 		#elseif php
 		write(untyped Bytes.ofString(__call__('pack', 'd', x)));
+		#elseif cs
+		var bytes = system.BitConverter.GetBytes(x);
+		if (bigEndian == system.BitConverter.IsLittleEndian)
+		{
+			writeByte(bytes[7]);
+			writeByte(bytes[6]);
+			writeByte(bytes[5]);
+			writeByte(bytes[4]);
+			writeByte(bytes[3]);
+			writeByte(bytes[2]);
+			writeByte(bytes[1]);
+			writeByte(bytes[0]);
+		} else {
+			writeByte(bytes[0]);
+			writeByte(bytes[1]);
+			writeByte(bytes[2]);
+			writeByte(bytes[3]);
+			writeByte(bytes[4]);
+			writeByte(bytes[5]);
+			writeByte(bytes[6]);
+			writeByte(bytes[7]);
+		}
 		#else
 		if (x == 0.0) 
 		{

+ 3 - 0
std/java/_std/String.hx

@@ -36,6 +36,7 @@ extern class String {
 	/**
 		Creates a copy from a given String.
 	**/
+	@:overload(function(b:haxe.io.BytesData, offset:Int, length:Int):Void { })
 	function new(string:String) : Void;
 
 	/**
@@ -100,6 +101,8 @@ extern class String {
 	private function compareTo( anotherString : String ) : Int;
 	
 	private function codePointAt( idx : Int ) : Int;
+	
+	private function getBytes() : haxe.io.BytesData;
 
 	static function fromCharCode( code : Int ) : String;