1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 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__() {
var DataView = untyped js.Lib.global.DataView || js.html.compat.DataView;
}
}
|