diff --git a/std/js/_std/haxe/io/Bytes.hx b/std/js/_std/haxe/io/Bytes.hx index 5c1c099c116..a9b13267068 100644 --- a/std/js/_std/haxe/io/Bytes.hx +++ b/std/js/_std/haxe/io/Bytes.hx @@ -21,6 +21,7 @@ */ package haxe.io; import js.html.compat.Uint8Array; +import js.html.compat.DataView; @:coreApi class Bytes { diff --git a/std/js/html/compat/ArrayBuffer.hx b/std/js/html/compat/ArrayBuffer.hx index f6db18c98cc..a38775d34a5 100644 --- a/std/js/html/compat/ArrayBuffer.hx +++ b/std/js/html/compat/ArrayBuffer.hx @@ -25,7 +25,7 @@ package js.html.compat; class ArrayBuffer { public var byteLength : Int; - var a : Array; + var a : Array; public function new( ?a : Dynamic ) { if( Std.is(a,Array) ) { diff --git a/std/js/html/compat/DataView.hx b/std/js/html/compat/DataView.hx new file mode 100644 index 00000000000..0db63244948 --- /dev/null +++ b/std/js/html/compat/DataView.hx @@ -0,0 +1,144 @@ +/* + * 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; +import haxe.io.Error; + +@:keep +@: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__() untyped { + var DataView = __js__('typeof(window) != "undefined" && window.DataView') || (__js__('typeof(global) != "undefined" && global.DataView')) || _new; + } + +} \ No newline at end of file diff --git a/tests/unit/src/unitstd/haxe/io/BytesBuffer.unit.hx b/tests/unit/src/unitstd/haxe/io/BytesBuffer.unit.hx index 19b55296ac5..d35981c2919 100644 --- a/tests/unit/src/unitstd/haxe/io/BytesBuffer.unit.hx +++ b/tests/unit/src/unitstd/haxe/io/BytesBuffer.unit.hx @@ -1,6 +1,4 @@ -if( !haxe.io.ArrayBufferView.EMULATED ) { // TODO : to reenable once we have html compat for IE8/DataView - var out = new haxe.io.BytesBuffer(); out.length == 0; @@ -21,7 +19,6 @@ b.length == str.length; for( i in 0...str.length ) b.get(i) == str.charCodeAt(i); - var out = new haxe.io.BytesBuffer(); out.addInt32(0xABCDEF00); out.addByte(42); @@ -45,5 +42,3 @@ b.get(5) == 102; b.get(9) == 51; b.get(17) == 0xD1; b.get(22) == 0xEF; - -} \ No newline at end of file