Stop using some v8::Context slot to find runtime.

Instead, use the object binding's pointer.  Puts the cart back
behind the horse.

Change-Id: I4c06ae991b871c6e90b0e6c70b69886addca2354
Reviewed-on: https://pdfium-review.googlesource.com/33630
Commit-Queue: dsinclair <dsinclair@chromium.org>
Reviewed-by: dsinclair <dsinclair@chromium.org>
diff --git a/fxjs/cfxjs_engine.cpp b/fxjs/cfxjs_engine.cpp
index 107ed3a..5d0f3b0 100644
--- a/fxjs/cfxjs_engine.cpp
+++ b/fxjs/cfxjs_engine.cpp
@@ -591,15 +591,16 @@
   GetIsolate()->ThrowException(NewString(message.AsStringView()));
 }
 
+// static
 CJS_Object* CFXJS_Engine::GetObjectPrivate(v8::Local<v8::Object> pObj) {
   CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj);
   if (!pData && !pObj.IsEmpty()) {
     // It could be a global proxy object.
     v8::Local<v8::Value> v = pObj->GetPrototype();
-    v8::Local<v8::Context> context = GetIsolate()->GetCurrentContext();
     if (v->IsObject()) {
       pData = CFXJS_PerObjectData::GetFromObject(
-          v->ToObject(context).ToLocalChecked());
+          v->ToObject(v8::Isolate::GetCurrent()->GetCurrentContext())
+              .ToLocalChecked());
     }
   }
   return pData ? pData->m_pPrivate.get() : nullptr;
diff --git a/fxjs/cfxjs_engine.h b/fxjs/cfxjs_engine.h
index d1fb70c..0c9d70d 100644
--- a/fxjs/cfxjs_engine.h
+++ b/fxjs/cfxjs_engine.h
@@ -85,7 +85,7 @@
   static CFXJS_Engine* EngineFromContext(v8::Local<v8::Context> pContext);
 
   static int GetObjDefnID(v8::Local<v8::Object> pObj);
-
+  static CJS_Object* GetObjectPrivate(v8::Local<v8::Object> pObj);
   static void SetObjectPrivate(v8::Local<v8::Object> pObj,
                                std::unique_ptr<CJS_Object> p);
   static void FreeObjectPrivate(v8::Local<v8::Object> pObj);
@@ -128,9 +128,6 @@
   v8::Local<v8::Object> GetThisObj();
   v8::Local<v8::Object> NewFXJSBoundObject(int nObjDefnID,
                                            bool bStatic = false);
