dom/xslt/xpath/txBooleanExpr.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/xslt/xpath/txBooleanExpr.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,81 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +/**
    1.11 + * Represents a BooleanExpr, a binary expression that
    1.12 + * performs a boolean operation between its lvalue and rvalue.
    1.13 +**/
    1.14 +
    1.15 +#include "txExpr.h"
    1.16 +#include "txIXPathContext.h"
    1.17 +
    1.18 +/**
    1.19 + * Evaluates this Expr based on the given context node and processor state
    1.20 + * @param context the context node for evaluation of this Expr
    1.21 + * @param ps the ContextState containing the stack information needed
    1.22 + * for evaluation
    1.23 + * @return the result of the evaluation
    1.24 +**/
    1.25 +nsresult
    1.26 +BooleanExpr::evaluate(txIEvalContext* aContext, txAExprResult** aResult)
    1.27 +{
    1.28 +    *aResult = nullptr;
    1.29 +
    1.30 +    bool lval;
    1.31 +    nsresult rv = leftExpr->evaluateToBool(aContext, lval);
    1.32 +    NS_ENSURE_SUCCESS(rv, rv);
    1.33 +    
    1.34 +    // check for early decision
    1.35 +    if (op == OR && lval) {
    1.36 +        aContext->recycler()->getBoolResult(true, aResult);
    1.37 +        
    1.38 +        return NS_OK;
    1.39 +    }
    1.40 +    if (op == AND && !lval) {
    1.41 +        aContext->recycler()->getBoolResult(false, aResult);
    1.42 +
    1.43 +        return NS_OK;
    1.44 +    }
    1.45 +
    1.46 +    bool rval;
    1.47 +    rv = rightExpr->evaluateToBool(aContext, rval);
    1.48 +    NS_ENSURE_SUCCESS(rv, rv);
    1.49 +
    1.50 +    // just use rval, since we already checked lval
    1.51 +    aContext->recycler()->getBoolResult(rval, aResult);
    1.52 +
    1.53 +    return NS_OK;
    1.54 +} //-- evaluate
    1.55 +
    1.56 +TX_IMPL_EXPR_STUBS_2(BooleanExpr, BOOLEAN_RESULT, leftExpr, rightExpr)
    1.57 +
    1.58 +bool
    1.59 +BooleanExpr::isSensitiveTo(ContextSensitivity aContext)
    1.60 +{
    1.61 +    return leftExpr->isSensitiveTo(aContext) ||
    1.62 +           rightExpr->isSensitiveTo(aContext);
    1.63 +}
    1.64 +
    1.65 +#ifdef TX_TO_STRING
    1.66 +void
    1.67 +BooleanExpr::toString(nsAString& str)
    1.68 +{
    1.69 +    if ( leftExpr ) leftExpr->toString(str);
    1.70 +    else str.AppendLiteral("null");
    1.71 +
    1.72 +    switch ( op ) {
    1.73 +        case OR:
    1.74 +            str.AppendLiteral(" or ");
    1.75 +            break;
    1.76 +        default:
    1.77 +            str.AppendLiteral(" and ");
    1.78 +            break;
    1.79 +    }
    1.80 +    if ( rightExpr ) rightExpr->toString(str);
    1.81 +    else str.AppendLiteral("null");
    1.82 +
    1.83 +}
    1.84 +#endif

mercurial