Bytes.hx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Copyright (C)2005-2017 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 hl;
  23. @:coreType abstract Bytes {
  24. @:extern public inline function new( v : Int ) {
  25. this = alloc(v);
  26. }
  27. @:hlNative("std","bytes_blit") public function blit( pos : Int, src : Bytes, srcPos : Int, len : Int ) : Void {
  28. }
  29. @:extern @:arrayAccess public inline function getUI8( pos : Int ) : Int {
  30. return untyped $bgetui8(this,pos);
  31. }
  32. @:extern @:arrayAccess public inline function setUI8( pos : Int, value : Int ) : Int {
  33. untyped $bsetui8(this,pos,value);
  34. return value;
  35. }
  36. @:extern public inline function getI32( pos : Int ) : Int {
  37. return untyped $bgeti32(this,pos);
  38. }
  39. public inline function getUI16( pos : Int ) : Int {
  40. return untyped $bgetui16(this, pos);
  41. }
  42. public inline function setUI16( pos : Int, v : Int ) {
  43. untyped $bsetui16(this,pos,v);
  44. }
  45. @:extern public inline function getF32( pos : Int ) : F32 {
  46. return untyped $bgetf32(this,pos);
  47. }
  48. @:extern public inline function getF64( pos : Int ) : Float {
  49. return untyped $bgetf64(this,pos);
  50. }
  51. @:extern public inline function setI32( pos : Int, value : Int ) : Void {
  52. untyped $bseti32(this, pos, value);
  53. }
  54. @:extern public inline function setF32( pos : Int, value : F32 ) : Void {
  55. untyped $bsetf32(this, pos, value);
  56. }
  57. @:extern public inline function setF64( pos : Int, value : Float ) : Void {
  58. untyped $bsetf64(this, pos, value);
  59. }
  60. @:hlNative("std","alloc_bytes")
  61. static function alloc( size : Int ) : Bytes {
  62. return null;
  63. }
  64. @:hlNative("std","parse_int")
  65. public function parseInt( pos : Int, size : Int ) : Null<Int> {
  66. return null;
  67. }
  68. @:hlNative("std","parse_float")
  69. public function parseFloat( pos : Int, size : Int ) : Float {
  70. return 0.;
  71. }
  72. @:hlNative("std","bytes_compare")
  73. public function compare( pos : Int, bytes : Bytes, bytesPos : Int, size : Int ) : Int {
  74. return 0;
  75. }
  76. @:hlNative("std","bytes_find")
  77. public function find( pos : Int, size : Int, bytes : Bytes, bytesPos : Int, bytesSize : Int ) : Int {
  78. return 0;
  79. }
  80. @:hlNative("std","bytes_fill")
  81. public function fill( pos : Int, size : Int, v : Int ) : Void {
  82. }
  83. @:hlNative("std","bsort_i32")
  84. public function sortI32( pos : Int, length : Int, f : Int->Int->Int ) : Void {
  85. }
  86. @:hlNative("std","bsort_f64")
  87. public function sortF64( pos : Int, length : Int, f : Float->Float->Int ) : Void {
  88. }
  89. /**
  90. Please note that you need to retain the original unoffset'ed Bytes so it does not get garbage collected, unless the pointer was not GC allocated.
  91. **/
  92. @:hlNative("std","bytes_offset")
  93. public function offset( delta : Int ) : Bytes {
  94. return null;
  95. }
  96. /**
  97. Returns an offset between the two pointers. This might overflow in 64 bits if the addresses of the two pointers differs by more than 4GB
  98. **/
  99. @:hlNative("std","bytes_subtract")
  100. public function subtract( other : Bytes ) : Int {
  101. return 0;
  102. }
  103. @:hlNative("std", "bytes_address")
  104. static function get_address( b : Bytes, high : Ref<Int> ) : Int {
  105. return 0;
  106. }
  107. @:hlNative("std", "bytes_from_address")
  108. static function from_address( low : Int, high : Int ) : Bytes {
  109. return null;
  110. }
  111. /**
  112. Creates an pointer at a given memory address (highly unsafe)
  113. **/
  114. public static inline function fromAddress( h : haxe.Int64 ) : Bytes {
  115. return from_address(h.low, h.high);
  116. }
  117. /**
  118. Returns the address value of the bytes. On 32 bit system the upper 32 bits will always be 0
  119. **/
  120. public function address() : haxe.Int64 {
  121. var high = 0;
  122. var low = get_address(this, high);
  123. return haxe.Int64.make(high,low);
  124. }
  125. public function sub( pos : Int, size : Int ) {
  126. var b = new Bytes(size);
  127. b.blit(0, this, pos, size);
  128. return b;
  129. }
  130. @:hlNative("std", "ucs2length")
  131. public function ucs2Length( bytePos : Int ) : Int {
  132. return 0;
  133. }
  134. @:hlNative("std","hash")
  135. function hash() : Int {
  136. return 0;
  137. }
  138. @:hlNative("std","utf8_to_utf16")
  139. public function utf8ToUtf16( bytePos : Int, outSize : Ref<Int> ) : Bytes {
  140. return null;
  141. }
  142. @:hlNative("std","utf16_to_utf8")
  143. public function utf16ToUtf8( bytePos : Int, outSize : Ref<Int> ) : Bytes {
  144. return null;
  145. }
  146. @:hlNative("std", "ucs2_upper")
  147. function ucs2Upper( bytePos : Int, size : Int ) : Bytes {
  148. return null;
  149. }
  150. @:hlNative("std", "ucs2_lower")
  151. function ucs2Lower( bytePos : Int, size : Int ) : Bytes {
  152. return null;
  153. }
  154. @:hlNative("std", "url_encode")
  155. function urlEncode( outSize : Ref<Int> ) : Bytes {
  156. return null;
  157. }
  158. @:hlNative("std", "url_decode")
  159. function urlDecode( outSize : Ref<Int> ) : Bytes {
  160. return null;
  161. }
  162. @:hlNative("std","value_to_string")
  163. public static function fromValue( v : Dynamic, length : Ref<Int> ) : Bytes {
  164. return null;
  165. }
  166. /**
  167. Get the bytes reference from an array of basic types (no copy occurs)
  168. **/
  169. @:extern public static inline function getArray<T>( a : Array<T> ) : Bytes {
  170. return untyped $abytes(a);
  171. }
  172. @:from
  173. public static inline function fromBytes( bytes : haxe.io.Bytes ) {
  174. return @:privateAccess bytes.b;
  175. }
  176. public inline function toBytes( len : Int ) {
  177. return @:privateAccess new haxe.io.Bytes(this, len);
  178. }
  179. }