Move some v8 objects from CJS back into FXJS

Create a new class to hold these, CFXJS_Engine (could have been
called Runtime, but there are too many "Runtimes" already). In a
subsequent patch, all the FXJS_*() functions that take an isolate
as the first argument can become methods on the engine.

CJS_ must still manage the isolates; this happens outside
the engine.

The IJS_Runtime abstraction moves up to fpdfsdk/javascript; it
remains to allow for either a real JS library or a stubb one to
be linked (for non-js builds).

Review-Url: https://codereview.chromium.org/2241483004
diff --git a/fpdfsdk/javascript/Consts.cpp b/fpdfsdk/javascript/Consts.cpp
index b71d0a3..19988a6 100644
--- a/fpdfsdk/javascript/Consts.cpp
+++ b/fpdfsdk/javascript/Consts.cpp
@@ -148,7 +148,7 @@
         (rt)->GetIsolate(), (name),                                        \
         [](const v8::FunctionCallbackInfo<v8::Value>& info) {              \
           CJS_Runtime* pLocalRuntime = static_cast<CJS_Runtime*>(          \
-              FXJS_GetRuntimeFromIsolate(info.GetIsolate()));              \
+              FXJS_GetCurrentEngineFromIsolate(info.GetIsolate()));        \
           if (pLocalRuntime)                                               \
             info.GetReturnValue().Set(pLocalRuntime->GetConstArray(name)); \
         });                                                                \
diff --git a/fpdfsdk/javascript/JS_Define.h b/fpdfsdk/javascript/JS_Define.h
index 4c24a4d..b9ae140 100644
--- a/fpdfsdk/javascript/JS_Define.h
+++ b/fpdfsdk/javascript/JS_Define.h
@@ -78,7 +78,7 @@
                   const v8::PropertyCallbackInfo<v8::Value>& info) {
   v8::Isolate* isolate = info.GetIsolate();
   CJS_Runtime* pRuntime =
-      static_cast<CJS_Runtime*>(FXJS_GetRuntimeFromIsolate(isolate));
+      static_cast<CJS_Runtime*>(FXJS_GetCurrentEngineFromIsolate(isolate));
   if (!pRuntime)
     return;
   IJS_Context* pContext = pRuntime->GetCurrentContext();
@@ -104,7 +104,7 @@
                   const v8::PropertyCallbackInfo<void>& info) {
   v8::Isolate* isolate = info.GetIsolate();
   CJS_Runtime* pRuntime =
-      static_cast<CJS_Runtime*>(FXJS_GetRuntimeFromIsolate(isolate));
+      static_cast<CJS_Runtime*>(FXJS_GetCurrentEngineFromIsolate(isolate));
   if (!pRuntime)
     return;
   IJS_Context* pContext = pRuntime->GetCurrentContext();
@@ -143,7 +143,7 @@
               const v8::FunctionCallbackInfo<v8::Value>& info) {
   v8::Isolate* isolate = info.GetIsolate();
   CJS_Runtime* pRuntime =
-      static_cast<CJS_Runtime*>(FXJS_GetRuntimeFromIsolate(isolate));
+      static_cast<CJS_Runtime*>(FXJS_GetCurrentEngineFromIsolate(isolate));
   if (!pRuntime)
     return;
   IJS_Context* pContext = pRuntime->GetCurrentContext();
@@ -245,7 +245,7 @@
   }
 
 #define DECLARE_JS_CLASS_RICH_PART()                                           \
-  static void JSConstructor(IJS_Runtime* pRuntime, v8::Local<v8::Object> obj); \
+  static void JSConstructor(CFXJS_Engine* pEngine, v8::Local<v8::Object> obj); \
   static void JSDestructor(v8::Local<v8::Object> obj);                         \
   static void DefineProps(v8::Isolate* pIsoalte);                              \
   static void DefineMethods(v8::Isolate* pIsoalte);                            \
@@ -254,12 +254,12 @@
 
 #define IMPLEMENT_JS_CLASS_RICH_PART(js_class_name, class_alternate,          \
                                      class_name)                              \
