JintLogicalAndExpression.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using Esprima.Ast;
  2. using Jint.Native;
  3. namespace Jint.Runtime.Interpreter.Expressions
  4. {
  5. internal sealed class JintLogicalAndExpression : JintExpression
  6. {
  7. private JintExpression _left = null!;
  8. private JintExpression _right = null!;
  9. private bool _initialized;
  10. public JintLogicalAndExpression(BinaryExpression expression) : base(expression)
  11. {
  12. }
  13. private void Initialize()
  14. {
  15. var expression = (BinaryExpression) _expression;
  16. _left = Build(expression.Left);
  17. _right = Build(expression.Right);
  18. }
  19. protected override object EvaluateInternal(EvaluationContext context)
  20. {
  21. if (!_initialized)
  22. {
  23. Initialize();
  24. _initialized = true;
  25. }
  26. var left = _left.GetValue(context);
  27. if (left is JsBoolean b && !b._value)
  28. {
  29. return b;
  30. }
  31. if (!TypeConverter.ToBoolean(left))
  32. {
  33. return left;
  34. }
  35. return _right.GetValue(context);
  36. }
  37. }
  38. }