More master side changes for convergence with XFA.
[pdfium.git] / fpdfsdk / src / javascript / JS_Runtime.cpp
index edc8c25..34a18dc 100644 (file)
@@ -4,94 +4,82 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
-#include "../../include/javascript/JavaScript.h"
-#include "../../include/javascript/IJavaScript.h"
-#include "../../include/javascript/JS_EventHandler.h"
-#include "../../include/javascript/JS_Runtime.h"
-#include "../../include/javascript/JS_Context.h"
-#include "../../include/javascript/JS_Define.h"
-#include "../../include/javascript/JS_Object.h"
-#include "../../include/javascript/JS_Value.h"
-#include "../../include/javascript/Document.h"
-#include "../../include/javascript/app.h"
-#include "../../include/javascript/color.h"
-#include "../../include/javascript/Consts.h"
-#include "../../include/javascript/Document.h"
-#include "../../include/javascript/event.h"
-#include "../../include/javascript/Field.h"
-#include "../../include/javascript/Icon.h"
-#include "../../include/javascript/PublicMethods.h"
-#include "../../include/javascript/report.h"
-#include "../../include/javascript/util.h"
-#include "../../include/javascript/JS_GlobalData.h"
-#include "../../include/javascript/global.h"
-#include "../../include/javascript/console.h"
-
-CJS_RuntimeFactory::~CJS_RuntimeFactory() {}
-
-IFXJS_Runtime* CJS_RuntimeFactory::NewJSRuntime(CPDFDoc_Environment* pApp) {
-  m_bInit = true;
-  return new CJS_Runtime(pApp);
-}
-void CJS_RuntimeFactory::AddRef() {
-  m_nRef++;
-}
-void CJS_RuntimeFactory::Release() {
-  if (m_bInit) {
-    if (--m_nRef == 0) {
-      FXJS_Release();
-      m_bInit = FALSE;
-    }
-  }
-}
+#include "JS_Runtime.h"
 
-void CJS_RuntimeFactory::DeleteJSRuntime(IFXJS_Runtime* pRuntime) {
-  delete (CJS_Runtime*)pRuntime;
+#include "../../include/fsdk_mgr.h"  // For CPDFDoc_Environment.
+#include "../../include/javascript/IJavaScript.h"
+#include "Consts.h"
+#include "Document.h"
+#include "Field.h"
+#include "Icon.h"
+#include "JS_Context.h"
+#include "JS_Define.h"
+#include "JS_EventHandler.h"
+#include "JS_GlobalData.h"
+#include "JS_Object.h"
+#include "JS_Value.h"
+#include "PublicMethods.h"
+#include "app.h"
+#include "color.h"
+#include "console.h"
+#include "event.h"
+#include "global.h"
+#include "report.h"
+#include "util.h"
+
+// static
+void IJS_Runtime::Initialize(unsigned int slot, void* isolate) {
+  FXJS_Initialize(slot, reinterpret_cast<v8::Isolate*>(isolate));
+}
+
+// static
+IJS_Runtime* IJS_Runtime::Create(CPDFDoc_Environment* pEnv) {
+  return new CJS_Runtime(pEnv);
+}
+
+// static
+CJS_Runtime* CJS_Runtime::FromContext(const IJS_Context* cc) {
+  const CJS_Context* pContext = static_cast<const CJS_Context*>(cc);
+  return pContext->GetJSRuntime();
 }
 
-/* ------------------------------ CJS_Runtime ------------------------------ */
-
 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
     : m_pApp(pApp),
       m_pDocument(NULL),
       m_bBlocking(FALSE),
-      m_pFieldEventPath(NULL),
       m_isolate(NULL),
       m_isolateManaged(false) {
-  unsigned int embedderDataSlot = 0;
-  if (m_pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) {
-    m_isolate = reinterpret_cast<v8::Isolate*>(
-        m_pApp->GetFormFillInfo()->m_pJsPlatform->m_isolate);
-    embedderDataSlot = pApp->GetFormFillInfo()->m_pJsPlatform->m_v8EmbedderSlot;
-  }
-  if (!m_isolate) {
-    m_pArrayBufferAllocator.reset(new FXJS_ArrayBufferAllocator());
-
-    v8::Isolate::CreateParams params;
-    params.array_buffer_allocator = m_pArrayBufferAllocator.get();
-    m_isolate = v8::Isolate::New(params);
-    m_isolateManaged = true;
+  IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform;
+  if (pPlatform->version <= 2) {
+    unsigned int embedderDataSlot = 0;
+    v8::Isolate* pExternalIsolate = nullptr;
+    if (pPlatform->version == 2) {
+      pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate);
+      embedderDataSlot = pPlatform->m_v8EmbedderSlot;
+    }
+    FXJS_Initialize(embedderDataSlot, pExternalIsolate);
   }
-
-  FXJS_Initialize(embedderDataSlot);
-  DefineJSObjects();
+  m_isolateManaged = FXJS_GetIsolate(&m_isolate);
+  if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0)
+    DefineJSObjects();
 
   CJS_Context* pContext = (CJS_Context*)NewContext();
-  FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context);
+  FXJS_InitializeRuntime(GetIsolate(), this, m_context);
   ReleaseContext(pContext);
 }
 
 CJS_Runtime::~CJS_Runtime() {
+  for (auto* obs : m_observers)
+    obs->OnDestroyed();
+
   for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++)
     delete m_ContextArray.GetAt(i);
 
   m_ContextArray.RemoveAll();
   FXJS_ReleaseRuntime(GetIsolate(), m_context);
