JavaScriptException.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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, bool overwriteExisting = false)
  45. {
  46. _jsErrorException.SetCallstack(engine, location, overwriteExisting);
  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, bool overwriteExisting)
  69. {
  70. Location = location;
  71. var errObj = Error.IsObject() ? Error.AsObject() : null;
  72. if (errObj == 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.FastAddProperty(CommonProperties.Stack, _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. }