ExceptionHelper.cs 7.4 KB

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