Float64Array.hx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 js.Lib.nativeThis;
  25. @:ifFeature("js.html.Float64Array.*")
  26. class Float64Array {
  27. static var BYTES_PER_ELEMENT = 8;
  28. static function _new( ?arg1 : Dynamic, ?offset : Int, ?length : Int ) : Dynamic {
  29. var arr : Array<Float>;
  30. if( js.Syntax.typeof(arg1) == 'number' ) {
  31. arr = new Array();
  32. for( i in 0...arg1 )
  33. arr[i] = 0;
  34. untyped {
  35. arr.byteLength = arr.length << 3;
  36. arr.byteOffset = 0;
  37. arr.buffer = new ArrayBuffer([for( i in 0...arr.length << 3 ) 0]); // no sync
  38. }
  39. } else if( Std.is(arg1,ArrayBuffer) ) {
  40. var buffer : ArrayBuffer = arg1;
  41. if( offset == null ) offset = 0;
  42. if( length == null ) length = (buffer.byteLength - offset) >> 3;
  43. arr = [];
  44. // decode buffer
  45. for( i in 0...length ) {
  46. var val1 = untyped buffer.a[offset++] | (buffer.a[offset++] << 8) | (buffer.a[offset++] << 16) | (buffer.a[offset++] << 24);
  47. var val2 = untyped buffer.a[offset++] | (buffer.a[offset++] << 8) | (buffer.a[offset++] << 16) | (buffer.a[offset++] << 24);
  48. arr.push(haxe.io.FPHelper.i64ToDouble(val1,val2));
  49. }
  50. untyped {
  51. arr.byteLength = arr.length<<3;
  52. arr.byteOffset = offset;
  53. arr.buffer = buffer;
  54. }
  55. } else if( Std.is(arg1, Array) ) {
  56. arr = (arg1 : Array<Float>).copy();
  57. // loss of memory sync between buffer and array
  58. var buffer = [];
  59. for( f in arr ) {
  60. var v = haxe.io.FPHelper.doubleToI64(f);
  61. var i = v.low;
  62. buffer.push(i&0xFF);
  63. buffer.push((i>>8)&0xFF);
  64. buffer.push((i>>16)&0xFF);
  65. buffer.push(i>>>24);
  66. var i = v.high;
  67. buffer.push(i&0xFF);
  68. buffer.push((i>>8)&0xFF);
  69. buffer.push((i>>16)&0xFF);
  70. buffer.push(i>>>24);
  71. }
  72. untyped {
  73. arr.byteLength = arr.length << 3;
  74. arr.byteOffset = 0;
  75. arr.buffer = new ArrayBuffer(buffer);
  76. }
  77. } else
  78. throw "TODO "+arg1;
  79. untyped {
  80. arr.subarray = _subarray;
  81. arr.set = _set;
  82. }
  83. return arr;
  84. }
  85. static function _set( ?arg : Dynamic, ?offset : Int ) {
  86. if( Std.is(arg.buffer,ArrayBuffer) ) {
  87. var a : Array<Int> = arg;
  88. if( arg.byteLength + offset > nativeThis.byteLength )
  89. throw "set() outside of range";
  90. for( i in 0...arg.byteLength )
  91. nativeThis[i + offset] = a[i];
  92. } else if( Std.is(arg,Array) ) {
  93. var a : Array<Int> = arg;
  94. if( a.length + offset > nativeThis.byteLength )
  95. throw "set() outside of range";
  96. for( i in 0...a.length )
  97. nativeThis[i + offset] = a[i];
  98. } else
  99. throw "TODO";
  100. }
  101. static function _subarray( start : Int, ?end : Int ) {
  102. var a = _new(nativeThis.slice(start,end));
  103. a.byteOffset = start * 8;
  104. return a;
  105. }
  106. static function __init__() {
  107. untyped __js__("var Float64Array = {0} || ({1} ? 'notsupported' : null) || {2}", js.Lib.global.Float64Array, js.Lib.global.Float32Array, _new);
  108. }
  109. }
  110. #end