CallStackElement.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using Jint.Native.Function;
  2. using Jint.Runtime.Interpreter.Expressions;
  3. namespace Jint.Runtime.CallStack
  4. {
  5. internal readonly struct CallStackElement : IEquatable<CallStackElement>
  6. {
  7. public CallStackElement(
  8. Function function,
  9. JintExpression? expression,
  10. in CallStackExecutionContext callingExecutionContext)
  11. {
  12. Function = function;
  13. Expression = expression;
  14. CallingExecutionContext = callingExecutionContext;
  15. }
  16. public readonly Function Function;
  17. public readonly JintExpression? Expression;
  18. public readonly CallStackExecutionContext CallingExecutionContext;
  19. public SourceLocation Location
  20. {
  21. get
  22. {
  23. var expressionLocation = Expression?._expression.Location;
  24. if (expressionLocation != null && expressionLocation.Value != default)
  25. {
  26. return expressionLocation.Value;
  27. }
  28. return ((Node?) Function._functionDefinition?.Function)?.Location ?? default;
  29. }
  30. }
  31. public NodeList<Node>? Arguments => Function._functionDefinition?.Function.Params;
  32. public override string ToString()
  33. {
  34. var name = TypeConverter.ToString(Function.Get(CommonProperties.Name));
  35. if (string.IsNullOrWhiteSpace(name))
  36. {
  37. if (Expression is not null)
  38. {
  39. name = JintExpression.ToString(Expression._expression);
  40. }
  41. }
  42. return name ?? "(anonymous)";
  43. }
  44. public bool Equals(CallStackElement other)
  45. {
  46. return Function.Equals(other.Function) && Equals(Expression, other.Expression);
  47. }
  48. public override bool Equals(object? obj)
  49. {
  50. return obj is CallStackElement other && Equals(other);
  51. }
  52. public override int GetHashCode()
  53. {
  54. unchecked
  55. {
  56. return (Function.GetHashCode() * 397) ^ (Expression != null ? Expression.GetHashCode() : 0);
  57. }
  58. }
  59. }
  60. }