Use std::set<> to track active event handlers.
[pdfium.git] / fpdfsdk / src / javascript / JS_Runtime.cpp
index 7779643..1e1486d 100644 (file)
-// Copyright 2014 PDFium Authors. All rights reserved.\r
-// Use of this source code is governed by a BSD-style license that can be\r
-// found in the LICENSE file.\r
\r
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
-\r
-#include "../../include/javascript/JavaScript.h"\r
-#include "../../include/javascript/IJavaScript.h"\r
-#include "../../include/javascript/JS_EventHandler.h"\r
-#include "../../include/javascript/JS_Runtime.h"\r
-#include "../../include/javascript/JS_Context.h"\r
-#include "../../include/javascript/JS_Define.h"\r
-#include "../../include/javascript/JS_Object.h"\r
-#include "../../include/javascript/JS_Value.h"\r
-#include "../../include/javascript/Document.h"\r
-#include "../../include/javascript/app.h"\r
-#include "../../include/javascript/color.h"\r
-#include "../../include/javascript/Consts.h"\r
-#include "../../include/javascript/Document.h"\r
-#include "../../include/javascript/event.h"\r
-#include "../../include/javascript/Field.h"\r
-#include "../../include/javascript/Icon.h"\r
-#include "../../include/javascript/PublicMethods.h"\r
-#include "../../include/javascript/report.h"\r
-#include "../../include/javascript/util.h"\r
-#include "../../include/javascript/JS_GlobalData.h"\r
-#include "../../include/javascript/global.h"\r
-#include "../../include/javascript/console.h"\r
-\r
-CJS_RuntimeFactory::~CJS_RuntimeFactory()\r
-{\r
-}\r
-\r
-IFXJS_Runtime*                                 CJS_RuntimeFactory::NewJSRuntime(CPDFDoc_Environment* pApp)\r
-{\r
-       if (!m_bInit)\r
-       {\r
-               JS_Initial();\r
-               \r
-               m_bInit = TRUE;\r
-       }\r
-       return new CJS_Runtime(pApp);\r
-}\r
-void                                                   CJS_RuntimeFactory::AddRef()\r
-{\r
-       //to do.Should be implemented as atom manipulation.\r
-       m_nRef++;\r
-}\r
-void                                                   CJS_RuntimeFactory::Release()\r
-{      \r
-       if(m_bInit)\r
-       {\r
-               //to do.Should be implemented as atom manipulation.\r
-               if (--m_nRef == 0)\r
-               {\r
-                       JS_Release();\r
-                       ReleaseGlobalData();\r
-                       m_bInit = FALSE;\r
-               }\r
-       }\r
-}\r
-\r
-void                                                   CJS_RuntimeFactory::DeleteJSRuntime(IFXJS_Runtime* pRuntime)\r
-{\r
-       if(pRuntime)\r
-               delete (CJS_Runtime*)pRuntime;\r
-}\r
-\r
-CJS_GlobalData*        CJS_RuntimeFactory::NewGlobalData(CPDFDoc_Environment* pApp)\r
-{\r
-       if (m_pGlobalData)\r
-       {\r
-               m_nGlobalDataCount++;\r
-               return m_pGlobalData;\r
-       }\r
-       else\r
-       {\r
-               m_nGlobalDataCount = 1;\r
-               m_pGlobalData = new CJS_GlobalData(pApp);\r
-               return m_pGlobalData;\r
-       }\r
-}\r
-\r
-void CJS_RuntimeFactory::ReleaseGlobalData()\r
-{\r
-       m_nGlobalDataCount--;\r
-       \r
-       if (m_nGlobalDataCount <= 0)\r
-       {\r
-               delete m_pGlobalData;\r
-               m_pGlobalData = NULL;\r
-       }\r
-}\r
-\r
-/* ------------------------------ CJS_Runtime ------------------------------ */\r
-\r
-CJS_Runtime::CJS_Runtime(CPDFDoc_Environment * pApp) : \r
-       m_pApp(pApp),\r
-       m_pDocument(NULL),\r
-       m_pFieldEventPath(NULL),\r
-       m_bBlocking(FALSE),\r
-       m_bRegistered(FALSE)\r
-{\r
-       m_isolate = v8::Isolate::New();\r
-       //m_isolate->Enter();\r
-\r
-       InitJSObjects();\r
-\r
-       CJS_Context * pContext = (CJS_Context*)NewContext();\r
-       JS_InitialRuntime(*this, this, pContext, m_context);\r
-       ReleaseContext(pContext);\r
-}\r
-\r
-CJS_Runtime::~CJS_Runtime()\r
-{\r
-       for (int i=0, sz=m_ContextArray.GetSize(); i<sz; i++)\r
-               delete m_ContextArray.GetAt(i);\r
-\r
-       m_ContextArray.RemoveAll();\r
-\r
-       JS_ReleaseRuntime(*this, m_context);\r
-\r
-       RemoveEventsInLoop(m_pFieldEventPath);\r
-\r
-       m_pApp = NULL;\r
-       m_pDocument = NULL;\r
-       m_pFieldEventPath = NULL;\r
-       m_context.Reset();\r
-\r
-       //m_isolate->Exit();\r
-       m_isolate->Dispose();\r
-}\r
-\r
-FX_BOOL CJS_Runtime::InitJSObjects()\r
-{\r
-       v8::Isolate::Scope isolate_scope(GetIsolate());\r
-       v8::HandleScope handle_scope(GetIsolate());\r
-       v8::Handle<v8::Context> context = v8::Context::New(GetIsolate());\r
-       v8::Context::Scope context_scope(context);\r
-       //0 - 8\r
-       if (CJS_Border::Init(*this, JS_STATIC) < 0) return FALSE;\r
-       if (CJS_Display::Init(*this, JS_STATIC) < 0) return FALSE;\r
-       if (CJS_Font::Init(*this, JS_STATIC) < 0) return FALSE;\r
-       if (CJS_Highlight::Init(*this, JS_STATIC) < 0) return FALSE;\r
-       if (CJS_Position::Init(*this, JS_STATIC) < 0) return FALSE;\r
-       if (CJS_ScaleHow::Init(*this, JS_STATIC) < 0) return FALSE;\r
-       if (CJS_ScaleWhen::Init(*this, JS_STATIC) < 0) return FALSE;\r
-       if (CJS_Style::Init(*this, JS_STATIC) < 0) return FALSE;        \r
-       if (CJS_Zoomtype::Init(*this, JS_STATIC) < 0) return FALSE;     \r
-\r
-       //9 - 11\r
-       if (CJS_App::Init(*this, JS_STATIC) < 0) return FALSE;\r
-       if (CJS_Color::Init(*this, JS_STATIC) < 0) return FALSE;   \r
-       if (CJS_Console::Init(*this, JS_STATIC) < 0) return FALSE;\r
-\r
-       //12 - 14\r
-       if (CJS_Document::Init(*this, JS_DYNAMIC) < 0) return FALSE;  \r
-       if (CJS_Event::Init(*this, JS_STATIC) < 0) return FALSE;                \r
-       if (CJS_Field::Init(*this, JS_DYNAMIC) < 0) return FALSE;    \r
-\r
-       //15 - 17\r
-       if (CJS_Global::Init(*this, JS_STATIC) < 0) return FALSE;               \r
-       if (CJS_Icon::Init(*this, JS_DYNAMIC) < 0) return FALSE;\r
-       if (CJS_Util::Init(*this, JS_STATIC) < 0) return FALSE;\r
-\r
-       if (CJS_PublicMethods::Init(*this) < 0) return FALSE;\r
-       if (CJS_GlobalConsts::Init(*this) < 0) return FALSE;\r
-       if (CJS_GlobalArrays::Init(*this) < 0) return FALSE;\r
-\r
-       if (CJS_TimerObj::Init(*this, JS_DYNAMIC) < 0) return FALSE;\r
-       if (CJS_PrintParamsObj::Init(*this, JS_DYNAMIC) <0) return FALSE;\r
-\r
-       return TRUE;\r
-}\r
-\r
-IFXJS_Context* CJS_Runtime::NewContext()\r
-{\r
-       CJS_Context * p = new CJS_Context(this);\r
-       m_ContextArray.Add(p);\r
-       return p;\r
-}\r
-\r
-void CJS_Runtime::ReleaseContext(IFXJS_Context * pContext)\r
-{\r
-       CJS_Context* pJSContext = (CJS_Context*)pContext;\r
-\r
-       for (int i=0, sz=m_ContextArray.GetSize(); i<sz; i++)\r
-       {\r
-               if (pJSContext == m_ContextArray.GetAt(i))\r
-               {\r
-                       delete pJSContext;\r
-                       m_ContextArray.RemoveAt(i);\r
-                       break;\r
-               }\r
-       }\r
-}\r
-\r
-IFXJS_Context* CJS_Runtime::GetCurrentContext()\r
-{\r
-       if(!m_ContextArray.GetSize())\r
-               return NULL;\r
-       return m_ContextArray.GetAt(m_ContextArray.GetSize()-1);\r
-}\r
-\r
-void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc)\r
-{\r
-       if (m_pDocument != pReaderDoc)\r
-       {\r
-               v8::Isolate::Scope isolate_scope(m_isolate);\r
-               v8::HandleScope handle_scope(m_isolate);\r
-               v8::Local<v8::Context> context =v8::Local<v8::Context>::New(m_isolate, m_context);\r
-               v8::Context::Scope context_scope(context);\r
-\r
-               m_pDocument = pReaderDoc;\r
-\r
-               if (pReaderDoc)\r
-               {\r
-                       JSObject pThis = JS_GetThisObj(*this);\r
-                       if(!pThis.IsEmpty())\r
-                       {\r
-                               if (JS_GetObjDefnID(pThis) == JS_GetObjDefnID(*this, L"Document"))\r
-                               {\r
-                                       if (CJS_Document* pJSDocument = (CJS_Document*)JS_GetPrivate(pThis))\r
-                                       {\r
-                                               if (Document * pDocument = (Document*)pJSDocument->GetEmbedObject())\r
-                                                       pDocument->AttachDoc(pReaderDoc);\r
-                                       }\r
-                               }\r
-                       }\r
-                       JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"Document"));\r
-               }\r
-               else\r
-               {\r
-                       JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"app"));\r
-               }\r
-       }\r
-}\r
-\r
-FX_BOOL        CJS_Runtime::AddEventToLoop(const CFX_WideString& sTargetName, JS_EVENT_T eEventType)\r
-{\r
-       if (m_pFieldEventPath == NULL)\r
-       {\r
-               m_pFieldEventPath = new CJS_FieldEvent;\r
-               m_pFieldEventPath->sTargetName = sTargetName;\r
-               m_pFieldEventPath->eEventType = eEventType;\r
-               m_pFieldEventPath->pNext = NULL;\r
-\r
-               return TRUE;\r
-       }\r
-\r
-       //to search\r
-       CJS_FieldEvent* p = m_pFieldEventPath;\r
-       CJS_FieldEvent* pLast = m_pFieldEventPath;\r
-       while (p)\r
-       {\r
-               if (p->eEventType == eEventType && p->sTargetName == sTargetName)\r
-                       return FALSE;\r
-\r
-               pLast = p;\r
-               p = p->pNext;\r
-       }\r
-\r
-       //to add\r
-       CJS_FieldEvent* pNew = new CJS_FieldEvent;\r
-       pNew->sTargetName = sTargetName;\r
-       pNew->eEventType = eEventType;\r
-       pNew->pNext = NULL;\r
-\r
-       pLast->pNext = pNew;\r
-\r
-       return TRUE;\r
-}\r
-\r
-void CJS_Runtime::RemoveEventInLoop(const CFX_WideString& sTargetName, JS_EVENT_T eEventType)\r
-{\r
-       FX_BOOL bFind = FALSE;\r
-\r
-       CJS_FieldEvent* p = m_pFieldEventPath;\r
-       CJS_FieldEvent* pLast = NULL;\r
-       while (p)\r
-       {\r
-               if (p->eEventType == eEventType && p->sTargetName == sTargetName)\r
-               {\r
-                       bFind = TRUE;\r
-                       break;\r
-               }\r
-\r
-               pLast = p;\r
-               p = p->pNext;\r
-       }\r
-\r
-       if (bFind)\r
-       {\r
-               RemoveEventsInLoop(p);\r
-\r
-               if (p == m_pFieldEventPath)\r
-                       m_pFieldEventPath = NULL;\r
-\r
-               if (pLast)\r
-                       pLast->pNext = NULL;\r
-       }\r
-}\r
-\r
-void CJS_Runtime::RemoveEventsInLoop(CJS_FieldEvent* pStart)\r
-{\r
-       CJS_FieldEvent* p = pStart;\r
-\r
-       while (p)\r
-       {\r
-               CJS_FieldEvent* pOld = p;\r
-               p = pOld->pNext;\r
-\r
-               delete pOld;\r
-       }\r
-}\r
-\r
-v8::Handle<v8::Context>        CJS_Runtime::NewJSContext()\r
-{\r
-       return v8::Local<v8::Context>::New(m_isolate, m_context);\r
-}\r
-\r
-CFX_WideString ChangeObjName(const CFX_WideString& str)\r
-{\r
-       CFX_WideString sRet = str;\r
-       sRet.Replace((FX_LPCWSTR)L"_", (FX_LPCWSTR)L".");\r
-       return sRet;\r
-}\r
-\r
-void CJS_Runtime::GetObjectNames(CFX_WideStringArray& array)\r
-{\r
-       array.RemoveAll();\r
-\r
-       array.Add(CJS_Border::m_pClassName);\r
-       array.Add(CJS_Display::m_pClassName);\r
-       array.Add(CJS_Font::m_pClassName);\r
-       array.Add(CJS_Highlight::m_pClassName);\r
-       array.Add(CJS_Position::m_pClassName);\r
-       array.Add(CJS_ScaleHow::m_pClassName);\r
-       array.Add(CJS_ScaleWhen::m_pClassName);\r
-       array.Add(CJS_Style::m_pClassName);\r
-       array.Add(CJS_Zoomtype::m_pClassName);\r
-\r
-       array.Add(CJS_App::m_pClassName);\r
-       array.Add((FX_LPCWSTR)"this"); \r
-       array.Add(CJS_Event::m_pClassName);     \r
-\r
-       array.Add(CJS_Global::m_pClassName);    \r
-       array.Add(CJS_Util::m_pClassName);\r
-}\r
-\r
-void CJS_Runtime::GetObjectConsts(const CFX_WideString& sObjName, CFX_WideStringArray& array)\r
-{\r
-       JSConstSpec* pConsts = NULL;\r
-       int nSize = 0;\r
-\r
-       if (sObjName == CJS_Border::m_pClassName)\r
-               CJS_Border::GetConsts(pConsts, nSize);\r
-       else if (sObjName == CJS_Display::m_pClassName)\r
-               CJS_Display::GetConsts(pConsts, nSize);\r
-       else if (sObjName == CJS_Font::m_pClassName)\r
-               CJS_Font::GetConsts(pConsts, nSize);\r
-       else if (sObjName == CJS_Highlight::m_pClassName)\r
-               CJS_Highlight::GetConsts(pConsts, nSize);\r
-       else if (sObjName == CJS_Position::m_pClassName)\r
-               CJS_Position::GetConsts(pConsts, nSize);\r
-       else if (sObjName == CJS_ScaleHow::m_pClassName)\r
-               CJS_ScaleHow::GetConsts(pConsts, nSize);\r
-       else if (sObjName == CJS_ScaleWhen::m_pClassName)\r
-               CJS_ScaleWhen::GetConsts(pConsts, nSize);\r
-       else if (sObjName == CJS_Style::m_pClassName)\r
-               CJS_Style::GetConsts(pConsts, nSize);\r
-       else if (sObjName == CJS_Zoomtype::m_pClassName)\r
-               CJS_Zoomtype::GetConsts(pConsts, nSize);\r
-\r
-       else if (sObjName == CJS_App::m_pClassName)\r
-               CJS_App::GetConsts(pConsts, nSize);\r
-       else if (sObjName == CJS_Color::m_pClassName)\r
-               CJS_Color::GetConsts(pConsts, nSize);   \r
-\r
-       else if (sObjName == L"this") \r
-       {\r
-               if (GetReaderDocument())\r
-                       CJS_Document::GetConsts(pConsts, nSize);\r
-               else \r
-                       CJS_App::GetConsts(pConsts, nSize);\r
-       }\r
-\r
-       if (sObjName == CJS_Event::m_pClassName)\r
-               CJS_Event::GetConsts(pConsts, nSize);   \r
-       else if (sObjName == CJS_Field::m_pClassName)\r
-               CJS_Field::GetConsts(pConsts, nSize);   \r
-       else if (sObjName == CJS_Global::m_pClassName)\r
-               CJS_Global::GetConsts(pConsts, nSize);  \r
-       else if (sObjName == CJS_Util::m_pClassName)\r
-               CJS_Util::GetConsts(pConsts, nSize);\r
-\r
-       for (int i=0; i<nSize; i++)\r
-               array.Add(pConsts[i].pName);\r
-}\r
-\r
-void CJS_Runtime::GetObjectProps(const CFX_WideString& sObjName, CFX_WideStringArray& array)\r
-{\r
-       JSPropertySpec* pProperties = NULL;\r
-       int nSize = 0;\r
-\r
-       if (sObjName == CJS_App::m_pClassName)\r
-               CJS_App::GetProperties(pProperties, nSize);\r
-       else if (sObjName == CJS_Color::m_pClassName)\r
-               CJS_Color::GetProperties(pProperties, nSize); \r
-       else if (sObjName == L"this")\r
-       {\r
-               if (GetReaderDocument())\r
-                       CJS_Document::GetProperties(pProperties, nSize);\r
-               else    \r
-                       CJS_App::GetProperties(pProperties, nSize);\r
-       }\r
-       else if (sObjName == CJS_Event::m_pClassName)\r
-               CJS_Event::GetProperties(pProperties, nSize);   \r
-       else if (sObjName == CJS_Field::m_pClassName)\r
-               CJS_Field::GetProperties(pProperties, nSize);   \r
-       else if (sObjName == CJS_Global::m_pClassName)\r
-               CJS_Global::GetProperties(pProperties, nSize);  \r
-       else if (sObjName == CJS_Util::m_pClassName)\r
-               CJS_Util::GetProperties(pProperties, nSize);\r
-\r
-       for (int i=0; i<nSize; i++)\r
-               array.Add(pProperties[i].pName);\r
-}\r
-\r
-void CJS_Runtime::GetObjectMethods(const CFX_WideString& sObjName, CFX_WideStringArray& array)\r
-{\r
-       JSMethodSpec* pMethods = NULL;\r
-       int nSize = 0;\r
-\r
-        if (sObjName == CJS_App::m_pClassName)\r
-               CJS_App::GetMethods(pMethods, nSize);\r
-       else if (sObjName == CJS_Color::m_pClassName)\r
-               CJS_Color::GetMethods(pMethods, nSize); \r
-       else if (sObjName == L"this") \r
-       {\r
-               if (GetReaderDocument())\r
-                       CJS_Document::GetMethods(pMethods, nSize);\r
-               else    \r
-                       CJS_App::GetMethods(pMethods, nSize);\r
-       }\r
-       else if (sObjName == CJS_Event::m_pClassName)\r
-               CJS_Event::GetMethods(pMethods, nSize); \r
-       else if (sObjName == CJS_Field::m_pClassName)\r
-               CJS_Field::GetMethods(pMethods, nSize); \r
-       else if (sObjName == CJS_Global::m_pClassName)\r
-               CJS_Global::GetMethods(pMethods, nSize);        \r
-       else if (sObjName == CJS_Util::m_pClassName)\r
-               CJS_Util::GetMethods(pMethods, nSize);\r
-\r
-       for (int i=0; i<nSize; i++)\r
-               array.Add(pMethods[i].pName);\r
-}\r
-\r
-FX_BOOL  CJS_Runtime::IsEntered()\r
-{\r
-       return v8::Isolate::GetCurrent() == m_isolate;\r
-}\r
-void   CJS_Runtime::Exit()\r
-{\r
-       if(m_isolate) m_isolate->Exit();\r
-}\r
-void   CJS_Runtime::Enter()\r
-{\r
-       if(m_isolate) m_isolate->Enter();\r
-}\r
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// 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_Runtime ------------------------------ */
+
+CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
+    : m_pApp(pApp),
+      m_pDocument(NULL),
+      m_bBlocking(FALSE),
+      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;
+  }
+
+  FXJS_Initialize(embedderDataSlot);
+  DefineJSObjects();
+
+  CJS_Context* pContext = (CJS_Context*)NewContext();
+  FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context);
+  ReleaseContext(pContext);
+}
+
+CJS_Runtime::~CJS_Runtime() {
+  for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++)
+    delete m_ContextArray.GetAt(i);
+
+  m_ContextArray.RemoveAll();
+  FXJS_ReleaseRuntime(GetIsolate(), m_context);
+
+  m_pApp = NULL;
+  m_pDocument = NULL;
+  m_context.Reset();
+
+  if (m_isolateManaged)
+    m_isolate->Dispose();
+}
+
+void CJS_Runtime::DefineJSObjects() {
+  v8::Isolate::Scope isolate_scope(GetIsolate());
+  v8::HandleScope handle_scope(GetIsolate());
+  v8::Local<v8::Context> context = v8::Context::New(GetIsolate());
+  v8::Context::Scope context_scope(context);
+
+  // 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);
+
+  // ObjDefIDs 3 - 5
+  CJS_Highlight::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+  CJS_Position::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+  CJS_ScaleHow::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+
+  // ObjDefIDs 6 - 8
+  CJS_ScaleWhen::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+  CJS_Style::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+  CJS_Zoomtype::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+
+  // ObjDefIDs 9 - 11
+  CJS_App::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+  CJS_Color::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+  CJS_Console::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+
+  // ObjDefIDs 12 - 14
+  CJS_Document::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
+  CJS_Event::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+  CJS_Field::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
+
+  // ObjDefIDs 15 - 17
+  CJS_Global::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+  CJS_Icon::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
+  CJS_Util::DefineJSObjects(GetIsolate(), FXJS_STATIC);
+
+  // ObjDefIDs 18 - 20 (these can't fail, return void).
+  CJS_PublicMethods::DefineJSObjects(GetIsolate());
+  CJS_GlobalConsts::DefineJSObjects(GetIsolate());
+  CJS_GlobalArrays::DefineJSObjects(GetIsolate());
+
+  // ObjDefIDs 21 - 22.
+  CJS_TimerObj::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
+  CJS_PrintParamsObj::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
+}
+
+IFXJS_Context* CJS_Runtime::NewContext() {
+  CJS_Context* p = new CJS_Context(this);
+  m_ContextArray.Add(p);
+  return p;
+}
+
+void CJS_Runtime::ReleaseContext(IFXJS_Context* pContext) {
+  CJS_Context* pJSContext = (CJS_Context*)pContext;
+
+  for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) {
+    if (pJSContext == m_ContextArray.GetAt(i)) {
+      delete pJSContext;
+      m_ContextArray.RemoveAt(i);
+      break;
+    }
+  }
+}
+
+IFXJS_Context* CJS_Runtime::GetCurrentContext() {
+  if (!m_ContextArray.GetSize())
+    return NULL;
+  return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1);
+}
+
+void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) {
+  if (m_pDocument != pReaderDoc) {
+    v8::Isolate::Scope isolate_scope(m_isolate);
+    v8::HandleScope handle_scope(m_isolate);
+    v8::Local<v8::Context> context =
+        v8::Local<v8::Context>::New(m_isolate, m_context);
+    v8::Context::Scope context_scope(context);
+
+    m_pDocument = pReaderDoc;
+    if (pReaderDoc) {
+      v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate());
+      if (!pThis.IsEmpty()) {
+        if (FXJS_GetObjDefnID(pThis) ==
+            FXJS_GetObjDefnID(GetIsolate(), L"Document")) {
+          if (CJS_Document* pJSDocument =
+                  (CJS_Document*)FXJS_GetPrivate(GetIsolate(), pThis)) {
+            if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject())
+              pDocument->AttachDoc(pReaderDoc);
+          }
+        }
+      }
+    }
+  }
+}
+
+bool CJS_Runtime::AddEventToSet(const FieldEvent& event) {
+  return m_FieldEventSet.insert(event).second;
+}
+
+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;
+}