-  void js_class_name::JSConstructor(IJS_Runtime* pIRuntime,                   \
+  void js_class_name::JSConstructor(CFXJS_Engine* pEngine,                    \
                                     v8::Local<v8::Object> obj) {              \
     CJS_Object* pObj = new js_class_name(obj);                                \
     pObj->SetEmbedObject(new class_alternate(pObj));                          \
     FXJS_SetPrivate(nullptr, obj, (void*)pObj);                               \
-    pObj->InitInstance(pIRuntime);                                            \
+    pObj->InitInstance(static_cast<CJS_Runtime*>(pEngine));                   \
   }                                                                           \
   void js_class_name::JSDestructor(v8::Local<v8::Object> obj) {               \
     js_class_name* pObj = (js_class_name*)FXJS_GetPrivate(nullptr, obj);      \
@@ -366,7 +366,7 @@
                       const v8::PropertyCallbackInfo<v8::Value>& info) {
   v8::Isolate* isolate = info.GetIsolate();
   CJS_Runtime* pRuntime =
-      static_cast<CJS_Runtime*>(FXJS_GetRuntimeFromIsolate(isolate));
+      static_cast<CJS_Runtime*>(FXJS_GetCurrentEngineFromIsolate(isolate));
   if (!pRuntime)
     return;
   IJS_Context* pContext = pRuntime->GetCurrentContext();
@@ -393,7 +393,7 @@
                       const v8::PropertyCallbackInfo<v8::Value>& info) {
   v8::Isolate* isolate = info.GetIsolate();
   CJS_Runtime* pRuntime =
-      static_cast<CJS_Runtime*>(FXJS_GetRuntimeFromIsolate(isolate));
+      static_cast<CJS_Runtime*>(FXJS_GetCurrentEngineFromIsolate(isolate));
   if (!pRuntime)
     return;
   IJS_Context* pContext = pRuntime->GetCurrentContext();
@@ -416,7 +416,8 @@
                       v8::Local<v8::String> property,
                       const v8::PropertyCallbackInfo<v8::Boolean>& info) {
   v8::Isolate* isolate = info.GetIsolate();
-  IJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate);
+  CJS_Runtime* pRuntime =
+      static_cast<CJS_Runtime*>(FXJS_GetCurrentEngineFromIsolate(isolate));
   if (!pRuntime)
     return;
   IJS_Context* pContext = pRuntime->GetCurrentContext();
@@ -440,8 +441,8 @@
                        CFX_WideString&)>
 void JSGlobalFunc(const char* func_name_string,
                   const v8::FunctionCallbackInfo<v8::Value>& info) {
-  CJS_Runtime* pRuntime =
-      static_cast<CJS_Runtime*>(FXJS_GetRuntimeFromIsolate(info.GetIsolate()));
+  CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(
+      FXJS_GetCurrentEngineFromIsolate(info.GetIsolate()));
   if (!pRuntime)
     return;
   IJS_Context* pContext = pRuntime->GetCurrentContext();
diff --git a/fpdfsdk/javascript/cjs_runtime.cpp b/fpdfsdk/javascript/cjs_runtime.cpp
index 7eaf145..36d888a 100644
--- a/fpdfsdk/javascript/cjs_runtime.cpp
+++ b/fpdfsdk/javascript/cjs_runtime.cpp
@@ -58,8 +58,7 @@
 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
     : m_pApp(pApp),
       m_pDocument(nullptr),
-      m_bBlocking(FALSE),
-      m_isolate(nullptr),
+      m_bBlocking(false),
       m_isolateManaged(false) {
 #ifndef PDF_ENABLE_XFA
   IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform;
@@ -96,7 +95,7 @@
   v8::HandleScope handle_scope(isolate);
   if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) {
     CJS_Context* pContext = (CJS_Context*)NewContext();
-    FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects);
+    FXJS_InitializeEngine(GetIsolate(), this, &m_context, &m_StaticObjects);
     ReleaseContext(pContext);
     return;
   }
@@ -110,7 +109,7 @@
 #endif
 
   CJS_Context* pContext = (CJS_Context*)NewContext();
-  FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects);
+  FXJS_InitializeEngine(GetIsolate(), this, &m_context, &m_StaticObjects);
   ReleaseContext(pContext);
 }
 
@@ -120,7 +119,7 @@
 
   m_ContextArray.clear();
   m_ConstArrays.clear();
-  FXJS_ReleaseRuntime(GetIsolate(), &m_context, &m_StaticObjects);
+  FXJS_ReleaseEngine(GetIsolate(), &m_context, &m_StaticObjects);
   m_context.Reset();
   if (m_isolateManaged)
     m_isolate->Dispose();
@@ -284,7 +283,7 @@
   // Do so now.
   // TODO(tsepez): redesign PDF-side objects to not rely on v8::Context's
   // embedder data slots, and/or to always use the right context.
-  FXJS_SetRuntimeForV8Context(old_context, this);
+  FXJS_SetEngineForV8Context(old_context, this);
 
   v8::Local<v8::Value> propvalue =
       context->Global()->Get(v8::String::NewFromUtf8(
diff --git a/fpdfsdk/javascript/cjs_runtime.h b/fpdfsdk/javascript/cjs_runtime.h
index c43a34b..16432d5 100644
--- a/fpdfsdk/javascript/cjs_runtime.h
+++ b/fpdfsdk/javascript/cjs_runtime.h
@@ -20,7 +20,7 @@
 
 class CJS_Context;
 
-class CJS_Runtime : public IJS_Runtime {
+class CJS_Runtime : public IJS_Runtime, public CFXJS_Engine {
  public:
   class Observer {
    public:
@@ -77,13 +77,9 @@
   std::vector<std::unique_ptr<CJS_Context>> m_ContextArray;
   CPDFDoc_Environment* const m_pApp;
   CPDFSDK_Document* m_pDocument;
-  FX_BOOL m_bBlocking;
-  std::set<FieldEvent> m_FieldEventSet;
-  v8::Isolate* m_isolate;
+  bool m_bBlocking;
   bool m_isolateManaged;
-  v8::Global<v8::Context> m_context;
-  std::vector<v8::Global<v8::Object>*> m_StaticObjects;
-  std::map<CFX_WideString, v8::Global<v8::Array>> m_ConstArrays;
+  std::set<FieldEvent> m_FieldEventSet;
   std::set<Observer*> m_observers;
 };