CallStackElement.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 ref readonly SourceLocation Location
  20. {
  21. get
  22. {
  23. ref readonly var expressionLocation = ref (Expression is not null ? ref Expression._expression.LocationRef : ref AstExtensions.DefaultLocation);
  24. if (expressionLocation != default)
  25. {
  26. return ref expressionLocation;
  27. }
  28. var function = (Node?) Function._functionDefinition?.Function;
  29. return ref (function is not null ? ref function.LocationRef : ref AstExtensions.DefaultLocation);
  30. }
  31. }
  32. public NodeList<Node>? Arguments => Function._functionDefinition?.Function.Params;
  33. public override string ToString()
  34. {
  35. var name = TypeConverter.ToString(Function.Get(CommonProperties.Name));
  36. if (string.IsNullOrWhiteSpace(name))
  37. {
  38. if (Expression is not null)
  39. {
  40. name = JintExpression.ToString(Expression._expression);
  41. }
  42. }
  43. return name ?? "(anonymous)";
  44. }
  45. public bool Equals(CallStackElement other)
  46. {
  47. return Function.Equals(other.Function) && Equals(Expression, other.Expression);
  48. }
  49. public override bool Equals(object? obj)
  50. {
  51. return obj is CallStackElement other && Equals(other);
  52. }
  53. public override int GetHashCode()
  54. {
  55. unchecked
  56. {
  57. return (Function.GetHashCode() * 397) ^ (Expression != null ? Expression.GetHashCode() : 0);
  58. }
  59. }
  60. }
  61. }