-  RemoveEventsInLoop(m_pFieldEventPath);
 
   m_pApp = NULL;
   m_pDocument = NULL;
-  m_pFieldEventPath = NULL;
   m_context.Reset();
 
   if (m_isolateManaged)
@@ -106,52 +94,52 @@ void CJS_Runtime::DefineJSObjects() {
 
   // The call order determines the "ObjDefID" assigned to each class.
   // ObjDefIDs 0 - 2
-  CJS_Border::DefineJSObjects(GetIsolate(), FXJS_STATIC);
-  CJS_Display::DefineJSObjects(GetIsolate(), FXJS_STATIC);
-  CJS_Font::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+  CJS_Border::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
+  CJS_Display::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
+  CJS_Font::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
 
   // ObjDefIDs 3 - 5
-  CJS_Highlight::DefineJSObjects(GetIsolate(), FXJS_STATIC);
-  CJS_Position::DefineJSObjects(GetIsolate(), FXJS_STATIC);
-  CJS_ScaleHow::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+  CJS_Highlight::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
+  CJS_Position::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
+  CJS_ScaleHow::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
 
   // ObjDefIDs 6 - 8
-  CJS_ScaleWhen::DefineJSObjects(GetIsolate(), FXJS_STATIC);
-  CJS_Style::DefineJSObjects(GetIsolate(), FXJS_STATIC);
-  CJS_Zoomtype::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+  CJS_ScaleWhen::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
+  CJS_Style::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
+  CJS_Zoomtype::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
 
   // ObjDefIDs 9 - 11
-  CJS_App::DefineJSObjects(GetIsolate(), FXJS_STATIC);
-  CJS_Color::DefineJSObjects(GetIsolate(), FXJS_STATIC);
-  CJS_Console::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+  CJS_App::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
+  CJS_Color::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
+  CJS_Console::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
 
   // ObjDefIDs 12 - 14
-  CJS_Document::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
-  CJS_Event::DefineJSObjects(GetIsolate(), FXJS_STATIC);
-  CJS_Field::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
+  CJS_Document::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_GLOBAL);
+  CJS_Event::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
+  CJS_Field::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
 
   // ObjDefIDs 15 - 17
-  CJS_Global::DefineJSObjects(GetIsolate(), FXJS_STATIC);
-  CJS_Icon::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
-  CJS_Util::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+  CJS_Global::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
+  CJS_Icon::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
+  CJS_Util::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
 
   // ObjDefIDs 18 - 20 (these can't fail, return void).
   CJS_PublicMethods::DefineJSObjects(GetIsolate());
-  CJS_GlobalConsts::DefineJSObjects(GetIsolate());
-  CJS_GlobalArrays::DefineJSObjects(GetIsolate());
+  CJS_GlobalConsts::DefineJSObjects(this);
+  CJS_GlobalArrays::DefineJSObjects(this);
 
   // ObjDefIDs 21 - 22.
-  CJS_TimerObj::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
-  CJS_PrintParamsObj::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
+  CJS_TimerObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
+  CJS_PrintParamsObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
 }
 
