michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: /** michael@0: * Represents a BooleanExpr, a binary expression that michael@0: * performs a boolean operation between its lvalue and rvalue. michael@0: **/ michael@0: michael@0: #include "txExpr.h" michael@0: #include "txIXPathContext.h" michael@0: michael@0: /** michael@0: * Evaluates this Expr based on the given context node and processor state michael@0: * @param context the context node for evaluation of this Expr michael@0: * @param ps the ContextState containing the stack information needed michael@0: * for evaluation michael@0: * @return the result of the evaluation michael@0: **/ michael@0: nsresult michael@0: BooleanExpr::evaluate(txIEvalContext* aContext, txAExprResult** aResult) michael@0: { michael@0: *aResult = nullptr; michael@0: michael@0: bool lval; michael@0: nsresult rv = leftExpr->evaluateToBool(aContext, lval); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // check for early decision michael@0: if (op == OR && lval) { michael@0: aContext->recycler()->getBoolResult(true, aResult); michael@0: michael@0: return NS_OK; michael@0: } michael@0: if (op == AND && !lval) { michael@0: aContext->recycler()->getBoolResult(false, aResult); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: bool rval; michael@0: rv = rightExpr->evaluateToBool(aContext, rval); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // just use rval, since we already checked lval michael@0: aContext->recycler()->getBoolResult(rval, aResult); michael@0: michael@0: return NS_OK; michael@0: } //-- evaluate michael@0: michael@0: TX_IMPL_EXPR_STUBS_2(BooleanExpr, BOOLEAN_RESULT, leftExpr, rightExpr) michael@0: michael@0: bool michael@0: BooleanExpr::isSensitiveTo(ContextSensitivity aContext) michael@0: { michael@0: return leftExpr->isSensitiveTo(aContext) || michael@0: rightExpr->isSensitiveTo(aContext); michael@0: } michael@0: michael@0: #ifdef TX_TO_STRING michael@0: void michael@0: BooleanExpr::toString(nsAString& str) michael@0: { michael@0: if ( leftExpr ) leftExpr->toString(str); michael@0: else str.AppendLiteral("null"); michael@0: michael@0: switch ( op ) { michael@0: case OR: michael@0: str.AppendLiteral(" or "); michael@0: break; michael@0: default: michael@0: str.AppendLiteral(" and "); michael@0: break; michael@0: } michael@0: if ( rightExpr ) rightExpr->toString(str); michael@0: else str.AppendLiteral("null"); michael@0: michael@0: } michael@0: #endif