summaryrefslogtreecommitdiff
path: root/fpdfsdk/javascript/cjs_runtime.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'fpdfsdk/javascript/cjs_runtime.cpp')
-rw-r--r--fpdfsdk/javascript/cjs_runtime.cpp25
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;
+}