ExceptionHelper.cs 7.3 KB

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