ExceptionHelper.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Reflection;
  4. using System.Runtime.ExceptionServices;
  5. using Esprima;
  6. using Jint.Native;
  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).SetLocation(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. ThrowReferenceError(realm, reference?.GetReferencedName()?.ToString());
  38. }
  39. [DoesNotReturn]
  40. public static void ThrowReferenceError(Realm realm, string name)
  41. {
  42. var message = name != null ? name + " is not defined" : null;
  43. throw new JavaScriptException(realm.Intrinsics.ReferenceError, message);
  44. }
  45. [DoesNotReturn]
  46. public static void ThrowTypeErrorNoEngine(string message = null, Exception exception = null)
  47. {
  48. throw new TypeErrorException(message);
  49. }
  50. [DoesNotReturn]
  51. public static void ThrowTypeError(Realm realm, string message = null, Exception exception = null)
  52. {
  53. throw new JavaScriptException(realm.Intrinsics.TypeError, message, exception);
  54. }
  55. [DoesNotReturn]
  56. public static void ThrowRangeError(Realm realm, string message = null)
  57. {
  58. throw new JavaScriptException(realm.Intrinsics.RangeError, message);
  59. }
  60. [DoesNotReturn]
  61. public static void ThrowUriError(Realm realm)
  62. {
  63. throw new JavaScriptException(realm.Intrinsics.UriError);
  64. }
  65. [DoesNotReturn]
  66. public static void ThrowNotImplementedException(string message = null)
  67. {
  68. throw new NotImplementedException(message);
  69. }
  70. [DoesNotReturn]
  71. public static void ThrowArgumentOutOfRangeException(string paramName, string message)
  72. {
  73. throw new ArgumentOutOfRangeException(paramName, message);
  74. }
  75. [DoesNotReturn]
  76. public static void ThrowTimeoutException()
  77. {
  78. throw new TimeoutException();
  79. }
  80. [DoesNotReturn]
  81. public static void ThrowStatementsCountOverflowException()
  82. {
  83. throw new StatementsCountOverflowException();
  84. }
  85. [DoesNotReturn]
  86. public static void ThrowArgumentOutOfRangeException()
  87. {
  88. throw new ArgumentOutOfRangeException();
  89. }
  90. [DoesNotReturn]
  91. public static void ThrowNotSupportedException(string message = null)
  92. {
  93. throw new NotSupportedException(message);
  94. }
  95. [DoesNotReturn]
  96. public static void ThrowInvalidOperationException(string message = null)
  97. {
  98. throw new InvalidOperationException(message);
  99. }
  100. [DoesNotReturn]
  101. public static void ThrowPromiseRejectedException(JsValue error)
  102. {
  103. throw new PromiseRejectedException(error);
  104. }
  105. [DoesNotReturn]
  106. public static void ThrowJavaScriptException(Engine engine, JsValue value, in Completion result)
  107. {
  108. throw new JavaScriptException(value).SetCallstack(engine, result.Location);
  109. }
  110. [DoesNotReturn]
  111. public static void ThrowRecursionDepthOverflowException(JintCallStack currentStack,
  112. string currentExpressionReference)
  113. {
  114. throw new RecursionDepthOverflowException(currentStack, currentExpressionReference);
  115. }
  116. [DoesNotReturn]
  117. public static void ThrowArgumentNullException(string paramName)
  118. {
  119. throw new ArgumentNullException(paramName);
  120. }
  121. [DoesNotReturn]
  122. public static void ThrowMeaningfulException(Engine engine, TargetInvocationException exception)
  123. {
  124. var meaningfulException = exception.InnerException ?? exception;
  125. if (engine.Options.Interop.ExceptionHandler(meaningfulException))
  126. {
  127. ThrowError(engine, meaningfulException.Message);
  128. }
  129. ExceptionDispatchInfo.Capture(meaningfulException).Throw();
  130. }
  131. [DoesNotReturn]
  132. internal static void ThrowError(Engine engine, string message)
  133. {
  134. throw new JavaScriptException(engine.Realm.Intrinsics.Error, message);
  135. }
  136. [DoesNotReturn]
  137. public static void ThrowPlatformNotSupportedException(string message)
  138. {
  139. throw new PlatformNotSupportedException(message);
  140. }
  141. [DoesNotReturn]
  142. public static void ThrowMemoryLimitExceededException(string message)
  143. {
  144. throw new MemoryLimitExceededException(message);
  145. }
  146. [DoesNotReturn]
  147. public static void ThrowExecutionCanceledException()
  148. {
  149. throw new ExecutionCanceledException();
  150. }
  151. [DoesNotReturn]
  152. public static void ThrowModuleResolutionException(string resolverAlgorithmError, string specifier, string parent)
  153. {
  154. throw new ModuleResolutionException(resolverAlgorithmError, specifier, parent);
  155. }
  156. }
  157. }