Completion.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Diagnostics;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. using Jint.Native;
  5. namespace Jint.Runtime;
  6. public enum CompletionType : byte
  7. {
  8. Normal = 0,
  9. Return = 1,
  10. Throw = 2,
  11. Break,
  12. Continue
  13. }
  14. /// <summary>
  15. /// https://tc39.es/ecma262/#sec-completion-record-specification-type
  16. /// </summary>
  17. [StructLayout(LayoutKind.Auto)]
  18. public readonly struct Completion
  19. {
  20. private static readonly Node _emptyNode = new Identifier("");
  21. private static readonly Completion _emptyCompletion = new(CompletionType.Normal, JsEmpty.Instance, _emptyNode);
  22. internal readonly Node _source;
  23. public Completion(CompletionType type, JsValue value, Node source)
  24. {
  25. Debug.Assert(value is not null);
  26. Type = type;
  27. Value = value!;
  28. _source = source;
  29. }
  30. public readonly CompletionType Type;
  31. public readonly JsValue Value;
  32. public readonly ref readonly SourceLocation Location => ref _source.LocationRef;
  33. public static ref readonly Completion Empty() => ref _emptyCompletion;
  34. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  35. public JsValue GetValueOrDefault() => Value.IsEmpty ? JsValue.Undefined : Value;
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public bool IsAbrupt() => Type != CompletionType.Normal;
  38. /// <summary>
  39. /// https://tc39.es/ecma262/#sec-updateempty
  40. /// </summary>
  41. internal Completion UpdateEmpty(JsValue value)
  42. {
  43. if (Value?._type != InternalTypes.Empty)
  44. {
  45. return this;
  46. }
  47. return new Completion(Type, value, _source);
  48. }
  49. }