Lib.hx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package java;
  2. /**
  3. Platform-specific Java Library. Provides some platform-specific functions for the Java target,
  4. such as conversion from haxe types to native types and vice-versa.
  5. **/
  6. //we cannot use the java package for custom classes, so we're redefining it as "haxe.java.Lib"
  7. @:native('haxe.java.Lib') class Lib
  8. {
  9. /**
  10. Returns a native array from the supplied Array. This native array is unsafe to be written on,
  11. as it may or may not be linked to the actual Array implementation.
  12. If equalLengthRequired is true, the result might be a copy of an array with the correct size.
  13. **/
  14. public static function nativeArray<T>(arr:Array<T>, equalLengthRequired:Bool):NativeArray<T>
  15. {
  16. var native:NativeArray<T> = untyped arr.__a;
  17. if (native.length == arr.length)
  18. {
  19. return native;
  20. } else {
  21. return null;
  22. }
  23. }
  24. /**
  25. Gets the native System.Type from the supplied object. Will throw an exception in case of null being passed.
  26. **/
  27. @:functionBody('
  28. return (java.lang.Class<T>) obj.getClass();
  29. ')
  30. public static function nativeType<T>(obj:T):java.lang.Class<T>
  31. {
  32. return null;
  33. }
  34. /**
  35. Returns a Haxe Array of a native Array.
  36. It won't copy the contents of the native array, so unless any operation triggers an array resize,
  37. all changes made to the Haxe array will affect the native array argument.
  38. **/
  39. public static function array<T>(native:java.NativeArray<T>):Array<T>
  40. {
  41. return untyped Array.ofNative(native);
  42. }
  43. /**
  44. Allocates a new Haxe Array with a predetermined size
  45. **/
  46. public static function arrayAlloc<T>(size:Int):Array<T>
  47. {
  48. return untyped Array.alloc(size);
  49. }
  50. /**
  51. Ensures that one thread does not enter a critical section of code while another thread
  52. is in the critical section. If another thread attempts to enter a locked code, it
  53. will wait, block, until the object is released.
  54. This is the equivalent to "synchronized" in java code.
  55. This method only exists at compile-time, so it can't be called via reflection.
  56. **/
  57. @:extern public static inline function lock(obj:Dynamic, block:Dynamic):Void
  58. {
  59. untyped __lock__(obj, block);
  60. }
  61. }