More master side changes for convergence with XFA.
[pdfium.git] / fpdfsdk / src / javascript / JS_Runtime.cpp
index fe197c5..34a18dc 100644 (file)
@@ -4,28 +4,44 @@
 
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
+#include "JS_Runtime.h"
+
+#include "../../include/fsdk_mgr.h"  // For CPDFDoc_Environment.
 #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/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 ------------------------------ */
+#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(CPDFDoc_Environment* pApp)
     : m_pApp(pApp),
@@ -48,7 +64,7 @@ CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
     DefineJSObjects();
 
   CJS_Context* pContext = (CJS_Context*)NewContext();
-  FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context);
+  FXJS_InitializeRuntime(GetIsolate(), this, m_context);
   ReleaseContext(pContext);
 }
 
@@ -78,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++) {
@@ -135,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);
@@ -153,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())
@@ -166,6 +181,18 @@ void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) {
   }
 }
 
+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);
+  }
+  return nRet;
+}
+
 bool CJS_Runtime::AddEventToSet(const FieldEvent& event) {
   return m_FieldEventSet.insert(event).second;
 }
@@ -187,9 +214,3 @@ void CJS_Runtime::RemoveObserver(Observer* observer) {
   ASSERT(m_observers.find(observer) != m_observers.end());
   m_observers.erase(observer);
 }
-
-CFX_WideString ChangeObjName(const CFX_WideString& str) {
-  CFX_WideString sRet = str;
-  sRet.Replace(L"_", L".");
-  return sRet;
-}