Make NewNull return an actual Null
This CL updates the CFXJS_Engine::NewNull method to return a real v8::Null
instead of an empty v8::Local. This also adds a NewUndefined and returns
undefined in most of the places null was returned previously.
Change-Id: If1a96bf253057892a3b709cbc72f8825c52503c3
Reviewed-on: https://pdfium-review.googlesource.com/16730
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: dsinclair <dsinclair@chromium.org>
diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp
index 9404ded..2217f44 100644
--- a/fpdfsdk/javascript/Document.cpp
+++ b/fpdfsdk/javascript/Document.cpp
@@ -213,7 +213,7 @@
bool Document::get_ADBE(CJS_Runtime* pRuntime,
CJS_Value* vp,
WideString* sError) {
- vp->Set(pRuntime->NewNull());
+ vp->Set(pRuntime->NewUndefined());
return true;
}
@@ -312,7 +312,7 @@
CPDFSDK_InterForm* pInterForm = m_pFormFillEnv->GetInterForm();
CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
if (pPDFForm->CountFields(wideName) <= 0) {
- vRet.Set(pRuntime->NewNull());
+ vRet.Set(pRuntime->NewUndefined());
return true;
}
@@ -1321,7 +1321,7 @@
const std::vector<v8::Local<v8::Value>>& params,
CJS_Value& vRet,
WideString& sError) {
- vRet.Set(pRuntime->NewNull());
+ vRet.Set(pRuntime->NewUndefined());
return true;
}
@@ -1387,7 +1387,7 @@
CJS_Value* vp,
WideString* sError) {
if (m_IconNames.empty()) {
- vp->Set(pRuntime->NewNull());
+ vp->Set(pRuntime->NewUndefined());
return true;
}
diff --git a/fpdfsdk/javascript/PublicMethods.cpp b/fpdfsdk/javascript/PublicMethods.cpp
index c0441a8..667127f 100644
--- a/fpdfsdk/javascript/PublicMethods.cpp
+++ b/fpdfsdk/javascript/PublicMethods.cpp
@@ -1840,7 +1840,7 @@
else
vRet = CJS_Value(nums.ToV8Value());
} else {
- vRet.Set(pRuntime->NewNull());
+ vRet.Set(pRuntime->NewUndefined());
}
return true;
diff --git a/fpdfsdk/javascript/app.cpp b/fpdfsdk/javascript/app.cpp
index ca440f6..cbc3145 100644
--- a/fpdfsdk/javascript/app.cpp
+++ b/fpdfsdk/javascript/app.cpp
@@ -237,7 +237,7 @@
else
vp->Set(aDocs.ToV8Value());
} else {
- vp->Set(pRuntime->NewNull());
+ vp->Set(pRuntime->NewUndefined());
}
return true;
diff --git a/fpdfsdk/javascript/global.cpp b/fpdfsdk/javascript/global.cpp
index 040c6e6..5dc6ac9 100644
--- a/fpdfsdk/javascript/global.cpp
+++ b/fpdfsdk/javascript/global.cpp
@@ -109,7 +109,8 @@
pRuntime->Error(JSFormatErrorString(class_name, "GetProperty", L""));
return;
}
- info.GetReturnValue().Set(value.ToV8Value());
+ if (!value.ToV8Value().IsEmpty())
+ info.GetReturnValue().Set(value.ToV8Value());
}
template <class Alt>
@@ -284,16 +285,12 @@
const wchar_t* propname,
CJS_Value* vp) {
auto it = m_MapGlobal.find(ByteString::FromUnicode(propname));
- if (it == m_MapGlobal.end()) {
- vp->Set(pRuntime->NewNull());
+ if (it == m_MapGlobal.end())
return true;
- }
JSGlobalData* pData = it->second.get();
- if (pData->bDeleted) {
- vp->Set(pRuntime->NewNull());
+ if (pData->bDeleted)
return true;
- }
switch (pData->nType) {
case JS_GlobalDataType::NUMBER:
diff --git a/fpdfsdk/javascript/util.cpp b/fpdfsdk/javascript/util.cpp
index ecec8d1..202a1ea 100644
--- a/fpdfsdk/javascript/util.cpp
+++ b/fpdfsdk/javascript/util.cpp
@@ -398,7 +398,7 @@
if (!std::isnan(dDate)) {
vRet = CJS_Value(CJS_Date(pRuntime, dDate).ToV8Value());
} else {
- vRet.Set(pRuntime->NewNull());
+ vRet.Set(pRuntime->NewUndefined());
}
return true;
diff --git a/fxjs/fxjs_v8.cpp b/fxjs/fxjs_v8.cpp
index 4c63988..4418482 100644
--- a/fxjs/fxjs_v8.cpp
+++ b/fxjs/fxjs_v8.cpp
@@ -664,7 +664,11 @@
}
v8::Local<v8::Value> CFXJS_Engine::NewNull() {
- return v8::Local<v8::Value>();
+ return v8::Null(m_isolate);
+}
+
+v8::Local<v8::Value> CFXJS_Engine::NewUndefined() {
+ return v8::Undefined(m_isolate);
}
v8::Local<v8::Date> CFXJS_Engine::NewDate(double d) {
diff --git a/fxjs/fxjs_v8.h b/fxjs/fxjs_v8.h
index f5e5241..eee85e0 100644
--- a/fxjs/fxjs_v8.h
+++ b/fxjs/fxjs_v8.h
@@ -179,6 +179,7 @@
v8::Local<v8::Object> GetThisObj();
v8::Local<v8::Value> NewNull();
+ v8::Local<v8::Value> NewUndefined();
v8::Local<v8::Array> NewArray();
v8::Local<v8::Number> NewNumber(int number);
v8::Local<v8::Number> NewNumber(double number);
diff --git a/fxjs/fxjs_v8_embeddertest.cpp b/fxjs/fxjs_v8_embeddertest.cpp
index 53fe8f2..d975264 100644
--- a/fxjs/fxjs_v8_embeddertest.cpp
+++ b/fxjs/fxjs_v8_embeddertest.cpp
@@ -98,11 +98,25 @@
EXPECT_FALSE(engine()->ToBoolean(nullz));
EXPECT_EQ(0, engine()->ToInt32(nullz));
EXPECT_EQ(0.0, engine()->ToDouble(nullz));
- EXPECT_EQ(L"", engine()->ToWideString(nullz));
+ EXPECT_EQ(L"null", engine()->ToWideString(nullz));
EXPECT_TRUE(engine()->ToObject(nullz).IsEmpty());
EXPECT_TRUE(engine()->ToArray(nullz).IsEmpty());
}
+TEST_F(FXJSV8EmbedderTest, NewUndefined) {
+ v8::Isolate::Scope isolate_scope(isolate());
+ v8::HandleScope handle_scope(isolate());
+ v8::Context::Scope context_scope(GetV8Context());
+
+ auto undef = engine()->NewUndefined();
+ EXPECT_FALSE(engine()->ToBoolean(undef));
+ EXPECT_EQ(0, engine()->ToInt32(undef));
+ EXPECT_TRUE(std::isnan(engine()->ToDouble(undef)));
+ EXPECT_EQ(L"undefined", engine()->ToWideString(undef));
+ EXPECT_TRUE(engine()->ToObject(undef).IsEmpty());
+ EXPECT_TRUE(engine()->ToArray(undef).IsEmpty());
+}
+
TEST_F(FXJSV8EmbedderTest, NewBoolean) {
v8::Isolate::Scope isolate_scope(isolate());
v8::HandleScope handle_scope(isolate());