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>
diff --git a/fpdfsdk/javascript/cjs_runtime.cpp b/fpdfsdk/javascript/cjs_runtime.cpp
index 720a15b..c1ec5fe 100644
--- a/fpdfsdk/javascript/cjs_runtime.cpp
+++ b/fpdfsdk/javascript/cjs_runtime.cpp
@@ -254,3 +254,28 @@
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;
+}