Convert IJS_Runtime to CPDFSDK_FormFillEnvironment

This CL changes the use of CPDFSDK_Document to CPDFSDK_FormFillEnvironment.

Review-Url: https://codereview.chromium.org/2394313002
diff --git a/fpdfsdk/cpdfsdk_interform.cpp b/fpdfsdk/cpdfsdk_interform.cpp
index 2cbdae9..69698ea 100644
--- a/fpdfsdk/cpdfsdk_interform.cpp
+++ b/fpdfsdk/cpdfsdk_interform.cpp
@@ -236,8 +236,6 @@
   }
 
   IJS_Runtime* pRuntime = m_pFormFillEnv->GetJSRuntime();
-  pRuntime->SetReaderDocument(m_pFormFillEnv->GetSDKDocument());
-
   int nSize = m_pInterForm->CountFieldsInCalculationOrder();
   for (int i = 0; i < nSize; i++) {
     CPDF_FormField* pField = m_pInterForm->GetFieldInCalculationOrder(i);
@@ -286,8 +284,6 @@
   }
 
   IJS_Runtime* pRuntime = m_pFormFillEnv->GetJSRuntime();
-  pRuntime->SetReaderDocument(m_pFormFillEnv->GetSDKDocument());
-
   if (pFormField->GetFieldType() == FIELDTYPE_COMBOBOX &&
       pFormField->CountSelectedItems() > 0) {
     int index = pFormField->GetSelectedIndex(0);
diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
index 6e8f671..1d4b36e 100644
--- a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
+++ b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp
@@ -1012,13 +1012,10 @@
       !m_pDocument->GetSDKDoc()->GetEnv()->GetJSRuntime())
     return FALSE;
 
-  if (!m_pJSContext) {
-    m_pDocument->GetSDKDoc()->GetEnv()->GetJSRuntime()->SetReaderDocument(
-        m_pDocument->GetSDKDoc());
-    m_pJSContext =
-        m_pDocument->GetSDKDoc()->GetEnv()->GetJSRuntime()->NewContext();
-  }
+  CPDFSDK_FormFillEnvironment* pFormFillEnv =
+      m_pDocument->GetSDKDoc()->GetEnv();
+  if (!m_pJSContext)
+    m_pJSContext = pFormFillEnv->GetJSRuntime()->NewContext();
 
-  return m_pDocument->GetSDKDoc()->GetEnv()->GetJSRuntime()->GetValueByName(
-      szPropName, pValue);
+  return pFormFillEnv->GetJSRuntime()->GetValueByName(szPropName, pValue);
 }
