ExceptionHelper.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. var location = realm.GlobalObject.Engine.GetLastSyntaxElement()?.Location ?? default;
  49. throw new JavaScriptException(realm.Intrinsics.ReferenceError, message).SetJavaScriptLocation(location);
  50. }
  51. [DoesNotReturn]
  52. public static void ThrowTypeErrorNoEngine(string? message = null)
  53. {
  54. throw new TypeErrorException(message);
  55. }
  56. [DoesNotReturn]
  57. public static void ThrowTypeError(Realm realm, string? message = null)
  58. {
  59. var location = realm.GlobalObject.Engine.GetLastSyntaxElement()?.Location ?? default;
  60. throw new JavaScriptException(realm.Intrinsics.TypeError, message).SetJavaScriptLocation(location);
  61. }
  62. [DoesNotReturn]
  63. public static void ThrowRangeError(Realm realm, string? message = null)
  64. {
  65. var location = realm.GlobalObject.Engine.GetLastSyntaxElement()?.Location ?? default;
  66. throw new JavaScriptException(realm.Intrinsics.RangeError, message).SetJavaScriptLocation(location);
  67. }
  68. [DoesNotReturn]
  69. public static void ThrowUriError(Realm realm)
  70. {
  71. var location = realm.GlobalObject.Engine.GetLastSyntaxElement()?.Location ?? default;
  72. throw new JavaScriptException(realm.Intrinsics.UriError).SetJavaScriptLocation(location);
  73. }
  74. [DoesNotReturn]
  75. public static void ThrowNotImplementedException(string? message = null)
  76. {
  77. throw new NotImplementedException(message);
  78. }
  79. [DoesNotReturn]
  80. public static void ThrowArgumentOutOfRangeException(string paramName, string message)
  81. {
  82. throw new ArgumentOutOfRangeException(paramName, message);
  83. }
  84. [DoesNotReturn]
  85. public static void ThrowTimeoutException()
  86. {
  87. throw new TimeoutException();
  88. }
  89. [DoesNotReturn]
  90. public static void ThrowStatementsCountOverflowException()
  91. {
  92. throw new StatementsCountOverflowException();
  93. }
  94. [DoesNotReturn]
  95. public static void ThrowArgumentOutOfRangeException()
  96. {
  97. throw new ArgumentOutOfRangeException();
  98. }
  99. [DoesNotReturn]
  100. public static void ThrowNotSupportedException(string? message = null)
  101. {
  102. throw new NotSupportedException(message);
  103. }
  104. [DoesNotReturn]
  105. public static void ThrowInvalidOperationException(string? message = null, Exception? exception = null)
  106. {
  107. throw new InvalidOperationException(message, exception);
  108. }
  109. [DoesNotReturn]
  110. public static void ThrowPromiseRejectedException(JsValue error)
  111. {
  112. throw new PromiseRejectedException(error);
  113. }
  114. [DoesNotReturn]
  115. public static void ThrowJavaScriptException(JsValue value)
  116. {
  117. throw new JavaScriptException(value);
  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(ErrorConstructor errorConstructor, string message)
  126. {
  127. throw new JavaScriptException(errorConstructor, message);
  128. }
  129. [DoesNotReturn]
  130. public static void ThrowJavaScriptException(ErrorConstructor errorConstructor, string message, Engine engine, Location location)
  131. {
  132. throw new JavaScriptException(errorConstructor, message).SetJavaScriptCallstack(engine, location);
  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. }