Pass v8::Platform across public APIs.

FXJS will need this when it comes time to create cppgc heaps. We
don't use the platform yet, instead just preserve it in a global.

Change-Id: I0ad86b9b757ed98b17589118a319881b09e94eb2
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/70292
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp
index 7d74625..c145839 100644
--- a/fpdfsdk/fpdf_view.cpp
+++ b/fpdfsdk/fpdf_view.cpp
@@ -127,9 +127,11 @@
 #ifdef PDF_ENABLE_XFA
   BC_Library_Init();
 #endif  // PDF_ENABLE_XFA
-  if (config && config->version >= 2)
-    IJS_Runtime::Initialize(config->m_v8EmbedderSlot, config->m_pIsolate);
-
+  if (config && config->version >= 2) {
+    void* platform = config->version >= 3 ? config->m_pPlatform : nullptr;
+    IJS_Runtime::Initialize(config->m_v8EmbedderSlot, config->m_pIsolate,
+                            platform);
+  }
   g_bLibraryInitialized = true;
 }
 
diff --git a/fxjs/cfxjs_engine.cpp b/fxjs/cfxjs_engine.cpp
index b5cf4bf..b1528cd 100644
--- a/fxjs/cfxjs_engine.cpp
+++ b/fxjs/cfxjs_engine.cpp
@@ -15,6 +15,7 @@
 #include "fxjs/fxv8.h"
 #include "fxjs/xfa/cfxjse_runtimedata.h"
 #include "third_party/base/stl_util.h"
+#include "v8/include/libplatform/libplatform.h"
 #include "v8/include/v8-util.h"
 
 class CFXJS_PerObjectData;
@@ -23,6 +24,7 @@
 
 unsigned int g_embedderDataSlot = 1u;
 v8::Isolate* g_isolate = nullptr;
+v8::Platform* g_platform = nullptr;
 size_t g_isolate_ref_count = 0;
 CFX_V8ArrayBufferAllocator* g_arrayBufferAllocator = nullptr;
 v8::Global<v8::ObjectTemplate>* g_DefaultGlobalObjectTemplate = nullptr;
@@ -270,7 +272,9 @@
   return pMap ? &pMap->m_map : nullptr;
 }
 
-void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) {
+void FXJS_Initialize(unsigned int embedderDataSlot,
+                     v8::Isolate* pIsolate,
+                     v8::Platform* pPlatform) {
   if (g_isolate) {
     ASSERT(g_embedderDataSlot == embedderDataSlot);
     ASSERT(g_isolate == pIsolate);
@@ -278,6 +282,7 @@
   }
   g_embedderDataSlot = embedderDataSlot;
   g_isolate = pIsolate;
+  g_platform = pPlatform;
 }
 
 void FXJS_Release() {
diff --git a/fxjs/cfxjs_engine.h b/fxjs/cfxjs_engine.h
index 933c250..cd337fe 100644
--- a/fxjs/cfxjs_engine.h
+++ b/fxjs/cfxjs_engine.h
@@ -63,7 +63,9 @@
   explicit FXJS_PerIsolateData(v8::Isolate* pIsolate);
 };
 
-void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate);
+void FXJS_Initialize(unsigned int embedderDataSlot,
+                     v8::Isolate* pIsolate,
+                     v8::Platform* platform);
 void FXJS_Release();
 
 // Gets the global isolate set by FXJS_Initialize(), or makes a new one each
diff --git a/fxjs/cfxjs_engine_unittest.cpp b/fxjs/cfxjs_engine_unittest.cpp
index c8e844c..d91fcd1 100644
--- a/fxjs/cfxjs_engine_unittest.cpp
+++ b/fxjs/cfxjs_engine_unittest.cpp
@@ -17,7 +17,7 @@
 
   void SetUp() override {
     FXV8UnitTest::SetUp();
-    FXJS_Initialize(1, isolate());
+    FXJS_Initialize(1, isolate(), nullptr);
     engine_ = std::make_unique<CFXJS_Engine>(isolate());
   }
 
