Merge to XFA: Fix V8 array buffer allocator.
[pdfium.git] / fpdfsdk / src / javascript / JS_Runtime.cpp
index 543540b..2ad5667 100644 (file)
@@ -26,8 +26,8 @@
 #include "../../include/javascript/JS_GlobalData.h"
 #include "../../include/javascript/global.h"
 #include "../../include/javascript/console.h"
-
-#include <libplatform/libplatform.h>
+#include "../../include/fpdfxfa/fpdfxfa_app.h"
+#include "../../../xfa/src/fxjse/src/value.h"
 
 CJS_RuntimeFactory::~CJS_RuntimeFactory()
 {
@@ -38,9 +38,6 @@ IFXJS_Runtime*                                        CJS_RuntimeFactory::NewJSRuntime(CPDFDoc_Environment* pApp)
        if (!m_bInit)
        {
                JS_Initial();
-                m_platform = v8::platform::CreateDefaultPlatform();
-                v8::V8::InitializePlatform(m_platform);
-               
                m_bInit = TRUE;
        }
        return new CJS_Runtime(pApp);
@@ -59,9 +56,6 @@ void                                                  CJS_RuntimeFactory::Release()
                {
                        JS_Release();
                        ReleaseGlobalData();
-                        v8::V8::ShutdownPlatform();
-                        delete m_platform;
-                        m_platform = NULL;
                        m_bInit = FALSE;
                }
        }
@@ -99,17 +93,46 @@ void CJS_RuntimeFactory::ReleaseGlobalData()
        }
 }
 
-/* ------------------------------ CJS_Runtime ------------------------------ */
+void* CJS_ArrayBufferAllocator::Allocate(size_t length) {
+    return calloc(1, length);
+}
+
+void* CJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) {
+    return malloc(length);
+}
 
-CJS_Runtime::CJS_Runtime(CPDFDoc_Environment * pApp) : 
+void CJS_ArrayBufferAllocator::Free(void* data, size_t length) {
+    free(data);
+}
+
+/* ------------------------------ CJS_Runtime ------------------------------ */
+extern v8::Persistent<v8::ObjectTemplate>& _getGlobalObjectTemplate(IJS_Runtime* pJSRuntime);
+CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) :
        m_pApp(pApp),
        m_pDocument(NULL),
-       m_pFieldEventPath(NULL),
        m_bBlocking(FALSE),
