diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2017-10-24 15:29:22 -0400 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-10-24 21:00:44 +0000 |
commit | e85107bc8ab5bbd5b2d3f97fd6071d7ce4a78bcc (patch) | |
tree | 77bc28ff0366d3ff873c9d62db0e262421aedc9a /fpdfsdk/javascript/cjs_runtime.cpp | |
parent | 037eae6e99ef16a42cb74a72b0b52d515a099b3a (diff) | |
download | pdfium-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/javascript/cjs_runtime.cpp')
-rw-r--r-- | fpdfsdk/javascript/cjs_runtime.cpp | 25 |
1 files changed, 25 insertions, 0 deletions
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; +} |