2
0

JavaScriptException.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Jint.Native;
  6. using Jint.Native.Error;
  7. using Jint.Parser;
  8. using Jint.Parser.Ast;
  9. using Jint.Runtime.CallStack;
  10. using Jint.Runtime.Descriptors;
  11. namespace Jint.Runtime
  12. {
  13. public class JavaScriptException : Exception
  14. {
  15. private readonly JsValue _errorObject;
  16. private string _callStack;
  17. public JavaScriptException(ErrorConstructor errorConstructor) : base("")
  18. {
  19. _errorObject = errorConstructor.Construct(Arguments.Empty);
  20. }
  21. public JavaScriptException(ErrorConstructor errorConstructor, string message)
  22. : base(message)
  23. {
  24. _errorObject = errorConstructor.Construct(new JsValue[] { message });
  25. }
  26. public JavaScriptException(JsValue error)
  27. : base(GetErrorMessage(error))
  28. {
  29. _errorObject = error;
  30. }
  31. public JavaScriptException SetCallstack(Engine engine, Location location = null)
  32. {
  33. Location = location;
  34. var sb = new StringBuilder();
  35. foreach (var cse in engine.CallStack)
  36. {
  37. sb.Append(" at ")
  38. .Append(cse)
  39. .Append("(");
  40. for (var index = 0; index < cse.CallExpression.Arguments.Count; index++)
  41. {
  42. if (index != 0)
  43. sb.Append(", ");
  44. var arg = cse.CallExpression.Arguments[index];
  45. if (arg is IPropertyKeyExpression pke)
  46. sb.Append(pke.GetKey());
  47. else
  48. sb.Append(arg);
  49. }
  50. sb.Append(") @ ")
  51. .Append(cse.CallExpression.Location.Source)
  52. .Append(" ")
  53. .Append(cse.CallExpression.Location.Start.Column)
  54. .Append(":")
  55. .Append(cse.CallExpression.Location.Start.Line)
  56. .AppendLine();
  57. }
  58. CallStack = sb.ToString();
  59. return this;
  60. }
  61. private static string GetErrorMessage(JsValue error)
  62. {
  63. if (error.IsObject())
  64. {
  65. var oi = error.AsObject();
  66. var message = oi.Get("message").AsString();
  67. return message;
  68. }
  69. if (error.IsString())
  70. return error.AsString();
  71. return error.ToString();
  72. }
  73. public JsValue Error { get { return _errorObject; } }
  74. public override string ToString()
  75. {
  76. return _errorObject.ToString();
  77. }
  78. public string CallStack
  79. {
  80. get
  81. {
  82. if (_callStack != null)
  83. return _callStack;
  84. if (_errorObject == null)
  85. return null;
  86. if (_errorObject.IsObject() == false)
  87. return null;
  88. var callstack = _errorObject.AsObject().Get("callstack");
  89. if (callstack == JsValue.Undefined)
  90. return null;
  91. return callstack.AsString();
  92. }
  93. set
  94. {
  95. _callStack = value;
  96. if (value != null && _errorObject.IsObject())
  97. {
  98. _errorObject.AsObject()
  99. .FastAddProperty("callstack", new JsValue(value), false, false, false);
  100. }
  101. }
  102. }
  103. public Jint.Parser.Location Location { get; set; }
  104. public int LineNumber { get { return null == Location ? 0 : Location.Start.Line; } }
  105. public int Column { get { return null == Location ? 0 : Location.Start.Column; } }
  106. }
  107. }