diff options
-rw-r--r-- | fpdfsdk/javascript/JS_Value.cpp | 63 | ||||
-rw-r--r-- | fpdfsdk/javascript/JS_Value.h | 1 | ||||
-rw-r--r-- | fpdfsdk/javascript/PublicMethods.cpp | 13 | ||||
-rw-r--r-- | fpdfsdk/javascript/util.cpp | 3 |
4 files changed, 26 insertions, 54 deletions
diff --git a/fpdfsdk/javascript/JS_Value.cpp b/fpdfsdk/javascript/JS_Value.cpp index 184ff82fa1..40b94fac64 100644 --- a/fpdfsdk/javascript/JS_Value.cpp +++ b/fpdfsdk/javascript/JS_Value.cpp @@ -19,12 +19,6 @@ namespace { -const uint32_t g_nan[2] = {0, 0x7FF80000}; - -double GetNan() { - return *(double*)g_nan; -} - double MakeDate(int year, int mon, int day, int hour, int min, int sec, int ms) { return JS_MakeDate(JS_MakeDay(year, mon, day), @@ -66,18 +60,6 @@ double Mod(double x, double y) { return r; } -int IsFinite(double v) { -#if defined(_MSC_VER) - return ::_finite(v); -#else - return std::fabs(v) < std::numeric_limits<double>::max(); -#endif -} - -double ToInteger(double n) { - return (n >= 0) ? floor(n) : -floor(-n); -} - bool IsLeapYear(int year) { return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0)); } @@ -102,12 +84,12 @@ double TimeFromYearMonth(int y, int m) { } int Day(double t) { - return (int)floor(t / 86400000); + return static_cast<int>(floor(t / 86400000.0)); } int YearFromTime(double t) { // estimate the time. - int y = 1970 + static_cast<int>(t / (365.2425 * 86400000)); + int y = 1970 + static_cast<int>(t / (365.2425 * 86400000.0)); if (TimeFromYear(y) <= t) { while (TimeFromYear(y + 1) <= t) y++; @@ -517,7 +499,7 @@ CJS_Date::CJS_Date(CJS_Runtime* pRuntime, CJS_Date::~CJS_Date() {} bool CJS_Date::IsValidDate(CJS_Runtime* pRuntime) const { - return !m_pDate.IsEmpty() && !JS_PortIsNan(pRuntime->ToDouble(m_pDate)); + return !m_pDate.IsEmpty() && !std::isnan(pRuntime->ToDouble(m_pDate)); } void CJS_Date::Attach(v8::Local<v8::Date> pDate) { @@ -682,7 +664,7 @@ double JS_DateParse(const CFX_WideString& str) { v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked(); if (v->IsNumber()) { double date = v->ToNumber(context).ToLocalChecked()->Value(); - if (!IsFinite(date)) + if (!std::isfinite(date)) return date; return JS_LocalTime(date); } @@ -692,44 +674,33 @@ double JS_DateParse(const CFX_WideString& str) { } double JS_MakeDay(int nYear, int nMonth, int nDate) { - if (!IsFinite(nYear) || !IsFinite(nMonth) || !IsFinite(nDate)) - return GetNan(); - double y = ToInteger(nYear); - double m = ToInteger(nMonth); - double dt = ToInteger(nDate); - double ym = y + floor((double)m / 12); + double y = static_cast<double>(nYear); + double m = static_cast<double>(nMonth); + double dt = static_cast<double>(nDate); + double ym = y + floor(m / 12); double mn = Mod(m, 12); - - double t = TimeFromYearMonth((int)ym, (int)mn); - + double t = TimeFromYearMonth(static_cast<int>(ym), static_cast<int>(mn)); if (YearFromTime(t) != ym || MonthFromTime(t) != mn || DateFromTime(t) != 1) - return GetNan(); + return std::nan(""); + return Day(t) + dt - 1; } double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) { - if (!IsFinite(nHour) || !IsFinite(nMin) || !IsFinite(nSec) || !IsFinite(nMs)) - return GetNan(); - - double h = ToInteger(nHour); - double m = ToInteger(nMin); - double s = ToInteger(nSec); - double milli = ToInteger(nMs); - + double h = static_cast<double>(nHour); + double m = static_cast<double>(nMin); + double s = static_cast<double>(nSec); + double milli = static_cast<double>(nMs); return h * 3600000 + m * 60000 + s * 1000 + milli; } double JS_MakeDate(double day, double time) { - if (!IsFinite(day) || !IsFinite(time)) - return GetNan(); + if (!std::isfinite(day) || !std::isfinite(time)) + return std::nan(""); return day * 86400000 + time; } -bool JS_PortIsNan(double d) { - return d != d; -} - double JS_LocalTime(double d) { return d + GetLocalTZA() + GetDaylightSavingTA(d); } diff --git a/fpdfsdk/javascript/JS_Value.h b/fpdfsdk/javascript/JS_Value.h index 1a6d47fcea..6b67839aff 100644 --- a/fpdfsdk/javascript/JS_Value.h +++ b/fpdfsdk/javascript/JS_Value.h @@ -197,7 +197,6 @@ double JS_DateParse(const CFX_WideString& str); double JS_MakeDay(int nYear, int nMonth, int nDay); double JS_MakeTime(int nHour, int nMin, int nSec, int nMs); double JS_MakeDate(double day, double time); -bool JS_PortIsNan(double d); double JS_LocalTime(double d); // Some JS methods have the bizarre convention that they may also be called diff --git a/fpdfsdk/javascript/PublicMethods.cpp b/fpdfsdk/javascript/PublicMethods.cpp index f5d3c68780..bf35a67edc 100644 --- a/fpdfsdk/javascript/PublicMethods.cpp +++ b/fpdfsdk/javascript/PublicMethods.cpp @@ -7,6 +7,7 @@ #include "fpdfsdk/javascript/PublicMethods.h" #include <algorithm> +#include <cmath> #include <cwctype> #include <iomanip> #include <limits> @@ -603,11 +604,11 @@ double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value, } else { dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay), JS_MakeTime(nHour, nMin, nSec, 0)); - if (JS_PortIsNan(dRet)) + if (std::isnan(dRet)) dRet = JS_DateParse(value); } - if (JS_PortIsNan(dRet)) + if (std::isnan(dRet)) dRet = ParseNormalDate(value, &bBadFormat); if (bWrongFormat) @@ -1120,7 +1121,7 @@ bool CJS_PublicMethods::AFDate_FormatEx(CJS_Runtime* pRuntime, dDate = MakeRegularDate(strValue, sFormat, nullptr); } - if (JS_PortIsNan(dDate)) { + if (std::isnan(dDate)) { CFX_WideString swMsg; swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(), sFormat.c_str()); @@ -1181,7 +1182,7 @@ double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) { int nYear = FX_atof(wsArray[7].AsStringC()); double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay), JS_MakeTime(nHour, nMin, nSec, 0)); - if (JS_PortIsNan(dRet)) + if (std::isnan(dRet)) dRet = JS_DateParse(strValue); return dRet; @@ -1210,7 +1211,7 @@ bool CJS_PublicMethods::AFDate_KeystrokeEx(CJS_Runtime* pRuntime, CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime); bool bWrongFormat = false; double dRet = MakeRegularDate(strValue, sFormat, &bWrongFormat); - if (bWrongFormat || JS_PortIsNan(dRet)) { + if (bWrongFormat || std::isnan(dRet)) { CFX_WideString swMsg; swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(), sFormat.c_str()); @@ -1559,7 +1560,7 @@ bool CJS_PublicMethods::AFParseDateEx(CJS_Runtime* pRuntime, CFX_WideString sValue = params[0].ToCFXWideString(pRuntime); CFX_WideString sFormat = params[1].ToCFXWideString(pRuntime); double dDate = MakeRegularDate(sValue, sFormat, nullptr); - if (JS_PortIsNan(dDate)) { + if (std::isnan(dDate)) { CFX_WideString swMsg; swMsg.Format(JSGetStringFromID(IDS_STRING_JSPARSEDATE).c_str(), sFormat.c_str()); diff --git a/fpdfsdk/javascript/util.cpp b/fpdfsdk/javascript/util.cpp index 93e178e9b7..e00f9e1560 100644 --- a/fpdfsdk/javascript/util.cpp +++ b/fpdfsdk/javascript/util.cpp @@ -9,6 +9,7 @@ #include <time.h> #include <algorithm> +#include <cmath> #include <cwctype> #include <string> #include <vector> @@ -395,7 +396,7 @@ bool util::scand(CJS_Runtime* pRuntime, dDate = CJS_PublicMethods::MakeRegularDate(sDate, sFormat, nullptr); } - if (!JS_PortIsNan(dDate)) { + if (!std::isnan(dDate)) { vRet = CJS_Value(pRuntime, CJS_Date(pRuntime, dDate)); } else { vRet.SetNull(pRuntime); |