Uint8Array.hx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Uint8Array {
  25. static var BYTES_PER_ELEMENT = 1;
  26. static function _new( ?arg1 : Dynamic, ?offset : Int, ?length : Int ) : Dynamic {
  27. var arr;
  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;
  34. arr.byteOffset = 0;
  35. arr.buffer = new ArrayBuffer(arr);
  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;
  41. if( offset == 0 )
  42. arr = cast @:privateAccess buffer.a;
  43. else
  44. // here we are losing the fact that we should reference the same data,
  45. // but I don't see another way to have this behaviour while keeping [] access
  46. arr = cast @:privateAccess buffer.a.slice(offset, offset+length);
  47. untyped {
  48. arr.byteLength = arr.length;
  49. arr.byteOffset = offset;
  50. arr.buffer = buffer;
  51. }
  52. } else if( Std.is(arg1, Array) ) {
  53. arr = (arg1 : Array<Int>).copy();
  54. untyped {
  55. arr.byteLength = arr.length;
  56. arr.byteOffset = 0;
  57. arr.buffer = new ArrayBuffer(arr);
  58. }
  59. } else
  60. throw "TODO "+arg1;
  61. untyped {
  62. arr.subarray = _subarray;
  63. arr.set = _set;
  64. }
  65. return arr;
  66. }
  67. static function _set( ?arg : Dynamic, ?offset : Int ) {
  68. var t : Dynamic = untyped __js__("this");
  69. if( Std.is(arg.buffer,ArrayBuffer) ) {
  70. var a : Array<Int> = arg;
  71. if( arg.byteLength + offset > t.byteLength )
  72. throw "set() outside of range";
  73. for( i in 0...arg.byteLength )
  74. t[i + offset] = a[i];
  75. } else if( Std.is(arg,Array) ) {
  76. var a : Array<Int> = arg;
  77. if( a.length + offset > t.byteLength )
  78. throw "set() outside of range";
  79. for( i in 0...a.length )
  80. t[i + offset] = a[i];
  81. } else
  82. throw "TODO";
  83. }
  84. static function _subarray( start : Int, ?end : Int ) {
  85. var t : Dynamic = untyped __js__("this");
  86. var a = _new(t.slice(start,end));
  87. a.byteOffset = start;
  88. return a;
  89. }
  90. static function __init__() untyped {
  91. var Uint8Array = __js__('typeof(window) != "undefined" && window.Uint8Array') || (__js__('typeof(global) != "undefined" && global.Uint8Array')) || _new;
  92. }
  93. }