1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is TransforMiiX XSLT processor code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * The MITRE Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1999
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Keith Visco <kvisco@ziplink.net> (Original Author)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "txExpr.h"
40 : #include <math.h>
41 : #include "txIXPathContext.h"
42 :
43 : nsresult
44 0 : txNumberExpr::evaluate(txIEvalContext* aContext, txAExprResult** aResult)
45 : {
46 0 : *aResult = nsnull;
47 :
48 0 : nsRefPtr<txAExprResult> exprRes;
49 0 : nsresult rv = mRightExpr->evaluate(aContext, getter_AddRefs(exprRes));
50 0 : NS_ENSURE_SUCCESS(rv, rv);
51 :
52 0 : double rightDbl = exprRes->numberValue();
53 :
54 0 : rv = mLeftExpr->evaluate(aContext, getter_AddRefs(exprRes));
55 0 : NS_ENSURE_SUCCESS(rv, rv);
56 :
57 0 : double leftDbl = exprRes->numberValue();
58 0 : double result = 0;
59 :
60 0 : switch (mOp) {
61 : case ADD:
62 0 : result = leftDbl + rightDbl;
63 0 : break;
64 :
65 : case SUBTRACT:
66 0 : result = leftDbl - rightDbl;
67 0 : break;
68 :
69 : case DIVIDE:
70 0 : if (rightDbl == 0) {
71 : #if defined(XP_WIN)
72 : /* XXX MSVC miscompiles such that (NaN == 0) */
73 : if (txDouble::isNaN(rightDbl))
74 : result = txDouble::NaN;
75 : else
76 : #endif
77 0 : if (leftDbl == 0 || txDouble::isNaN(leftDbl))
78 0 : result = txDouble::NaN;
79 0 : else if (txDouble::isNeg(leftDbl) ^ txDouble::isNeg(rightDbl))
80 0 : result = txDouble::NEGATIVE_INFINITY;
81 : else
82 0 : result = txDouble::POSITIVE_INFINITY;
83 : }
84 : else
85 0 : result = leftDbl / rightDbl;
86 0 : break;
87 :
88 : case MODULUS:
89 0 : if (rightDbl == 0) {
90 0 : result = txDouble::NaN;
91 : }
92 : else {
93 : #if defined(XP_WIN)
94 : /* Workaround MS fmod bug where 42 % (1/0) => NaN, not 42. */
95 : if (!txDouble::isInfinite(leftDbl) && txDouble::isInfinite(rightDbl))
96 : result = leftDbl;
97 : else
98 : #endif
99 0 : result = fmod(leftDbl, rightDbl);
100 : }
101 0 : break;
102 :
103 : case MULTIPLY:
104 0 : result = leftDbl * rightDbl;
105 0 : break;
106 : }
107 :
108 0 : return aContext->recycler()->getNumberResult(result, aResult);
109 : } //-- evaluate
110 :
111 0 : TX_IMPL_EXPR_STUBS_2(txNumberExpr, NUMBER_RESULT, mLeftExpr, mRightExpr)
112 :
113 : bool
114 0 : txNumberExpr::isSensitiveTo(ContextSensitivity aContext)
115 : {
116 0 : return mLeftExpr->isSensitiveTo(aContext) ||
117 0 : mRightExpr->isSensitiveTo(aContext);
118 : }
119 :
120 : #ifdef TX_TO_STRING
121 : void
122 0 : txNumberExpr::toString(nsAString& str)
123 : {
124 0 : mLeftExpr->toString(str);
125 :
126 0 : switch (mOp) {
127 : case ADD:
128 0 : str.AppendLiteral(" + ");
129 0 : break;
130 : case SUBTRACT:
131 0 : str.AppendLiteral(" - ");
132 0 : break;
133 : case DIVIDE:
134 0 : str.AppendLiteral(" div ");
135 0 : break;
136 : case MODULUS:
137 0 : str.AppendLiteral(" mod ");
138 0 : break;
139 : case MULTIPLY:
140 0 : str.AppendLiteral(" * ");
141 0 : break;
142 : }
143 :
144 0 : mRightExpr->toString(str);
145 :
146 0 : }
147 : #endif
|