-  // Retrieve native object binding.
-  CJS_Object* GetObjectPrivate(v8::Local<v8::Object> pObj);
-
   void Error(const WideString& message);
 
   v8::Local<v8::Context> GetV8Context() {
diff --git a/fxjs/cjs_app.cpp b/fxjs/cjs_app.cpp
index 3f9244c..1997e9a 100644
--- a/fxjs/cjs_app.cpp
+++ b/fxjs/cjs_app.cpp
@@ -99,9 +99,10 @@
 CJS_Return CJS_App::get_active_docs(CJS_Runtime* pRuntime) {
   CJS_Document* pJSDocument = nullptr;
   v8::Local<v8::Object> pObj = pRuntime->GetThisObj();
-  if (CFXJS_Engine::GetObjDefnID(pObj) == CJS_Document::GetObjDefnID())
-    pJSDocument = static_cast<CJS_Document*>(pRuntime->GetObjectPrivate(pObj));
-
+  if (CFXJS_Engine::GetObjDefnID(pObj) == CJS_Document::GetObjDefnID()) {
+    pJSDocument =
+        static_cast<CJS_Document*>(CFXJS_Engine::GetObjectPrivate(pObj));
+  }
   v8::Local<v8::Array> aDocs = pRuntime->NewArray();
   pRuntime->PutArrayElement(
       aDocs, 0,
@@ -326,8 +327,9 @@
   if (pRetObj.IsEmpty())
     return CJS_Return(false);
 
-  CJS_TimerObj* pJS_TimerObj =
-      static_cast<CJS_TimerObj*>(pRuntime->GetObjectPrivate(pRetObj));
+  auto* pJS_TimerObj =
+      static_cast<CJS_TimerObj*>(CFXJS_Engine::GetObjectPrivate(pRetObj));
+
   pJS_TimerObj->SetTimer(pTimerRef);
   return CJS_Return(pRetObj);
 }
@@ -354,8 +356,9 @@
   if (pRetObj.IsEmpty())
     return CJS_Return(false);
 
-  CJS_TimerObj* pJS_TimerObj =
-      static_cast<CJS_TimerObj*>(pRuntime->GetObjectPrivate(pRetObj));
+  auto* pJS_TimerObj =
+      static_cast<CJS_TimerObj*>(CFXJS_Engine::GetObjectPrivate(pRetObj));
+
   pJS_TimerObj->SetTimer(pTimerRef);
   return CJS_Return(pRetObj);
 }
@@ -389,12 +392,11 @@
   if (CFXJS_Engine::GetObjDefnID(pObj) != CJS_TimerObj::GetObjDefnID())
     return;
 
-  CJS_Object* pJSObj = pRuntime->GetObjectPrivate(pObj);
+  CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(pObj);
   if (!pJSObj)
     return;
 
-  CJS_TimerObj* pJS_TimerObj = static_cast<CJS_TimerObj*>(pJSObj);
-  GlobalTimer::Cancel(pJS_TimerObj->GetTimerID());
+  GlobalTimer::Cancel(static_cast<CJS_TimerObj*>(pJSObj)->GetTimerID());
 }
 
 CJS_Return CJS_App::execMenuItem(
diff --git a/fxjs/cjs_document.cpp b/fxjs/cjs_document.cpp
index 3022519..d6dc4d6 100644
--- a/fxjs/cjs_document.cpp
+++ b/fxjs/cjs_document.cpp
@@ -254,12 +254,12 @@
   if (pFieldObj.IsEmpty())
     return CJS_Return(false);
 
-  CJS_Field* pJSField =
-      static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
-  pJSField->AttachField(this, wideName);
+  auto* pJSField =
+      static_cast<CJS_Field*>(CFXJS_Engine::GetObjectPrivate(pFieldObj));
   if (!pJSField)
     return CJS_Return(false);
 
+  pJSField->AttachField(this, wideName);
   return CJS_Return(pJSField->ToV8Object());
 }
 