-       m_bRegistered(FALSE)
+       m_bRegistered(FALSE),
+       m_pFieldEventPath(NULL)
 {
-       m_isolate = v8::Isolate::New();
-       //m_isolate->Enter();
+       if (FPDFXFA_GetApp()->GetJSERuntime()) {
+               m_isolate = (v8::Isolate*)FPDFXFA_GetApp()->GetJSERuntime();
+       } else {
+               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* isolate = m_isolate;
+       v8::Isolate::Scope isolate_scope(isolate);
+       v8::Locker locker(isolate);
+       v8::HandleScope handle_scope(isolate);
+       if (FPDFXFA_GetApp()->InitRuntime(FALSE)) {
+               CJS_Context * pContext = (CJS_Context*)NewContext();
+               JS_InitialRuntime(*this, this, pContext, m_context);
+               ReleaseContext(pContext);
+               return;
+       }
 
        InitJSObjects();
 
@@ -120,12 +143,13 @@ CJS_Runtime::CJS_Runtime(CPDFDoc_Environment * pApp) :
 
 CJS_Runtime::~CJS_Runtime()
 {
-       for (int i=0, sz=m_ContextArray.GetSize(); i<sz; i++)
+       int size = m_ContextArray.GetSize();
+       for (int i=0;i < size; i++)
                delete m_ContextArray.GetAt(i);
 
        m_ContextArray.RemoveAll();
 
-       JS_ReleaseRuntime(*this, m_context);
+       //JS_ReleaseRuntime(*this, m_context);
 
        RemoveEventsInLoop(m_pFieldEventPath);
 
@@ -135,12 +159,14 @@ CJS_Runtime::~CJS_Runtime()
        m_context.Reset();
 
        //m_isolate->Exit();
-       m_isolate->Dispose();
+       //m_isolate->Dispose();
+       m_isolate = NULL;
 }
 
 FX_BOOL CJS_Runtime::InitJSObjects()
 {
        v8::Isolate::Scope isolate_scope(GetIsolate());
+       v8::Locker locker(GetIsolate());
        v8::HandleScope handle_scope(GetIsolate());
        v8::Handle<v8::Context> context = v8::Context::New(GetIsolate());
        v8::Context::Scope context_scope(context);
@@ -214,6 +240,7 @@ void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc)
        if (m_pDocument != pReaderDoc)
        {
                v8::Isolate::Scope isolate_scope(m_isolate);
+               v8::Locker locker(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);
@@ -329,149 +356,48 @@ v8::Handle<v8::Context>  CJS_Runtime::NewJSContext()
 CFX_WideString ChangeObjName(const CFX_WideString& str)
 {
        CFX_WideString sRet = str;
-       sRet.Replace((FX_LPCWSTR)L"_", (FX_LPCWSTR)L".");
+       sRet.Replace(L"_", L".");
        return sRet;
 }
-
-void CJS_Runtime::GetObjectNames(CFX_WideStringArray& array)
-{
-       array.RemoveAll();
-
-       array.Add(CJS_Border::m_pClassName);
-       array.Add(CJS_Display::m_pClassName);
-       array.Add(CJS_Font::m_pClassName);
-       array.Add(CJS_Highlight::m_pClassName);
-       array.Add(CJS_Position::m_pClassName);
-       array.Add(CJS_ScaleHow::m_pClassName);
-       array.Add(CJS_ScaleWhen::m_pClassName);
-       array.Add(CJS_Style::m_pClassName);
-       array.Add(CJS_Zoomtype::m_pClassName);
-
-       array.Add(CJS_App::m_pClassName);
-       array.Add((FX_LPCWSTR)"this"); 
-       array.Add(CJS_Event::m_pClassName);     
-
-       array.Add(CJS_Global::m_pClassName);    
-       array.Add(CJS_Util::m_pClassName);
-}
-
-void CJS_Runtime::GetObjectConsts(const CFX_WideString& sObjName, CFX_WideStringArray& array)
-{
-       JSConstSpec* pConsts = NULL;
-       int nSize = 0;
-
-       if (sObjName == CJS_Border::m_pClassName)
-               CJS_Border::GetConsts(pConsts, nSize);
-       else if (sObjName == CJS_Display::m_pClassName)
-               CJS_Display::GetConsts(pConsts, nSize);
-       else if (sObjName == CJS_Font::m_pClassName)
-               CJS_Font::GetConsts(pConsts, nSize);
-       else if (sObjName == CJS_Highlight::m_pClassName)
-               CJS_Highlight::GetConsts(pConsts, nSize);
-       else if (sObjName == CJS_Position::m_pClassName)
-               CJS_Position::GetConsts(pConsts, nSize);
-       else if (sObjName == CJS_ScaleHow::m_pClassName)
-               CJS_ScaleHow::GetConsts(pConsts, nSize);
-       else if (sObjName == CJS_ScaleWhen::m_pClassName)
-               CJS_ScaleWhen::GetConsts(pConsts, nSize);
-       else if (sObjName == CJS_Style::m_pClassName)
-               CJS_Style::GetConsts(pConsts, nSize);
-       else if (sObjName == CJS_Zoomtype::m_pClassName)
-               CJS_Zoomtype::GetConsts(pConsts, nSize);
-
-       else if (sObjName == CJS_App::m_pClassName)
-               CJS_App::GetConsts(pConsts, nSize);
-       else if (sObjName == CJS_Color::m_pClassName)
-               CJS_Color::GetConsts(pConsts, nSize);   
-
-       else if (sObjName == L"this") 
-       {
-               if (GetReaderDocument())
-                       CJS_Document::GetConsts(pConsts, nSize);
-               else 
-                       CJS_App::GetConsts(pConsts, nSize);
-       }
-
-       if (sObjName == CJS_Event::m_pClassName)
-               CJS_Event::GetConsts(pConsts, nSize);   
-       else if (sObjName == CJS_Field::m_pClassName)
-               CJS_Field::GetConsts(pConsts, nSize);   
-       else if (sObjName == CJS_Global::m_pClassName)
-               CJS_Global::GetConsts(pConsts, nSize);  
-       else if (sObjName == CJS_Util::m_pClassName)
-               CJS_Util::GetConsts(pConsts, nSize);
-
-       for (int i=0; i<nSize; i++)
-               array.Add(pConsts[i].pName);
-}
-
-void CJS_Runtime::GetObjectProps(const CFX_WideString& sObjName, CFX_WideStringArray& array)
+FX_BOOL        CJS_Runtime::GetHValueByName(FX_BSTR utf8Name, FXJSE_HVALUE hValue)
 {
-       JSPropertySpec* pProperties = NULL;
-       int nSize = 0;
-
-       if (sObjName == CJS_App::m_pClassName)
-               CJS_App::GetProperties(pProperties, nSize);
-       else if (sObjName == CJS_Color::m_pClassName)
-               CJS_Color::GetProperties(pProperties, nSize); 
-       else if (sObjName == L"this")
-       {
-               if (GetReaderDocument())
-                       CJS_Document::GetProperties(pProperties, nSize);
-               else    
-                       CJS_App::GetProperties(pProperties, nSize);
+       FX_LPCSTR name = utf8Name.GetCStr();
+
+       v8::Locker lock(GetIsolate());
+    v8::Isolate::Scope isolate_scope(GetIsolate());
+    v8::HandleScope handle_scope(GetIsolate());
+    v8::Local<v8::Context> context =
+        v8::Local<v8::Context>::New(GetIsolate(), m_context);
+    v8::Context::Scope context_scope(context);
+
+
+       //v8::Local<v8::Context> tmpCotext = v8::Local<v8::Context>::New(GetIsolate(), m_context);
+       v8::Local<v8::Value> propvalue = context->Global()->Get(v8::String::NewFromUtf8(GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength()));
+        
+       if (propvalue.IsEmpty()) {
+               FXJSE_Value_SetUndefined(hValue);
+               return FALSE;
        }
-       else if (sObjName == CJS_Event::m_pClassName)
-               CJS_Event::GetProperties(pProperties, nSize);   
-       else if (sObjName == CJS_Field::m_pClassName)
-               CJS_Field::GetProperties(pProperties, nSize);   
-       else if (sObjName == CJS_Global::m_pClassName)
-               CJS_Global::GetProperties(pProperties, nSize);  
-       else if (sObjName == CJS_Util::m_pClassName)
-               CJS_Util::GetProperties(pProperties, nSize);
-
-       for (int i=0; i<nSize; i++)
-               array.Add(pProperties[i].pName);
-}
+       ((CFXJSE_Value*)hValue)->ForceSetValue(propvalue);
 
-void CJS_Runtime::GetObjectMethods(const CFX_WideString& sObjName, CFX_WideStringArray& array)
-{
-       JSMethodSpec* pMethods = NULL;
-       int nSize = 0;
-
-        if (sObjName == CJS_App::m_pClassName)
-               CJS_App::GetMethods(pMethods, nSize);
-       else if (sObjName == CJS_Color::m_pClassName)
-               CJS_Color::GetMethods(pMethods, nSize); 
-       else if (sObjName == L"this") 
-       {
-               if (GetReaderDocument())
-                       CJS_Document::GetMethods(pMethods, nSize);
-               else    
-                       CJS_App::GetMethods(pMethods, nSize);
-       }
-       else if (sObjName == CJS_Event::m_pClassName)
-               CJS_Event::GetMethods(pMethods, nSize); 
-       else if (sObjName == CJS_Field::m_pClassName)
-               CJS_Field::GetMethods(pMethods, nSize); 
-       else if (sObjName == CJS_Global::m_pClassName)
-               CJS_Global::GetMethods(pMethods, nSize);        
-       else if (sObjName == CJS_Util::m_pClassName)
-               CJS_Util::GetMethods(pMethods, nSize);
-
-       for (int i=0; i<nSize; i++)
-               array.Add(pMethods[i].pName);
-}
-
-FX_BOOL  CJS_Runtime::IsEntered()
-{
-       return v8::Isolate::GetCurrent() == m_isolate;
-}
-void   CJS_Runtime::Exit()
-{
-       if(m_isolate) m_isolate->Exit();
+       return TRUE;
 }
-void   CJS_Runtime::Enter()
+FX_BOOL        CJS_Runtime::SetHValueByName(FX_BSTR utf8Name, FXJSE_HVALUE hValue)
 {
-       if(m_isolate) m_isolate->Enter();
+       if (utf8Name.IsEmpty() || hValue == NULL)
+               return FALSE;
+       FX_LPCSTR name = utf8Name.GetCStr();
+       v8::Isolate* pIsolate = GetIsolate();
+       v8::Locker lock(pIsolate);
+    v8::Isolate::Scope isolate_scope(pIsolate);
+    v8::HandleScope handle_scope(pIsolate);
+    v8::Local<v8::Context> context =
+        v8::Local<v8::Context>::New(pIsolate, m_context);
+    v8::Context::Scope context_scope(context);
+
+       //v8::Local<v8::Context> tmpCotext = v8::Local<v8::Context>::New(GetIsolate(), m_context);
+       v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New(GetIsolate(),((CFXJSE_Value*)hValue)->DirectGetValue());
+       context->Global()->Set(v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, utf8Name.GetLength()), propvalue);
+        
+       return TRUE;
 }