Coverage Report

Created: 2018-09-25 14:53

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