2
0

ExceptionHelper.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #pragma warning disable MA0015
  101. throw new ArgumentOutOfRangeException();
  102. #pragma warning restore MA0015
  103. }
  104. [DoesNotReturn]
  105. public static void ThrowNotSupportedException(string? message = null)
  106. {
  107. throw new NotSupportedException(message);
  108. }
  109. [DoesNotReturn]
  110. public static void ThrowInvalidOperationException(string? message = null, Exception? exception = null)
  111. {
  112. throw new InvalidOperationException(message, exception);
  113. }
  114. [DoesNotReturn]
  115. public static void ThrowPromiseRejectedException(JsValue error)
  116. {
  117. throw new PromiseRejectedException(error);
  118. }
  119. [DoesNotReturn]
  120. public static void ThrowJavaScriptException(Engine engine, JsValue value, in Completion result)
  121. {
  122. throw new JavaScriptException(value).SetJavaScriptCallstack(engine, result.Location);
  123. }
  124. [DoesNotReturn]
  125. public static void ThrowJavaScriptException(Engine engine, JsValue value, in Location location)
  126. {
  127. throw new JavaScriptException(value).SetJavaScriptCallstack(engine, location);
  128. }
  129. [DoesNotReturn]
  130. public static void ThrowJavaScriptException(ErrorConstructor errorConstructor, string message)
  131. {
  132. throw new JavaScriptException(errorConstructor, message);
  133. }
  134. [DoesNotReturn]
  135. public static void ThrowRecursionDepthOverflowException(JintCallStack currentStack)
  136. {
  137. var currentExpressionReference = currentStack.Pop().ToString();
  138. throw new RecursionDepthOverflowException(currentStack, currentExpressionReference);
  139. }
  140. [DoesNotReturn]
  141. public static void ThrowArgumentNullException(string paramName)
  142. {
  143. throw new ArgumentNullException(paramName);
  144. }
  145. [DoesNotReturn]
  146. public static void ThrowMeaningfulException(Engine engine, TargetInvocationException exception)
  147. {
  148. var meaningfulException = exception.InnerException ?? exception;
  149. if (engine.Options.Interop.ExceptionHandler(meaningfulException))
  150. {
  151. ThrowError(engine, meaningfulException.Message);
  152. }
  153. ExceptionDispatchInfo.Capture(meaningfulException).Throw();
  154. #pragma warning disable CS8763
  155. }
  156. #pragma warning restore CS8763
  157. [DoesNotReturn]
  158. internal static void ThrowError(Engine engine, string message)
  159. {
  160. throw new JavaScriptException(engine.Realm.Intrinsics.Error, message);
  161. }
  162. [DoesNotReturn]
  163. public static void ThrowPlatformNotSupportedException(string message)
  164. {
  165. throw new PlatformNotSupportedException(message);
  166. }
  167. [DoesNotReturn]
  168. public static void ThrowMemoryLimitExceededException(string message)
  169. {
  170. throw new MemoryLimitExceededException(message);
  171. }
  172. [DoesNotReturn]
  173. public static void ThrowExecutionCanceledException()
  174. {
  175. throw new ExecutionCanceledException();
  176. }
  177. [DoesNotReturn]
  178. public static void ThrowModuleResolutionException(string resolverAlgorithmError, string specifier, string? parent)
  179. {
  180. throw new ModuleResolutionException(resolverAlgorithmError, specifier, parent);
  181. }
  182. }
  183. }