diff --git a/fxjs/cjs_runtime.cpp b/fxjs/cjs_runtime.cpp
index adf1f50..b6b5722 100644
--- a/fxjs/cjs_runtime.cpp
+++ b/fxjs/cjs_runtime.cpp
@@ -44,13 +44,15 @@
   v8::Isolate* pIsolate = nullptr;
   IPDF_JSPLATFORM* pPlatform = m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform;
   if (pPlatform->version <= 2) {
+    // Backwards compatibility - JS now initialized earlier in more modern
+    // JSPLATFORM versions.
     unsigned int embedderDataSlot = 0;
     v8::Isolate* pExternalIsolate = nullptr;
     if (pPlatform->version == 2) {
       pExternalIsolate = static_cast<v8::Isolate*>(pPlatform->m_isolate);
       embedderDataSlot = pPlatform->m_v8EmbedderSlot;
     }
-    FXJS_Initialize(embedderDataSlot, pExternalIsolate);
+    FXJS_Initialize(embedderDataSlot, pExternalIsolate, nullptr);
   }
   m_isolateManaged = FXJS_GetIsolate(&pIsolate);
   SetIsolate(pIsolate);
diff --git a/fxjs/ijs_runtime.cpp b/fxjs/ijs_runtime.cpp
index 4427a5c..f16385a 100644
--- a/fxjs/ijs_runtime.cpp
+++ b/fxjs/ijs_runtime.cpp
@@ -19,9 +19,10 @@
 }
 
 // static
-void IJS_Runtime::Initialize(unsigned int slot, void* isolate) {
+void IJS_Runtime::Initialize(unsigned int slot, void* isolate, void* platform) {
 #ifdef PDF_ENABLE_V8
-  FXJS_Initialize(slot, static_cast<v8::Isolate*>(isolate));
+  FXJS_Initialize(slot, static_cast<v8::Isolate*>(isolate),
+                  static_cast<v8::Platform*>(platform));
 #endif
 }
 
diff --git a/fxjs/ijs_runtime.h b/fxjs/ijs_runtime.h
index b61eca8..e18b8d4 100644
--- a/fxjs/ijs_runtime.h
+++ b/fxjs/ijs_runtime.h
@@ -44,7 +44,7 @@
     UnownedPtr<IJS_EventContext> m_pContext;
   };
 
-  static void Initialize(unsigned int slot, void* isolate);
+  static void Initialize(unsigned int slot, void* isolate, void* platform);
   static void Destroy();
   static std::unique_ptr<IJS_Runtime> Create(
       CPDFSDK_FormFillEnvironment* pFormFillEnv);
diff --git a/public/fpdfview.h b/public/fpdfview.h
index 079a856..debe083 100644
--- a/public/fpdfview.h
+++ b/public/fpdfview.h
@@ -232,7 +232,7 @@
 
   // Version 2.
 
-  // pointer to the v8::Isolate to use, or NULL to force PDFium to create one.
+  // Pointer to the v8::Isolate to use, or NULL to force PDFium to create one.
   void* m_pIsolate;
 
   // The embedder data slot to use in the v8::Isolate to store PDFium's
@@ -240,6 +240,12 @@
   // [0, |v8::Internals::kNumIsolateDataLots|). Note that 0 is fine for most
   // embedders.
   unsigned int m_v8EmbedderSlot;
+
+  // Version 3 - Experimantal,
+
+  // Pointer to the V8::Platform to use.
+  void* m_pPlatform;
+
 } FPDF_LIBRARY_CONFIG;
 
 // Function: FPDF_InitLibraryWithConfig
diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc
index 45500dc..6c88ed9 100644
--- a/samples/pdfium_test.cc
+++ b/samples/pdfium_test.cc
@@ -1131,10 +1131,11 @@
   }
 
   FPDF_LIBRARY_CONFIG config;
-  config.version = 2;
+  config.version = 3;
   config.m_pUserFontPaths = nullptr;
   config.m_pIsolate = nullptr;
   config.m_v8EmbedderSlot = 0;
+  config.m_pPlatform = nullptr;
 
 #ifdef PDF_ENABLE_V8
 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
@@ -1149,6 +1150,8 @@
 #else   // V8_USE_EXTERNAL_STARTUP_DATA
     platform = InitializeV8ForPDFium(options.exe_path);
 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
+    config.m_pPlatform = platform.get();
+
     v8::Isolate::CreateParams params;
     params.array_buffer_allocator = static_cast<v8::ArrayBuffer::Allocator*>(
         FPDF_GetArrayBufferAllocatorSharedInstance());