Introduce kPerIsolateDataIndex and tidy JS_Define.h
authorTom Sepez <tsepez@chromium.org>
Mon, 28 Sep 2015 16:44:56 +0000 (09:44 -0700)
committerTom Sepez <tsepez@chromium.org>
Mon, 28 Sep 2015 16:44:56 +0000 (09:44 -0700)
Follow-up from https://codereview.chromium.org/1366053003/

- use kPerIsolateDataIndex rather than magic constant 1.
- make a helper function for common code in JS_Define.h
- remove dead prototypes missed in earlier CL.
- fxjs_v8 can't include generic fpdfsdk includes (layering).

R=jochen@chromium.org

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

fpdfsdk/include/javascript/JS_Define.h
fpdfsdk/include/jsapi/fxjs_v8.h
fpdfsdk/src/jsapi/fxjs_v8.cpp

index 04f5ece..0f93b0b 100644 (file)
@@ -81,12 +81,9 @@ void JSPropGetter(const char* prop_name_string,
                   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* pContext = pRuntime->GetCurrentContext();
   CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder());
   C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject());
@@ -111,12 +108,9 @@ void JSPropSetter(const char* prop_name_string,
                   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* pContext = pRuntime->GetCurrentContext();
   CJS_Object* pJSObj = (CJS_Object*)FXJS_GetPrivate(isolate, info.Holder());
   C* pObj = reinterpret_cast<C*>(pJSObj->GetEmbedObject());
@@ -155,12 +149,9 @@ void JSMethod(const char* method_name_string,
               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* cc = pRuntime->GetCurrentContext();
   CJS_Parameters parameters;
   for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) {
@@ -286,12 +277,9 @@ void JSSpecialPropGet(const char* class_name,
                       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()));
@@ -315,12 +303,9 @@ void JSSpecialPropPut(const char* class_name,
                       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()));
@@ -341,12 +326,9 @@ void JSSpecialPropDel(const char* class_name,
                       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()));
@@ -454,12 +436,9 @@ template <FX_BOOL (*F)(IFXJS_Context* cc,
 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* cc = pRuntime->GetCurrentContext();
   CJS_Parameters parameters;
   for (unsigned int i = 0; i < (unsigned int)info.Length(); i++) {
index 15800e2..60fcea6 100644 (file)
@@ -69,10 +69,6 @@ void FXJS_Release();
 // 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);
@@ -117,6 +113,7 @@ void FXJS_InitializeRuntime(v8::Isolate* pIsolate,
                             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,
index d5673e6..7b93a2a 100644 (file)
@@ -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 @@ const wchar_t kFXJSValueNameUndefined[] = L"undefined";
 
 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 @@ FXJS_PerIsolateData* FXJS_PerIsolateData::Get(v8::Isolate* pIsolate) {
       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,
@@ -268,8 +280,7 @@ void FXJS_InitializeRuntime(v8::Isolate* pIsolate,
   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) {
@@ -338,11 +349,10 @@ void FXJS_ReleaseRuntime(v8::Isolate* pIsolate,
   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,