2
0

JavaScriptException.cs 4.7 KB

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