JavaScriptException.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using Esprima;
  3. using Esprima.Ast;
  4. using Jint.Native;
  5. using Jint.Native.Error;
  6. using Jint.Pooling;
  7. namespace Jint.Runtime
  8. {
  9. public class JavaScriptException : JintException
  10. {
  11. private string _callStack;
  12. public JavaScriptException(ErrorConstructor errorConstructor) : base("")
  13. {
  14. Error = errorConstructor.Construct(Arguments.Empty);
  15. }
  16. public JavaScriptException(ErrorConstructor errorConstructor, string message, Exception innerException)
  17. : base(message, innerException)
  18. {
  19. Error = errorConstructor.Construct(new JsValue[] { message });
  20. }
  21. public JavaScriptException(ErrorConstructor errorConstructor, string message)
  22. : base(message)
  23. {
  24. Error = errorConstructor.Construct(new JsValue[] { message });
  25. }
  26. public JavaScriptException(JsValue error)
  27. : base(GetErrorMessage(error))
  28. {
  29. Error = error;
  30. }
  31. public JavaScriptException SetCallstack(Engine engine, Location? location = null)
  32. {
  33. Location = location ?? default;
  34. using (var sb = StringBuilderPool.Rent())
  35. {
  36. foreach (var cse in engine.CallStack)
  37. {
  38. sb.Builder.Append(" at ")
  39. .Append(cse)
  40. .Append("(");
  41. for (var index = 0; index < cse.CallExpression.Arguments.Count; index++)
  42. {
  43. if (index != 0)
  44. {
  45. sb.Builder.Append(", ");
  46. }
  47. var arg = cse.CallExpression.Arguments[index];
  48. if (arg is Expression pke)
  49. {
  50. sb.Builder.Append(GetPropertyKey(pke));
  51. }
  52. else
  53. {
  54. sb.Builder.Append(arg);
  55. }
  56. }
  57. sb.Builder.Append(") @ ")
  58. .Append(cse.CallExpression.Location.Source)
  59. .Append(" ")
  60. .Append(cse.CallExpression.Location.Start.Column)
  61. .Append(":")
  62. .Append(cse.CallExpression.Location.Start.Line)
  63. .AppendLine();
  64. }
  65. CallStack = sb.ToString();
  66. }
  67. return this;
  68. }
  69. /// <summary>
  70. /// A version of <see cref="EsprimaExtensions.GetKey"/> that cannot get into loop as we are already building a stack.
  71. /// </summary>
  72. private static string GetPropertyKey(Expression expression)
  73. {
  74. if (expression is Literal literal)
  75. {
  76. return EsprimaExtensions.LiteralKeyToString(literal);
  77. }
  78. if (expression is Identifier identifier)
  79. {
  80. return identifier.Name;
  81. }
  82. if (expression is StaticMemberExpression staticMemberExpression)
  83. {
  84. return GetPropertyKey(staticMemberExpression.Object) + "." + GetPropertyKey(staticMemberExpression.Property);
  85. }
  86. return "?";
  87. }
  88. private static string GetErrorMessage(JsValue error)
  89. {
  90. if (error.IsObject())
  91. {
  92. var oi = error.AsObject();
  93. var message = oi.Get("message", oi).ToString();
  94. return message;
  95. }
  96. if (error.IsString())
  97. return error.ToString();
  98. return error.ToString();
  99. }
  100. public JsValue Error { get; }
  101. public override string ToString()
  102. {
  103. return Error.ToString();
  104. }
  105. public string CallStack
  106. {
  107. get
  108. {
  109. if (_callStack != null)
  110. return _callStack;
  111. if (ReferenceEquals(Error, null))
  112. return null;
  113. if (Error.IsObject() == false)
  114. return null;
  115. var callstack = Error.AsObject().Get("callstack", Error);
  116. if (callstack.IsUndefined())
  117. return null;
  118. return callstack.AsString();
  119. }
  120. set
  121. {
  122. _callStack = value;
  123. if (value != null && Error.IsObject())
  124. {
  125. Error.AsObject()
  126. .FastAddProperty("callstack", new JsString(value), false, false, false);
  127. }
  128. }
  129. }
  130. public Location Location { get; set; }
  131. public int LineNumber => Location.Start.Line;
  132. public int Column => Location.Start.Column;
  133. }
  134. }