Clean up CPDF_AnnotList.
[pdfium.git] / fpdfsdk / include / jsapi / fxjs_v8.h
index e455bfc..13fbb02 100644 (file)
@@ -4,18 +4,28 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
-// PDFium wrapper around V8 APIs. PDFium code should include this file rather
-// than including V8 headers directly.
+// FXJS_V8 is a layer that makes it easier to define native objects in V8, but
+// has no knowledge of PDF-specific native objects. It could in theory be used
+// to implement other sets of native objects.
+
+// PDFium code should include this file rather than including V8 headers
+// directly.
 
 #ifndef FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_
 #define FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_
 
 #include <v8.h>
-#include "../../../core/include/fxcrt/fx_string.h"  // For CFX_WideString
+#include "../../../core/include/fxcrt/fx_basic.h"
+
+// FXJS_V8 places no restrictions on these two classes; it merely passes them
+// on to caller-provided methods.
+class IJS_Context;  // A description of the event that caused JS execution.
+class IJS_Runtime;  // A native runtime, typically owns the v8::Context.
 
 enum FXJSOBJTYPE {
-  FXJS_DYNAMIC = 0,
-  FXJS_STATIC = 1,
+  FXJSOBJTYPE_DYNAMIC = 0,  // Created by native method and returned to JS.
+  FXJSOBJTYPE_STATIC,       // Created by init and hung off of global object.
+  FXJSOBJTYPE_GLOBAL,       // The global object itself (may only appear once).
 };
 
 struct FXJSErr {
@@ -24,6 +34,17 @@ struct FXJSErr {
   unsigned linnum;
 };
 
+class FXJS_PerIsolateData {
+ public:
+  static void SetUp(v8::Isolate* pIsolate);
+  static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate);
+
+  CFX_PtrArray m_ObjectDefnArray;
+
+ protected:
+  FXJS_PerIsolateData() {}
+};
+
 extern const wchar_t kFXJSValueNameString[];
 extern const wchar_t kFXJSValueNameNumber[];
 extern const wchar_t kFXJSValueNameBoolean[];
@@ -33,10 +54,6 @@ extern const wchar_t kFXJSValueNameFxobj[];
 extern const wchar_t kFXJSValueNameNull[];
 extern const wchar_t kFXJSValueNameUndefined[];
 
-// FXJS_V8 places no interpretation on these two classes; it merely
-// passes them on to the caller-provided FXJS_CONSTRUCTORs.
-class IFXJS_Context;
-class IFXJS_Runtime;
 
 class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
   void* Allocate(size_t length) override;
@@ -44,23 +61,25 @@ class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
   void Free(void* data, size_t length) override;
 };
 
-using FXJS_CONSTRUCTOR = void (*)(IFXJS_Context* cc,
-                                  v8::Local<v8::Object> obj,
-                                  v8::Local<v8::Object> global);
+using FXJS_CONSTRUCTOR = void (*)(IJS_Runtime* cc, v8::Local<v8::Object> obj);
 using FXJS_DESTRUCTOR = void (*)(v8::Local<v8::Object> obj);
 
 // Call before making FXJS_PrepareIsolate call.
-void FXJS_Initialize(unsigned int embedderDataSlot);
+void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate);
 void FXJS_Release();
 
+// Gets the global isolate set by FXJS_Initialize(), or makes a new one each
+// time if there is no such isolate. Returns true if a new isolate had to be
+// created.
+bool FXJS_GetIsolate(v8::Isolate** pResultIsolate);
+
+// Get the global isolate's ref count.
+size_t FXJS_GlobalIsolateRefCount();
+
 // Call before making FXJS_Define* calls. Resources allocated here are cleared
 // 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);
@@ -100,46 +119,49 @@ void FXJS_DefineGlobalConst(v8::Isolate* pIsolate,
 
 // Called after FXJS_Define* calls made.
 void FXJS_InitializeRuntime(v8::Isolate* pIsolate,
-                            IFXJS_Runtime* pFXRuntime,
-                            IFXJS_Context* context,
+                            IJS_Runtime* pIRuntime,
                             v8::Global<v8::Context>& v8PersistentContext);
 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate,
                          v8::Global<v8::Context>& v8PersistentContext);
+IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate);
 
 // Called after FXJS_InitializeRuntime call made.
 int FXJS_Execute(v8::Isolate* pIsolate,
-                 IFXJS_Context* pJSContext,
+                 IJS_Context* pJSContext,
                  const wchar_t* script,
-                 long length,
                  FXJSErr* perror);
 
 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate,
-                                           IFXJS_Context* pJSContext,
+                                           IJS_Runtime* pJSContext,
                                            int nObjDefnID);
-v8::Local<v8::Object> FXJS_GetStaticObj(v8::Isolate* pIsolate, int nObjDefnID);
 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate);
 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj);
 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj);
-int FXJS_GetObjDefnID(v8::Isolate* pIsolate, const wchar_t* pObjName);
-void FXJS_Error(v8::Isolate* isolate, const CFX_WideString& message);
 const wchar_t* FXJS_GetTypeof(v8::Local<v8::Value> pObj);
+
 void FXJS_SetPrivate(v8::Isolate* pIsolate,
                      v8::Local<v8::Object> pObj,
                      void* p);
 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj);
-void FXJS_SetPrivate(v8::Local<v8::Object> pObj, void* p);
-void* FXJS_GetPrivate(v8::Local<v8::Object> pObj);
 void FXJS_FreePrivate(void* p);
 void FXJS_FreePrivate(v8::Local<v8::Object> pObj);
+
+void FXJS_Error(v8::Isolate* isolate, const CFX_WideString& message);
 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate,
                                         const wchar_t* PropertyName,
                                         int Len = -1);
-v8::Local<v8::Value> FXJS_GetObjectValue(v8::Local<v8::Object> pObj);
+
 v8::Local<v8::Value> FXJS_GetObjectElement(v8::Isolate* pIsolate,
                                            v8::Local<v8::Object> pObj,
                                            const wchar_t* PropertyName);
 v8::Local<v8::Array> FXJS_GetObjectElementNames(v8::Isolate* pIsolate,
                                                 v8::Local<v8::Object> pObj);
+
+v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate,
+                                          v8::Local<v8::Array> pArray,
+                                          unsigned index);
+unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray);
+
 void FXJS_PutObjectString(v8::Isolate* pIsolate,
                           v8::Local<v8::Object> pObj,
                           const wchar_t* PropertyName,
@@ -171,13 +193,6 @@ unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate,
                               v8::Local<v8::Array> pArray,
                               unsigned index,
                               v8::Local<v8::Value> pValue);
-v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate,
-                                          v8::Local<v8::Array> pArray,
-                                          unsigned index);
-unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray);
-v8::Local<v8::Value> FXJS_GetListValue(v8::Isolate* pIsolate,
-                                       v8::Local<v8::Value> pList,
-                                       int index);
 
 v8::Local<v8::Array> FXJS_NewArray(v8::Isolate* pIsolate);
 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, int number);
@@ -190,12 +205,8 @@ v8::Local<v8::Value> FXJS_NewObject2(v8::Isolate* pIsolate,
                                      v8::Local<v8::Array> pObj);
 v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate,
                                     const wchar_t* string);
-v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate,
-                                    const wchar_t* string,
-                                    unsigned nLen);
 v8::Local<v8::Value> FXJS_NewNull();
 v8::Local<v8::Value> FXJS_NewDate(v8::Isolate* pIsolate, double d);
-v8::Local<v8::Value> FXJS_NewValue(v8::Isolate* pIsolate);
 
 int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);
 bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue);