Completion.cs 2.3 KB

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