Merge to XFA: Fix V8 array buffer allocator.
authorTom Sepez <tsepez@chromium.org>
Thu, 30 Apr 2015 22:19:03 +0000 (15:19 -0700)
committerTom Sepez <tsepez@chromium.org>
Thu, 30 Apr 2015 22:19:03 +0000 (15:19 -0700)
R=thestig@chromium.org

Review URL: https://codereview.chromium.org/1118143003

DEPS
fpdfsdk/include/javascript/JS_Runtime.h
fpdfsdk/src/javascript/JS_Runtime.cpp
xfa/src/fxjse/src/runtime.cpp

diff --git a/DEPS b/DEPS
index 6f8417f..88e3a3c 100644 (file)
--- a/DEPS
+++ b/DEPS
@@ -12,7 +12,7 @@ deps = {
      "https://chromium.googlesource.com/external/googletest.git@8245545b6dc9c4703e6496d1efd19e975ad2b038",
 
   "v8":
-    "https://chromium.googlesource.com/v8/v8.git@6988aec61f071a1caec96abae1cf39353a234455",
+    "https://chromium.googlesource.com/v8/v8.git",
 
   "v8/third_party/icu":
     "https://chromium.googlesource.com/chromium/deps/icu46",
index 86d4971..07e4e43 100644 (file)
@@ -7,6 +7,7 @@
 #ifndef _JS_RUNTIME_H_
 #define _JS_RUNTIME_H_
 
+#include "../../../third_party/base/nonstd_unique_ptr.h"
 #include "../../../core/include/fxcrt/fx_basic.h"
 #include "../jsapi/fxjs_v8.h"
 #include "IJavaScript.h"
 
 class CJS_Context;
 
+class CJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
+    void* Allocate(size_t length) override;
+    void* AllocateUninitialized(size_t length) override;
+    void Free(void* data, size_t length) override;
+};
+
 class CJS_FieldEvent
 {
 public:
@@ -57,15 +64,16 @@ public:
        virtual FX_BOOL                                                 GetHValueByName(FX_BSTR utf8Name, FXJSE_HVALUE hValue);
        virtual FX_BOOL                                                 SetHValueByName(FX_BSTR utf8Name, FXJSE_HVALUE hValue);
 protected:
-       CFX_ArrayTemplate<CJS_Context *>                m_ContextArray;
-       CPDFDoc_Environment *                                   m_pApp;
-       CPDFSDK_Document *                                              m_pDocument;
+       CFX_ArrayTemplate<CJS_Context*>         m_ContextArray;
+       CPDFDoc_Environment*                                                    m_pApp;
+       CPDFSDK_Document*                                               m_pDocument;
        FX_BOOL                                                                 m_bBlocking;
+       FX_BOOL                                                                 m_bRegistered;
        CJS_FieldEvent*                                                 m_pFieldEventPath;
 
-       v8::Isolate*                                                    m_isolate;
-       v8::Persistent<v8::Context>                             m_context;
-       FX_BOOL                                                                 m_bRegistered;
+       v8::Isolate* m_isolate;
+       nonstd::unique_ptr<CJS_ArrayBufferAllocator> m_pArrayBufferAllocator;
+       v8::Persistent<v8::Context> m_context;
 };
 
 #endif //_JS_RUNTIME_H_
index 4b4328c..2ad5667 100644 (file)
@@ -93,17 +93,36 @@ void CJS_RuntimeFactory::ReleaseGlobalData()
        }
 }
 
+void* CJS_ArrayBufferAllocator::Allocate(size_t length) {
+    return calloc(1, length);
+}
+
+void* CJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) {
+    return malloc(length);
+}
+
+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_bBlocking(FALSE),
-       m_pFieldEventPath(NULL),
-       m_bRegistered(FALSE)
+       m_bRegistered(FALSE),
+       m_pFieldEventPath(NULL)
 {
-       m_isolate = FPDFXFA_GetApp()->GetJSERuntime()?(v8::Isolate*)FPDFXFA_GetApp()->GetJSERuntime():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);
index c4dc61c..d880606 100644 (file)
@@ -8,6 +8,21 @@
 #include "fxv8.h"\r
 #include "runtime.h"\r
 #include "scope_inline.h"\r
+\r
+// Duplicates fpdfsdk's JS_Runtime.h, but keeps XFA from depending on it.\r
+// TODO(tsepez): make a single version of this.\r
+class FXJSE_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {\r
+    void* Allocate(size_t length) override {\r
+        return calloc(1, length);\r
+    }\r
+    void* AllocateUninitialized(size_t length) override {\r
+        return malloc(length);\r
+    }\r
+    void Free(void* data, size_t length) override {\r
+        free(data);\r
+    }\r
+};\r
+\r
 static void FXJSE_KillV8()\r
 {\r
     v8::V8::Dispose();\r
@@ -51,7 +66,9 @@ void FXJSE_Finalize()
 }\r
 FXJSE_HRUNTIME FXJSE_Runtime_Create()\r
 {\r
-    v8::Isolate* pIsolate = v8::Isolate::New();\r
+    v8::Isolate::CreateParams params;\r
+    params.array_buffer_allocator = new FXJSE_ArrayBufferAllocator();\r
+    v8::Isolate* pIsolate = v8::Isolate::New(params);\r
     ASSERT(pIsolate && CFXJSE_RuntimeData::g_RuntimeList);\r
     CFXJSE_RuntimeData::g_RuntimeList->AppendRuntime(pIsolate);\r
     return reinterpret_cast<FXJSE_HRUNTIME>(pIsolate);\r