Catch more exceptions from reentrant V8 conversions.

Avoid tripping a check in debug builds.

- Move these down to a lower level to cover all possible callers
- Update one test that no longer sees the caught exception.
- Use GetIsolate() more consistently while we're at it.

Bug: chromium:1112206
Change-Id: Idd3cd4ecb68b929740f564fd780cde4b5623e630
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/72671
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fxjs/cfx_v8.cpp b/fxjs/cfx_v8.cpp
index 2901e07..1acb7de 100644
--- a/fxjs/cfx_v8.cpp
+++ b/fxjs/cfx_v8.cpp
@@ -17,19 +17,18 @@
 v8::Local<v8::Value> CFX_V8::GetObjectProperty(
     v8::Local<v8::Object> pObj,
     ByteStringView bsUTF8PropertyName) {
-  return fxv8::ReentrantGetObjectPropertyHelper(m_pIsolate.Get(), pObj,
+  return fxv8::ReentrantGetObjectPropertyHelper(GetIsolate(), pObj,
                                                 bsUTF8PropertyName);
 }
 
 std::vector<WideString> CFX_V8::GetObjectPropertyNames(
     v8::Local<v8::Object> pObj) {
-  return fxv8::ReentrantGetObjectPropertyNamesHelper(m_pIsolate.Get(), pObj);
+  return fxv8::ReentrantGetObjectPropertyNamesHelper(GetIsolate(), pObj);
 }
 
 void CFX_V8::PutObjectProperty(v8::Local<v8::Object> pObj,
                                ByteStringView bsUTF8PropertyName,
                                v8::Local<v8::Value> pPut) {
-  v8::TryCatch squash_exceptions(GetIsolate());
   fxv8::ReentrantPutObjectPropertyHelper(GetIsolate(), pObj, bsUTF8PropertyName,
                                          pPut);
 }
@@ -50,7 +49,6 @@
 void CFX_V8::PutArrayElement(v8::Local<v8::Array> pArray,
                              unsigned index,
                              v8::Local<v8::Value> pValue) {
-  v8::TryCatch squash_exceptions(GetIsolate());
   fxv8::ReentrantPutArrayElementHelper(GetIsolate(), pArray, index, pValue);
 }
 
@@ -104,23 +102,23 @@
 }
 
 int CFX_V8::ToInt32(v8::Local<v8::Value> pValue) {
-  return fxv8::ReentrantToInt32Helper(m_pIsolate.Get(), pValue);
+  return fxv8::ReentrantToInt32Helper(GetIsolate(), pValue);
 }
 
 bool CFX_V8::ToBoolean(v8::Local<v8::Value> pValue) {
-  return fxv8::ReentrantToBooleanHelper(m_pIsolate.Get(), pValue);
+  return fxv8::ReentrantToBooleanHelper(GetIsolate(), pValue);
 }
 
 double CFX_V8::ToDouble(v8::Local<v8::Value> pValue) {
-  return fxv8::ReentrantToDoubleHelper(m_pIsolate.Get(), pValue);
+  return fxv8::ReentrantToDoubleHelper(GetIsolate(), pValue);
 }
 
 WideString CFX_V8::ToWideString(v8::Local<v8::Value> pValue) {
-  return fxv8::ReentrantToWideStringHelper(m_pIsolate.Get(), pValue);
+  return fxv8::ReentrantToWideStringHelper(GetIsolate(), pValue);
 }
 
 ByteString CFX_V8::ToByteString(v8::Local<v8::Value> pValue) {
-  return fxv8::ReentrantToByteStringHelper(m_pIsolate.Get(), pValue);
+  return fxv8::ReentrantToByteStringHelper(GetIsolate(), pValue);
 }
 
 v8::Local<v8::Object> CFX_V8::ToObject(v8::Local<v8::Value> pValue) {
diff --git a/fxjs/fxv8.cpp b/fxjs/fxv8.cpp
index b655d2d..4d5804c 100644
--- a/fxjs/fxv8.cpp
+++ b/fxjs/fxv8.cpp
@@ -61,6 +61,7 @@
 int ReentrantToInt32Helper(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
   if (pValue.IsEmpty())
     return 0;
+  v8::TryCatch squash_exceptions(pIsolate);
   return pValue->Int32Value(pIsolate->GetCurrentContext()).FromMaybe(0);
 }
 
@@ -68,6 +69,7 @@
                               v8::Local<v8::Value> pValue) {
   if (pValue.IsEmpty())
     return false;
+  v8::TryCatch squash_exceptions(pIsolate);
   return pValue->BooleanValue(pIsolate);
 }
 
