Bytes.hx 5.9 KB

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