From 4237aedc2efb0e7a8479148592af349addf86a89 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 10 Nov 2015 15:19:17 -0800 Subject: Merge to XFA: Keep "static" objects per-context rather than per isolate. Original Review URL: https://codereview.chromium.org/1424933013 . (cherry picked from commit 7d4c74d1b96d82681d49519c129262a1568e87d4) TBR=jochen@chromium.org Review URL: https://codereview.chromium.org/1407063012 . --- fpdfsdk/include/jsapi/fxjs_v8.h | 12 +++++---- fpdfsdk/src/javascript/JS_Runtime.cpp | 4 +-- fpdfsdk/src/javascript/JS_Runtime.h | 2 ++ fpdfsdk/src/jsapi/fxjs_v8.cpp | 43 +++++++++++++++++------------- fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp | 6 +++-- 5 files changed, 39 insertions(+), 28 deletions(-) diff --git a/fpdfsdk/include/jsapi/fxjs_v8.h b/fpdfsdk/include/jsapi/fxjs_v8.h index 001823f5fd..3b5e6af180 100644 --- a/fpdfsdk/include/jsapi/fxjs_v8.h +++ b/fpdfsdk/include/jsapi/fxjs_v8.h @@ -64,7 +64,6 @@ extern const wchar_t kFXJSValueNameFxobj[]; extern const wchar_t kFXJSValueNameNull[]; extern const wchar_t kFXJSValueNameUndefined[]; - class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { void* Allocate(size_t length) override; void* AllocateUninitialized(size_t length) override; @@ -128,11 +127,14 @@ void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, v8::Local pDefault); // Called after FXJS_Define* calls made. -void FXJS_InitializeRuntime(v8::Isolate* pIsolate, - IJS_Runtime* pIRuntime, - v8::Global& v8PersistentContext); +void FXJS_InitializeRuntime( + v8::Isolate* pIsolate, + IJS_Runtime* pIRuntime, + v8::Global* pV8PersistentContext, + std::vector*>* pStaticObjects); void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, - v8::Global& v8PersistentContext); + v8::Global* pV8PersistentContext, + std::vector*>* pStaticObjects); // Called as part of FXJS_InitializeRuntime, exposed so PDF can make its // own contexts compatible with XFA or vice versa. diff --git a/fpdfsdk/src/javascript/JS_Runtime.cpp b/fpdfsdk/src/javascript/JS_Runtime.cpp index ecb53d2d72..3e1f74beed 100644 --- a/fpdfsdk/src/javascript/JS_Runtime.cpp +++ b/fpdfsdk/src/javascript/JS_Runtime.cpp @@ -77,7 +77,7 @@ CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) v8::HandleScope handle_scope(isolate); if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) { CJS_Context* pContext = (CJS_Context*)NewContext(); - FXJS_InitializeRuntime(GetIsolate(), this, m_context); + FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); ReleaseContext(pContext); return; } @@ -88,7 +88,7 @@ CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); CJS_Context* pContext = (CJS_Context*)NewContext(); - FXJS_InitializeRuntime(GetIsolate(), this, m_context); + FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects); ReleaseContext(pContext); } diff --git a/fpdfsdk/src/javascript/JS_Runtime.h b/fpdfsdk/src/javascript/JS_Runtime.h index db77eeb8ed..9e6b3abede 100644 --- a/fpdfsdk/src/javascript/JS_Runtime.h +++ b/fpdfsdk/src/javascript/JS_Runtime.h @@ -9,6 +9,7 @@ #include #include +#include #include "../../include/javascript/IJavaScript.h" #include "../../include/jsapi/fxjs_v8.h" @@ -77,6 +78,7 @@ class CJS_Runtime : public IJS_Runtime { v8::Isolate* m_isolate; bool m_isolateManaged; v8::Global m_context; + std::vector*> m_StaticObjects; std::set m_observers; }; diff --git a/fpdfsdk/src/jsapi/fxjs_v8.cpp b/fpdfsdk/src/jsapi/fxjs_v8.cpp index 3413d72bb3..f099289666 100644 --- a/fpdfsdk/src/jsapi/fxjs_v8.cpp +++ b/fpdfsdk/src/jsapi/fxjs_v8.cpp @@ -95,7 +95,6 @@ class CFXJS_ObjDefinition { v8::Isolate* m_pIsolate; v8::Global m_FunctionTemplate; v8::Global m_Signature; - v8::Global m_StaticObj; }; static v8::Local GetGlobalObjectTemplate( @@ -273,9 +272,11 @@ void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, pDefault, v8::ReadOnly); } -void FXJS_InitializeRuntime(v8::Isolate* pIsolate, - IJS_Runtime* pIRuntime, - v8::Global& v8PersistentContext) { +void FXJS_InitializeRuntime( + v8::Isolate* pIsolate, + IJS_Runtime* pIRuntime, + v8::Global* pV8PersistentContext, + std::vector*>* pStaticObjects) { if (pIsolate == g_isolate) ++g_isolate_ref_count; @@ -290,14 +291,9 @@ void FXJS_InitializeRuntime(v8::Isolate* pIsolate, FXJS_SetRuntimeForV8Context(v8Context, pIRuntime); int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); + pStaticObjects->resize(maxID + 1); for (int i = 0; i < maxID; ++i) { CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); - CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); - v8::Local m_ObjName = - v8::String::NewFromUtf8(pIsolate, bs.c_str(), - v8::NewStringType::kNormal, - bs.GetLength()).ToLocalChecked(); - if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { v8Context->Global() ->GetPrototype() @@ -311,24 +307,28 @@ void FXJS_InitializeRuntime(v8::Isolate* pIsolate, ->ToObject(v8Context) .ToLocalChecked()); } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { + CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); + v8::Local m_ObjName = + v8::String::NewFromUtf8(pIsolate, bs.c_str(), + v8::NewStringType::kNormal, + bs.GetLength()).ToLocalChecked(); + v8::Local obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i); v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); - pObjDef->m_StaticObj.Reset(pIsolate, obj); + pStaticObjects->at(i) = new v8::Global(pIsolate, obj); } } - v8PersistentContext.Reset(pIsolate, v8Context); + pV8PersistentContext->Reset(pIsolate, v8Context); } void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, - v8::Global& v8PersistentContext) { - if (pIsolate == g_isolate && --g_isolate_ref_count > 0) - return; - + v8::Global* pV8PersistentContext, + std::vector*>* pStaticObjects) { v8::Isolate::Scope isolate_scope(pIsolate); v8::Locker locker(pIsolate); v8::HandleScope handle_scope(pIsolate); v8::Local context = - v8::Local::New(pIsolate, v8PersistentContext); + v8::Local::New(pIsolate, *pV8PersistentContext); v8::Context::Scope context_scope(context); FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); @@ -345,8 +345,10 @@ void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { pObj = context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); - } else if (!pObjDef->m_StaticObj.IsEmpty()) { - pObj = v8::Local::New(pIsolate, pObjDef->m_StaticObj); + } else if (pStaticObjects->at(i) && !pStaticObjects->at(i)->IsEmpty()) { + pObj = v8::Local::New(pIsolate, *pStaticObjects->at(i)); + delete pStaticObjects->at(i); + pStaticObjects->at(i) = nullptr; } if (!pObj.IsEmpty()) { @@ -357,6 +359,9 @@ void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, delete pObjDef; } + if (pIsolate == g_isolate && --g_isolate_ref_count > 0) + return; + pIsolate->SetData(g_embedderDataSlot, nullptr); delete pData; } diff --git a/fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp b/fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp index 01f12adb57..9cde507203 100644 --- a/fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp +++ b/fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp @@ -33,11 +33,12 @@ class FXJSV8Embeddertest : public EmbedderTest { v8::Locker locker(m_pIsolate); v8::HandleScope handle_scope(m_pIsolate); FXJS_PerIsolateData::SetUp(m_pIsolate); - FXJS_InitializeRuntime(m_pIsolate, nullptr, m_pPersistentContext); + FXJS_InitializeRuntime(m_pIsolate, nullptr, &m_pPersistentContext, + &m_StaticObjects); } void TearDown() override { - FXJS_ReleaseRuntime(m_pIsolate, m_pPersistentContext); + FXJS_ReleaseRuntime(m_pIsolate, &m_pPersistentContext, &m_StaticObjects); m_pPersistentContext.Reset(); FXJS_Release(); EmbedderTest::TearDown(); @@ -52,6 +53,7 @@ class FXJSV8Embeddertest : public EmbedderTest { nonstd::unique_ptr m_pArrayBufferAllocator; v8::Isolate* m_pIsolate; v8::Global m_pPersistentContext; + std::vector*> m_StaticObjects; }; TEST_F(FXJSV8Embeddertest, Getters) { -- cgit v1.2.3