JavaScriptException.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #nullable enable
  2. using System;
  3. using Esprima;
  4. using Jint.Native;
  5. using Jint.Native.Error;
  6. using Jint.Native.Object;
  7. using Jint.Pooling;
  8. namespace Jint.Runtime;
  9. public class JavaScriptException : JintException
  10. {
  11. private static string GetMessage(JsValue error)
  12. {
  13. string? ret;
  14. if (error is ObjectInstance oi)
  15. {
  16. ret = oi.Get(CommonProperties.Message).ToString();
  17. }
  18. else
  19. {
  20. ret = null;
  21. }
  22. return ret ?? TypeConverter.ToString(error);
  23. }
  24. private readonly JavaScriptErrorWrapperException _jsErrorException;
  25. public string? JavaScriptStackTrace => _jsErrorException.StackTrace;
  26. public Location Location => _jsErrorException.Location;
  27. public JsValue Error => _jsErrorException.Error;
  28. internal JavaScriptException(ErrorConstructor errorConstructor)
  29. : base("", new JavaScriptErrorWrapperException(errorConstructor.Construct(Arguments.Empty), ""))
  30. {
  31. _jsErrorException = (JavaScriptErrorWrapperException) InnerException!;
  32. }
  33. internal JavaScriptException(ErrorConstructor errorConstructor, string? message)
  34. : base(message, new JavaScriptErrorWrapperException(errorConstructor.Construct(new JsValue[] { message }), message))
  35. {
  36. _jsErrorException = (JavaScriptErrorWrapperException) InnerException!;
  37. }
  38. internal JavaScriptException(JsValue error)
  39. : base(GetMessage(error), new JavaScriptErrorWrapperException(error, GetMessage(error)))
  40. {
  41. _jsErrorException = (JavaScriptErrorWrapperException) InnerException!;
  42. }
  43. public string GetJavaScriptErrorString() => _jsErrorException.ToString();
  44. public JavaScriptException SetJavaScriptCallstack(Engine engine, Location location)
  45. {
  46. _jsErrorException.SetCallstack(engine, location);
  47. return this;
  48. }
  49. public JavaScriptException SetJavaScriptLocation(Location location)
  50. {
  51. _jsErrorException.SetLocation(location);
  52. return this;
  53. }
  54. private class JavaScriptErrorWrapperException : JintException
  55. {
  56. private string? _callStack;
  57. public JsValue Error { get; }
  58. public Location Location { get; private set; }
  59. internal JavaScriptErrorWrapperException(JsValue error, string? message = null)
  60. : base(message ?? GetMessage(error))
  61. {
  62. Error = error;
  63. }
  64. internal void SetLocation(Location location)
  65. {
  66. Location = location;
  67. }
  68. internal void SetCallstack(Engine engine, Location location)
  69. {
  70. Location = location;
  71. var value = engine.CallStack.BuildCallStackString(location);
  72. _callStack = value;
  73. if (Error.IsObject())
  74. {
  75. Error.AsObject()
  76. .FastAddProperty(CommonProperties.Stack, new JsString(value), false, false, false);
  77. }
  78. }
  79. /// <summary>
  80. /// Returns the call stack of the JavaScript exception.
  81. /// </summary>
  82. public override string? StackTrace
  83. {
  84. get
  85. {
  86. if (_callStack is not null)
  87. {
  88. return _callStack;
  89. }
  90. if (Error is not ObjectInstance oi)
  91. {
  92. return null;
  93. }
  94. var callstack = oi.Get(CommonProperties.Stack, Error);
  95. return callstack.IsUndefined()
  96. ? null
  97. : callstack.AsString();
  98. }
  99. }
  100. public override string ToString()
  101. {
  102. using var rent = StringBuilderPool.Rent();
  103. var sb = rent.Builder;
  104. sb.Append("Error");
  105. var message = Message;
  106. if (!string.IsNullOrEmpty(message))
  107. {
  108. sb.Append(": ");
  109. sb.Append(message);
  110. }
  111. var stackTrace = StackTrace;
  112. if (stackTrace != null)
  113. {
  114. sb.Append(Environment.NewLine);
  115. sb.Append(stackTrace);
  116. }
  117. return rent.ToString();
  118. }
  119. }
  120. }