diff --git a/fpdfsdk/fsdk_actionhandler.cpp b/fpdfsdk/fsdk_actionhandler.cpp
index f6ba4c9..8a2b1fa 100644
--- a/fpdfsdk/fsdk_actionhandler.cpp
+++ b/fpdfsdk/fsdk_actionhandler.cpp
@@ -155,8 +155,6 @@
       CFX_WideString swJS = action.GetJavaScript();
       if (!swJS.IsEmpty()) {
         IJS_Runtime* pRuntime = pFormFillEnv->GetJSRuntime();
-        pRuntime->SetReaderDocument(pDocument);
-
         IJS_Context* pContext = pRuntime->NewContext();
         pContext->OnLink_MouseUp(pFormFillEnv);
 
@@ -284,8 +282,6 @@
       CFX_WideString swJS = action.GetJavaScript();
       if (!swJS.IsEmpty()) {
         IJS_Runtime* pRuntime = pDocument->GetJsRuntime();
-        pRuntime->SetReaderDocument(pDocument);
-
         IJS_Context* pContext = pRuntime->NewContext();
         CFX_WideString csInfo;
         FX_BOOL bRet = pContext->RunScript(swJS, &csInfo);
@@ -327,8 +323,6 @@
       CFX_WideString swJS = action.GetJavaScript();
       if (!swJS.IsEmpty()) {
         IJS_Runtime* pRuntime = pDocument->GetJsRuntime();
-        pRuntime->SetReaderDocument(pDocument);
-
         IJS_Context* pContext = pRuntime->NewContext();
         pContext->OnBookmark_MouseUp(pBookmark);
 
@@ -478,8 +472,6 @@
   ASSERT(type != CPDF_AAction::Format);
 
   IJS_Runtime* pRuntime = pDocument->GetJsRuntime();
-  pRuntime->SetReaderDocument(pDocument);
-
   IJS_Context* pContext = pRuntime->NewContext();
   switch (type) {
     case CPDF_AAction::CursorEnter:
@@ -532,7 +524,6 @@
     const CFX_WideString& sScriptName,
     const CFX_WideString& script) {
   IJS_Runtime* pRuntime = pDocument->GetJsRuntime();
-  pRuntime->SetReaderDocument(pDocument);
   IJS_Context* pContext = pRuntime->NewContext();
   pContext->OnDoc_Open(pDocument->GetEnv(), sScriptName);
 
@@ -550,8 +541,6 @@
     CPDF_AAction::AActionType type,
     const CFX_WideString& script) {
   IJS_Runtime* pRuntime = pDocument->GetJsRuntime();
-  pRuntime->SetReaderDocument(pDocument);
-
   IJS_Context* pContext = pRuntime->NewContext();
   switch (type) {
     case CPDF_AAction::OpenPage:
diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp
index f8edf97..ac94e10 100644
--- a/fpdfsdk/javascript/Document.cpp
+++ b/fpdfsdk/javascript/Document.cpp
@@ -149,12 +149,12 @@
 void CJS_Document::InitInstance(IJS_Runtime* pIRuntime) {
   CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime);
   Document* pDoc = static_cast<Document*>(GetEmbedObject());
-  pDoc->AttachDoc(pRuntime->GetReaderDocument());
+  pDoc->SetFormFillEnv(pRuntime->GetFormFillEnv());
 }
 
 Document::Document(CJS_Object* pJSObject)
     : CJS_EmbedObj(pJSObject),
-      m_pDocument(nullptr),
+      m_pFormFillEnv(nullptr),
       m_cwBaseURL(L""),
       m_bDelay(FALSE) {}
 
@@ -169,11 +169,12 @@
     sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
     return FALSE;
   }
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
+  CPDFSDK_InterForm* pInterForm =
+      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
   CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
   vp << static_cast<int>(pPDFForm->CountFields(CFX_WideString()));
   return TRUE;
@@ -182,20 +183,20 @@
 FX_BOOL Document::dirty(IJS_Context* cc,
                         CJS_PropValue& vp,
                         CFX_WideString& sError) {
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
   if (vp.IsGetting()) {
-    vp << !!m_pDocument->GetChangeMark();
+    vp << !!m_pFormFillEnv->GetSDKDocument()->GetChangeMark();
   } else {
     bool bChanged = false;
     vp >> bChanged;
 
     if (bChanged)
-      m_pDocument->SetChangeMark();
+      m_pFormFillEnv->GetSDKDocument()->SetChangeMark();
     else
-      m_pDocument->ClearChangeMark();
+      m_pFormFillEnv->GetSDKDocument()->ClearChangeMark();
   }
   return TRUE;
 }
@@ -212,26 +213,26 @@
 FX_BOOL Document::pageNum(IJS_Context* cc,
                           CJS_PropValue& vp,
                           CFX_WideString& sError) {
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
   if (vp.IsGetting()) {
-    if (CPDFSDK_PageView* pPageView = m_pDocument->GetCurrentView()) {
+    if (CPDFSDK_PageView* pPageView =
+            m_pFormFillEnv->GetSDKDocument()->GetCurrentView()) {
       vp << pPageView->GetPageIndex();
     }
   } else {
-    int iPageCount = m_pDocument->GetPageCount();
+    int iPageCount = m_pFormFillEnv->GetSDKDocument()->GetPageCount();
     int iPageNum = 0;
     vp >> iPageNum;
 
-    CPDFSDK_FormFillEnvironment* pEnv = m_pDocument->GetEnv();
     if (iPageNum >= 0 && iPageNum < iPageCount) {
-      pEnv->JS_docgotoPage(iPageNum);
+      m_pFormFillEnv->JS_docgotoPage(iPageNum);
     } else if (iPageNum >= iPageCount) {
-      pEnv->JS_docgotoPage(iPageCount - 1);
+      m_pFormFillEnv->JS_docgotoPage(iPageCount - 1);
     } else if (iPageNum < 0) {
-      pEnv->JS_docgotoPage(0);
+      m_pFormFillEnv->JS_docgotoPage(0);
     }
   }
 
@@ -291,14 +292,15 @@
     sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
   CJS_Context* pContext = static_cast<CJS_Context*>(cc);
   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
   CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
-  CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
+  CPDFSDK_InterForm* pInterForm =
+      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
   CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
   if (pPDFForm->CountFields(wideName) <= 0) {
     vRet.SetNull(pRuntime);
@@ -325,7 +327,7 @@
     sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
@@ -336,7 +338,8 @@
     sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
     return FALSE;
   }
-  CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
+  CPDFSDK_InterForm* pInterForm =
+      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
   CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
   CPDF_FormField* pField = pPDFForm->GetField(nIndex, CFX_WideString());
   if (!pField)
@@ -381,11 +384,12 @@
                            const std::vector<CJS_Value>& params,
                            CJS_Value& vRet,
                            CFX_WideString& sError) {
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
+  if (!m_pFormFillEnv->GetSDKDocument()->GetPermissions(
+          FPDFPERM_EXTRACT_ACCESS)) {
     sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
     return FALSE;
   }
@@ -402,7 +406,8 @@
       iLength > 4 ? params[4].ToCFXWideString(pRuntime) : L"";
   CFX_WideString cMsg = iLength > 5 ? params[5].ToCFXWideString(pRuntime) : L"";
 
-  CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
+  CPDFSDK_InterForm* pInterForm =
+      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
   CFX_ByteTextBuf textBuf;
   if (!pInterForm->ExportFormToFDFTextBuf(textBuf))
     return FALSE;
@@ -420,7 +425,7 @@
                         const std::vector<CJS_Value>& params,
                         CJS_Value& vRet,
                         CFX_WideString& sError) {
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
@@ -477,9 +482,9 @@
       bAnnotations = params[7].ToBool(pRuntime);
   }
 
-  if (CPDFSDK_FormFillEnvironment* pEnv = m_pDocument->GetEnv()) {
-    pEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit, bPrintAsImage,
-                      bReverse, bAnnotations);
+  if (m_pFormFillEnv) {
+    m_pFormFillEnv->JS_docprint(bUI, nStart, nEnd, bSilent, bShrinkToFit,
+                                bPrintAsImage, bReverse, bAnnotations);
     return TRUE;
   }
   return FALSE;
@@ -497,19 +502,21 @@
     sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
-        m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM))) {
+  if (!(m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_MODIFY) ||
+        m_pFormFillEnv->GetSDKDocument()->GetPermissions(
+            FPDFPERM_ANNOT_FORM))) {
     sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
     return FALSE;
   }
   CJS_Context* pContext = static_cast<CJS_Context*>(cc);
   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
   CFX_WideString sFieldName = params[0].ToCFXWideString(pRuntime);
-  CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
+  CPDFSDK_InterForm* pInterForm =
+      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
   std::vector<CPDFSDK_Widget*> widgets;
   pInterForm->GetWidgets(sFieldName, &widgets);
   if (widgets.empty())
@@ -529,7 +536,8 @@
     // If there is currently no pageview associated with the page being used
     // do not create one. We may be in the process of tearing down the document
     // and creating a new pageview at this point will cause bad things.
-    CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(pPage, false);
+    CPDFSDK_PageView* pPageView =
+        m_pFormFillEnv->GetSDKDocument()->GetPageView(pPage, false);
     if (pPageView) {
 #if PDF_ENABLE_XFA
       pPageView->DeleteAnnot(pWidget);
@@ -537,7 +545,7 @@
       pPageView->UpdateRects(aRefresh);
     }
   }
-  m_pDocument->SetChangeMark();
+  m_pFormFillEnv->GetSDKDocument()->SetChangeMark();
 
   return TRUE;
 }
