Lib.hx 8.3 KB

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