2
0

ExceptionHelper.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Reflection;
  3. using System.Runtime.ExceptionServices;
  4. using Esprima;
  5. using Jint.Native;
  6. using Jint.Native.Error;
  7. using Jint.Runtime.CallStack;
  8. using Jint.Runtime.Modules;
  9. using Jint.Runtime.References;
  10. namespace Jint.Runtime
  11. {
  12. internal static class ExceptionHelper
  13. {
  14. [DoesNotReturn]
  15. public static void ThrowSyntaxError(Realm realm, string? message = null)
  16. {
  17. throw new JavaScriptException(realm.Intrinsics.SyntaxError, message);
  18. }
  19. [DoesNotReturn]
  20. public static void ThrowSyntaxError(Realm realm, string message, Location location)
  21. {
  22. throw new JavaScriptException(realm.Intrinsics.SyntaxError, message).SetJavaScriptLocation(location);
  23. }
  24. [DoesNotReturn]
  25. public static void ThrowArgumentException(string? message = null)
  26. {
  27. ThrowArgumentException(message, null);
  28. }
  29. [DoesNotReturn]
  30. public static void ThrowArgumentException(string? message, string? paramName)
  31. {
  32. throw new ArgumentException(message, paramName);
  33. }
  34. [DoesNotReturn]
  35. public static void ThrowReferenceError(Realm realm, Reference reference)
  36. {
  37. ThrowReferenceNameError(realm, reference?.GetReferencedName()?.ToString());
  38. }
  39. [DoesNotReturn]
  40. public static void ThrowReferenceNameError(Realm realm, string? name)
  41. {
  42. var message = name != null ? name + " is not defined" : null;
  43. ThrowReferenceError(realm, message);
  44. }
  45. [DoesNotReturn]
  46. public static void ThrowReferenceError(Realm realm, string? message)
  47. {
  48. throw new JavaScriptException(realm.Intrinsics.ReferenceError, message);
  49. }
  50. [DoesNotReturn]
  51. public static void ThrowTypeErrorNoEngine(string? message = null)
  52. {
  53. throw new TypeErrorException(message);
  54. }
  55. [DoesNotReturn]
  56. public static void ThrowTypeError(Realm realm, string? message = null)
  57. {
  58. throw new JavaScriptException(realm.Intrinsics.TypeError, message);
  59. }
  60. [DoesNotReturn]
  61. public static void ThrowRangeError(Realm realm, string? message = null)
  62. {
  63. throw new JavaScriptException(realm.Intrinsics.RangeError, message);
  64. }
  65. [DoesNotReturn]
  66. public static void ThrowUriError(Realm realm)
  67. {
  68. throw new JavaScriptException(realm.Intrinsics.UriError);
  69. }
  70. [DoesNotReturn]
  71. public static void ThrowNotImplementedException(string? message = null)
  72. {
  73. throw new NotImplementedException(message);
  74. }
  75. [DoesNotReturn]
  76. public static void ThrowArgumentOutOfRangeException(string paramName, string message)
  77. {
  78. throw new ArgumentOutOfRangeException(paramName, message);
  79. }
  80. [DoesNotReturn]
  81. public static void ThrowTimeoutException()
  82. {
  83. throw new TimeoutException();
  84. }
  85. [DoesNotReturn]
  86. public static void ThrowStatementsCountOverflowException()
  87. {
  88. throw new StatementsCountOverflowException();
  89. }
  90. [DoesNotReturn]
  91. public static void ThrowArgumentOutOfRangeException()
  92. {
  93. throw new ArgumentOutOfRangeException();
  94. }
  95. [DoesNotReturn]
  96. public static void ThrowNotSupportedException(string? message = null)
  97. {
  98. throw new NotSupportedException(message);
  99. }
  100. [DoesNotReturn]
  101. public static void ThrowInvalidOperationException(string? message = null, Exception? exception = null)
  102. {
  103. throw new InvalidOperationException(message, exception);
  104. }
  105. [DoesNotReturn]
  106. public static void ThrowPromiseRejectedException(JsValue error)
  107. {
  108. throw new PromiseRejectedException(error);
  109. }
  110. [DoesNotReturn]
  111. public static void ThrowJavaScriptException(JsValue value)
  112. {
  113. throw new JavaScriptException(value);
  114. }
  115. [DoesNotReturn]
  116. public static void ThrowJavaScriptException(Engine engine, JsValue value, in Completion result)
  117. {
  118. throw new JavaScriptException(value).SetJavaScriptCallstack(engine, result.Location);
  119. }
  120. [DoesNotReturn]
  121. public static void ThrowJavaScriptException(ErrorConstructor errorConstructor, string message)
  122. {
  123. throw new JavaScriptException(errorConstructor, message);
  124. }
  125. [DoesNotReturn]
  126. public static void ThrowJavaScriptException(ErrorConstructor errorConstructor, string message, Engine engine, Location location)
  127. {
  128. throw new JavaScriptException(errorConstructor, message).SetJavaScriptCallstack(engine, location);
  129. }
  130. [DoesNotReturn]
  131. public static void ThrowRecursionDepthOverflowException(JintCallStack currentStack)
  132. {
  133. var currentExpressionReference = currentStack.Pop().ToString();
  134. throw new RecursionDepthOverflowException(currentStack, currentExpressionReference);
  135. }
  136. [DoesNotReturn]
  137. public static void ThrowArgumentNullException(string paramName)
  138. {
  139. throw new ArgumentNullException(paramName);
  140. }
  141. [DoesNotReturn]
  142. public static void ThrowMeaningfulException(Engine engine, TargetInvocationException exception)
  143. {
  144. var meaningfulException = exception.InnerException ?? exception;
  145. if (engine.Options.Interop.ExceptionHandler(meaningfulException))
  146. {
  147. ThrowError(engine, meaningfulException.Message);
  148. }
  149. ExceptionDispatchInfo.Capture(meaningfulException).Throw();
  150. #pragma warning disable CS8763
  151. }
  152. #pragma warning restore CS8763
  153. [DoesNotReturn]
  154. internal static void ThrowError(Engine engine, string message)
  155. {
  156. throw new JavaScriptException(engine.Realm.Intrinsics.Error, message);
  157. }
  158. [DoesNotReturn]
  159. public static void ThrowPlatformNotSupportedException(string message)
  160. {
  161. throw new PlatformNotSupportedException(message);
  162. }
  163. [DoesNotReturn]
  164. public static void ThrowMemoryLimitExceededException(string message)
  165. {
  166. throw new MemoryLimitExceededException(message);
  167. }
  168. [DoesNotReturn]
  169. public static void ThrowExecutionCanceledException()
  170. {
  171. throw new ExecutionCanceledException();
  172. }
  173. [DoesNotReturn]
  174. public static void ThrowModuleResolutionException(string resolverAlgorithmError, string specifier, string? parent)
  175. {
  176. throw new ModuleResolutionException(resolverAlgorithmError, specifier, parent);
  177. }
  178. }
  179. }