Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

041-ControlFlowsTryCatchFinally.cs 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.Burst;
  5. using Unity.Collections;
  6. using Unity.Collections.LowLevel.Unsafe;
  7. namespace Burst.Compiler.IL.Tests
  8. {
  9. internal class ControlFlowsTryCatchFinally
  10. {
  11. [TestCompiler(-10)]
  12. [TestCompiler(0)]
  13. [TestCompiler(10)]
  14. public static int TryFinallySimple(int i)
  15. {
  16. try
  17. {
  18. if (i == 0) // case 0
  19. {
  20. return 1;
  21. }
  22. else if (i > 0) // case 10
  23. {
  24. i = i * 2;
  25. }
  26. else
  27. {
  28. i = i * 3; // case -10
  29. }
  30. }
  31. finally
  32. {
  33. i = i + 1;
  34. }
  35. return i; // both case 10 and -10
  36. }
  37. static void Oof()
  38. {
  39. }
  40. [TestCompiler]
  41. public static void TryFinallyFirstBlock()
  42. {
  43. try
  44. {
  45. }
  46. finally
  47. {
  48. Oof();
  49. }
  50. }
  51. [TestCompiler(-3)]
  52. [TestCompiler(0)]
  53. [TestCompiler(3)]
  54. public static int TryFinallyComplex1(int i)
  55. {
  56. try
  57. {
  58. try
  59. {
  60. if (i == 0)
  61. {
  62. return i - 1;
  63. }
  64. i += 3;
  65. }
  66. finally
  67. {
  68. if (i == 0) // case i: -3
  69. {
  70. i = 1;
  71. }
  72. else
  73. {
  74. i = i * 10; // case i: 3
  75. }
  76. }
  77. }
  78. finally
  79. {
  80. i = i * 2; // both -3 and 3
  81. }
  82. return i + 1;
  83. }
  84. [TestCompiler(-10)]
  85. [TestCompiler(0)] // case 0
  86. [TestCompiler(10)]
  87. public static int TryFinallyComplex2(int i)
  88. {
  89. // First block of nested try/catch
  90. try
  91. {
  92. try
  93. {
  94. if (i == 0) // case 0
  95. {
  96. return i - 1;
  97. }
  98. i = i * 2;
  99. }
  100. finally
  101. {
  102. i++;
  103. }
  104. }
  105. finally
  106. {
  107. i = i * 3;
  108. }
  109. // Second block of nested try/catch
  110. try
  111. {
  112. i = i - 2;
  113. try
  114. {
  115. if (i < 0) // case -10
  116. {
  117. return i * 5;
  118. }
  119. i += 3; // case 10
  120. }
  121. finally
  122. {
  123. i += 11;
  124. }
  125. }
  126. finally
  127. {
  128. i = i * 3;
  129. }
  130. return i + 1; // case 10
  131. }
  132. [TestCompiler]
  133. public static int TryUsingDispose()
  134. {
  135. using (var buffer = new CustomBuffer(32))
  136. {
  137. return buffer.Hash();
  138. }
  139. }
  140. [TestCompiler]
  141. public static int ForEachTryFinally()
  142. {
  143. int hashCode = 0;
  144. foreach (var value in new RangeEnumerable(1, 100))
  145. {
  146. hashCode = (hashCode * 397) ^ value;
  147. }
  148. return hashCode;
  149. }
  150. [TestCompiler(ExpectCompilerException = true, ExpectedDiagnosticId = DiagnosticId.ERR_CatchConstructionNotSupported)]
  151. public static int TryCatch()
  152. {
  153. try
  154. {
  155. return default(int);
  156. }
  157. catch (InvalidOperationException)
  158. {
  159. return 1;
  160. }
  161. }
  162. private unsafe struct CustomBuffer : IDisposable
  163. {
  164. private readonly int _size;
  165. private byte* _buffer;
  166. public CustomBuffer(int size)
  167. {
  168. _size = size;
  169. _buffer = (byte*)UnsafeUtility.Malloc(size, 4, Allocator.Persistent);
  170. for (int i = 0; i < size; i++)
  171. {
  172. _buffer[i] = (byte)(i + 1);
  173. }
  174. }
  175. public int Hash()
  176. {
  177. int hashCode = _size;
  178. for (int i = 0; i < _size; i++)
  179. {
  180. hashCode = (hashCode * 397) ^ (byte)_buffer[i];
  181. }
  182. return hashCode;
  183. }
  184. public unsafe void Dispose()
  185. {
  186. if (_buffer != null)
  187. {
  188. UnsafeUtility.Free(_buffer, Allocator.Persistent);
  189. _buffer = (byte*) 0;
  190. }
  191. }
  192. }
  193. private struct RangeEnumerable : IEnumerable<int>
  194. {
  195. private readonly int _from;
  196. private readonly int _to;
  197. public RangeEnumerable(int from, int to)
  198. {
  199. _from = @from;
  200. _to = to;
  201. }
  202. public Enumerator GetEnumerator()
  203. {
  204. return new Enumerator();
  205. }
  206. IEnumerator<int> IEnumerable<int>.GetEnumerator()
  207. {
  208. return GetEnumerator();
  209. }
  210. IEnumerator IEnumerable.GetEnumerator()
  211. {
  212. return GetEnumerator();
  213. }
  214. public struct Enumerator : IEnumerator<int>
  215. {
  216. private readonly int _from;
  217. private readonly int _to;
  218. public Enumerator(int from, int to)
  219. {
  220. _from = @from;
  221. _to = to;
  222. Current = -1;
  223. }
  224. public void Dispose()
  225. {
  226. // nothing to do
  227. }
  228. public bool MoveNext()
  229. {
  230. if (Current < 0)
  231. {
  232. Current = _from;
  233. return true;
  234. }
  235. int nextIndex = Current + 1;
  236. if (nextIndex >= _from && nextIndex <= _to)
  237. {
  238. Current = nextIndex;
  239. return true;
  240. }
  241. return false;
  242. }
  243. public void Reset()
  244. {
  245. }
  246. public int Current { get; private set; }
  247. object IEnumerator.Current => Current;
  248. }
  249. }
  250. }
  251. }