@@ -550,24 +558,25 @@
                             const std::vector<CJS_Value>& params,
                             CJS_Value& vRet,
                             CFX_WideString& sError) {
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
-        m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM) ||
-        m_pDocument->GetPermissions(FPDFPERM_FILL_FORM))) {
+  if (!(m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_MODIFY) ||
+        m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_ANNOT_FORM) ||
+        m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_FILL_FORM))) {
     sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
     return FALSE;
   }
 
-  CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
+  CPDFSDK_InterForm* pInterForm =
+      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
   CPDF_InterForm* pPDFForm = pInterForm->GetInterForm();
   CJS_Array aName;
 
   if (params.empty()) {
     pPDFForm->ResetForm(TRUE);
-    m_pDocument->SetChangeMark();
+    m_pFormFillEnv->GetSDKDocument()->SetChangeMark();
     return TRUE;
   }
 
@@ -594,7 +603,7 @@
 
   if (!aFields.empty()) {
     pPDFForm->ResetForm(aFields, TRUE, TRUE);
-    m_pDocument->SetChangeMark();
+    m_pFormFillEnv->GetSDKDocument()->SetChangeMark();
   }
 
   return TRUE;
@@ -624,7 +633,7 @@
     sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
@@ -659,7 +668,8 @@
     aFields.Attach(CJS_Value(pRuntime, pValue).ToV8Array(pRuntime));
   }
 
-  CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
+  CPDFSDK_InterForm* pInterForm =
+      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
   CPDF_InterForm* pPDFInterForm = pInterForm->GetInterForm();
   if (aFields.GetLength(pRuntime) == 0 && bEmpty) {
     if (pPDFInterForm->CheckRequiredFields(nullptr, true)) {
@@ -694,8 +704,8 @@
   return TRUE;
 }
 
-void Document::AttachDoc(CPDFSDK_Document* pDoc) {
-  m_pDocument.Reset(pDoc);
+void Document::SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
+  m_pFormFillEnv.Reset(pFormFillEnv);
 }
 
 FX_BOOL Document::bookmarkRoot(IJS_Context* cc,
@@ -757,7 +767,7 @@
   }
 
   pRuntime->BeginBlock();
-  CPDFSDK_FormFillEnvironment* pEnv = pRuntime->GetReaderEnv();
+  CPDFSDK_FormFillEnvironment* pEnv = pRuntime->GetFormFillEnv();
   pEnv->JS_docmailForm(nullptr, 0, bUI, cTo.c_str(), cSubject.c_str(),
                        cCc.c_str(), cBcc.c_str(), cMsg.c_str());
   pRuntime->EndBlock();
@@ -778,11 +788,12 @@
     sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
     return FALSE;
   }
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
+  CPDF_Dictionary* pDictionary =
+      m_pFormFillEnv->GetSDKDocument()->GetPDFDocument()->GetInfo();
   if (!pDictionary)
     return FALSE;
 
@@ -832,25 +843,26 @@
                                       CJS_PropValue& vp,
                                       const CFX_ByteString& propName,
                                       CFX_WideString& sError) {
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  CPDF_Dictionary* pDictionary = m_pDocument->GetPDFDocument()->GetInfo();
+  CPDF_Dictionary* pDictionary =
+      m_pFormFillEnv->GetSDKDocument()->GetPDFDocument()->GetInfo();
   if (!pDictionary)
     return FALSE;
 
   if (vp.IsGetting()) {
     vp << pDictionary->GetUnicodeTextFor(propName);
   } else {
-    if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) {
+    if (!m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_MODIFY)) {
       sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
       return FALSE;
     }
     CFX_WideString csProperty;
     vp >> csProperty;
     pDictionary->SetStringFor(propName, PDF_EncodeText(csProperty));
-    m_pDocument->SetChangeMark();
+    m_pFormFillEnv->GetSDKDocument()->SetChangeMark();
   }
   return TRUE;
 }
@@ -870,14 +882,14 @@
 FX_BOOL Document::delay(IJS_Context* cc,
                         CJS_PropValue& vp,
                         CFX_WideString& sError) {
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
   if (vp.IsGetting()) {
     vp << m_bDelay;
   } else {
-    if (!m_pDocument->GetPermissions(FPDFPERM_MODIFY)) {
+    if (!m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_MODIFY)) {
       sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
       return FALSE;
     }
@@ -887,10 +899,8 @@
     } else {
       std::list<std::unique_ptr<CJS_DelayData>> DelayDataToProcess;
       DelayDataToProcess.swap(m_DelayData);
-      for (const auto& pData : DelayDataToProcess) {
-        if (m_pDocument.Get())
-          Field::DoDelay(m_pDocument->GetEnv(), pData.get());
-      }
+      for (const auto& pData : DelayDataToProcess)
+        Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
     }
   }
   return TRUE;
@@ -923,7 +933,7 @@
 FX_BOOL Document::title(IJS_Context* cc,
                         CJS_PropValue& vp,
                         CFX_WideString& sError) {
-  if (!m_pDocument || !m_pDocument->GetUnderlyingDocument()) {
+  if (!m_pFormFillEnv || !m_pFormFillEnv->GetUnderlyingDocument()) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
@@ -937,11 +947,11 @@
     sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
     return FALSE;
   }
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  vp << m_pDocument->GetPageCount();
+  vp << m_pFormFillEnv->GetSDKDocument()->GetPageCount();
   return TRUE;
 }
 
@@ -985,11 +995,11 @@
     sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
     return FALSE;
   }
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  vp << m_pDocument->GetPath();
+  vp << m_pFormFillEnv->GetSDKDocument()->GetPath();
   return TRUE;
 }
 
