JavaScriptException.cs 4.7 KB

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