DataView.hx 5.2 KB

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