@@ -1007,11 +1017,12 @@
 FX_BOOL Document::calculate(IJS_Context* cc,
                             CJS_PropValue& vp,
                             CFX_WideString& sError) {
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm();
+  CPDFSDK_InterForm* pInterForm =
+      m_pFormFillEnv->GetSDKDocument()->GetInterForm();
   if (vp.IsGetting()) {
     vp << !!pInterForm->IsCalculateEnabled();
   } else {
@@ -1029,11 +1040,11 @@
     sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
     return FALSE;
   }
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  CFX_WideString wsFilePath = m_pDocument->GetPath();
+  CFX_WideString wsFilePath = m_pFormFillEnv->GetSDKDocument()->GetPath();
   int32_t i = wsFilePath.GetLength() - 1;
   for (; i >= 0; i--) {
     if (wsFilePath.GetAt(i) == L'\\' || wsFilePath.GetAt(i) == L'/')
@@ -1054,11 +1065,11 @@
     sError = JSGetStringFromID(IDS_STRING_JSREADONLY);
     return FALSE;
   }
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  vp << app::SysPathToPDFPath(m_pDocument->GetPath());
+  vp << app::SysPathToPDFPath(m_pFormFillEnv->GetSDKDocument()->GetPath());
   return TRUE;
 }
 
@@ -1103,7 +1114,7 @@
     sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
@@ -1111,7 +1122,8 @@
   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
   int nPageNo = params[0].ToInt(pRuntime);
   CFX_WideString swAnnotName = params[1].ToCFXWideString(pRuntime);
-  CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(nPageNo);
+  CPDFSDK_PageView* pPageView =
+      m_pFormFillEnv->GetSDKDocument()->GetPageView(nPageNo);
   if (!pPageView)
     return FALSE;
 
@@ -1152,7 +1164,7 @@
                             const std::vector<CJS_Value>& params,
                             CJS_Value& vRet,
                             CFX_WideString& sError) {
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
@@ -1162,11 +1174,12 @@
   // TODO(tonikitoo): Add support supported parameters as per
   // the PDF spec.
 
-  int nPageNo = m_pDocument->GetPageCount();
+  int nPageNo = m_pFormFillEnv->GetSDKDocument()->GetPageCount();
   CJS_Array annots;
 
   for (int i = 0; i < nPageNo; ++i) {
-    CPDFSDK_PageView* pPageView = m_pDocument->GetPageView(i);
+    CPDFSDK_PageView* pPageView =
+        m_pFormFillEnv->GetSDKDocument()->GetPageView(i);
     if (!pPageView)
       return FALSE;
 
@@ -1379,17 +1392,17 @@
                                const std::vector<CJS_Value>& params,
                                CJS_Value& vRet,
                                CFX_WideString& sError) {
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) ||
-        m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM) ||
-        m_pDocument->GetPermissions(FPDFPERM_FILL_FORM))) {
+  if (!(m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_MODIFY) ||
+        m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_ANNOT_FORM) ||
+        m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_FILL_FORM))) {
     sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
     return FALSE;
   }
-  m_pDocument->GetInterForm()->OnCalculate();
+  m_pFormFillEnv->GetSDKDocument()->GetInterForm()->OnCalculate();
   return TRUE;
 }
 
@@ -1403,11 +1416,12 @@
                                  const std::vector<CJS_Value>& params,
                                  CJS_Value& vRet,
                                  CFX_WideString& sError) {
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
+  if (!m_pFormFillEnv->GetSDKDocument()->GetPermissions(
+          FPDFPERM_EXTRACT_ACCESS)) {
     sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
     return FALSE;
   }
@@ -1419,7 +1433,7 @@
   int nWordNo = params.size() > 1 ? params[1].ToInt(pRuntime) : 0;
   bool bStrip = params.size() > 2 ? params[2].ToBool(pRuntime) : true;
 
-  CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
+  CPDF_Document* pDocument = m_pFormFillEnv->GetSDKDocument()->GetPDFDocument();
   if (!pDocument)
     return FALSE;
 
@@ -1462,11 +1476,12 @@
                                       const std::vector<CJS_Value>& params,
                                       CJS_Value& vRet,
                                       CFX_WideString& sError) {
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
+  if (!m_pFormFillEnv->GetSDKDocument()->GetPermissions(
+          FPDFPERM_EXTRACT_ACCESS)) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
@@ -1477,17 +1492,18 @@
                                   const std::vector<CJS_Value>& params,
                                   CJS_Value& vRet,
                                   CFX_WideString& sError) {
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
-  if (!m_pDocument->GetPermissions(FPDFPERM_EXTRACT_ACCESS)) {
+  if (!m_pFormFillEnv->GetSDKDocument()->GetPermissions(
+          FPDFPERM_EXTRACT_ACCESS)) {
     sError = JSGetStringFromID(IDS_STRING_JSNOPERMISSION);
     return FALSE;
   }
   CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
   int nPageNo = params.size() > 0 ? params[0].ToInt(pRuntime) : 0;
-  CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
+  CPDF_Document* pDocument = m_pFormFillEnv->GetSDKDocument()->GetPDFDocument();
   if (nPageNo < 0 || nPageNo >= pDocument->GetPageCount()) {
     sError = JSGetStringFromID(IDS_STRING_JSVALUEERROR);
     return FALSE;
@@ -1667,14 +1683,14 @@
     sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR);
     return FALSE;
   }
-  if (!m_pDocument) {
+  if (!m_pFormFillEnv) {
     sError = JSGetStringFromID(IDS_STRING_JSBADOBJECT);
     return FALSE;
   }
   CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
   CFX_WideString wideName = params[0].ToCFXWideString(pRuntime);
   CFX_ByteString utf8Name = wideName.UTF8Encode();
-  CPDF_Document* pDocument = m_pDocument->GetPDFDocument();
+  CPDF_Document* pDocument = m_pFormFillEnv->GetSDKDocument()->GetPDFDocument();
   if (!pDocument)
     return FALSE;
 
@@ -1698,9 +1714,9 @@
   }
 
   pRuntime->BeginBlock();
-  CPDFSDK_FormFillEnvironment* pApp = m_pDocument->GetEnv();
-  pApp->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
-                     scrollPositionArray.get(), scrollPositionArraySize);
+  m_pFormFillEnv->DoGoToAction(dest.GetPageIndex(pDocument), dest.GetZoomMode(),
+                               scrollPositionArray.get(),
+                               scrollPositionArraySize);
   pRuntime->EndBlock();
 
   return TRUE;
