DataView.hx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (C)2005-2016 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. #if !nodejs
  24. import haxe.io.Error;
  25. @:ifFeature("js.html.DataView.*")
  26. @:access(js.html.compat.ArrayBuffer)
  27. class DataView {
  28. var buf : ArrayBuffer;
  29. var offset : Int;
  30. var length : Int;
  31. public function new( buffer : ArrayBuffer, ?byteOffset : Int, ?byteLength : Int ) : Void {
  32. this.buf = buffer;
  33. this.offset = byteOffset == null ? 0 : byteOffset;
  34. this.length = byteLength == null ? buffer.byteLength - this.offset : byteLength;
  35. if( offset < 0 || length < 0 || offset+length > buffer.byteLength )
  36. throw OutsideBounds;
  37. }
  38. public function getInt8( byteOffset : Int ) : Int {
  39. var v = buf.a[offset + byteOffset];
  40. return v >= 0x80 ? v - 256 : v;
  41. }
  42. public function getUint8( byteOffset : Int ) : Int {
  43. return buf.a[offset + byteOffset];
  44. }
  45. public function getInt16( byteOffset : Int, ?littleEndian : Bool ) : Int {
  46. var v = getUint16(byteOffset, littleEndian);
  47. return v >= 0x8000 ? v - 65536 : v;
  48. }
  49. public function getUint16( byteOffset : Int, ?littleEndian : Bool ) : Int {
  50. return littleEndian ? buf.a[offset + byteOffset] | (buf.a[offset + byteOffset + 1] << 8) : (buf.a[offset + byteOffset]<<8) | buf.a[offset + byteOffset + 1];
  51. }
  52. public function getInt32( byteOffset : Int, ?littleEndian : Bool ) : Int {
  53. var p = offset + byteOffset;
  54. var a = buf.a[p++];
  55. var b = buf.a[p++];
  56. var c = buf.a[p++];
  57. var d = buf.a[p++];
  58. return littleEndian ? a | (b<<8) | (c<<16) | (d<<24) : d | (c << 8) | (b << 16) | (a << 24);
  59. }
  60. public function getUint32( byteOffset : Int, ?littleEndian : Bool ) : Int {
  61. var v = getInt32(byteOffset, littleEndian);
  62. return v < 0 ? cast (v + 4294967296.) : v;
  63. }
  64. public function getFloat32( byteOffset : Int, ?littleEndian : Bool ) : Float {
  65. return haxe.io.FPHelper.i32ToFloat(getInt32(byteOffset,littleEndian));
  66. }
  67. public function getFloat64( byteOffset : Int, ?littleEndian : Bool ) : Float {
  68. var a = getInt32(byteOffset, littleEndian);
  69. var b = getInt32(byteOffset + 4, littleEndian);
  70. return haxe.io.FPHelper.i64ToDouble(littleEndian?a:b,littleEndian?b:a);
  71. }
  72. public function setInt8( byteOffset : Int, value : Int ) : Void {
  73. buf.a[byteOffset + offset] = (value < 0) ? (value + 128) & 0xFF : value & 0xFF;
  74. }
  75. public function setUint8( byteOffset : Int, value : Int ) : Void {
  76. buf.a[byteOffset + offset] = value & 0xFF;
  77. }
  78. public function setInt16( byteOffset : Int, value : Int, ?littleEndian : Bool ) : Void {
  79. setUint16(byteOffset, value < 0 ? value + 65536 : value, littleEndian);
  80. }
  81. public function setUint16( byteOffset : Int, value : Int, ?littleEndian : Bool ) : Void {
  82. var p = byteOffset + offset;
  83. if( littleEndian ) {
  84. buf.a[p] = value&0xFF;
  85. buf.a[p++] = (value>>8) & 0xFF;
  86. } else {
  87. buf.a[p++] = (value>>8) & 0xFF;
  88. buf.a[p] = value&0xFF;
  89. }
  90. }
  91. public function setInt32( byteOffset : Int, value : Int, ?littleEndian : Bool ) : Void {
  92. setUint32(byteOffset, value, littleEndian);
  93. }
  94. public function setUint32( byteOffset : Int, value : Int, ?littleEndian : Bool ) : Void {
  95. var p = byteOffset + offset;
  96. if( littleEndian ) {
  97. buf.a[p++] = value & 0xFF;
  98. buf.a[p++] = (value>>8) & 0xFF;
  99. buf.a[p++] = (value>>16) & 0xFF;
  100. buf.a[p++] = value >>> 24;
  101. } else {
  102. buf.a[p++] = value >>> 24;
  103. buf.a[p++] = (value>>16) & 0xFF;
  104. buf.a[p++] = (value>>8) & 0xFF;
  105. buf.a[p++] = value & 0xFF;
  106. }
  107. }
  108. public function setFloat32( byteOffset : Int, value : Float, ?littleEndian : Bool ) : Void {
  109. setUint32(byteOffset, haxe.io.FPHelper.floatToI32(value),littleEndian);
  110. }
  111. public function setFloat64( byteOffset : Int, value : Float, ?littleEndian : Bool ) : Void {
  112. var i64 = haxe.io.FPHelper.doubleToI64(value);
  113. if( littleEndian ) {
  114. setUint32(byteOffset, i64.low);
  115. setUint32(byteOffset, i64.high);
  116. } else {
  117. setUint32(byteOffset, i64.high);
  118. setUint32(byteOffset, i64.low);
  119. }
  120. }
  121. static function __init__() {
  122. var DataView = untyped js.Lib.global.DataView || js.html.compat.DataView;
  123. }
  124. }
  125. #end