ExceptionHelper.cs 7.2 KB

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