@@ -1723,10 +1739,8 @@
     }
   }
 
-  for (const auto& pData : DelayDataForFieldAndControlIndex) {
-    if (m_pDocument.Get())
-      Field::DoDelay(m_pDocument->GetEnv(), pData.get());
-  }
+  for (const auto& pData : DelayDataForFieldAndControlIndex)
+    Field::DoDelay(m_pFormFillEnv.Get(), pData.get());
 }
 
 CJS_Document* Document::GetCJSDoc() const {
diff --git a/fpdfsdk/javascript/Document.h b/fpdfsdk/javascript/Document.h
index 5f33cf1..a72316c 100644
--- a/fpdfsdk/javascript/Document.h
+++ b/fpdfsdk/javascript/Document.h
@@ -13,7 +13,7 @@
 
 #include "core/fpdfapi/page/cpdf_pageobject.h"
 #include "core/fpdfapi/page/cpdf_textobject.h"
-#include "fpdfsdk/cpdfsdk_document.h"
+#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
 #include "fpdfsdk/javascript/JS_Define.h"
 
 class PrintParamsObj : public CJS_EmbedObj {
@@ -270,8 +270,10 @@
                      CFX_WideString& sError);
   FX_BOOL URL(IJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError);
 
-  void AttachDoc(CPDFSDK_Document* pDoc);
-  CPDFSDK_Document* GetReaderDoc() const { return m_pDocument.Get(); }
+  void SetFormFillEnv(CPDFSDK_FormFillEnvironment* pFormFillEnv);
+  CPDFSDK_FormFillEnvironment* GetFormFillEnv() const {
+    return m_pFormFillEnv.Get();
+  }
   void AddDelayData(CJS_DelayData* pData);
   void DoFieldDelay(const CFX_WideString& sFieldName, int nControlIndex);
   CJS_Document* GetCJSDoc() const;
@@ -286,7 +288,7 @@
                               const CFX_ByteString& propName,
                               CFX_WideString& sError);
 
-  CPDFSDK_Document::ObservedPtr m_pDocument;
+  CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv;
   CFX_WideString m_cwBaseURL;
   std::list<std::unique_ptr<CJS_DelayData>> m_DelayData;
   std::list<std::unique_ptr<IconElement>> m_IconList;
diff --git a/fpdfsdk/javascript/Field.cpp b/fpdfsdk/javascript/Field.cpp
index 51724c4..652f1e6 100644
--- a/fpdfsdk/javascript/Field.cpp
+++ b/fpdfsdk/javascript/Field.cpp
@@ -212,7 +212,7 @@
 FX_BOOL Field::AttachField(Document* pDocument,
                            const CFX_WideString& csFieldName) {
   m_pJSDoc = pDocument;
-  m_pFormFillEnv.Reset(pDocument->GetReaderDoc()->GetEnv());
+  m_pFormFillEnv.Reset(pDocument->GetFormFillEnv());
   m_bCanSet =
       m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_FILL_FORM) ||
       m_pFormFillEnv->GetSDKDocument()->GetPermissions(FPDFPERM_ANNOT_FORM) ||
diff --git a/fpdfsdk/javascript/JS_EventHandler.cpp b/fpdfsdk/javascript/JS_EventHandler.cpp
index 731d803..27223cf 100644
--- a/fpdfsdk/javascript/JS_EventHandler.cpp
+++ b/fpdfsdk/javascript/JS_EventHandler.cpp
@@ -602,9 +602,9 @@
   CJS_Document* pJSDocument =
       static_cast<CJS_Document*>(pRuntime->GetObjectPrivate(pDocObj));
   Document* pDocument = (Document*)pJSDocument->GetEmbedObject();
-  pDocument->AttachDoc(m_pTargetFormFillEnv
-                           ? m_pTargetFormFillEnv->GetSDKDocument()
-                           : m_pJSContext->GetFormFillEnv()->GetSDKDocument());
+  pDocument->SetFormFillEnv(m_pTargetFormFillEnv
+                                ? m_pTargetFormFillEnv
+                                : m_pJSContext->GetFormFillEnv());
 
   CJS_Field* pJSField =
       static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
@@ -626,9 +626,9 @@
   CJS_Document* pJSDocument =
       static_cast<CJS_Document*>(pRuntime->GetObjectPrivate(pDocObj));
   Document* pDocument = (Document*)pJSDocument->GetEmbedObject();
-  pDocument->AttachDoc(m_pTargetFormFillEnv
-                           ? m_pTargetFormFillEnv->GetSDKDocument()
-                           : m_pJSContext->GetFormFillEnv()->GetSDKDocument());
+  pDocument->SetFormFillEnv(m_pTargetFormFillEnv
+                                ? m_pTargetFormFillEnv
+                                : m_pJSContext->GetFormFillEnv());
 
   CJS_Field* pJSField =
       static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
