JavaScriptException.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #nullable enable
  2. using Esprima;
  3. using Jint.Native;
  4. using Jint.Native.Error;
  5. using Jint.Native.Object;
  6. using Jint.Pooling;
  7. namespace Jint.Runtime;
  8. public class JavaScriptException : JintException
  9. {
  10. private static string GetMessage(JsValue error)
  11. {
  12. string? ret;
  13. if (error is ObjectInstance oi)
  14. {
  15. ret = oi.Get(CommonProperties.Message).ToString();
  16. }
  17. else
  18. {
  19. ret = null;
  20. }
  21. return ret ?? TypeConverter.ToString(error);
  22. }
  23. private readonly JavaScriptErrorWrapperException _jsErrorException;
  24. public string? JavaScriptStackTrace => _jsErrorException.StackTrace;
  25. public Location Location => _jsErrorException.Location;
  26. public JsValue Error => _jsErrorException.Error;
  27. internal JavaScriptException(ErrorConstructor errorConstructor)
  28. : base("", new JavaScriptErrorWrapperException(errorConstructor.Construct(Arguments.Empty), ""))
  29. {
  30. _jsErrorException = (JavaScriptErrorWrapperException) InnerException!;
  31. }
  32. internal JavaScriptException(ErrorConstructor errorConstructor, string? message)
  33. : base(message, new JavaScriptErrorWrapperException(errorConstructor.Construct(new JsValue[] { message }), message))
  34. {
  35. _jsErrorException = (JavaScriptErrorWrapperException) InnerException!;
  36. }
  37. internal 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, Location location, bool overwriteExisting = false)
  44. {
  45. _jsErrorException.SetCallstack(engine, location, overwriteExisting);
  46. return this;
  47. }
  48. public JavaScriptException SetJavaScriptLocation(Location location)
  49. {
  50. _jsErrorException.SetLocation(location);
  51. return this;
  52. }
  53. private class JavaScriptErrorWrapperException : JintException
  54. {
  55. private string? _callStack;
  56. public JsValue Error { get; }
  57. public Location Location { get; private set; }
  58. internal JavaScriptErrorWrapperException(JsValue error, string? message = null)
  59. : base(message ?? GetMessage(error))
  60. {
  61. Error = error;
  62. }
  63. internal void SetLocation(Location location)
  64. {
  65. Location = location;
  66. }
  67. internal void SetCallstack(Engine engine, Location location, bool overwriteExisting)
  68. {
  69. Location = location;
  70. var errObj = Error.IsObject() ? Error.AsObject() : null;
  71. if (errObj == null)
  72. {
  73. _callStack = engine.CallStack.BuildCallStackString(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(location);
  84. errObj.FastAddProperty(CommonProperties.Stack, _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. using var rent = StringBuilderPool.Rent();
  111. var sb = rent.Builder;
  112. sb.Append("Error");
  113. var message = Message;
  114. if (!string.IsNullOrEmpty(message))
  115. {
  116. sb.Append(": ");
  117. sb.Append(message);
  118. }
  119. var stackTrace = StackTrace;
  120. if (stackTrace != null)
  121. {
  122. sb.Append(Environment.NewLine);
  123. sb.Append(stackTrace);
  124. }
  125. return rent.ToString();
  126. }
  127. }
  128. }