Browse Source

implemented python based Bytes, get getBytes and saveBytes working

frabbit 11 năm trước cách đây
mục cha
commit
75e763ec17

+ 15 - 0
std/haxe/io/Bytes.hx

@@ -42,6 +42,8 @@ class Bytes {
 		return untyped b[pos];
 		#elseif java
 		return untyped b[pos] & 0xFF;
+		#elseif python
+		return untyped __python_array_get__(b, pos);
 		#else
 		return b[pos];
 		#end
@@ -60,6 +62,8 @@ class Bytes {
 		b[pos] = cast v;
 		#elseif cs
 		b[pos] = cast v;
+		#elseif python
+		untyped __python_array_set__(b, pos, v & 0xFF);
 		#else
 		b[pos] = v & 0xFF;
 		#end
@@ -80,6 +84,8 @@ class Bytes {
 		java.lang.System.arraycopy(src.b, srcpos, b, pos, len);
 		#elseif cs
 		cs.system.Array.Copy(src.b, srcpos, b, pos, len);
+		#elseif python
+		untyped __python__("self.b[pos:pos+len] = src.b[srcpos:srcpos+len]");
 		#else
 		var b1 = b;
 		var b2 = src.b;
@@ -134,6 +140,8 @@ class Bytes {
 		var newarr = new cs.NativeArray(len);
 		cs.system.Array.Copy(b, pos, newarr, 0, len);
 		return new Bytes(len, newarr);
+		#elseif python
+		return new Bytes(len, untyped __python_array_get__(b, pos, pos+len) );
 		#else
 		return new Bytes(len,b.slice(pos,pos+len));
 		#end
@@ -284,6 +292,8 @@ class Bytes {
 		return new Bytes(length, new cs.NativeArray(length));
 		#elseif java
 		return new Bytes(length, new java.NativeArray(length));
+		#elseif python
+		return new Bytes(length, python.lib.Builtin.bytearray(length));
 		#else
 		var a = new Array();
 		for( i in 0...length )
@@ -316,6 +326,11 @@ class Bytes {
 			return new Bytes(b.length, b);
 		}
 		catch (e:Dynamic) throw e;
+
+		#elseif python
+			var b:BytesData = python.lib.Builtin.bytearray(s, "UTF-8");
+			return new Bytes(b.length, b);
+
 		#else
 		var a = new Array();
 		// utf16-decode and utf8-encode

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

@@ -150,6 +150,9 @@ class BytesBuffer {
 		#elseif java
 		var buf = b.toByteArray();
 		var bytes = new Bytes(buf.length, buf);
+		#elseif python
+		var buf = python.lib.Builtin.bytearray(b);
+		var bytes = new Bytes(ba.length, buf);
 		#else
 		var bytes = new Bytes(b.length,b);
 		#end

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

@@ -34,6 +34,8 @@ package haxe.io;
 	typedef BytesData = java.NativeArray<java.StdTypes.Int8>;
 #elseif cs
 	typedef BytesData = cs.NativeArray<cs.StdTypes.UInt8>;
+#elseif python
+		typedef BytesData = python.lib.Types.ByteArray;
 #else
 	typedef BytesData = Array<Int>;
 #end

+ 10 - 4
std/python/_std/sys/io/File.hx

@@ -31,24 +31,30 @@ class File {
 
 	public static function getContent( path : String ) : String
 	{
-		var f = python.lib.Builtin.open(path, "r", -1, "utf-8");
+		var f:python.lib.io.TextIOBase = cast python.lib.Builtin.open(path, "r", 0, "utf-8");
 		var content = f.read(-1);
 		f.close();
 		return content;
 	}
 
 	public static function saveContent( path : String, content : String ) : Void {
-		var f = python.lib.Builtin.open(path, "w", -1, "utf-8");
+		var f:python.lib.io.TextIOBase = cast python.lib.Builtin.open(path, "w", 0, "utf-8");
 		f.write(content);
 		f.close();
 	}
 
 	public static function getBytes( path : String ) : haxe.io.Bytes {
-		return throw "not implemented";
+		var f:python.lib.io.RawIOBase = cast python.lib.Builtin.open(path, "rb", 0);
+		var size = f.readall();
+		var b = haxe.io.Bytes.ofData(size);
+		f.close();
+		return b;
 	}
 
 	public static function saveBytes( path : String, bytes : haxe.io.Bytes ) : Void {
-		throw "not implemented";
+		var f:python.lib.io.RawIOBase = cast python.lib.Builtin.open(path, "wb", 0);
+		f.write(bytes.getData());
+		f.close();
 	}
 
 	public static function read( path : String, binary : Bool = true ) : FileInput {

+ 7 - 3
std/python/lib/Builtin.hx

@@ -2,7 +2,7 @@
 package python.lib;
 
 
-import python.lib.io.TextIOBase;
+import python.lib.io.IOBase;
 import python.lib.Types;
 
 
@@ -38,10 +38,11 @@ extern class Builtin {
 	@:overload(function (f:Dict<Dynamic, Dynamic>):Int {})
 	@:overload(function (f:Bytes):Int {})
 	@:overload(function (f:DictView<Dynamic>):Int {})
+	@:overload(function (f:ByteArray):Int {})
 	@:overload(function (f:Tuple<Dynamic>):Int {})
 	public static function len(x:String):Int;
 
-	public static function open(file:String, mode:String, ?buffering:Int = -1, ?encoding:String = null, ?errors : String, ?newline:String, ?closefd:Bool, ?opener:String->Int->FileDescriptor):TextIOBase;
+	public static function open(file:String, mode:String, ?buffering:Int = -1, ?encoding:String = null, ?errors : String, ?newline:String, ?closefd:Bool, ?opener:String->Int->FileDescriptor):IOBase;
 
 	//public static function divmod():Void;
 	//public static function input():Void;
@@ -81,7 +82,10 @@ extern class Builtin {
 
 	//public static function range():Void;
 	//public static function type():Void;
-	//public static function bytearray():Void;
+	@:overload(function (it:Array<Int>):python.lib.Types.ByteArray {})
+	@:overload(function (it:PyIterable<Int>):python.lib.Types.ByteArray {})
+	@:overload(function (size:Int):python.lib.Types.ByteArray {})
+	public static function bytearray(source:String,encoding:String,?errors:Dynamic):python.lib.Types.ByteArray;
 	//public static function float():Void;
 
 	@:overload(function <T>(f:Array<T>):Array<T> {})

+ 8 - 5
std/python/lib/Types.hx

@@ -26,14 +26,17 @@ abstract VarArgs (Array<Dynamic>) to Array<Dynamic> from Array<Dynamic>
 }
 
 
-extern class ByteArray {
+extern class ByteArray implements ArrayAccess<Int> {
+	public var length(get, null):Int;
+	public inline function get_length ():Int {
+		return Builtin.len(this);
+	}
 	public function decode(encoding:String="utf-8", errors:String="strict"):String;
 }
 
-extern class Bytes {
-	public var length(get, null):Int;
-	public inline function get_length ():Int return Builtin.len(this);
-	public function decode(encoding:String="utf-8", errors:String="strict"):String;
+extern class Bytes extends ByteArray {
+
+	//public function decode(encoding:String="utf-8", errors:String="strict"):String;
 
 	static function __init__ ():Void
 	{

+ 2 - 2
std/python/lib/io/RawIOBase.hx

@@ -3,12 +3,12 @@ package python.lib.io;
 
 import python.lib.io.FileIO;
 import python.lib.io.IOBase;
-import python.lib.Types.Bytes;
+import python.lib.Types;
 
 extern class RawIOBase extends IOBase{
 
 	public function readall():Bytes;
 	public function read(n:Int=-1):Null<Bytes>;
-	public function write(b:Bytes):Null<Int>;
+	public function write(b:ByteArray):Null<Int>;
 
 }