Merge to XFA: Introduce kPerIsolateDataIndex and tidy JS_Define.h

(cherry picked from commit d6d9dc6b9a45368abdf43477592ee9f6cdb6102e)
(cherry picked from commit cfff2f65aaec70247d020188bc68a0dc4fb34c3e)

Original Review URL: https://codereview.chromium.org/1372963003 .
Original Review URL: https://codereview.chromium.org/1367813003 .

TBR=jochen@chromium.org

Review URL: https://codereview.chromium.org/1370133007 .
diff --git a/DEPS b/DEPS
index 3103fff..534bb97 100644
--- a/DEPS
+++ b/DEPS
@@ -15,7 +15,7 @@
      "https://chromium.googlesource.com/external/googletest.git@8245545b6dc9c4703e6496d1efd19e975ad2b038",
 
   "v8":
-    "https://chromium.googlesource.com/v8/v8.git",
+    "https://chromium.googlesource.com/v8/v8.git@4d03c3aabad6517ff058124bb799ca6a4156e570",
 
   "v8/third_party/icu":
     "https://chromium.googlesource.com/chromium/deps/icu46",
diff --git a/fpdfsdk/include/javascript/JS_Define.h b/fpdfsdk/include/javascript/JS_Define.h
index adf69ec..e91ce03 100644
--- a/fpdfsdk/include/javascript/JS_Define.h
+++ b/fpdfsdk/include/javascript/JS_Define.h
@@ -79,12 +79,9 @@
                   v8::Local<v8::String> property,
                   const v8::PropertyCallbackInfo<v8::Value>& info) {
   v8::Isolate* isolate = info.GetIsolate();
-  v8::Local<v8::Context> context = isolate->GetCurrentContext();
-  v8::Local<v8::Value> v = context->GetEmbedderData(1);
-  if (v.IsEmpty())
+  IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate);
+  if (!pRuntime)
     return;
-  v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
-  IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
   CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder());
   C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject());
@@ -107,12 +104,9 @@
                   v8::Local<v8::Value> value,
                   const v8::PropertyCallbackInfo<void>& info) {
   v8::Isolate* isolate = info.GetIsolate();
-  v8::Local<v8::Context> context = isolate->GetCurrentContext();
-  v8::Local<v8::Value> v = context->GetEmbedderData(1);
-  if (v.IsEmpty())
+  IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate);
+  if (!pRuntime)
     return;
-  v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
-  IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
   CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder());
   C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject());
@@ -151,12 +145,9 @@
               const char* class_name_string,
               const v8::FunctionCallbackInfo<v8::Value>& info) {
   v8::Isolate* isolate = info.GetIsolate();
-  v8::Local<v8::Context> context = isolate->GetCurrentContext();
-  v8::Local<v8::Value> v = context->GetEmbedderData(1);
-  if (v.IsEmpty())
+  IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate);
+  if (!pRuntime)
     return;
-  v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
-  IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
   CJS_Parameters parameters;
   for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) {
@@ -282,12 +273,9 @@
                       v8::Local<v8::String> property,
                       const v8::PropertyCallbackInfo<v8::Value>& info) {
   v8::Isolate* isolate = info.GetIsolate();
-  v8::Local<v8::Context> context = isolate->GetCurrentContext();
-  v8::Local<v8::Value> v = context->GetEmbedderData(1);
-  if (v.IsEmpty())
+  IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate);
+  if (!pRuntime)
     return;
-  v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
-  IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
   CJS_Object* pJSObj =
       reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder()));
@@ -311,12 +299,9 @@
                       v8::Local<v8::Value> value,
                       const v8::PropertyCallbackInfo<v8::Value>& info) {
   v8::Isolate* isolate = info.GetIsolate();
-  v8::Local<v8::Context> context = isolate->GetCurrentContext();
-  v8::Local<v8::Value> v = context->GetEmbedderData(1);
-  if (v.IsEmpty())
+  IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate);
+  if (!pRuntime)
     return;
-  v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
-  IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
   CJS_Object* pJSObj =
       reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder()));
@@ -337,12 +322,9 @@
                       v8::Local<v8::String> property,
                       const v8::PropertyCallbackInfo<v8::Boolean>& info) {
   v8::Isolate* isolate = info.GetIsolate();
-  v8::Local<v8::Context> context = isolate->GetCurrentContext();
-  v8::Local<v8::Value> v = context->GetEmbedderData(1);
-  if (v.IsEmpty())
+  IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate);
+  if (!pRuntime)
     return;
-  v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
-  IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
   CJS_Object* pJSObj =
       reinterpret_cast<CJS_Object*>(FXJS_GetPrivate(isolate, info.Holder()));