@@ -362,7 +362,7 @@
       if (CFXJS_Engine::GetObjDefnID(pObj) ==
           CJS_PrintParamsObj::GetObjDefnID()) {
         v8::Local<v8::Object> pObj = pRuntime->ToObject(params[8]);
-        CJS_Object* pJSObj = pRuntime->GetObjectPrivate(pObj);
+        CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(pObj);
         if (pJSObj) {
           CJS_PrintParamsObj* printObj =
               static_cast<CJS_PrintParamsObj*>(pJSObj);
@@ -1013,8 +1013,7 @@
   CPDFSDK_AnnotIteration annotIteration(pPageView, false);
   CPDFSDK_BAAnnot* pSDKBAAnnot = nullptr;
   for (const auto& pSDKAnnotCur : annotIteration) {
-    CPDFSDK_BAAnnot* pBAAnnot =
-        static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get());
+    auto* pBAAnnot = static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get());
     if (pBAAnnot && pBAAnnot->GetAnnotName() == swAnnotName) {
       pSDKBAAnnot = pBAAnnot;
       break;
@@ -1028,8 +1027,8 @@
   if (pObj.IsEmpty())
     return CJS_Return(false);
 
-  CJS_Annot* pJS_Annot =
-      static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
+  auto* pJS_Annot =
+      static_cast<CJS_Annot*>(CFXJS_Engine::GetObjectPrivate(pObj));
   if (!pJS_Annot)
     return CJS_Return(false);
 
@@ -1063,8 +1062,8 @@
       if (pObj.IsEmpty())
         return CJS_Return(false);
 
-      CJS_Annot* pJS_Annot =
-          static_cast<CJS_Annot*>(pRuntime->GetObjectPrivate(pObj));
+      auto* pJS_Annot =
+          static_cast<CJS_Annot*>(CFXJS_Engine::GetObjectPrivate(pObj));
       pJS_Annot->SetSDKAnnot(static_cast<CPDFSDK_BAAnnot*>(pSDKAnnotCur.Get()));
       pRuntime->PutArrayElement(
           annots, i,
@@ -1119,7 +1118,7 @@
     return CJS_Return(JSGetStringFromID(JSMessage::kTypeError));
 
   v8::Local<v8::Object> pObj = pRuntime->ToObject(params[1]);
-  CJS_Object* obj = pRuntime->GetObjectPrivate(pObj);
+  CJS_Object* obj = CFXJS_Engine::GetObjectPrivate(pObj);
   if (!obj)
     return CJS_Return(JSGetStringFromID(JSMessage::kTypeError));
 
@@ -1140,8 +1139,8 @@
     if (pObj.IsEmpty())
       return CJS_Return(false);
 
-    CJS_Icon* pJS_Icon =
-        static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
+    auto* pJS_Icon =
+        static_cast<CJS_Icon*>(CFXJS_Engine::GetObjectPrivate(pObj));
     pJS_Icon->SetIconName(name);
     pRuntime->PutArrayElement(Icons, i++,
                               pJS_Icon
@@ -1172,12 +1171,12 @@
   if (pObj.IsEmpty())
     return CJS_Return(false);
 
-  CJS_Icon* pJS_Icon = static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
-  if (!pJS_Icon)
+  auto* pJSIcon = static_cast<CJS_Icon*>(CFXJS_Engine::GetObjectPrivate(pObj));
+  if (!pJSIcon)
     return CJS_Return(false);
 
-  pJS_Icon->SetIconName(*it);
-  return CJS_Return(pJS_Icon->ToV8Object());
+  pJSIcon->SetIconName(*it);
+  return CJS_Return(pJSIcon->ToV8Object());
 }
 
 CJS_Return CJS_Document::removeIcon(
diff --git a/fxjs/cjs_eventhandler.cpp b/fxjs/cjs_eventhandler.cpp
index 2f42d41..0fd330f 100644
--- a/fxjs/cjs_eventhandler.cpp
+++ b/fxjs/cjs_eventhandler.cpp
@@ -580,10 +580,11 @@
   if (pFieldObj.IsEmpty())
     return nullptr;
 
-  CJS_Document* pJSDocument =
-      static_cast<CJS_Document*>(pRuntime->GetObjectPrivate(pDocObj));
-  CJS_Field* pJSField =
-      static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
+  auto* pJSDocument =
+      static_cast<CJS_Document*>(CFXJS_Engine::GetObjectPrivate(pDocObj));
+
+  auto* pJSField =
+      static_cast<CJS_Field*>(CFXJS_Engine::GetObjectPrivate(pFieldObj));
 
   pJSDocument->SetFormFillEnv(m_pTargetFormFillEnv
                                   ? m_pTargetFormFillEnv.Get()
@@ -605,10 +606,11 @@
   if (pFieldObj.IsEmpty())
     return nullptr;
 
-  CJS_Document* pJSDocument =
-      static_cast<CJS_Document*>(pRuntime->GetObjectPrivate(pDocObj));
-  CJS_Field* pJSField =
-      static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pFieldObj));
+  auto* pJSDocument =
+      static_cast<CJS_Document*>(CFXJS_Engine::GetObjectPrivate(pDocObj));
+
+  auto* pJSField =
+      static_cast<CJS_Field*>(CFXJS_Engine::GetObjectPrivate(pFieldObj));
 
   pJSDocument->SetFormFillEnv(m_pTargetFormFillEnv
                                   ? m_pTargetFormFillEnv.Get()
diff --git a/fxjs/cjs_field.cpp b/fxjs/cjs_field.cpp
index 0a32247..cdbe0df 100644
--- a/fxjs/cjs_field.cpp
+++ b/fxjs/cjs_field.cpp
@@ -2279,10 +2279,8 @@
   if (pObj.IsEmpty())
     return CJS_Return(false);
 
-  CJS_Icon* pJS_Icon = static_cast<CJS_Icon*>(pRuntime->GetObjectPrivate(pObj));
-  if (!pJS_Icon)
-    return CJS_Return(false);
-  return CJS_Return(pJS_Icon->ToV8Object());
+  auto* pJS_Icon = static_cast<CJS_Icon*>(CFXJS_Engine::GetObjectPrivate(pObj));
+  return pJS_Icon ? CJS_Return(pJS_Icon->ToV8Object()) : CJS_Return(false);
 }
 
 CJS_Return CJS_Field::buttonImportIcon(
@@ -2399,8 +2397,8 @@
     if (pObj.IsEmpty())
       return CJS_Return(false);
 
-    CJS_Field* pJSField =
-        static_cast<CJS_Field*>(pRuntime->GetObjectPrivate(pObj));
+    auto* pJSField =
+        static_cast<CJS_Field*>(CFXJS_Engine::GetObjectPrivate(pObj));
     pJSField->AttachField(m_pJSDoc, *pStr);
     pRuntime->PutArrayElement(FormFieldArray, j++,
                               pJSField
diff --git a/fxjs/cjs_global.cpp b/fxjs/cjs_global.cpp
index 9a1f541..5834ab9 100644
--- a/fxjs/cjs_global.cpp
+++ b/fxjs/cjs_global.cpp
@@ -32,18 +32,17 @@
 void JSSpecialPropQuery(const char*,
                         v8::Local<v8::String> property,
                         const v8::PropertyCallbackInfo<v8::Integer>& info) {
-  CJS_Runtime* pRuntime =
-      CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate());
-  if (!pRuntime)
-    return;
-
-  CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder());
+  CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(info.Holder());
   if (!pJSObj)
     return;
 
-  Alt* pObj = static_cast<Alt*>(pJSObj);
-  CJS_Return result =
-      pObj->QueryProperty(PropFromV8Prop(info.GetIsolate(), property).c_str());
+  CJS_Runtime* pRuntime = pJSObj->GetRuntime();
+  if (!pRuntime)
+    return;
+
+  CJS_Return result = static_cast<Alt*>(pJSObj)->QueryProperty(
+      PropFromV8Prop(info.GetIsolate(), property).c_str());
+
   info.GetReturnValue().Set(!result.HasError() ? 4 : 0);
 }
 
@@ -51,24 +50,22 @@
 void JSSpecialPropGet(const char* class_name,
                       v8::Local<v8::String> property,
                       const v8::PropertyCallbackInfo<v8::Value>& info) {
-  CJS_Runtime* pRuntime =
-      CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate());
-  if (!pRuntime)
-    return;
-
-  CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder());
+  CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(info.Holder());
   if (!pJSObj)
     return;
 
-  Alt* pObj = static_cast<Alt*>(pJSObj);
-  CJS_Return result = pObj->GetProperty(
+  CJS_Runtime* pRuntime = pJSObj->GetRuntime();
+  if (!pRuntime)
+    return;
+
+  CJS_Return result = static_cast<Alt*>(pJSObj)->GetProperty(
       pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str());
+
   if (result.HasError()) {
     pRuntime->Error(
         JSFormatErrorString(class_name, "GetProperty", result.Error()));
     return;
   }
-
   if (result.HasReturn())
     info.GetReturnValue().Set(result.Return());
 }
@@ -78,18 +75,17 @@
                       v8::Local<v8::String> property,
                       v8::Local<v8::Value> value,
                       const v8::PropertyCallbackInfo<v8::Value>& info) {
-  CJS_Runtime* pRuntime =
-      CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate());
-  if (!pRuntime)
-    return;
-
-  CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder());
+  CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(info.Holder());
   if (!pJSObj)
     return;
 
