CallStackElement.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using Jint.Native.Function;
  2. using Jint.Runtime.Interpreter.Expressions;
  3. namespace Jint.Runtime.CallStack;
  4. internal readonly struct CallStackElement : IEquatable<CallStackElement>
  5. {
  6. public CallStackElement(
  7. Function function,
  8. JintExpression? expression,
  9. in CallStackExecutionContext callingExecutionContext)
  10. {
  11. Function = function;
  12. Expression = expression;
  13. CallingExecutionContext = callingExecutionContext;
  14. }
  15. public readonly Function Function;
  16. public readonly JintExpression? Expression;
  17. public readonly CallStackExecutionContext CallingExecutionContext;
  18. public ref readonly SourceLocation Location
  19. {
  20. get
  21. {
  22. ref readonly var expressionLocation = ref (Expression is not null ? ref Expression._expression.LocationRef : ref AstExtensions.DefaultLocation);
  23. if (expressionLocation != default)
  24. {
  25. return ref expressionLocation;
  26. }
  27. var function = (Node?) Function._functionDefinition?.Function;
  28. return ref (function is not null ? ref function.LocationRef : ref AstExtensions.DefaultLocation);
  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. }