txBooleanExpr.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /**
  6. * Represents a BooleanExpr, a binary expression that
  7. * performs a boolean operation between its lvalue and rvalue.
  8. **/
  9. #include "txExpr.h"
  10. #include "txIXPathContext.h"
  11. /**
  12. * Evaluates this Expr based on the given context node and processor state
  13. * @param context the context node for evaluation of this Expr
  14. * @param ps the ContextState containing the stack information needed
  15. * for evaluation
  16. * @return the result of the evaluation
  17. **/
  18. nsresult
  19. BooleanExpr::evaluate(txIEvalContext* aContext, txAExprResult** aResult)
  20. {
  21. *aResult = nullptr;
  22. bool lval;
  23. nsresult rv = leftExpr->evaluateToBool(aContext, lval);
  24. NS_ENSURE_SUCCESS(rv, rv);
  25. // check for early decision
  26. if (op == OR && lval) {
  27. aContext->recycler()->getBoolResult(true, aResult);
  28. return NS_OK;
  29. }
  30. if (op == AND && !lval) {
  31. aContext->recycler()->getBoolResult(false, aResult);
  32. return NS_OK;
  33. }
  34. bool rval;
  35. rv = rightExpr->evaluateToBool(aContext, rval);
  36. NS_ENSURE_SUCCESS(rv, rv);
  37. // just use rval, since we already checked lval
  38. aContext->recycler()->getBoolResult(rval, aResult);
  39. return NS_OK;
  40. } //-- evaluate
  41. TX_IMPL_EXPR_STUBS_2(BooleanExpr, BOOLEAN_RESULT, leftExpr, rightExpr)
  42. bool
  43. BooleanExpr::isSensitiveTo(ContextSensitivity aContext)
  44. {
  45. return leftExpr->isSensitiveTo(aContext) ||
  46. rightExpr->isSensitiveTo(aContext);
  47. }
  48. #ifdef TX_TO_STRING
  49. void
  50. BooleanExpr::toString(nsAString& str)
  51. {
  52. if ( leftExpr ) leftExpr->toString(str);
  53. else str.AppendLiteral("null");
  54. switch ( op ) {
  55. case OR:
  56. str.AppendLiteral(" or ");
  57. break;
  58. default:
  59. str.AppendLiteral(" and ");
  60. break;
  61. }
  62. if ( rightExpr ) rightExpr->toString(str);
  63. else str.AppendLiteral("null");
  64. }
  65. #endif