Skip to content

Commit 146c9b4

Browse files
committed
feat: convert v8 double value of Nan/Inf to CefValue in string type
1 parent 454c724 commit 146c9b4

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/CefWing/Bridge/CefViewBridgeObject.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
#include <include/cef_version.h>
44

5+
#include <cmath>
6+
57
#include <Common/CefViewCoreLog.h>
68

79
#include <CefViewCoreProtocol.h>
810

9-
1011
#if CEF_VERSION_MAJOR >= 119
1112
class CefViewArrayBuffer : public CefV8ArrayBufferReleaseCallback
1213
{
@@ -236,7 +237,8 @@ CefViewBridgeObject::CefViewBridgeObject(CefRefPtr<CefBrowser> browser,
236237
static_cast<CefV8Value::PropertyAttribute>(V8_PROPERTY_ATTRIBUTE_READONLY |
237238
V8_PROPERTY_ATTRIBUTE_DONTENUM |
238239
V8_PROPERTY_ATTRIBUTE_DONTDELETE));
239-
frame_->ExecuteJavaScript("console.info('[JSRuntime]:window." + name_.ToString() + " [object] created');", frame_->GetURL(), 0);
240+
frame_->ExecuteJavaScript(
241+
"console.info('[JSRuntime]:window." + name_.ToString() + " [object] created');", frame_->GetURL(), 0);
240242

241243
// create "__cefview_report_js_result__" function and mount it on the global context(window)
242244
reportJSResultFunction_ = CefV8Value::CreateFunction(kCefViewReportJSResultFunctionName, v8Handler_);
@@ -362,9 +364,15 @@ CefViewBridgeObject::V8ValueToCefValue(CefV8Value* v8Value)
362364
cefValue->SetBool(v8Value->GetBoolValue());
363365
else if (v8Value->IsInt())
364366
cefValue->SetInt(v8Value->GetIntValue());
365-
else if (v8Value->IsDouble())
366-
cefValue->SetDouble(v8Value->GetDoubleValue());
367-
else if (v8Value->IsString())
367+
else if (v8Value->IsDouble()) {
368+
auto v = v8Value->GetDoubleValue();
369+
if (std::isfinite(v)) {
370+
cefValue->SetDouble(v);
371+
} else {
372+
// convert non-representable value(NaN/+inf/-inf) to string
373+
cefValue->SetString(std::to_string(v));
374+
}
375+
} else if (v8Value->IsString())
368376
cefValue->SetString(v8Value->GetStringValue());
369377
else if (v8Value->IsArrayBuffer()) {
370378
#if CEF_VERSION_MAJOR >= 119

0 commit comments

Comments
 (0)