summaryrefslogtreecommitdiff
path: root/fpdfsdk
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2017-10-24 15:29:22 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-10-24 21:00:44 +0000
commite85107bc8ab5bbd5b2d3f97fd6071d7ce4a78bcc (patch)
tree77bc28ff0366d3ff873c9d62db0e262421aedc9a /fpdfsdk
parent037eae6e99ef16a42cb74a72b0b52d515a099b3a (diff)
downloadpdfium-e85107bc8ab5bbd5b2d3f97fd6071d7ce4a78bcc.tar.xz
Move MaybeCoerceToNumber to CJS_Runtime
This CL moves MaybeCoerceToNumber from CJS_Value to CJS_Runtime. Change-Id: I22bb605045daa63f405ef256e4b8a5c7ffb78425 Reviewed-on: https://pdfium-review.googlesource.com/16617 Commit-Queue: dsinclair <dsinclair@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'fpdfsdk')
-rw-r--r--fpdfsdk/javascript/Field.cpp2
-rw-r--r--fpdfsdk/javascript/JS_Value.cpp22
-rw-r--r--fpdfsdk/javascript/JS_Value.h4
-rw-r--r--fpdfsdk/javascript/PublicMethods.cpp4
-rw-r--r--fpdfsdk/javascript/cjs_runtime.cpp25
-rw-r--r--fpdfsdk/javascript/cjs_runtime.h4
6 files changed, 32 insertions, 29 deletions
diff --git a/fpdfsdk/javascript/Field.cpp b/fpdfsdk/javascript/Field.cpp
index 35b808c733..93017c4da9 100644
--- a/fpdfsdk/javascript/Field.cpp
+++ b/fpdfsdk/javascript/Field.cpp
@@ -2331,7 +2331,7 @@ bool Field::get_value(CJS_Runtime* pRuntime,
vp->Set(pRuntime->NewString(pFormField->GetValue().c_str()));
break;
}
- vp->MaybeCoerceToNumber(pRuntime);
+ vp->Set(pRuntime->MaybeCoerceToNumber(vp->ToV8Value()));
return true;
}
diff --git a/fpdfsdk/javascript/JS_Value.cpp b/fpdfsdk/javascript/JS_Value.cpp
index 9275de3d52..ddea0a4caa 100644
--- a/fpdfsdk/javascript/JS_Value.cpp
+++ b/fpdfsdk/javascript/JS_Value.cpp
@@ -194,28 +194,6 @@ v8::Local<v8::Value> CJS_Value::ToV8Value() const {
return m_pValue;
}
-void CJS_Value::MaybeCoerceToNumber(CJS_Runtime* pRuntime) {
- bool bAllowNaN = false;
- if (ToV8Value()->IsString()) {
- ByteString bstr =
- ByteString::FromUnicode(pRuntime->ToWideString(ToV8Value()));
- if (bstr.GetLength() == 0)
- return;
- if (bstr == "NaN")
- bAllowNaN = true;
- }
- v8::Isolate* pIsolate = pRuntime->GetIsolate();
- v8::TryCatch try_catch(pIsolate);
- v8::MaybeLocal<v8::Number> maybeNum =
- m_pValue->ToNumber(pIsolate->GetCurrentContext());
- if (maybeNum.IsEmpty())
- return;
- v8::Local<v8::Number> num = maybeNum.ToLocalChecked();
- if (std::isnan(num->Value()) && !bAllowNaN)
- return;
- m_pValue = num;
-}
-
CJS_Array::CJS_Array() {}
CJS_Array::CJS_Array(v8::Local<v8::Array> pArray) : m_pArray(pArray) {}
diff --git a/fpdfsdk/javascript/JS_Value.h b/fpdfsdk/javascript/JS_Value.h
index ff3bf9c51e..d098812361 100644
--- a/fpdfsdk/javascript/JS_Value.h
+++ b/fpdfsdk/javascript/JS_Value.h
@@ -29,10 +29,6 @@ class CJS_Value {
v8::Local<v8::Value> ToV8Value() const;
- // Replace the current |m_pValue| with a v8::Number if possible
- // to make one from the current |m_pValue|.
- void MaybeCoerceToNumber(CJS_Runtime* pRuntime);
-
private:
v8::Local<v8::Value> m_pValue;
};
diff --git a/fpdfsdk/javascript/PublicMethods.cpp b/fpdfsdk/javascript/PublicMethods.cpp
index 200e7d1046..867a265b80 100644
--- a/fpdfsdk/javascript/PublicMethods.cpp
+++ b/fpdfsdk/javascript/PublicMethods.cpp
@@ -1658,8 +1658,8 @@ bool CJS_PublicMethods::AFMakeNumber(CJS_Runtime* pRuntime,
WideString ws = pRuntime->ToWideString(params[0].ToV8Value());
ws.Replace(L",", L".");
- vRet = CJS_Value(pRuntime->NewString(ws.c_str()));
- vRet.MaybeCoerceToNumber(pRuntime);
+ vRet =
+ CJS_Value(pRuntime->MaybeCoerceToNumber(pRuntime->NewString(ws.c_str())));
if (!vRet.ToV8Value()->IsNumber())
vRet = CJS_Value(pRuntime->NewNumber(0));
return true;
diff --git a/fpdfsdk/javascript/cjs_runtime.cpp b/fpdfsdk/javascript/cjs_runtime.cpp
index 720a15b4bd..c1ec5feac6 100644
--- a/fpdfsdk/javascript/cjs_runtime.cpp
+++ b/fpdfsdk/javascript/cjs_runtime.cpp
@@ -254,3 +254,28 @@ bool CJS_Runtime::SetValueByName(const ByteStringView& utf8Name,
return true;
}
#endif
+
+v8::Local<v8::Value> CJS_Runtime::MaybeCoerceToNumber(
+ const v8::Local<v8::Value>& value) {
+ bool bAllowNaN = false;
+ if (value->IsString()) {
+ ByteString bstr = ByteString::FromUnicode(ToWideString(value));
+ if (bstr.GetLength() == 0)
+ return value;
+ if (bstr == "NaN")
+ bAllowNaN = true;
+ }
+
+ v8::Isolate* pIsolate = GetIsolate();
+ v8::TryCatch try_catch(pIsolate);
+ v8::MaybeLocal<v8::Number> maybeNum =
+ value->ToNumber(pIsolate->GetCurrentContext());
+ if (maybeNum.IsEmpty())
+ return value;
+
+ v8::Local<v8::Number> num = maybeNum.ToLocalChecked();
+ if (std::isnan(num->Value()) && !bAllowNaN)
+ return value;
+
+ return num;
+}
diff --git a/fpdfsdk/javascript/cjs_runtime.h b/fpdfsdk/javascript/cjs_runtime.h
index 0d6951f8c0..90ec23ee7a 100644
--- a/fpdfsdk/javascript/cjs_runtime.h
+++ b/fpdfsdk/javascript/cjs_runtime.h
@@ -48,6 +48,10 @@ class CJS_Runtime : public IJS_Runtime,
void EndBlock() { m_bBlocking = false; }
bool IsBlocking() const { return m_bBlocking; }
+ // Attempt to convert the |value| into a number. If successful the number
+ // value will be returned, otherwise |value| is returned.
+ v8::Local<v8::Value> MaybeCoerceToNumber(const v8::Local<v8::Value>& value);
+
#ifdef PDF_ENABLE_XFA
bool GetValueByName(const ByteStringView& utf8Name,
CFXJSE_Value* pValue) override;