@@ -448,12 +430,9 @@
 void JSGlobalFunc(const char* func_name_string,
                   const v8::FunctionCallbackInfo<v8::Value>& info) {
   v8::Isolate* isolate = info.GetIsolate();
-  v8::Local<v8::Context> context = isolate->GetCurrentContext();
-  v8::Local<v8::Value> v = context->GetEmbedderData(1);
-  if (v.IsEmpty())
+  IFXJS_Runtime* pRuntime = FXJS_GetRuntimeFromIsolate(isolate);
+  if (!pRuntime)
     return;
-  v8::Local<v8::External> field = v8::Local<v8::External>::Cast(v);
-  IFXJS_Runtime* pRuntime = (IFXJS_Runtime*)field->Value();
   IFXJS_Context* pRuntimeContext = pRuntime->GetCurrentContext();
   CJS_Parameters parameters;
   for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) {
diff --git a/fpdfsdk/include/jsapi/fxjs_v8.h b/fpdfsdk/include/jsapi/fxjs_v8.h
index 0b51332..ed65294 100644
--- a/fpdfsdk/include/jsapi/fxjs_v8.h
+++ b/fpdfsdk/include/jsapi/fxjs_v8.h
@@ -74,10 +74,6 @@
 // as part of FXJS_ReleaseRuntime().
 void FXJS_PrepareIsolate(v8::Isolate* pIsolate);
 
-// Call before making JS_PrepareIsolate call.
-void JS_Initialize(unsigned int embedderDataSlot);
-void JS_Release();
-
 // Call before making JS_Define* calls. Resources allocated here are cleared
 // as part of JS_ReleaseRuntime().
 void JS_PrepareIsolate(v8::Isolate* pIsolate);
@@ -122,6 +118,7 @@
                             v8::Global<v8::Context>& v8PersistentContext);
 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate,
                          v8::Global<v8::Context>& v8PersistentContext);
+IFXJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate);
 
 // Called after FXJS_InitializeRuntime call made.
 int FXJS_Execute(v8::Isolate* pIsolate,
diff --git a/fpdfsdk/src/jsapi/fxjs_v8.cpp b/fpdfsdk/src/jsapi/fxjs_v8.cpp
index d7868f1..7009b44 100644
--- a/fpdfsdk/src/jsapi/fxjs_v8.cpp
+++ b/fpdfsdk/src/jsapi/fxjs_v8.cpp
@@ -5,7 +5,6 @@
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
 #include "../../../core/include/fxcrt/fx_basic.h"
-#include "../../include/fsdk_define.h"
 #include "../../include/jsapi/fxjs_v8.h"
 
 const wchar_t kFXJSValueNameString[] = L"string";
@@ -19,6 +18,12 @@
 
 static unsigned int g_embedderDataSlot = 1u;
 
+// Keep this consistent with the values defined in gin/public/context_holder.h
+// (without actually requiring a dependency on gin itself for the standalone
+// embedders of PDFIum). The value we want to use is:
+//   kPerContextDataStartIndex + kEmbedderPDFium, which is 3.
+static const unsigned int kPerContextDataIndex = 3u;
+
 class CFXJS_PrivateData {
  public:
   CFXJS_PrivateData(int nObjDefID) : ObjDefID(nObjDefID), pPrivate(NULL) {}
@@ -100,6 +105,13 @@
       pIsolate->GetData(g_embedderDataSlot));
 }
 
+void FXJS_Initialize(unsigned int embedderDataSlot) {
+  g_embedderDataSlot = embedderDataSlot;
+}
+
+void FXJS_Release() {
+}
+
 int FXJS_DefineObj(v8::Isolate* pIsolate,
                    const wchar_t* sObjName,
                    FXJSOBJTYPE eObjType,
@@ -269,8 +281,7 @@
   v8::Context::Scope context_scope(v8Context);
 
   FXJS_PerIsolateData::SetUp(pIsolate);
-  v8::Local<v8::External> ptr = v8::External::New(pIsolate, pFXRuntime);
-  v8Context->SetEmbedderData(1, ptr);
+  v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime);
 
   int maxID = CFXJS_ObjDefinition::MaxID(pIsolate);
   for (int i = 0; i < maxID; ++i) {
@@ -343,11 +354,10 @@
   delete pData;
 }
 
-void FXJS_Initialize(unsigned int embedderDataSlot) {
-  g_embedderDataSlot = embedderDataSlot;
-}
-
-void FXJS_Release() {
+IFXJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate) {
+  v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
+  return static_cast<IFXJS_Runtime*>(
+      context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex));
 }
 
 int FXJS_Execute(v8::Isolate* pIsolate,
diff --git a/testing/resources/javascript/app_props_expected.txt b/testing/resources/javascript/app_props_expected.txt
index 6e78ba5..a17e0f8 100644
--- a/testing/resources/javascript/app_props_expected.txt
+++ b/testing/resources/javascript/app_props_expected.txt
@@ -1,5 +1,5 @@
 Alert: *** Getting properties ***
-Alert: app.activeDocs is object [object Object]
+Alert: app.activeDocs is object [object global]
 Alert: app.calculate is boolean true
 Alert: app.formsVersion is number 7
 Alert: ERROR: app.fs: 
@@ -25,7 +25,7 @@
 Alert: ERROR: app.viewerVariation: 
 Alert: ERROR: app.viewerVersion: 
 Alert: *** Getting properties ***
-Alert: app.activeDocs is object [object Object]
+Alert: app.activeDocs is object [object global]
 Alert: app.calculate is boolean true
 Alert: app.formsVersion is number 7
 Alert: ERROR: app.fs: