Revert "Fix else-after-returns throughout pdfium."
[pdfium.git] / fpdfsdk / src / javascript / JS_Runtime.cpp
index 31d61e3..34a451b 100644 (file)
@@ -1,7 +1,7 @@
 // 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"
@@ -35,7 +35,11 @@ IFXJS_Runtime*                                       CJS_RuntimeFactory::NewJSRuntime(CPDFDoc_Environment* pApp)
 {
        if (!m_bInit)
        {
-               JS_Initial();
+               unsigned int embedderDataSlot = 0;
+               if (pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) {
+                       embedderDataSlot = pApp->GetFormFillInfo()->m_pJsPlatform->m_v8EmbedderSlot;
+                }
+               JS_Initial(embedderDataSlot);
                m_bInit = TRUE;
        }
        return new CJS_Runtime(pApp);
@@ -46,7 +50,7 @@ void                                                  CJS_RuntimeFactory::AddRef()
        m_nRef++;
 }
 void                                                   CJS_RuntimeFactory::Release()
-{      
+{
        if(m_bInit)
        {
                //to do.Should be implemented as atom manipulation.
@@ -61,8 +65,7 @@ void                                                  CJS_RuntimeFactory::Release()
 
 void                                                   CJS_RuntimeFactory::DeleteJSRuntime(IFXJS_Runtime* pRuntime)
 {
-       if(pRuntime)
-               delete (CJS_Runtime*)pRuntime;
+    delete (CJS_Runtime*)pRuntime;
 }
 
 CJS_GlobalData*        CJS_RuntimeFactory::NewGlobalData(CPDFDoc_Environment* pApp)
@@ -83,7 +86,7 @@ CJS_GlobalData*       CJS_RuntimeFactory::NewGlobalData(CPDFDoc_Environment* pApp)
 void CJS_RuntimeFactory::ReleaseGlobalData()
 {
        m_nGlobalDataCount--;
-       
+
        if (m_nGlobalDataCount <= 0)
        {
                delete m_pGlobalData;
@@ -105,18 +108,23 @@ void CJS_ArrayBufferAllocator::Free(void* data, size_t length) {
 
 /* ------------------------------ CJS_Runtime ------------------------------ */
 
-CJS_Runtime::CJS_Runtime(CPDFDoc_Environment * pApp) : 
+CJS_Runtime::CJS_Runtime(CPDFDoc_Environment * pApp) :
        m_pApp(pApp),
        m_pDocument(NULL),
        m_bBlocking(FALSE),
-       m_bRegistered(FALSE),
-       m_pFieldEventPath(NULL)
+       m_pFieldEventPath(NULL),
+        m_isolate(NULL)
 {
-       m_pArrayBufferAllocator.reset(new CJS_ArrayBufferAllocator());
+       if (m_pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) {
+               m_isolate = reinterpret_cast<v8::Isolate*>(m_pApp->GetFormFillInfo()->m_pJsPlatform->m_isolate);
+       }
+       if (!m_isolate) {
+               m_pArrayBufferAllocator.reset(new CJS_ArrayBufferAllocator());
 
-       v8::Isolate::CreateParams params;
-       params.array_buffer_allocator = m_pArrayBufferAllocator.get();
-       m_isolate = v8::Isolate::New(params);
+               v8::Isolate::CreateParams params;
+               params.array_buffer_allocator = m_pArrayBufferAllocator.get();
+               m_isolate = v8::Isolate::New(params);
+        }
 
        InitJSObjects();
 
@@ -159,21 +167,21 @@ FX_BOOL CJS_Runtime::InitJSObjects()
        if (CJS_Position::Init(*this, JS_STATIC) < 0) return FALSE;
        if (CJS_ScaleHow::Init(*this, JS_STATIC) < 0) return FALSE;
        if (CJS_ScaleWhen::Init(*this, JS_STATIC) < 0) return FALSE;
-       if (CJS_Style::Init(*this, JS_STATIC) < 0) return FALSE;        
-       if (CJS_Zoomtype::Init(*this, JS_STATIC) < 0) return FALSE;     
+       if (CJS_Style::Init(*this, JS_STATIC) < 0) return FALSE;
+       if (CJS_Zoomtype::Init(*this, JS_STATIC) < 0) return FALSE;
 
        //9 - 11
        if (CJS_App::Init(*this, JS_STATIC) < 0) return FALSE;
-       if (CJS_Color::Init(*this, JS_STATIC) < 0) return FALSE;   
+       if (CJS_Color::Init(*this, JS_STATIC) < 0) return FALSE;
        if (CJS_Console::Init(*this, JS_STATIC) < 0) return FALSE;
 
        //12 - 14
-       if (CJS_Document::Init(*this, JS_DYNAMIC) < 0) return FALSE;  
-       if (CJS_Event::Init(*this, JS_STATIC) < 0) return FALSE;                
-       if (CJS_Field::Init(*this, JS_DYNAMIC) < 0) return FALSE;    
+       if (CJS_Document::Init(*this, JS_DYNAMIC) < 0) return FALSE;
+       if (CJS_Event::Init(*this, JS_STATIC) < 0) return FALSE;
+       if (CJS_Field::Init(*this, JS_DYNAMIC) < 0) return FALSE;
 
        //15 - 17
-       if (CJS_Global::Init(*this, JS_STATIC) < 0) return FALSE;               
+       if (CJS_Global::Init(*this, JS_STATIC) < 0) return FALSE;
        if (CJS_Icon::Init(*this, JS_DYNAMIC) < 0) return FALSE;
        if (CJS_Util::Init(*this, JS_STATIC) < 0) return FALSE;