Uint8Array.hx 3.2 KB

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