Completion.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #nullable enable
  2. using System.Runtime.CompilerServices;
  3. using Esprima;
  4. using Jint.Native;
  5. using Jint.Runtime.Interpreter.Expressions;
  6. namespace Jint.Runtime
  7. {
  8. public enum CompletionType
  9. {
  10. Normal = 0,
  11. Return = 1,
  12. Throw = 2,
  13. Break,
  14. Continue
  15. }
  16. /// <summary>
  17. /// https://tc39.es/ecma262/#sec-completion-record-specification-type
  18. /// </summary>
  19. public readonly struct Completion
  20. {
  21. internal Completion(CompletionType type, JsValue value, string? target, in Location location)
  22. {
  23. Type = type;
  24. Value = value;
  25. Target = target;
  26. Location = location;
  27. }
  28. public Completion(CompletionType type, JsValue value, in Location location)
  29. : this(type, value, null, location)
  30. {
  31. }
  32. public Completion(CompletionType type, string target, in Location location)
  33. : this(type, null!, target, location)
  34. {
  35. }
  36. internal Completion(in ExpressionResult result)
  37. {
  38. Type = (CompletionType) result.Type;
  39. // this cast protects us from getting from type
  40. Value = (JsValue) result.Value;
  41. Target = null;
  42. Location = result.Location;
  43. }
  44. public readonly CompletionType Type;
  45. public readonly JsValue Value;
  46. public readonly string? Target;
  47. public readonly Location Location;
  48. public static Completion Normal(JsValue value, in Location location)
  49. => new Completion(CompletionType.Normal, value, location);
  50. public static Completion Empty()
  51. => new Completion(CompletionType.Normal, null!, default);
  52. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  53. public JsValue GetValueOrDefault()
  54. {
  55. return Value ?? Undefined.Instance;
  56. }
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public bool IsAbrupt()
  59. {
  60. return Type != CompletionType.Normal;
  61. }
  62. /// <summary>
  63. /// https://tc39.es/ecma262/#sec-updateempty
  64. /// </summary>
  65. internal Completion UpdateEmpty(JsValue value)
  66. {
  67. if (Value is not null)
  68. {
  69. return this;
  70. }
  71. return new Completion(Type, value, Target, Location);
  72. }
  73. }
  74. }