diff --git a/fpdfsdk/javascript/JS_Runtime_Stub.cpp b/fpdfsdk/javascript/JS_Runtime_Stub.cpp
index 904ee12..e7853fb 100644
--- a/fpdfsdk/javascript/JS_Runtime_Stub.cpp
+++ b/fpdfsdk/javascript/JS_Runtime_Stub.cpp
@@ -121,7 +121,8 @@
 
 class CJS_RuntimeStub final : public IJS_Runtime {
  public:
-  CJS_RuntimeStub() : m_pDoc(nullptr) {}
+  CJS_RuntimeStub(CPDFSDK_FormFillEnvironment* pFormFillEnv)
+      : m_pFormFillEnv(pFormFillEnv) {}
   ~CJS_RuntimeStub() override {}
 
   IJS_Context* NewContext() override {
@@ -133,10 +134,9 @@
   IJS_Context* GetCurrentContext() override { return m_pContext.get(); }
   void ReleaseContext(IJS_Context* pContext) override {}
 
-  void SetReaderDocument(CPDFSDK_Document* pReaderDoc) override {
-    m_pDoc = pReaderDoc;
+  CPDFSDK_FormFillEnvironment* GetFormFillEnv() const override {
+    return m_pFormFillEnv;
   }
-  CPDFSDK_Document* GetReaderDocument() override { return m_pDoc; }
 
 #ifdef PDF_ENABLE_XFA
   FX_BOOL GetValueByName(const CFX_ByteStringC&, CFXJSE_Value*) override {
@@ -154,7 +154,7 @@
   }
 
  protected:
-  CPDFSDK_Document* m_pDoc;
+  CPDFSDK_FormFillEnvironment* m_pFormFillEnv;
   std::unique_ptr<CJS_ContextStub> m_pContext;
 };
 
@@ -165,6 +165,6 @@
 void IJS_Runtime::Destroy() {}
 
 // static
-IJS_Runtime* IJS_Runtime::Create(CPDFSDK_FormFillEnvironment* pEnv) {
-  return new CJS_RuntimeStub;
+IJS_Runtime* IJS_Runtime::Create(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
+  return new CJS_RuntimeStub(pFormFillEnv);
 }
diff --git a/fpdfsdk/javascript/app.cpp b/fpdfsdk/javascript/app.cpp
index 048bea7..b41b872 100644
--- a/fpdfsdk/javascript/app.cpp
+++ b/fpdfsdk/javascript/app.cpp
@@ -25,7 +25,7 @@
 class GlobalTimer {
  public:
   GlobalTimer(app* pObj,
-              CPDFSDK_FormFillEnvironment* pEnv,
+              CPDFSDK_FormFillEnvironment* pFormFillEnv,
               CJS_Runtime* pRuntime,
               int nType,
               const CFX_WideString& script,
@@ -55,11 +55,11 @@
   const uint32_t m_dwTimeOut;
   const CFX_WideString m_swJScript;
   CJS_Runtime::ObservedPtr m_pRuntime;
-  CPDFSDK_FormFillEnvironment* const m_pEnv;
+  CPDFSDK_FormFillEnvironment* const m_pFormFillEnv;
 };
 
 GlobalTimer::GlobalTimer(app* pObj,
-                         CPDFSDK_FormFillEnvironment* pEnv,
+                         CPDFSDK_FormFillEnvironment* pFormFillEnv,
                          CJS_Runtime* pRuntime,
                          int nType,
                          const CFX_WideString& script,
@@ -72,8 +72,8 @@
       m_dwTimeOut(dwTimeOut),
       m_swJScript(script),
       m_pRuntime(pRuntime),
-      m_pEnv(pEnv) {
-  CFX_SystemHandler* pHandler = m_pEnv->GetSysHandler();
+      m_pFormFillEnv(pFormFillEnv) {
+  CFX_SystemHandler* pHandler = m_pFormFillEnv->GetSysHandler();
   m_nTimerID = pHandler->SetTimer(dwElapse, Trigger);
   (*GetGlobalTimerMap())[m_nTimerID] = this;
 }
@@ -83,7 +83,7 @@
     return;
 
   if (GetRuntime())
-    m_pEnv->GetSysHandler()->KillTimer(m_nTimerID);
+    m_pFormFillEnv->GetSysHandler()->KillTimer(m_nTimerID);
 
   GetGlobalTimerMap()->erase(m_nTimerID);
 }
@@ -217,27 +217,16 @@
     return FALSE;
 
   CJS_Context* pContext = (CJS_Context*)cc;
-  CPDFSDK_FormFillEnvironment* pFormFillEnv = pContext->GetFormFillEnv();
   CJS_Runtime* pRuntime = pContext->GetJSRuntime();
-  CPDFSDK_Document* pCurDoc = pContext->GetJSRuntime()->GetReaderDocument();
-  CJS_Array aDocs;
-  if (CPDFSDK_Document* pDoc = pFormFillEnv->GetSDKDocument()) {
-    CJS_Document* pJSDocument = nullptr;
-    if (pDoc == pCurDoc) {
-      v8::Local<v8::Object> pObj = pRuntime->GetThisObj();
-      if (CFXJS_Engine::GetObjDefnID(pObj) == CJS_Document::g_nObjDefnID) {
-        pJSDocument =
-            static_cast<CJS_Document*>(pRuntime->GetObjectPrivate(pObj));
-      }
-    } else {
-      v8::Local<v8::Object> pObj =
-          pRuntime->NewFxDynamicObj(CJS_Document::g_nObjDefnID);
-      pJSDocument =
-          static_cast<CJS_Document*>(pRuntime->GetObjectPrivate(pObj));
-      ASSERT(pJSDocument);
-    }
-    aDocs.SetElement(pRuntime, 0, CJS_Value(pRuntime, pJSDocument));
+  CJS_Document* pJSDocument = nullptr;
+  v8::Local<v8::Object> pObj = pRuntime->GetThisObj();
+  if (CFXJS_Engine::GetObjDefnID(pObj) == CJS_Document::g_nObjDefnID) {
+    pJSDocument = static_cast<CJS_Document*>(pRuntime->GetObjectPrivate(pObj));
   }
+
+  CJS_Array aDocs;
+  aDocs.SetElement(pRuntime, 0, CJS_Value(pRuntime, pJSDocument));
+
   if (aDocs.GetLength(pRuntime) > 0)
     vp << aDocs;
   else
@@ -322,11 +311,11 @@
   if (!vp.IsGetting())
     return FALSE;
 #ifdef PDF_ENABLE_XFA
-  CPDFSDK_FormFillEnvironment* pEnv =
-      static_cast<CJS_Context*>(cc)->GetJSRuntime()->GetReaderEnv();
-  if (!pEnv)
+  CPDFSDK_FormFillEnvironment* pFormFillEnv =
+      static_cast<CJS_Context*>(cc)->GetJSRuntime()->GetFormFillEnv();
+  if (!pFormFillEnv)
     return FALSE;
-  CFX_WideString platfrom = pEnv->GetPlatform();
+  CFX_WideString platfrom = pFormFillEnv->GetPlatform();
   if (!platfrom.IsEmpty()) {
     vp << platfrom;
     return TRUE;
@@ -342,11 +331,11 @@
   if (!vp.IsGetting())
     return FALSE;
 #ifdef PDF_ENABLE_XFA
-  CPDFSDK_FormFillEnvironment* pEnv =
-      static_cast<CJS_Context*>(cc)->GetJSRuntime()->GetReaderEnv();
-  if (!pEnv)
+  CPDFSDK_FormFillEnvironment* pFormFillEnv =
+      static_cast<CJS_Context*>(cc)->GetJSRuntime()->GetFormFillEnv();
+  if (!pFormFillEnv)
     return FALSE;
-  CFX_WideString language = pEnv->GetLanguage();
+  CFX_WideString language = pFormFillEnv->GetLanguage();
   if (!language.IsEmpty()) {
     vp << language;
     return TRUE;
@@ -393,8 +382,8 @@
     return FALSE;
   }
 
-  CPDFSDK_FormFillEnvironment* pEnv = pRuntime->GetReaderEnv();
-  if (!pEnv) {
+  CPDFSDK_FormFillEnvironment* pFormFillEnv = pRuntime->GetFormFillEnv();
+  if (!pFormFillEnv) {
     vRet = CJS_Value(pRuntime, 0);
     return TRUE;
   }
@@ -434,10 +423,10 @@
     swTitle = JSGetStringFromID(IDS_STRING_JSALERT);
 
   pRuntime->BeginBlock();
-  pEnv->GetSDKDocument()->KillFocusAnnot(0);
+  pFormFillEnv->GetSDKDocument()->KillFocusAnnot(0);
 
-  vRet = CJS_Value(pRuntime, pEnv->JS_appAlert(swMsg.c_str(), swTitle.c_str(),
-                                               iType, iIcon));
+  vRet = CJS_Value(pRuntime, pFormFillEnv->JS_appAlert(
+                                 swMsg.c_str(), swTitle.c_str(), iType, iIcon));
   pRuntime->EndBlock();
   return TRUE;
 }
@@ -448,8 +437,7 @@
                   CFX_WideString& sError) {
   if (params.size() == 1) {
     CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc);
-    CPDFSDK_FormFillEnvironment* pEnv = pRuntime->GetReaderEnv();
-    pEnv->JS_appBeep(params[0].ToInt(pRuntime));
+    pRuntime->GetFormFillEnv()->JS_appBeep(params[0].ToInt(pRuntime));
     return TRUE;
   }
 
@@ -493,10 +481,9 @@
   }
 
   uint32_t dwInterval = params.size() > 1 ? params[1].ToInt(pRuntime) : 1000;
-  CPDFSDK_FormFillEnvironment* pEnv = pRuntime->GetReaderEnv();
 
-  GlobalTimer* timerRef =
-      new GlobalTimer(this, pEnv, pRuntime, 0, script, dwInterval, 0);
+  GlobalTimer* timerRef = new GlobalTimer(this, pRuntime->GetFormFillEnv(),
+                                          pRuntime, 0, script, dwInterval, 0);
   m_Timers.insert(std::unique_ptr<GlobalTimer>(timerRef));
 
   v8::Local<v8::Object> pRetObj =
@@ -527,10 +514,9 @@
   }
 
   uint32_t dwTimeOut = params.size() > 1 ? params[1].ToInt(pRuntime) : 1000;
-  CPDFSDK_FormFillEnvironment* pEnv = pRuntime->GetReaderEnv();
-
   GlobalTimer* timerRef =
-      new GlobalTimer(this, pEnv, pRuntime, 1, script, dwTimeOut, dwTimeOut);
+      new GlobalTimer(this, pRuntime->GetFormFillEnv(), pRuntime, 1, script,
+                      dwTimeOut, dwTimeOut);
   m_Timers.insert(std::unique_ptr<GlobalTimer>(timerRef));
 
   v8::Local<v8::Object> pRetObj =
diff --git a/fpdfsdk/javascript/cjs_context.cpp b/fpdfsdk/javascript/cjs_context.cpp
index d50bc1d..0a34b35 100644
--- a/fpdfsdk/javascript/cjs_context.cpp
+++ b/fpdfsdk/javascript/cjs_context.cpp
@@ -18,7 +18,7 @@
 CJS_Context::~CJS_Context() {}
 
 CPDFSDK_FormFillEnvironment* CJS_Context::GetFormFillEnv() {
-  return m_pRuntime->GetReaderEnv();
+  return m_pRuntime->GetFormFillEnv();
 }
 
 FX_BOOL CJS_Context::RunScript(const CFX_WideString& script,
diff --git a/fpdfsdk/javascript/cjs_runtime.cpp b/fpdfsdk/javascript/cjs_runtime.cpp
index af70dd1..d41726b 100644
--- a/fpdfsdk/javascript/cjs_runtime.cpp
+++ b/fpdfsdk/javascript/cjs_runtime.cpp
@@ -47,8 +47,8 @@
 }
 
 // static
-IJS_Runtime* IJS_Runtime::Create(CPDFSDK_FormFillEnvironment* pEnv) {
-  return new CJS_Runtime(pEnv);
+IJS_Runtime* IJS_Runtime::Create(CPDFSDK_FormFillEnvironment* pFormFillEnv) {
+  return new CJS_Runtime(pFormFillEnv);
 }
 
 // static
@@ -63,14 +63,13 @@
       CFXJS_Engine::CurrentEngineFromIsolate(pIsolate));
 }
 
-CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pEnv)
-    : m_pEnv(pEnv),
-      m_pDocument(nullptr),
+CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv)
+    : m_pFormFillEnv(pFormFillEnv),
       m_bBlocking(false),
       m_isolateManaged(false) {
   v8::Isolate* pIsolate = nullptr;
 #ifndef PDF_ENABLE_XFA
-  IPDF_JSPLATFORM* pPlatform = m_pEnv->GetFormFillInfo()->m_pJsPlatform;
+  IPDF_JSPLATFORM* pPlatform = m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform;
   if (pPlatform->version <= 2) {
     unsigned int embedderDataSlot = 0;
     v8::Isolate* pExternalIsolate = nullptr;
@@ -88,7 +87,8 @@
     pIsolate = CPDFXFA_App::GetInstance()->GetJSERuntime();
     SetIsolate(pIsolate);
   } else {
-    IPDF_JSPLATFORM* pPlatform = m_pEnv->GetFormFillInfo()->m_pJsPlatform;
+    IPDF_JSPLATFORM* pPlatform =
+        m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform;
     if (pPlatform->version <= 2) {
       unsigned int embedderDataSlot = 0;
       v8::Isolate* pExternalIsolate = nullptr;
@@ -122,6 +122,8 @@
   CJS_Context* pContext = (CJS_Context*)NewContext();
   InitializeEngine();
   ReleaseContext(pContext);
+
+  SetFormFillEnvToDocument();
 }
 
 CJS_Runtime::~CJS_Runtime() {
@@ -199,19 +201,12 @@
   return m_ContextArray.empty() ? nullptr : m_ContextArray.back().get();
 }
 
-void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) {
-  if (m_pDocument == pReaderDoc)
-    return;
-
+void CJS_Runtime::SetFormFillEnvToDocument() {
   v8::Isolate::Scope isolate_scope(GetIsolate());
   v8::HandleScope handle_scope(GetIsolate());
   v8::Local<v8::Context> context = NewLocalContext();
   v8::Context::Scope context_scope(context);
 
-  m_pDocument = pReaderDoc;
-  if (!pReaderDoc)
-    return;
-
   v8::Local<v8::Object> pThis = GetThisObj();
   if (pThis.IsEmpty())
     return;
@@ -228,11 +223,11 @@
   if (!pDocument)
     return;
 
-  pDocument->AttachDoc(pReaderDoc);
+  pDocument->SetFormFillEnv(m_pFormFillEnv);
 }
 
-CPDFSDK_Document* CJS_Runtime::GetReaderDocument() {
-  return m_pDocument;
+CPDFSDK_FormFillEnvironment* CJS_Runtime::GetFormFillEnv() const {
+  return m_pFormFillEnv;
 }
 
 int CJS_Runtime::ExecuteScript(const CFX_WideString& script,
diff --git a/fpdfsdk/javascript/cjs_runtime.h b/fpdfsdk/javascript/cjs_runtime.h
index 2b075af..1b004de 100644
--- a/fpdfsdk/javascript/cjs_runtime.h
+++ b/fpdfsdk/javascript/cjs_runtime.h
@@ -30,20 +30,19 @@
   static CJS_Runtime* FromContext(const IJS_Context* cc);
   static CJS_Runtime* CurrentRuntimeFromIsolate(v8::Isolate* pIsolate);
 
-  explicit CJS_Runtime(CPDFSDK_FormFillEnvironment* pApp);
+  explicit CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv);
   ~CJS_Runtime() override;
 
   // IJS_Runtime
   IJS_Context* NewContext() override;
   void ReleaseContext(IJS_Context* pContext) override;
   IJS_Context* GetCurrentContext() override;
-  void SetReaderDocument(CPDFSDK_Document* pReaderDoc) override;
-  CPDFSDK_Document* GetReaderDocument() override;
+
+  CPDFSDK_FormFillEnvironment* GetFormFillEnv() const override;
+
   int ExecuteScript(const CFX_WideString& script,
                     CFX_WideString* info) override;
 
-  CPDFSDK_FormFillEnvironment* GetReaderEnv() const { return m_pEnv; }
-
   // Returns true if the event isn't already found in the set.
   bool AddEventToSet(const FieldEvent& event);
   void RemoveEventFromSet(const FieldEvent& event);
@@ -61,10 +60,10 @@
 
  private:
   void DefineJSObjects();
+  void SetFormFillEnvToDocument();
 
   std::vector<std::unique_ptr<CJS_Context>> m_ContextArray;
-  CPDFSDK_FormFillEnvironment* const m_pEnv;
-  CPDFSDK_Document* m_pDocument;
+  CPDFSDK_FormFillEnvironment* const m_pFormFillEnv;
   bool m_bBlocking;
   bool m_isolateManaged;
   std::set<FieldEvent> m_FieldEventSet;
diff --git a/fpdfsdk/javascript/global.cpp b/fpdfsdk/javascript/global.cpp
index 78da703..3ac8c40 100644
--- a/fpdfsdk/javascript/global.cpp
+++ b/fpdfsdk/javascript/global.cpp
@@ -33,7 +33,7 @@
   CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime);
   JSGlobalAlternate* pGlobal =
       static_cast<JSGlobalAlternate*>(GetEmbedObject());
-  pGlobal->Initial(pRuntime->GetReaderEnv());
+  pGlobal->Initial(pRuntime->GetFormFillEnv());
 }
 
 JSGlobalData::JSGlobalData()
diff --git a/fpdfsdk/javascript/ijs_runtime.h b/fpdfsdk/javascript/ijs_runtime.h
index e656545..c14ed7e 100644
--- a/fpdfsdk/javascript/ijs_runtime.h
+++ b/fpdfsdk/javascript/ijs_runtime.h
@@ -15,7 +15,6 @@
 #endif  // PDF_ENABLE_XFA
 
 class CPDFSDK_FormFillEnvironment;
-class CPDFSDK_Document;
 class IJS_Context;
 
 // Owns the FJXS objects needed to actually execute JS.
@@ -23,14 +22,15 @@
  public:
   static void Initialize(unsigned int slot, void* isolate);
   static void Destroy();
-  static IJS_Runtime* Create(CPDFSDK_FormFillEnvironment* pEnv);
+  static IJS_Runtime* Create(CPDFSDK_FormFillEnvironment* pFormFillEnv);
   virtual ~IJS_Runtime() {}
 
   virtual IJS_Context* NewContext() = 0;
   virtual void ReleaseContext(IJS_Context* pContext) = 0;
   virtual IJS_Context* GetCurrentContext() = 0;
-  virtual void SetReaderDocument(CPDFSDK_Document* pReaderDoc) = 0;
-  virtual CPDFSDK_Document* GetReaderDocument() = 0;
+
+  virtual CPDFSDK_FormFillEnvironment* GetFormFillEnv() const = 0;
+
   virtual int ExecuteScript(const CFX_WideString& script,
                             CFX_WideString* info) = 0;