@@ -75,6 +77,7 @@
                                v8::Local<v8::Value> pValue) {
   if (pValue.IsEmpty())
     return 0.0;
+  v8::TryCatch squash_exceptions(pIsolate);
   return pValue->NumberValue(pIsolate->GetCurrentContext()).FromMaybe(0.0);
 }
 
@@ -83,6 +86,7 @@
   if (pValue.IsEmpty())
     return WideString();
 
+  v8::TryCatch squash_exceptions(pIsolate);
   v8::MaybeLocal<v8::String> maybe_string =
       pValue->ToString(pIsolate->GetCurrentContext());
   if (maybe_string.IsEmpty())
@@ -97,6 +101,7 @@
   if (pValue.IsEmpty())
     return ByteString();
 
+  v8::TryCatch squash_exceptions(pIsolate);
   v8::MaybeLocal<v8::String> maybe_string =
       pValue->ToString(pIsolate->GetCurrentContext());
   if (maybe_string.IsEmpty())
@@ -110,6 +115,8 @@
                                               v8::Local<v8::Value> pValue) {
   if (pValue.IsEmpty() || !pValue->IsObject())
     return v8::Local<v8::Object>();
+
+  v8::TryCatch squash_exceptions(pIsolate);
   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
   return pValue->ToObject(context).ToLocalChecked();
 }
@@ -118,6 +125,8 @@
                                             v8::Local<v8::Value> pValue) {
   if (pValue.IsEmpty() || !pValue->IsArray())
     return v8::Local<v8::Array>();
+
+  v8::TryCatch squash_exceptions(pIsolate);
   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
   return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
 }
@@ -129,6 +138,7 @@
   if (pObj.IsEmpty())
     return v8::Local<v8::Value>();
 
+  v8::TryCatch squash_exceptions(pIsolate);
   v8::Local<v8::Value> val;
   if (!pObj->Get(pIsolate->GetCurrentContext(),
                  NewStringHelper(pIsolate, bsUTF8PropertyName))
@@ -144,6 +154,7 @@
   if (pObj.IsEmpty())
     return std::vector<WideString>();
 
+  v8::TryCatch squash_exceptions(pIsolate);
   v8::Local<v8::Array> val;
   v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
   if (!pObj->GetPropertyNames(context).ToLocal(&val))
@@ -165,6 +176,7 @@
   if (pObj.IsEmpty())
     return false;
 
+  v8::TryCatch squash_exceptions(pIsolate);
   v8::Local<v8::String> name = NewStringHelper(pIsolate, bsUTF8PropertyName);
   v8::Maybe<bool> result = pObj->Set(pIsolate->GetCurrentContext(), name, pPut);
   return result.IsJust() && result.FromJust();
@@ -177,6 +189,7 @@
   if (pArray.IsEmpty())
     return false;
 
+  v8::TryCatch squash_exceptions(pIsolate);
   v8::Maybe<bool> result =
       pArray->Set(pIsolate->GetCurrentContext(), index, pValue);
   return result.IsJust() && result.FromJust();
@@ -187,6 +200,8 @@
                                                     unsigned index) {
   if (pArray.IsEmpty())
     return v8::Local<v8::Value>();
+
+  v8::TryCatch squash_exceptions(pIsolate);
   v8::Local<v8::Value> val;
   if (!pArray->Get(pIsolate->GetCurrentContext(), index).ToLocal(&val))
     return v8::Local<v8::Value>();
diff --git a/fxjs/xfa/cfxjse_formcalc_context_embeddertest.cpp b/fxjs/xfa/cfxjse_formcalc_context_embeddertest.cpp
index 9e2532c..97b7b87 100644
--- a/fxjs/xfa/cfxjse_formcalc_context_embeddertest.cpp
+++ b/fxjs/xfa/cfxjse_formcalc_context_embeddertest.cpp
@@ -1705,6 +1705,5 @@
 // Should not crash.
 TEST_F(CFXJSE_FormCalcContextEmbedderTest, BUG_1223) {
   ASSERT_TRUE(OpenDocument("simple_xfa.pdf"));
-
-  EXPECT_FALSE(Execute("!.somExpression=0"));
+  EXPECT_TRUE(Execute("!.somExpression=0"));
 }