DataView.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C)2005-2014 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package js.html.compat;
  23. import haxe.io.Error;
  24. @:keep
  25. @:access(js.html.compat.ArrayBuffer)
  26. class DataView {
  27. var buf : ArrayBuffer;
  28. var offset : Int;
  29. var length : Int;
  30. public function new( buffer : ArrayBuffer, ?byteOffset : Int, ?byteLength : Int ) : Void {
  31. this.buf = buffer;
  32. this.offset = byteOffset == null ? 0 : byteOffset;
  33. this.length = byteLength == null ? buffer.byteLength - this.offset : byteLength;
  34. if( offset < 0 || length < 0 || offset+length > buffer.byteLength )
  35. throw OutsideBounds;
  36. }
  37. public function getInt8( byteOffset : Int ) : Int {
  38. var v = buf.a[offset + byteOffset];
  39. return v >= 0x80 ? v - 256 : v;
  40. }
  41. public function getUint8( byteOffset : Int ) : Int {
  42. return buf.a[offset + byteOffset];
  43. }
  44. public function getInt16( byteOffset : Int, ?littleEndian : Bool ) : Int {
  45. var v = getUint16(byteOffset, littleEndian);
  46. return v >= 0x8000 ? v - 65536 : v;
  47. }
  48. public function getUint16( byteOffset : Int, ?littleEndian : Bool ) : Int {
  49. return littleEndian ? buf.a[offset + byteOffset] | (buf.a[offset + byteOffset + 1] << 8) : (buf.a[offset + byteOffset]<<8) | buf.a[offset + byteOffset + 1];
  50. }
  51. public function getInt32( byteOffset : Int, ?littleEndian : Bool ) : Int {
  52. var p = offset + byteOffset;
  53. var a = buf.a[p++];
  54. var b = buf.a[p++];
  55. var c = buf.a[p++];
  56. var d = buf.a[p++];
  57. return littleEndian ? a | (b<<8) | (c<<16) | (d<<24) : d | (c << 8) | (b << 16) | (a << 24);
  58. }
  59. public function getUint32( byteOffset : Int, ?littleEndian : Bool ) : Int {
  60. var v = getInt32(byteOffset, littleEndian);
  61. return v < 0 ? cast (v + 4294967296.) : v;
  62. }
  63. public function getFloat32( byteOffset : Int, ?littleEndian : Bool ) : Float {
  64. return haxe.io.FPHelper.i32ToFloat(getInt32(byteOffset,littleEndian));
  65. }
  66. public function getFloat64( byteOffset : Int, ?littleEndian : Bool ) : Float {
  67. var a = getInt32(byteOffset, littleEndian);
  68. var b = getInt32(byteOffset + 4, littleEndian);
  69. return haxe.io.FPHelper.i64ToDouble(littleEndian?a:b,littleEndian?b:a);
  70. }
  71. public function setInt8( byteOffset : Int, value : Int ) : Void {
  72. buf.a[byteOffset + offset] = (value < 0) ? (value + 128) & 0xFF : value & 0xFF;
  73. }
  74. public function setUint8( byteOffset : Int, value : Int ) : Void {
  75. buf.a[byteOffset + offset] = value & 0xFF;
  76. }
  77. public function setInt16( byteOffset : Int, value : Int, ?littleEndian : Bool ) : Void {
  78. setUint16(byteOffset, value < 0 ? value + 65536 : value, littleEndian);
  79. }
  80. public function setUint16( byteOffset : Int, value : Int, ?littleEndian : Bool ) : Void {
  81. var p = byteOffset + offset;
  82. if( littleEndian ) {
  83. buf.a[p] = value&0xFF;
  84. buf.a[p++] = (value>>8) & 0xFF;
  85. } else {
  86. buf.a[p++] = (value>>8) & 0xFF;
  87. buf.a[p] = value&0xFF;
  88. }
  89. }
  90. public function setInt32( byteOffset : Int, value : Int, ?littleEndian : Bool ) : Void {
  91. setUint32(byteOffset, value, littleEndian);
  92. }
  93. public function setUint32( byteOffset : Int, value : Int, ?littleEndian : Bool ) : Void {
  94. var p = byteOffset + offset;
  95. if( littleEndian ) {
  96. buf.a[p++] = value & 0xFF;
  97. buf.a[p++] = (value>>8) & 0xFF;
  98. buf.a[p++] = (value>>16) & 0xFF;
  99. buf.a[p++] = value >>> 24;
  100. } else {
  101. buf.a[p++] = value >>> 24;
  102. buf.a[p++] = (value>>16) & 0xFF;
  103. buf.a[p++] = (value>>8) & 0xFF;
  104. buf.a[p++] = value & 0xFF;
  105. }
  106. }
  107. public function setFloat32( byteOffset : Int, value : Float, ?littleEndian : Bool ) : Void {
  108. setUint32(byteOffset, haxe.io.FPHelper.floatToI32(value),littleEndian);
  109. }
  110. public function setFloat64( byteOffset : Int, value : Float, ?littleEndian : Bool ) : Void {
  111. var i64 = haxe.io.FPHelper.doubleToI64(value);
  112. if( littleEndian ) {
  113. setUint32(byteOffset, i64.low);
  114. setUint32(byteOffset, i64.high);
  115. } else {
  116. setUint32(byteOffset, i64.high);
  117. setUint32(byteOffset, i64.low);
  118. }
  119. }
  120. static function __init__() untyped {
  121. var DataView = __js__('typeof(window) != "undefined" && window.DataView') || (__js__('typeof(global) != "undefined" && global.DataView')) || DataView;
  122. }
  123. }