Merge to XFA: Remove FX_BSTR and FX_WSTR typedefs.
[pdfium.git] / fpdfsdk / src / javascript / JS_Runtime.cpp
index 4b4328c..d8fea8d 100644 (file)
@@ -93,22 +93,41 @@ 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);
+extern v8::Global<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 (CPDFXFA_App::GetInstance()->GetJSERuntime()) {
+               m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->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)) {
+       if (CPDFXFA_App::GetInstance()->InitRuntime(FALSE)) {
                CJS_Context * pContext = (CJS_Context*)NewContext();
                JS_InitialRuntime(*this, this, pContext, m_context);
                ReleaseContext(pContext);
@@ -149,7 +168,7 @@ 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::Local<v8::Context> context = v8::Context::New(GetIsolate());
        v8::Context::Scope context_scope(context);
        //0 - 8
        if (CJS_Border::Init(*this, JS_STATIC) < 0) return FALSE;
@@ -329,7 +348,7 @@ void CJS_Runtime::RemoveEventsInLoop(CJS_FieldEvent* pStart)
        }
 }
 
-v8::Handle<v8::Context>        CJS_Runtime::NewJSContext()
+v8::Local<v8::Context> CJS_Runtime::NewJSContext()
 {
        return v8::Local<v8::Context>::New(m_isolate, m_context);
 }
@@ -340,9 +359,9 @@ CFX_WideString ChangeObjName(const CFX_WideString& str)
        sRet.Replace(L"_", L".");
        return sRet;
 }
-FX_BOOL        CJS_Runtime::GetHValueByName(FX_BSTR utf8Name, FXJSE_HVALUE hValue)
+FX_BOOL        CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name, FXJSE_HVALUE hValue)
 {
-       FX_LPCSTR name = utf8Name.GetCStr();
+       const FX_CHAR* name = utf8Name.GetCStr();
 
        v8::Locker lock(GetIsolate());
     v8::Isolate::Scope isolate_scope(GetIsolate());
@@ -363,11 +382,11 @@ FX_BOOL   CJS_Runtime::GetHValueByName(FX_BSTR utf8Name, FXJSE_HVALUE hValue)
 
        return TRUE;
 }
-FX_BOOL        CJS_Runtime::SetHValueByName(FX_BSTR utf8Name, FXJSE_HVALUE hValue)
+FX_BOOL        CJS_Runtime::SetHValueByName(const CFX_ByteStringC& utf8Name, FXJSE_HVALUE hValue)
 {
        if (utf8Name.IsEmpty() || hValue == NULL)
                return FALSE;
-       FX_LPCSTR name = utf8Name.GetCStr();
+       const FX_CHAR* name = utf8Name.GetCStr();
        v8::Isolate* pIsolate = GetIsolate();
        v8::Locker lock(pIsolate);
     v8::Isolate::Scope isolate_scope(pIsolate);