Browse Source

adds compatibility fallback for ArrayBuffer and Uint8Array

Nicolas Cannasse 11 years ago
parent
commit
7cce06eb62
3 changed files with 154 additions and 1 deletions
  1. 2 1
      std/js/_std/haxe/io/Bytes.hx
  2. 54 0
      std/js/html/compat/ArrayBuffer.hx
  3. 98 0
      std/js/html/compat/Uint8Array.hx

+ 2 - 1
std/js/_std/haxe/io/Bytes.hx

@@ -20,6 +20,7 @@
  * DEALINGS IN THE SOFTWARE.
  * DEALINGS IN THE SOFTWARE.
  */
  */
 package haxe.io;
 package haxe.io;
+import js.html.compat.Uint8array;
 
 
 @:coreApi
 @:coreApi
 class Bytes {
 class Bytes {
@@ -38,7 +39,7 @@ class Bytes {
 	}
 	}
 
 
 	public inline function set( pos : Int, v : Int ) : Void {
 	public inline function set( pos : Int, v : Int ) : Void {
-		b[pos] = v;
+		b[pos] = v & 0xFF; // the &0xFF is necessary for js.html.compat support
 	}
 	}
 
 
 	public function blit( pos : Int, src : Bytes, srcpos : Int, len : Int ) : Void {
 	public function blit( pos : Int, src : Bytes, srcpos : Int, len : Int ) : Void {

+ 54 - 0
std/js/html/compat/ArrayBuffer.hx

@@ -0,0 +1,54 @@
+/*
+ * Copyright (C)2005-2014 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package js.html.compat;
+
+@:keep
+class ArrayBuffer {
+
+	public var byteLength : Int;
+	var a : Array<Dynamic>;
+	
+	public function new( ?a : Dynamic ) {
+		if( Std.is(a,Array) ) {
+			this.a = a;
+			byteLength = a.length;
+		} else
+			throw "TODO";
+	}
+	
+	public function slice(begin,?end) {
+		return new ArrayBuffer(a.slice(begin,end));
+	}
+	
+	static function sliceImpl(begin,?end) {
+		var u = new js.html.Uint8Array(untyped __js__('this'), begin, end == null ? null : end - begin);
+        var result = new js.html.ArrayBuffer(u.byteLength);
+        var resultArray = new js.html.Uint8Array(result);
+		resultArray.set(u);
+        return result;
+	}
+
+	static function __init__() untyped {
+		var ArrayBuffer = __js__('typeof(window) != "undefined" && window.ArrayBuffer') || ArrayBuffer;
+		if( ArrayBuffer.prototype.slice == null ) ArrayBuffer.prototype.slice = sliceImpl; // IE10
+	}
+}

+ 98 - 0
std/js/html/compat/Uint8Array.hx

@@ -0,0 +1,98 @@
+/*
+ * Copyright (C)2005-2014 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package js.html.compat;
+
+@:keep
+class Uint8Array {
+
+	static var BYTES_PER_ELEMENT = 1;
+
+	static function _new( ?arg1 : Dynamic, ?offset : Int, ?length : Int ) {
+		var arr;
+		if( untyped __typeof__(arg1) == 'number' ) {
+			arr = new Array();
+			for( i in 0...arg1 )
+				arr[i] = 0;
+			untyped {
+				arr.byteLength = arr.length;
+				arr.byteOffset = 0;
+				arr.buffer = new ArrayBuffer(arr);
+			}
+		} else if( Std.is(arg1,ArrayBuffer) ) {
+			var buffer : ArrayBuffer = arg1;
+			if( offset == null ) offset = 0;
+			if( length == null ) length = buffer.byteLength - offset;
+			if( offset == 0 )
+				arr = cast @:privateAccess buffer.a;
+			else
+				// here we are losing the fact that we should reference the same data,
+				// but I don't see another way to have this behaviour while keeping [] access
+				arr = cast @:privateAccess buffer.a.slice(offset, offset+length);
+			untyped {
+				arr.byteLength = arr.length;
+				arr.byteOffset = offset;
+				arr.buffer = buffer;
+			}
+		} else if( Std.is(arg1, Array) ) {
+			arr = (arg1 : Array<Int>).copy();
+			untyped {
+				arr.byteLength = arr.length;
+				arr.byteOffset = 0;
+				arr.buffer = new ArrayBuffer(arr);
+			}
+		} else
+			throw "TODO";
+		untyped {
+			arr.subarray = _subarray;
+			arr.set = _set;
+		}
+		return arr;
+	}
+
+	static function _set( ?arg : Dynamic, ?offset : Int ) {
+		var t : Dynamic = untyped __js__("this");
+		if( Std.is(arg.buffer,ArrayBuffer) ) {
+			var a : Array<Int> = arg;
+			if( arg.byteLength + offset > t.byteLength )
+				throw "set() outside of range";
+			for( i in 0...arg.byteLength )
+				t[i + offset] = a[i];
+		} else if( Std.is(arg,Array) ) {
+			var a : Array<Int> = arg;
+			if( a.length + offset > t.byteLength )
+				throw "set() outside of range";
+			for( i in 0...a.length )
+				t[i + offset] = a[i];
+		} else
+			throw "TODO";
+	}
+
+	static function _subarray( start : Int, ?end : Int ) {
+		var t : Dynamic = untyped __js__("this");
+		return _new(t.buffer, start + t.byteOffset, (end == null ? null : end - start));
+	}
+
+	static function __init__() untyped {
+		var Uint8Array = __js__('typeof(window) != "undefined" && window.Uint8Array') || _new;
+	}
+
+}