Completion.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Runtime.CompilerServices;
  2. using System.Runtime.InteropServices;
  3. using Esprima;
  4. using Esprima.Ast;
  5. using Jint.Native;
  6. namespace Jint.Runtime;
  7. public enum CompletionType : byte
  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. [StructLayout(LayoutKind.Auto)]
  19. public readonly struct Completion
  20. {
  21. private static readonly Node _emptyNode = new Identifier("");
  22. private static readonly Completion _emptyCompletion = new(CompletionType.Normal, null!, _emptyNode);
  23. internal readonly SyntaxElement _source;
  24. public Completion(CompletionType type, JsValue value, SyntaxElement source)
  25. {
  26. Type = type;
  27. Value = value;
  28. _source = source;
  29. }
  30. public readonly CompletionType Type;
  31. public readonly JsValue Value;
  32. public ref readonly Location Location => ref _source.Location;
  33. public static ref readonly Completion Empty() => ref _emptyCompletion;
  34. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  35. public JsValue GetValueOrDefault()
  36. {
  37. return Value ?? JsValue.Undefined;
  38. }
  39. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  40. public bool IsAbrupt()
  41. {
  42. return Type != CompletionType.Normal;
  43. }
  44. /// <summary>
  45. /// https://tc39.es/ecma262/#sec-updateempty
  46. /// </summary>
  47. internal Completion UpdateEmpty(JsValue value)
  48. {
  49. if (Value is not null)
  50. {
  51. return this;
  52. }
  53. return new Completion(Type, value, _source);
  54. }
  55. }