summaryrefslogtreecommitdiff
path: root/fpdfsdk/src/javascript/JS_Value.cpp
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2016-01-20 11:34:01 -0800
committerTom Sepez <tsepez@chromium.org>2016-01-20 11:34:01 -0800
commitf13d510cf267c27f4c123494de67670ec201cedc (patch)
tree2b5e279bef4fb9eb4a3a7e963cae93c65bc80fba /fpdfsdk/src/javascript/JS_Value.cpp
parentb196c7bebad66c9938d2705ccf64961bcdd774e2 (diff)
downloadpdfium-f13d510cf267c27f4c123494de67670ec201cedc.tar.xz
Bugs in CJS_PublicMethods::ParseNumber().
Fix the bugs by removing ParseNumber() entirely. For PDFium's JavaScript bindings, we want to get out of the numeric conversion business and inflict that on V8 as possible, avoiding platform-specific issue in strtod(). For other uses, there is a FX_atof() which is similarly buggy, but we can consolidate the use. Add an overloaded FX_atof() to handle wide strings more simply. BUG=pdfium:361 R=jochen@chromium.org Review URL: https://codereview.chromium.org/1586203006 .
Diffstat (limited to 'fpdfsdk/src/javascript/JS_Value.cpp')
-rw-r--r--fpdfsdk/src/javascript/JS_Value.cpp30
1 files changed, 20 insertions, 10 deletions
diff --git a/fpdfsdk/src/javascript/JS_Value.cpp b/fpdfsdk/src/javascript/JS_Value.cpp
index bd00adcf49..cfa565e4d4 100644
--- a/fpdfsdk/src/javascript/JS_Value.cpp
+++ b/fpdfsdk/src/javascript/JS_Value.cpp
@@ -19,8 +19,6 @@ static double GetNan() {
return *(double*)g_nan;
}
-/* ---------------------------- CJS_Value ---------------------------- */
-
CJS_Value::CJS_Value(CJS_Runtime* pRuntime)
: m_eType(VT_unknown), m_pJSRuntime(pRuntime) {
}
@@ -98,9 +96,6 @@ void CJS_Value::Detach() {
m_eType = VT_unknown;
}
-/* ----------------------------------------------------------------------------------------
- */
-
int CJS_Value::ToInt() const {
return FXJS_ToInt32(m_pJSRuntime->GetIsolate(), m_pValue);
}
@@ -146,8 +141,26 @@ v8::Local<v8::Array> CJS_Value::ToV8Array() const {
return v8::Local<v8::Array>();
}
-/* ----------------------------------------------------------------------------------------
- */
+void CJS_Value::MaybeCoerceToNumber() {
+ bool bAllowNaN = false;
+ if (m_eType == VT_string) {
+ CFX_ByteString bstr = ToCFXByteString();
+ if (bstr.GetLength() == 0)
+ return;
+ if (bstr == "NaN")
+ bAllowNaN = true;
+ }
+ v8::TryCatch(m_pJSRuntime->GetIsolate());
+ v8::MaybeLocal<v8::Number> maybeNum =
+ m_pValue->ToNumber(m_pJSRuntime->GetIsolate()->GetCurrentContext());
+ if (maybeNum.IsEmpty())
+ return;
+ v8::Local<v8::Number> num = maybeNum.ToLocalChecked();
+ if (std::isnan(num->Value()) && !bAllowNaN)
+ return;
+ m_pValue = num;
+ m_eType = VT_number;
+}
void CJS_Value::operator=(int iValue) {
m_pValue = FXJS_NewNumber(m_pJSRuntime->GetIsolate(), iValue);
@@ -217,9 +230,6 @@ void CJS_Value::operator=(CJS_Value value) {
m_pJSRuntime = value.m_pJSRuntime;
}
-/* ----------------------------------------------------------------------------------------
- */
-
CJS_Value::Type CJS_Value::GetType() const {
if (m_pValue.IsEmpty())
return VT_unknown;