Lib.hx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package cs;
  2. import cs.system.Type;
  3. /**
  4. Platform-specific C# Library. Provides some platform-specific functions for the C# target,
  5. such as conversion from haxe types to native types and vice-versa.
  6. **/
  7. class Lib
  8. {
  9. @:keep private static var decimalSeparator:String;
  10. /**
  11. Changes the current culture settings to allow a consistent cross-target behavior.
  12. Currently the only change made is in regard to the decimal separator, which is always set to "."
  13. **/
  14. @:functionBody('
  15. System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.Name, true);
  16. decimalSeparator = ci.NumberFormat.NumberDecimalSeparator;
  17. ci.NumberFormat.NumberDecimalSeparator = ".";
  18. System.Threading.Thread.CurrentThread.CurrentCulture = ci;
  19. ')
  20. @:keep public static function applyCultureChanges():Void
  21. {
  22. }
  23. /**
  24. Reverts the culture changes to the default settings.
  25. **/
  26. @:functionBody('
  27. System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.Name, true);
  28. System.Threading.Thread.CurrentThread.CurrentCulture = ci;
  29. ')
  30. public static function revertDefaultCulture():Void
  31. {
  32. }
  33. /**
  34. Returns a native array from the supplied Array. This native array is unsafe to be written on,
  35. as it may or may not be linked to the actual Array implementation.
  36. If equalLengthRequired is true, the result might be a copy of an array with the correct size.
  37. **/
  38. public static function nativeArray<T>(arr:Array<T>, equalLengthRequired:Bool):NativeArray<T>
  39. {
  40. var native:NativeArray<T> = untyped arr.__a;
  41. var len = arr.length;
  42. if (!equalLengthRequired || native.Length == len)
  43. {
  44. return native;
  45. } else {
  46. var ret = new NativeArray<T>(len);
  47. cs.system.Array.Copy(native, 0, ret, 0, len);
  48. return ret;
  49. }
  50. }
  51. /**
  52. Provides support for the "as" keyword in C#.
  53. If the object is not of the supplied type "T", it will return null instead of rasing an exception.
  54. This function will not work with Value Types (such as Int, Float, Bool...)
  55. **/
  56. @:functionBody('
  57. throw new haxe.lang.HaxeException("This function cannot be accessed at runtime");
  58. ')
  59. @:extern public static inline function as<T>(obj:Dynamic, cl:Class<T>):T
  60. {
  61. return untyped __as__(obj, cl);
  62. }
  63. /**
  64. Returns a Class<> equivalent to the native System.Type type.
  65. Currently Haxe's Class<> is equivalent to System.Type, but this is an implementation detail.
  66. This may change in the future, so use this function whenever you need to perform such conversion.
  67. **/
  68. public static inline function fromNativeType(t:cs.system.Type):Class<Dynamic>
  69. {
  70. return untyped t;
  71. }
  72. /**
  73. Returns a System.Type equivalent to the Haxe Class<> type.
  74. Currently Haxe's Class<> is equivalent to System.Type, but this is an implementation detail.
  75. This may change in the future, so use this function whenever you need to perform such conversion.
  76. **/
  77. public static inline function toNativeType(cl:Class<Dynamic>):Type
  78. {
  79. return untyped cl;
  80. }
  81. /**
  82. Gets the native System.Type from the supplied object. Will throw an exception in case of null being passed.
  83. **/
  84. public static function nativeType(obj:Dynamic):Type
  85. {
  86. return untyped obj.GetType();
  87. }
  88. /**
  89. Returns a Haxe Array of a native Array.
  90. It won't copy the contents of the native array, so unless any operation triggers an array resize,
  91. all changes made to the Haxe array will affect the native array argument.
  92. **/
  93. public static function array<T>(native:cs.NativeArray<T>):Array<T>
  94. {
  95. return untyped Array.ofNative(native);
  96. }
  97. /**
  98. Allocates a new Haxe Array with a predetermined size
  99. **/
  100. public static function arrayAlloc<T>(size:Int):Array<T>
  101. {
  102. return untyped Array.alloc(size);
  103. }
  104. /**
  105. Creates a "checked" block, which throws exceptions for overflows.
  106. Usage:
  107. cs.Lib.checked({
  108. var x = 1000;
  109. while(true)
  110. {
  111. x *= x;
  112. }
  113. });
  114. This method only exists at compile-time, so it can't be called via reflection.
  115. **/
  116. @:extern public static inline function checked(block:Dynamic):Void
  117. {
  118. untyped __checked__(block);
  119. }
  120. /**
  121. Ensures that one thread does not enter a critical section of code while another thread
  122. is in the critical section. If another thread attempts to enter a locked code, it
  123. will wait, block, until the object is released.
  124. This method only exists at compile-time, so it can't be called via reflection.
  125. **/
  126. @:extern public static inline function lock(obj:Dynamic, block:Dynamic):Void
  127. {
  128. untyped __lock__(obj, block);
  129. }
  130. //Unsafe code manipulation
  131. #if unsafe
  132. /**
  133. Marks its parameters as fixed objects inside the defined block.
  134. The first variable declarations that use cs.Lib.pointerOfArray() will be the fixed definitions.
  135. Usage:
  136. cs.Lib.fixed({
  137. var obj1 = cs.Lib.pointerOfArray(someArray);
  138. var obj2 = cs.Lib.pointerOfArray(someArray2);
  139. var obj3 = cs.Lib.pointerOfArray(someArray3);
  140. //from now on, obj1, obj2 and obj3 are fixed
  141. //we cannot change obj1, obj2 or obj3 variables like this:
  142. //obj1++;
  143. });
  144. This method only exists at compile-time, so it can't be called via reflection.
  145. **/
  146. @:extern public static inline function fixed(block:Dynamic):Void
  147. {
  148. untyped __fixed__(block);
  149. }
  150. /**
  151. Marks the contained block as an unsafe block, meaning that it can contain unsafe code.
  152. Usage:
  153. cs.Lib.unsafe({
  154. //unsafe code is allowed inside here
  155. });
  156. This method only exists at compile-time, so it can't be called via reflection.
  157. **/
  158. @:extern public static inline function unsafe(block:Dynamic):Void
  159. {
  160. untyped __unsafe__(block);
  161. }
  162. /**
  163. Gets the pointer to the address of current local. Equivalent to the "&" operator in C#
  164. Usage:
  165. var x:Int = 0;
  166. cs.Lib.unsafe({
  167. var addr = cs.Lib.addressOf(x);
  168. x[0] = 42;
  169. });
  170. trace(x); //42
  171. This method only exists at compile-time, so it can't be called via reflection.
  172. Warning: This method will only work if a local variable is passed as an argument.
  173. **/
  174. @:extern public static inline function addressOf<T>(variable:T):cs.Pointer<T>
  175. {
  176. return untyped __addressOf__(variable);
  177. }
  178. /**
  179. Gets the value of the pointer address.
  180. Usage:
  181. var x:Int = 0;
  182. cs.Lib.unsafe({
  183. var addr = cs.Lib.addressOf(x);
  184. trace(cs.Lib.valueOf(addr)); //0
  185. addr[0] = 42;
  186. trace(cs.Lib.valueOf(addr)); //42
  187. });
  188. trace(x); //42
  189. This method only exists at compile-time, so it can't be called via reflection.
  190. **/
  191. @:extern public static inline function valueOf<T>(pointer:cs.Pointer<T>):T
  192. {
  193. return untyped __valueOf__(pointer);
  194. }
  195. /**
  196. Transforms a managed native array into a Pointer. Must be inside a fixed statement
  197. Usage:
  198. var x:cs.NativeArray<Int> = new cs.NativeArray(1);
  199. cs.Lib.unsafe({
  200. cs.Lib.fixed({
  201. var addr = cs.Lib.pointerOfArray(x);
  202. trace(cs.Lib.valueOf(addr)); //0
  203. addr[0] = 42;
  204. trace(cs.Lib.valueOf(addr)); //42
  205. });
  206. });
  207. trace(x[0]); //42
  208. This method only exists at compile-time, so it can't be called via reflection.
  209. **/
  210. @:extern public static inline function pointerOfArray<T>(array:cs.NativeArray<T>):cs.Pointer<T>
  211. {
  212. return cast array;
  213. }
  214. /**
  215. Returns the byte size of the given struct. Only works with structs and basic types.
  216. **/
  217. @:extern public static inline function sizeof(struct:Class<Dynamic>):Int
  218. {
  219. return untyped __sizeof__(struct);
  220. }
  221. #end
  222. }