1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/xslt/xpath/txNumberExpr.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,116 @@ 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 +#include "mozilla/FloatingPoint.h" 1.10 + 1.11 +#include "txExpr.h" 1.12 +#include <math.h> 1.13 +#include "txIXPathContext.h" 1.14 + 1.15 +nsresult 1.16 +txNumberExpr::evaluate(txIEvalContext* aContext, txAExprResult** aResult) 1.17 +{ 1.18 + *aResult = nullptr; 1.19 + 1.20 + nsRefPtr<txAExprResult> exprRes; 1.21 + nsresult rv = mRightExpr->evaluate(aContext, getter_AddRefs(exprRes)); 1.22 + NS_ENSURE_SUCCESS(rv, rv); 1.23 + 1.24 + double rightDbl = exprRes->numberValue(); 1.25 + 1.26 + rv = mLeftExpr->evaluate(aContext, getter_AddRefs(exprRes)); 1.27 + NS_ENSURE_SUCCESS(rv, rv); 1.28 + 1.29 + double leftDbl = exprRes->numberValue(); 1.30 + double result = 0; 1.31 + 1.32 + switch (mOp) { 1.33 + case ADD: 1.34 + result = leftDbl + rightDbl; 1.35 + break; 1.36 + 1.37 + case SUBTRACT: 1.38 + result = leftDbl - rightDbl; 1.39 + break; 1.40 + 1.41 + case DIVIDE: 1.42 + if (rightDbl == 0) { 1.43 +#if defined(XP_WIN) 1.44 + /* XXX MSVC miscompiles such that (NaN == 0) */ 1.45 + if (mozilla::IsNaN(rightDbl)) 1.46 + result = mozilla::UnspecifiedNaN<double>(); 1.47 + else 1.48 +#endif 1.49 + if (leftDbl == 0 || mozilla::IsNaN(leftDbl)) 1.50 + result = mozilla::UnspecifiedNaN<double>(); 1.51 + else if (mozilla::IsNegative(leftDbl) != mozilla::IsNegative(rightDbl)) 1.52 + result = mozilla::NegativeInfinity<double>(); 1.53 + else 1.54 + result = mozilla::PositiveInfinity<double>(); 1.55 + } 1.56 + else 1.57 + result = leftDbl / rightDbl; 1.58 + break; 1.59 + 1.60 + case MODULUS: 1.61 + if (rightDbl == 0) { 1.62 + result = mozilla::UnspecifiedNaN<double>(); 1.63 + } 1.64 + else { 1.65 +#if defined(XP_WIN) 1.66 + /* Workaround MS fmod bug where 42 % (1/0) => NaN, not 42. */ 1.67 + if (!mozilla::IsInfinite(leftDbl) && mozilla::IsInfinite(rightDbl)) 1.68 + result = leftDbl; 1.69 + else 1.70 +#endif 1.71 + result = fmod(leftDbl, rightDbl); 1.72 + } 1.73 + break; 1.74 + 1.75 + case MULTIPLY: 1.76 + result = leftDbl * rightDbl; 1.77 + break; 1.78 + } 1.79 + 1.80 + return aContext->recycler()->getNumberResult(result, aResult); 1.81 +} //-- evaluate 1.82 + 1.83 +TX_IMPL_EXPR_STUBS_2(txNumberExpr, NUMBER_RESULT, mLeftExpr, mRightExpr) 1.84 + 1.85 +bool 1.86 +txNumberExpr::isSensitiveTo(ContextSensitivity aContext) 1.87 +{ 1.88 + return mLeftExpr->isSensitiveTo(aContext) || 1.89 + mRightExpr->isSensitiveTo(aContext); 1.90 +} 1.91 + 1.92 +#ifdef TX_TO_STRING 1.93 +void 1.94 +txNumberExpr::toString(nsAString& str) 1.95 +{ 1.96 + mLeftExpr->toString(str); 1.97 + 1.98 + switch (mOp) { 1.99 + case ADD: 1.100 + str.AppendLiteral(" + "); 1.101 + break; 1.102 + case SUBTRACT: 1.103 + str.AppendLiteral(" - "); 1.104 + break; 1.105 + case DIVIDE: 1.106 + str.AppendLiteral(" div "); 1.107 + break; 1.108 + case MODULUS: 1.109 + str.AppendLiteral(" mod "); 1.110 + break; 1.111 + case MULTIPLY: 1.112 + str.AppendLiteral(" * "); 1.113 + break; 1.114 + } 1.115 + 1.116 + mRightExpr->toString(str); 1.117 + 1.118 +} 1.119 +#endif