-  Alt* pObj = static_cast<Alt*>(pJSObj);
-  CJS_Return result = pObj->SetProperty(
+  CJS_Runtime* pRuntime = pJSObj->GetRuntime();
+  if (!pRuntime)
+    return;
+
+  CJS_Return result = static_cast<Alt*>(pJSObj)->SetProperty(
       pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str(), value);
+
   if (result.HasError()) {
     pRuntime->Error(
         JSFormatErrorString(class_name, "PutProperty", result.Error()));
@@ -100,17 +96,15 @@
 void JSSpecialPropDel(const char* class_name,
                       v8::Local<v8::String> property,
                       const v8::PropertyCallbackInfo<v8::Boolean>& info) {
-  CJS_Runtime* pRuntime =
-      CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate());
-  if (!pRuntime)
-    return;
-
-  CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder());
+  CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(info.Holder());
   if (!pJSObj)
     return;
 
-  Alt* pObj = static_cast<Alt*>(pJSObj);
-  CJS_Return result = pObj->DelProperty(
+  CJS_Runtime* pRuntime = pJSObj->GetRuntime();
+  if (!pRuntime)
+    return;
+
+  CJS_Return result = static_cast<Alt*>(pJSObj)->DelProperty(
       pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str());
   if (result.HasError()) {
     // TODO(dsinclair): Should this set the pRuntime->Error result?
diff --git a/fxjs/cjs_runtime.cpp b/fxjs/cjs_runtime.cpp
index a8640ea..2896c5f 100644
--- a/fxjs/cjs_runtime.cpp
+++ b/fxjs/cjs_runtime.cpp
@@ -168,14 +168,12 @@
   v8::Context::Scope context_scope(context);
 
   v8::Local<v8::Object> pThis = GetThisObj();
-  if (pThis.IsEmpty())
+  if (pThis.IsEmpty() ||
+      CFXJS_Engine::GetObjDefnID(pThis) != CJS_Document::GetObjDefnID()) {
     return;
-
-  if (CFXJS_Engine::GetObjDefnID(pThis) != CJS_Document::GetObjDefnID())
-    return;
-
-  CJS_Document* pJSDocument =
-      static_cast<CJS_Document*>(GetObjectPrivate(pThis));
+  }
+  auto* pJSDocument =
+      static_cast<CJS_Document*>(CFXJS_Engine::GetObjectPrivate(pThis));
   if (!pJSDocument)
     return;
 
diff --git a/fxjs/js_define.h b/fxjs/js_define.h
index 325642d..93dfd49 100644
--- a/fxjs/js_define.h
+++ b/fxjs/js_define.h
@@ -63,13 +63,12 @@
                   const char* class_name_string,
                   v8::Local<v8::String> property,
                   const v8::PropertyCallbackInfo<v8::Value>& info) {
-  CJS_Runtime* pRuntime =
-      CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate());
-  if (!pRuntime)
+  CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(info.Holder());
+  if (!pJSObj)
     return;
 
-  CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder());
-  if (!pJSObj)
+  CJS_Runtime* pRuntime = pJSObj->GetRuntime();
+  if (!pRuntime)
     return;
 
   C* pObj = static_cast<C*>(pJSObj);
@@ -90,13 +89,12 @@
                   v8::Local<v8::String> property,
                   v8::Local<v8::Value> value,
                   const v8::PropertyCallbackInfo<void>& info) {
-  CJS_Runtime* pRuntime =
-      CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate());
-  if (!pRuntime)
+  CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(info.Holder());
+  if (!pJSObj)
     return;
 
-  CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder());
-  if (!pJSObj)
+  CJS_Runtime* pRuntime = pJSObj->GetRuntime();
+  if (!pRuntime)
     return;
 
   C* pObj = static_cast<C*>(pJSObj);
@@ -113,13 +111,12 @@
 void JSMethod(const char* method_name_string,
               const char* class_name_string,
               const v8::FunctionCallbackInfo<v8::Value>& info) {
-  CJS_Runtime* pRuntime =
-      CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate());
-  if (!pRuntime)
+  CJS_Object* pJSObj = CFXJS_Engine::GetObjectPrivate(info.Holder());
+  if (!pJSObj)
     return;
 
-  CJS_Object* pJSObj = pRuntime->GetObjectPrivate(info.Holder());
-  if (!pJSObj)
+  CJS_Runtime* pRuntime = pJSObj->GetRuntime();
+  if (!pRuntime)
     return;
 
   std::vector<v8::Local<v8::Value>> parameters;