Throw.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.Reflection;
  3. using System.Runtime.ExceptionServices;
  4. using Jint.Native;
  5. using Jint.Native.Error;
  6. using Jint.Runtime.CallStack;
  7. using Jint.Runtime.Modules;
  8. namespace Jint.Runtime;
  9. /// <summary>
  10. /// Wraps known runtime type error information.
  11. /// </summary>
  12. internal sealed record ErrorDispatchInfo(ErrorConstructor ErrorConstructor, string? Message = null);
  13. internal static class Throw
  14. {
  15. [DoesNotReturn]
  16. public static void SyntaxError(Realm realm, string? message = null)
  17. {
  18. throw CreateSyntaxError(realm, message);
  19. }
  20. [DoesNotReturn]
  21. public static void SyntaxError(Realm realm, string message, in SourceLocation location)
  22. {
  23. throw CreateSyntaxError(realm, message).SetJavaScriptLocation(location);
  24. }
  25. public static JavaScriptException CreateSyntaxError(Realm realm, string? message)
  26. {
  27. return new JavaScriptException(realm.Intrinsics.SyntaxError, message);
  28. }
  29. [DoesNotReturn]
  30. public static void ArgumentException(string? message = null)
  31. {
  32. ArgumentException(message, paramName: null);
  33. }
  34. [DoesNotReturn]
  35. public static void ArgumentException(string? message, string? paramName)
  36. {
  37. throw new ArgumentException(message, paramName);
  38. }
  39. [DoesNotReturn]
  40. public static void ReferenceError(Realm realm, Reference reference)
  41. {
  42. ReferenceNameError(realm, reference?.ReferencedName?.ToString());
  43. }
  44. [DoesNotReturn]
  45. public static void ReferenceNameError(Realm realm, string? name)
  46. {
  47. var message = name != null ? name + " is not defined" : null;
  48. ReferenceError(realm, message);
  49. }
  50. [DoesNotReturn]
  51. public static void ReferenceError(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 TypeErrorNoEngine(string? message = null, Node? source = null)
  58. {
  59. throw new TypeErrorException(message, source);
  60. }
  61. [DoesNotReturn]
  62. public static void TypeError(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 RangeError(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. public static ErrorDispatchInfo CreateRangeError(Realm realm, string message)
  78. {
  79. return new ErrorDispatchInfo(realm.Intrinsics.RangeError, message);
  80. }
  81. [DoesNotReturn]
  82. public static void NotImplementedException(string? message = null)
  83. {
  84. throw new NotImplementedException(message);
  85. }
  86. [DoesNotReturn]
  87. public static void ArgumentOutOfRangeException(string paramName, string message)
  88. {
  89. throw new ArgumentOutOfRangeException(paramName, message);
  90. }
  91. [DoesNotReturn]
  92. public static void TimeoutException()
  93. {
  94. throw new TimeoutException();
  95. }
  96. [DoesNotReturn]
  97. public static void StatementsCountOverflowException()
  98. {
  99. throw new StatementsCountOverflowException();
  100. }
  101. [DoesNotReturn]
  102. public static void ArgumentOutOfRangeException()
  103. {
  104. #pragma warning disable MA0015
  105. throw new ArgumentOutOfRangeException();
  106. #pragma warning restore MA0015
  107. }
  108. [DoesNotReturn]
  109. public static void NotSupportedException(string? message = null)
  110. {
  111. throw new NotSupportedException(message);
  112. }
  113. [DoesNotReturn]
  114. public static void InvalidOperationException(string? message = null, Exception? exception = null)
  115. {
  116. throw new InvalidOperationException(message, exception);
  117. }
  118. [DoesNotReturn]
  119. public static void PromiseRejectedException(JsValue error)
  120. {
  121. throw new PromiseRejectedException(error);
  122. }
  123. [DoesNotReturn]
  124. public static void JavaScriptException(Engine engine, JsValue value, in Completion result)
  125. {
  126. throw new JavaScriptException(value).SetJavaScriptCallstack(engine, result.Location);
  127. }
  128. [DoesNotReturn]
  129. public static void JavaScriptException(Engine engine, JsValue value, in SourceLocation location)
  130. {
  131. throw new JavaScriptException(value).SetJavaScriptCallstack(engine, location);
  132. }
  133. [DoesNotReturn]
  134. public static void JavaScriptException(ErrorConstructor errorConstructor, string message)
  135. {
  136. throw new JavaScriptException(errorConstructor, message);
  137. }
  138. [DoesNotReturn]
  139. public static void RecursionDepthOverflowException(JintCallStack currentStack)
  140. {
  141. var currentExpressionReference = currentStack.Pop().ToString();
  142. throw new RecursionDepthOverflowException(currentStack, currentExpressionReference);
  143. }
  144. [DoesNotReturn]
  145. public static void ArgumentNullException(string paramName)
  146. {
  147. throw new ArgumentNullException(paramName);
  148. }
  149. [DoesNotReturn]
  150. public static void MeaningfulException(Engine engine, TargetInvocationException exception)
  151. {
  152. var meaningfulException = exception.InnerException ?? exception;
  153. if (engine.Options.Interop.ExceptionHandler(meaningfulException))
  154. {
  155. Error(engine, meaningfulException.Message);
  156. }
  157. ExceptionDispatchInfo.Capture(meaningfulException).Throw();
  158. #pragma warning disable CS8763
  159. }
  160. #pragma warning restore CS8763
  161. [DoesNotReturn]
  162. internal static void Error(Engine engine, string message)
  163. {
  164. throw new JavaScriptException(engine.Realm.Intrinsics.Error, message);
  165. }
  166. [DoesNotReturn]
  167. public static void PlatformNotSupportedException(string message)
  168. {
  169. throw new PlatformNotSupportedException(message);
  170. }
  171. [DoesNotReturn]
  172. public static void MemoryLimitExceededException(string message)
  173. {
  174. throw new MemoryLimitExceededException(message);
  175. }
  176. [DoesNotReturn]
  177. public static void ExecutionCanceledException()
  178. {
  179. throw new ExecutionCanceledException();
  180. }
  181. [DoesNotReturn]
  182. public static void ModuleResolutionException(string message, string specifier, string? parent, string? filePath = null)
  183. {
  184. throw new ModuleResolutionException(message, specifier, parent, filePath);
  185. }
  186. [DoesNotReturn]
  187. public static void InvalidPreparedScriptArgumentException(string paramName)
  188. {
  189. throw new ArgumentException($"Instances of {typeof(Prepared<Script>)} returned by {nameof(Engine.PrepareScript)} are allowed only.", paramName);
  190. }
  191. [DoesNotReturn]
  192. public static void InvalidPreparedModuleArgumentException(string paramName)
  193. {
  194. throw new ArgumentException($"Instances of {typeof(Prepared<AstModule>)} returned by {nameof(Engine.PrepareModule)} are allowed only.", paramName);
  195. }
  196. }