txBooleanExpr.cpp (2064B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 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 /** 7 * Represents a BooleanExpr, a binary expression that 8 * performs a boolean operation between its lvalue and rvalue. 9 **/ 10 11 #include "txExpr.h" 12 #include "txIXPathContext.h" 13 14 /** 15 * Evaluates this Expr based on the given context node and processor state 16 * @param context the context node for evaluation of this Expr 17 * @param ps the ContextState containing the stack information needed 18 * for evaluation 19 * @return the result of the evaluation 20 **/ 21 nsresult BooleanExpr::evaluate(txIEvalContext* aContext, 22 txAExprResult** aResult) { 23 *aResult = nullptr; 24 25 bool lval; 26 nsresult rv = leftExpr->evaluateToBool(aContext, lval); 27 NS_ENSURE_SUCCESS(rv, rv); 28 29 // check for early decision 30 if (op == OR && lval) { 31 aContext->recycler()->getBoolResult(true, aResult); 32 33 return NS_OK; 34 } 35 if (op == AND && !lval) { 36 aContext->recycler()->getBoolResult(false, aResult); 37 38 return NS_OK; 39 } 40 41 bool rval; 42 rv = rightExpr->evaluateToBool(aContext, rval); 43 NS_ENSURE_SUCCESS(rv, rv); 44 45 // just use rval, since we already checked lval 46 aContext->recycler()->getBoolResult(rval, aResult); 47 48 return NS_OK; 49 } //-- evaluate 50 51 TX_IMPL_EXPR_STUBS_2(BooleanExpr, BOOLEAN_RESULT, leftExpr, rightExpr) 52 53 bool BooleanExpr::isSensitiveTo(ContextSensitivity aContext) { 54 return leftExpr->isSensitiveTo(aContext) || 55 rightExpr->isSensitiveTo(aContext); 56 } 57 58 #ifdef TX_TO_STRING 59 void BooleanExpr::toString(nsAString& str) { 60 if (leftExpr) 61 leftExpr->toString(str); 62 else 63 str.AppendLiteral("null"); 64 65 switch (op) { 66 case OR: 67 str.AppendLiteral(" or "); 68 break; 69 default: 70 str.AppendLiteral(" and "); 71 break; 72 } 73 if (rightExpr) 74 rightExpr->toString(str); 75 else 76 str.AppendLiteral("null"); 77 } 78 #endif