tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

txFunctionCall.cpp (3174B)


      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 #include "nsAtom.h"
      7 #include "txExpr.h"
      8 #include "txIXPathContext.h"
      9 #include "txNodeSet.h"
     10 
     11 #ifdef TX_TO_STRING
     12 #  include "nsReadableUtils.h"
     13 #endif
     14 
     15 /**
     16 * This class represents a FunctionCall as defined by the XSL Working Draft
     17 **/
     18 
     19 //------------------/
     20 //- Public Methods -/
     21 //------------------/
     22 
     23 /*
     24 * Evaluates the given Expression and converts its result to a number.
     25 */
     26 // static
     27 nsresult FunctionCall::evaluateToNumber(Expr* aExpr, txIEvalContext* aContext,
     28                                        double* aResult) {
     29  NS_ASSERTION(aExpr, "missing expression");
     30  RefPtr<txAExprResult> exprResult;
     31  nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprResult));
     32  NS_ENSURE_SUCCESS(rv, rv);
     33 
     34  *aResult = exprResult->numberValue();
     35 
     36  return NS_OK;
     37 }
     38 
     39 /*
     40 * Evaluates the given Expression and converts its result to a NodeSet.
     41 * If the result is not a NodeSet nullptr is returned.
     42 */
     43 nsresult FunctionCall::evaluateToNodeSet(Expr* aExpr, txIEvalContext* aContext,
     44                                         txNodeSet** aResult) {
     45  NS_ASSERTION(aExpr, "Missing expression to evaluate");
     46  *aResult = nullptr;
     47 
     48  RefPtr<txAExprResult> exprRes;
     49  nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprRes));
     50  NS_ENSURE_SUCCESS(rv, rv);
     51 
     52  if (exprRes->getResultType() != txAExprResult::NODESET) {
     53    aContext->receiveError(u"NodeSet expected as argument"_ns,
     54                           NS_ERROR_XSLT_NODESET_EXPECTED);
     55    return NS_ERROR_XSLT_NODESET_EXPECTED;
     56  }
     57 
     58  *aResult = static_cast<txNodeSet*>(static_cast<txAExprResult*>(exprRes));
     59  NS_ADDREF(*aResult);
     60 
     61  return NS_OK;
     62 }
     63 
     64 bool FunctionCall::requireParams(int32_t aParamCountMin, int32_t aParamCountMax,
     65                                 txIEvalContext* aContext) {
     66  int32_t argc = mParams.Length();
     67  if (argc < aParamCountMin || (aParamCountMax > -1 && argc > aParamCountMax)) {
     68    nsAutoString err(u"invalid number of parameters for function"_ns);
     69 #ifdef TX_TO_STRING
     70    err.AppendLiteral(": ");
     71    toString(err);
     72 #endif
     73    aContext->receiveError(err, NS_ERROR_XPATH_INVALID_ARG);
     74 
     75    return false;
     76  }
     77 
     78  return true;
     79 }
     80 
     81 Expr* FunctionCall::getSubExprAt(uint32_t aPos) {
     82  return mParams.SafeElementAt(aPos);
     83 }
     84 
     85 void FunctionCall::setSubExprAt(uint32_t aPos, Expr* aExpr) {
     86  NS_ASSERTION(aPos < mParams.Length(), "setting bad subexpression index");
     87  mParams[aPos] = aExpr;
     88 }
     89 
     90 bool FunctionCall::argsSensitiveTo(ContextSensitivity aContext) {
     91  uint32_t i, len = mParams.Length();
     92  for (i = 0; i < len; ++i) {
     93    if (mParams[i]->isSensitiveTo(aContext)) {
     94      return true;
     95    }
     96  }
     97 
     98  return false;
     99 }
    100 
    101 #ifdef TX_TO_STRING
    102 void FunctionCall::toString(nsAString& aDest) {
    103  appendName(aDest);
    104  aDest.AppendLiteral("(");
    105  StringJoinAppend(
    106      aDest, u","_ns, mParams,
    107      [](nsAString& dest, const auto& param) { param->toString(dest); });
    108  aDest.Append(char16_t(')'));
    109 }
    110 #endif