2
0
Dan Korostelev 9 жил өмнө
parent
commit
1e6193f8b2

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

@@ -0,0 +1,61 @@
+/*
+ * Copyright (C)2005-2016 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;
+
+#if !nodejs
+@:ifFeature("js.html.ArrayBuffer.*")
+class ArrayBuffer {
+
+	public var byteLength : Int;
+	var a : Array<Int>;
+
+	public function new( ?a : Dynamic ) {
+		if( Std.is(a,Array) ) {
+			this.a = a;
+			byteLength = a.length;
+		} else {
+			var len : Int = a;
+			this.a = [];
+			for( i in 0...len )
+				this.a[i] = 0;
+			byteLength = len;
+		}
+	}
+
+	public function slice(begin,?end) {
+		return new ArrayBuffer(a.slice(begin,end));
+	}
+
+	static function sliceImpl(begin,?end) {
+		var u = new js.html.Uint8Array(js.Lib.nativeThis, 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.Lib.global.ArrayBuffer || js.html.compat.ArrayBuffer;
+		if( ArrayBuffer.prototype.slice == null ) ArrayBuffer.prototype.slice = sliceImpl; // IE10
+	}
+}
+#end

+ 147 - 0
std/js/html/compat/DataView.hx

@@ -0,0 +1,147 @@
+/*
+ * Copyright (C)2005-2016 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;
+
+#if !nodejs
+import haxe.io.Error;
+
+@:ifFeature("js.html.DataView.*")
+@:access(js.html.compat.ArrayBuffer)
+class DataView {
+
+	var buf : ArrayBuffer;
+	var offset : Int;
+	var length : Int;
+
+	public function new( buffer : ArrayBuffer, ?byteOffset : Int, ?byteLength : Int ) : Void {
+		this.buf = buffer;
+		this.offset = byteOffset == null ? 0 : byteOffset;
+		this.length = byteLength == null ? buffer.byteLength - this.offset : byteLength;
+		if( offset < 0 || length < 0 || offset+length > buffer.byteLength )
+			throw OutsideBounds;
+	}
+
+	public function getInt8( byteOffset : Int ) : Int {
+		var v = buf.a[offset + byteOffset];
+		return v >= 0x80 ? v - 256 : v;
+	}
+
+	public function getUint8( byteOffset : Int ) : Int {
+		return buf.a[offset + byteOffset];
+	}
+
+	public function getInt16( byteOffset : Int, ?littleEndian : Bool ) : Int {
+		var v = getUint16(byteOffset, littleEndian);
+		return v >= 0x8000 ? v - 65536 : v;
+	}
+
+	public function getUint16( byteOffset : Int, ?littleEndian : Bool ) : Int {
+		return littleEndian ? buf.a[offset + byteOffset] | (buf.a[offset + byteOffset + 1] << 8) : (buf.a[offset + byteOffset]<<8) | buf.a[offset + byteOffset + 1];
+	}
+
+	public function getInt32( byteOffset : Int, ?littleEndian : Bool ) : Int {
+		var p = offset + byteOffset;
+		var a = buf.a[p++];
+		var b = buf.a[p++];
+		var c = buf.a[p++];
+		var d = buf.a[p++];
+		return littleEndian ? a | (b<<8) | (c<<16) | (d<<24) : d | (c << 8) | (b << 16) | (a << 24);
+	}
+
+	public function getUint32( byteOffset : Int, ?littleEndian : Bool ) : Int {
+		var v = getInt32(byteOffset, littleEndian);
+		return v < 0 ? cast (v + 4294967296.) : v;
+	}
+
+	public function getFloat32( byteOffset : Int, ?littleEndian : Bool ) : Float {
+		return haxe.io.FPHelper.i32ToFloat(getInt32(byteOffset,littleEndian));
+	}
+
+	public function getFloat64( byteOffset : Int, ?littleEndian : Bool ) : Float {
+		var a = getInt32(byteOffset, littleEndian);
+		var b = getInt32(byteOffset + 4, littleEndian);
+		return haxe.io.FPHelper.i64ToDouble(littleEndian?a:b,littleEndian?b:a);
+	}
+
+	public function setInt8( byteOffset : Int, value : Int ) : Void {
+		buf.a[byteOffset + offset] = (value < 0) ? (value + 128) & 0xFF : value & 0xFF;
+	}
+
+	public function setUint8( byteOffset : Int, value : Int ) : Void {
+		buf.a[byteOffset + offset] = value & 0xFF;
+	}
+
+	public function setInt16( byteOffset : Int, value : Int, ?littleEndian : Bool ) : Void {
+		setUint16(byteOffset, value < 0 ? value + 65536 : value, littleEndian);
+	}
+
+	public function setUint16( byteOffset : Int, value : Int, ?littleEndian : Bool ) : Void {
+		var p = byteOffset + offset;
+		if( littleEndian ) {
+			buf.a[p] = value&0xFF;
+			buf.a[p++] = (value>>8) & 0xFF;
+		} else {
+			buf.a[p++] = (value>>8) & 0xFF;
+			buf.a[p] = value&0xFF;
+		}
+	}
+
+	public function setInt32( byteOffset : Int, value : Int, ?littleEndian : Bool ) : Void {
+		setUint32(byteOffset, value, littleEndian);
+	}
+
+	public function setUint32( byteOffset : Int, value : Int, ?littleEndian : Bool ) : Void {
+		var p = byteOffset + offset;
+		if( littleEndian ) {
+			buf.a[p++] = value & 0xFF;
+			buf.a[p++] = (value>>8) & 0xFF;
+			buf.a[p++] = (value>>16) & 0xFF;
+			buf.a[p++] = value >>> 24;
+		} else {
+			buf.a[p++] = value >>> 24;
+			buf.a[p++] = (value>>16) & 0xFF;
+			buf.a[p++] = (value>>8) & 0xFF;
+			buf.a[p++] = value & 0xFF;
+		}
+	}
+
+	public function setFloat32( byteOffset : Int, value : Float, ?littleEndian : Bool ) : Void {
+		setUint32(byteOffset, haxe.io.FPHelper.floatToI32(value),littleEndian);
+	}
+
+	public function setFloat64( byteOffset : Int, value : Float, ?littleEndian : Bool ) : Void {
+		var i64 = haxe.io.FPHelper.doubleToI64(value);
+		if( littleEndian ) {
+			setUint32(byteOffset, i64.low);
+			setUint32(byteOffset, i64.high);
+		} else {
+			setUint32(byteOffset, i64.high);
+			setUint32(byteOffset, i64.low);
+		}
+	}
+
+	static function __init__() {
+		var DataView = untyped js.Lib.global.DataView || js.html.compat.DataView;
+	}
+
+}
+#end

+ 111 - 0
std/js/html/compat/Float32Array.hx

@@ -0,0 +1,111 @@
+/*
+ * Copyright (C)2005-2016 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;
+
+#if !nodejs
+import js.Lib.nativeThis;
+
+@:ifFeature("js.html.Float32Array.*")
+class Float32Array {
+
+	static var BYTES_PER_ELEMENT = 4;
+
+	static function _new( ?arg1 : Dynamic, ?offset : Int, ?length : Int ) : Dynamic {
+		var arr : Array<Float>;
+		if( untyped __typeof__(arg1) == 'number' ) {
+			arr = new Array();
+			for( i in 0...arg1 )
+				arr[i] = 0;
+			untyped {
+				arr.byteLength = arr.length << 2;
+				arr.byteOffset = 0;
+				arr.buffer = new ArrayBuffer([for( i in 0...arr.length << 2 ) 0]); // no sync
+			}
+		} else if( Std.is(arg1,ArrayBuffer) ) {
+			var buffer : ArrayBuffer = arg1;
+			if( offset == null ) offset = 0;
+			if( length == null ) length = (buffer.byteLength - offset) >> 2;
+			arr = [];
+			// decode buffer
+			for( i in 0...length ) {
+				var val = untyped buffer.a[offset++] | (buffer.a[offset++] << 8) | (buffer.a[offset++] << 16) | (buffer.a[offset++] << 24);
+				arr.push(haxe.io.FPHelper.i32ToFloat(val));
+			}
+			untyped {
+				arr.byteLength = arr.length<<2;
+				arr.byteOffset = offset;
+				arr.buffer = buffer;
+			}
+		} else if( Std.is(arg1, Array) ) {
+			arr = (arg1 : Array<Float>).copy();
+			// loss of memory sync between buffer and array
+			var buffer = [];
+			for( f in arr ) {
+				var i = haxe.io.FPHelper.floatToI32(f);
+				buffer.push(i&0xFF);
+				buffer.push((i>>8)&0xFF);
+				buffer.push((i>>16)&0xFF);
+				buffer.push(i>>>24);
+			}
+			untyped {
+				arr.byteLength = arr.length << 2;
+				arr.byteOffset = 0;
+				arr.buffer = new ArrayBuffer(buffer);
+			}
+		} else
+			throw "TODO "+arg1;
+		untyped {
+			arr.subarray = _subarray;
+			arr.set = _set;
+		}
+		return arr;
+	}
+
+	static function _set( ?arg : Dynamic, ?offset : Int ) {
+		if( Std.is(arg.buffer,ArrayBuffer) ) {
+			var a : Array<Int> = arg;
+			if( arg.byteLength + offset > nativeThis.byteLength )
+				throw "set() outside of range";
+			for( i in 0...arg.byteLength )
+				nativeThis[i + offset] = a[i];
+		} else if( Std.is(arg,Array) ) {
+			var a : Array<Int> = arg;
+			if( a.length + offset > nativeThis.byteLength )
+				throw "set() outside of range";
+			for( i in 0...a.length )
+				nativeThis[i + offset] = a[i];
+		} else
+			throw "TODO";
+	}
+
+	static function _subarray( start : Int, ?end : Int ) {
+		var a = _new(nativeThis.slice(start,end));
+		a.byteOffset = start * 4;
+		return a;
+	}
+
+	static function __init__() {
+		var Float32Array = untyped js.Lib.global.Float32Array || _new;
+	}
+
+}
+#end

+ 118 - 0
std/js/html/compat/Float64Array.hx

@@ -0,0 +1,118 @@
+/*
+ * Copyright (C)2005-2016 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;
+
+#if !nodejs
+import js.Lib.nativeThis;
+
+@:ifFeature("js.html.Float64Array.*")
+class Float64Array {
+
+	static var BYTES_PER_ELEMENT = 8;
+
+	static function _new( ?arg1 : Dynamic, ?offset : Int, ?length : Int ) : Dynamic {
+		var arr : Array<Float>;
+		if( untyped __typeof__(arg1) == 'number' ) {
+			arr = new Array();
+			for( i in 0...arg1 )
+				arr[i] = 0;
+			untyped {
+				arr.byteLength = arr.length << 3;
+				arr.byteOffset = 0;
+				arr.buffer = new ArrayBuffer([for( i in 0...arr.length << 3 ) 0]); // no sync
+			}
+		} else if( Std.is(arg1,ArrayBuffer) ) {
+			var buffer : ArrayBuffer = arg1;
+			if( offset == null ) offset = 0;
+			if( length == null ) length = (buffer.byteLength - offset) >> 3;
+			arr = [];
+			// decode buffer
+			for( i in 0...length ) {
+				var val1 = untyped buffer.a[offset++] | (buffer.a[offset++] << 8) | (buffer.a[offset++] << 16) | (buffer.a[offset++] << 24);
+				var val2 = untyped buffer.a[offset++] | (buffer.a[offset++] << 8) | (buffer.a[offset++] << 16) | (buffer.a[offset++] << 24);
+				arr.push(haxe.io.FPHelper.i64ToDouble(val1,val2));
+			}
+			untyped {
+				arr.byteLength = arr.length<<3;
+				arr.byteOffset = offset;
+				arr.buffer = buffer;
+			}
+		} else if( Std.is(arg1, Array) ) {
+			arr = (arg1 : Array<Float>).copy();
+			// loss of memory sync between buffer and array
+			var buffer = [];
+			for( f in arr ) {
+				var v = haxe.io.FPHelper.doubleToI64(f);
+				var i = v.low;
+				buffer.push(i&0xFF);
+				buffer.push((i>>8)&0xFF);
+				buffer.push((i>>16)&0xFF);
+				buffer.push(i>>>24);
+				var i = v.high;
+				buffer.push(i&0xFF);
+				buffer.push((i>>8)&0xFF);
+				buffer.push((i>>16)&0xFF);
+				buffer.push(i>>>24);
+			}
+			untyped {
+				arr.byteLength = arr.length << 3;
+				arr.byteOffset = 0;
+				arr.buffer = new ArrayBuffer(buffer);
+			}
+		} else
+			throw "TODO "+arg1;
+		untyped {
+			arr.subarray = _subarray;
+			arr.set = _set;
+		}
+		return arr;
+	}
+
+	static function _set( ?arg : Dynamic, ?offset : Int ) {
+		if( Std.is(arg.buffer,ArrayBuffer) ) {
+			var a : Array<Int> = arg;
+			if( arg.byteLength + offset > nativeThis.byteLength )
+				throw "set() outside of range";
+			for( i in 0...arg.byteLength )
+				nativeThis[i + offset] = a[i];
+		} else if( Std.is(arg,Array) ) {
+			var a : Array<Int> = arg;
+			if( a.length + offset > nativeThis.byteLength )
+				throw "set() outside of range";
+			for( i in 0...a.length )
+				nativeThis[i + offset] = a[i];
+		} else
+			throw "TODO";
+	}
+
+	static function _subarray( start : Int, ?end : Int ) {
+		var a = _new(nativeThis.slice(start,end));
+		a.byteOffset = start * 8;
+		return a;
+	}
+
+	static function __init__() {
+		var Float64Array = untyped js.Lib.global.Float64Array || (js.Lib.global.Float32Array ? 'notsupported' : null) || _new;
+	}
+
+}
+#end

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

@@ -0,0 +1,102 @@
+/*
+ * Copyright (C)2005-2016 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;
+
+#if !nodejs
+import js.Lib.nativeThis;
+
+@:ifFeature("js.html.Uint8Array.*")
+class Uint8Array {
+
+	static var BYTES_PER_ELEMENT = 1;
+
+	static function _new( ?arg1 : Dynamic, ?offset : Int, ?length : Int ) : Dynamic {
+		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 "+arg1;
+		untyped {
+			arr.subarray = _subarray;
+			arr.set = _set;
+		}
+		return arr;
+	}
+
+	static function _set( ?arg : Dynamic, ?offset : Int ) {
+		if( Std.is(arg.buffer,ArrayBuffer) ) {
+			var a : Array<Int> = arg;
+			if( arg.byteLength + offset > nativeThis.byteLength )
+				throw "set() outside of range";
+			for( i in 0...arg.byteLength )
+				nativeThis[i + offset] = a[i];
+		} else if( Std.is(arg,Array) ) {
+			var a : Array<Int> = arg;
+			if( a.length + offset > nativeThis.byteLength )
+				throw "set() outside of range";
+			for( i in 0...a.length )
+				nativeThis[i + offset] = a[i];
+		} else
+			throw "TODO";
+	}
+
+	static function _subarray( start : Int, ?end : Int ) {
+		var a = _new(nativeThis.slice(start,end));
+		a.byteOffset = start;
+		return a;
+	}
+
+	static function __init__() {
+		var Uint8Array = untyped js.Lib.global.Uint8Array || _new;
+	}
+
+}
+#end