Lib.hx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 java;
  23. /**
  24. Platform-specific Java Library. Provides some platform-specific functions for the Java target,
  25. such as conversion from Haxe types to native types and vice-versa.
  26. **/
  27. //we cannot use the java package for custom classes, so we're redefining it as "haxe.java.Lib"
  28. @:native('haxe.java.Lib') class Lib
  29. {
  30. /**
  31. Print the specified value on the default output.
  32. **/
  33. inline public static function print( v : Dynamic ) : Void {
  34. Sys.print(v);
  35. }
  36. /**
  37. Print the specified value on the default output followed by a newline character.
  38. **/
  39. inline public static function println( v : Dynamic ) : Void {
  40. Sys.println(v);
  41. }
  42. /**
  43. Returns a native array from the supplied Array. This native array is unsafe to be written on,
  44. as it may or may not be linked to the actual Array implementation.
  45. If `equalLengthRequired` is true, the result might be a copy of an array with the correct size.
  46. **/
  47. inline public static function nativeArray<T>(arr:Array<T>, equalLengthRequired:Bool):NativeArray<T>
  48. {
  49. var ret = new NativeArray(arr.length);
  50. for (i in 0...arr.length)
  51. {
  52. ret[i] = arr[i];
  53. }
  54. return ret;
  55. }
  56. /**
  57. Gets the native `java.lang.Class` from the supplied object. Will throw an exception in case of null being passed.
  58. [deprecated] - use `getNativeType` instead
  59. **/
  60. @:deprecated('The function `nativeType` is deprecated and will be removed in later versions. Please use `getNativeType` instead')
  61. inline public static function nativeType<T>(obj:T):java.lang.Class<T>
  62. {
  63. return untyped obj.getClass();
  64. }
  65. /**
  66. Gets the native `java.lang.Class` from the supplied object. Will throw an exception in case of null being passed.
  67. **/
  68. inline public static function getNativeType<T>(obj:T):java.lang.Class<T>
  69. {
  70. return untyped obj.getClass();
  71. }
  72. /**
  73. Returns a Class<> equivalent to the native java.lang.Class type.
  74. **/
  75. public static inline function fromNativeType<T>(t:java.lang.Class<T>):Class<T>
  76. {
  77. return untyped t;
  78. }
  79. /**
  80. Returns a java.lang.Class equivalent to the Haxe Class<> type.
  81. **/
  82. public static inline function toNativeType<T>(cl:Class<T>):java.lang.Class<T>
  83. {
  84. return untyped cl;
  85. }
  86. /**
  87. Returns a java.lang.Class equivalent to the Haxe Enum<> type.
  88. **/
  89. public static inline function toNativeEnum<T>(cl:Enum<T>):java.lang.Class<T>
  90. {
  91. return untyped cl;
  92. }
  93. /**
  94. Returns a Haxe Array of a native Array.
  95. Unless `copy` is true, it won't copy the contents of the native array,
  96. so unless any operation triggers an array resize, all changes made to the Haxe array will affect the native array argument.
  97. **/
  98. @:generic public static function array<T>(native:java.NativeArray<T>):Array<T>
  99. {
  100. return untyped Array.ofNative(native);
  101. }
  102. @:extern inline private static function doArray<T>(native:java.NativeArray<T>):Array<T>
  103. {
  104. var ret:NativeArray<Dynamic> = new NativeArray(native.length);
  105. for (i in 0...native.length)
  106. {
  107. ret[i] = native[i];
  108. }
  109. return untyped Array.ofNative(ret);
  110. }
  111. public static function array_Int(native:java.NativeArray<Int>):Array<Int>
  112. {
  113. return doArray(native);
  114. }
  115. public static function array_Float(native:java.NativeArray<Float>):Array<Float>
  116. {
  117. return doArray(native);
  118. }
  119. public static function array_Bool(native:java.NativeArray<Bool>):Array<Bool>
  120. {
  121. return doArray(native);
  122. }
  123. public static function array_java_Int8(native:java.NativeArray<java.StdTypes.Int8>):Array<java.StdTypes.Int8>
  124. {
  125. return doArray(native);
  126. }
  127. public static function array_java_Int16(native:java.NativeArray<java.StdTypes.Int16>):Array<java.StdTypes.Int16>
  128. {
  129. return doArray(native);
  130. }
  131. public static function array_java_Char16(native:java.NativeArray<java.StdTypes.Char16>):Array<java.StdTypes.Char16>
  132. {
  133. return doArray(native);
  134. }
  135. public static function array_Single(native:java.NativeArray<Single>):Array<Single>
  136. {
  137. return doArray(native);
  138. }
  139. public static function array_haxe_Int64(native:java.NativeArray<haxe.Int64>):Array<haxe.Int64>
  140. {
  141. return doArray(native);
  142. }
  143. /**
  144. Allocates a new Haxe Array with a predetermined size
  145. **/
  146. public static function arrayAlloc<T>(size:Int):Array<T>
  147. {
  148. return untyped Array.alloc(size);
  149. }
  150. /**
  151. Ensures that one thread does not enter a critical section of code while another thread
  152. is in the critical section. If another thread attempts to enter a locked code, it
  153. will wait, block, until the object is released.
  154. This is the equivalent to "synchronized" in java code.
  155. This method only exists at compile-time, so it can't be called via reflection.
  156. **/
  157. @:extern public static inline function lock<T>(obj:Dynamic, block:T):Void
  158. {
  159. untyped __lock__(obj, block);
  160. }
  161. }