-IFXJS_Context* CJS_Runtime::NewContext() {
+IJS_Context* CJS_Runtime::NewContext() {
   CJS_Context* p = new CJS_Context(this);
   m_ContextArray.Add(p);
   return p;
 }
 
-void CJS_Runtime::ReleaseContext(IFXJS_Context* pContext) {
+void CJS_Runtime::ReleaseContext(IJS_Context* pContext) {
   CJS_Context* pJSContext = (CJS_Context*)pContext;
 
   for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) {
@@ -163,7 +151,7 @@ void CJS_Runtime::ReleaseContext(IFXJS_Context* pContext) {
   }
 }
 
-IFXJS_Context* CJS_Runtime::GetCurrentContext() {
+IJS_Context* CJS_Runtime::GetCurrentContext() {
   if (!m_ContextArray.GetSize())
     return NULL;
   return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1);
@@ -181,8 +169,7 @@ void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) {
     if (pReaderDoc) {
       v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate());
       if (!pThis.IsEmpty()) {
-        if (FXJS_GetObjDefnID(pThis) ==
-            FXJS_GetObjDefnID(GetIsolate(), L"Document")) {
+        if (FXJS_GetObjDefnID(pThis) == CJS_Document::g_nObjDefnID) {
           if (CJS_Document* pJSDocument =
                   (CJS_Document*)FXJS_GetPrivate(GetIsolate(), pThis)) {
             if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject())
@@ -194,83 +181,36 @@ void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) {
   }
 }
 
-FX_BOOL CJS_Runtime::AddEventToLoop(const CFX_WideString& sTargetName,
-                                    JS_EVENT_T eEventType) {
-  if (m_pFieldEventPath == NULL) {
-    m_pFieldEventPath = new CJS_FieldEvent;
-    m_pFieldEventPath->sTargetName = sTargetName;
-    m_pFieldEventPath->eEventType = eEventType;
-    m_pFieldEventPath->pNext = NULL;
-
-    return TRUE;
-  }
-
-  // to search
-  CJS_FieldEvent* p = m_pFieldEventPath;
-  CJS_FieldEvent* pLast = m_pFieldEventPath;
-  while (p) {
-    if (p->eEventType == eEventType && p->sTargetName == sTargetName)
-      return FALSE;
-
-    pLast = p;
-    p = p->pNext;
+int CJS_Runtime::Execute(IJS_Context* cc,
+                         const wchar_t* script,
+                         CFX_WideString* info) {
+  FXJSErr error = {};
+  int nRet = FXJS_Execute(m_isolate, cc, script, &error);
+  if (nRet < 0) {
+    info->Format(L"[ Line: %05d { %s } ] : %s", error.linnum - 1, error.srcline,
+                 error.message);
   }
-
-  // to add
-  CJS_FieldEvent* pNew = new CJS_FieldEvent;
-  pNew->sTargetName = sTargetName;
-  pNew->eEventType = eEventType;
-  pNew->pNext = NULL;
-
-  pLast->pNext = pNew;
-
-  return TRUE;
+  return nRet;
 }
 
-void CJS_Runtime::RemoveEventInLoop(const CFX_WideString& sTargetName,
-                                    JS_EVENT_T eEventType) {
-  FX_BOOL bFind = FALSE;
-
-  CJS_FieldEvent* p = m_pFieldEventPath;
-  CJS_FieldEvent* pLast = NULL;
-  while (p) {
-    if (p->eEventType == eEventType && p->sTargetName == sTargetName) {
-      bFind = TRUE;
-      break;
-    }
-
-    pLast = p;
-    p = p->pNext;
-  }
-
-  if (bFind) {
-    RemoveEventsInLoop(p);
-
-    if (p == m_pFieldEventPath)
-      m_pFieldEventPath = NULL;
-
-    if (pLast)
-      pLast->pNext = NULL;
-  }
+bool CJS_Runtime::AddEventToSet(const FieldEvent& event) {
+  return m_FieldEventSet.insert(event).second;
 }
 
-void CJS_Runtime::RemoveEventsInLoop(CJS_FieldEvent* pStart) {
-  CJS_FieldEvent* p = pStart;
-
-  while (p) {
-    CJS_FieldEvent* pOld = p;
-    p = pOld->pNext;
-
-    delete pOld;
-  }
+void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) {
+  m_FieldEventSet.erase(event);
 }
 
 v8::Local<v8::Context> CJS_Runtime::NewJSContext() {
   return v8::Local<v8::Context>::New(m_isolate, m_context);
 }
 
-CFX_WideString ChangeObjName(const CFX_WideString& str) {
-  CFX_WideString sRet = str;
-  sRet.Replace(L"_", L".");
-  return sRet;
+void CJS_Runtime::AddObserver(Observer* observer) {
+  ASSERT(m_observers.find(observer) == m_observers.end());
+  m_observers.insert(observer);
+}
+
+void CJS_Runtime::RemoveObserver(Observer* observer) {
+  ASSERT(m_observers.find(observer) != m_observers.end());
+  m_observers.erase(observer);
 }