Bytes.hx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (C)2005-2019 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. @:arrayAccess extern public inline function getUI8( pos : Int ) : Int {
  30. return untyped $bgetui8(this,pos);
  31. }
  32. @:arrayAccess extern 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. #if (hl_ver >= version("1.10"))
  77. @:hlNative("std","bytes_compare16")
  78. public function compare16( bytes : Bytes, size : Int ) : Int {
  79. return 0;
  80. }
  81. #end
  82. @:hlNative("std","bytes_find")
  83. public function find( pos : Int, size : Int, bytes : Bytes, bytesPos : Int, bytesSize : Int ) : Int {
  84. return 0;
  85. }
  86. @:hlNative("std","bytes_fill")
  87. public function fill( pos : Int, size : Int, v : Int ) : Void {
  88. }
  89. @:hlNative("std","bsort_i32")
  90. public function sortI32( pos : Int, length : Int, f : Int->Int->Int ) : Void {
  91. }
  92. @:hlNative("std","bsort_f64")
  93. public function sortF64( pos : Int, length : Int, f : Float->Float->Int ) : Void {
  94. }
  95. /**
  96. 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.
  97. **/
  98. @:hlNative("std","bytes_offset")
  99. public function offset( delta : Int ) : Bytes {
  100. return null;
  101. }
  102. /**
  103. 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
  104. **/
  105. @:hlNative("std","bytes_subtract")
  106. public function subtract( other : Bytes ) : Int {
  107. return 0;
  108. }
  109. @:hlNative("std", "bytes_address")
  110. static function get_address( b : Bytes, high : Ref<Int> ) : Int {
  111. return 0;
  112. }
  113. @:hlNative("std", "bytes_from_address")
  114. static function from_address( low : Int, high : Int ) : Bytes {
  115. return null;
  116. }
  117. /**
  118. Creates an pointer at a given memory address (highly unsafe)
  119. **/
  120. public static inline function fromAddress( h : haxe.Int64 ) : Bytes {
  121. return from_address(h.low, h.high);
  122. }
  123. /**
  124. Returns the address value of the bytes. On 32 bit system the upper 32 bits will always be 0
  125. **/
  126. public function address() : haxe.Int64 {
  127. var high = 0;
  128. var low = get_address(this, high);
  129. return haxe.Int64.make(high,low);
  130. }
  131. public function sub( pos : Int, size : Int ) {
  132. var b = new Bytes(size);
  133. b.blit(0, this, pos, size);
  134. return b;
  135. }
  136. @:hlNative("std", "ucs2length")
  137. public function ucs2Length( bytePos : Int ) : Int {
  138. return 0;
  139. }
  140. @:hlNative("std","hash")
  141. function hash() : Int {
  142. return 0;
  143. }
  144. @:hlNative("std","utf8_to_utf16")
  145. public function utf8ToUtf16( bytePos : Int, outSize : Ref<Int> ) : Bytes {
  146. return null;
  147. }
  148. @:hlNative("std","utf16_to_utf8")
  149. public function utf16ToUtf8( bytePos : Int, outSize : Ref<Int> ) : Bytes {
  150. return null;
  151. }
  152. @:hlNative("std", "ucs2_upper")
  153. function ucs2Upper( bytePos : Int, size : Int ) : Bytes {
  154. return null;
  155. }
  156. @:hlNative("std", "ucs2_lower")
  157. function ucs2Lower( bytePos : Int, size : Int ) : Bytes {
  158. return null;
  159. }
  160. @:hlNative("std", "url_encode")
  161. function urlEncode( outSize : Ref<Int> ) : Bytes {
  162. return null;
  163. }
  164. @:hlNative("std", "url_decode")
  165. function urlDecode( outSize : Ref<Int> ) : Bytes {
  166. return null;
  167. }
  168. @:hlNative("std","value_to_string")
  169. public static function fromValue( v : Dynamic, length : Ref<Int> ) : Bytes {
  170. return null;
  171. }
  172. /**
  173. Get the bytes reference from an array of basic types (no copy occurs)
  174. **/
  175. extern public static inline function getArray<T>( a : Array<T> ) : Bytes {
  176. return untyped $abytes(a);
  177. }
  178. @:from
  179. public static inline function fromBytes( bytes : haxe.io.Bytes ) {
  180. return @:privateAccess bytes.b;
  181. }
  182. public inline function toBytes( len : Int ) {
  183. return @:privateAccess new haxe.io.Bytes(this, len);
  184. }
  185. }