Float32Array.hx 3.5 KB

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