From 7d4c74d1b96d82681d49519c129262a1568e87d4 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 10 Nov 2015 15:07:08 -0800 Subject: Keep "static" objects per-context rather than per isolate. Every time we initialize a new v8::Context, we make a new set of pre-existing native objects, and overwrite the v8::Global handles to those in the previous set. Thus, we may be theoretically releasing some v8::Globals early. R=jochen@chromium.org Review URL: https://codereview.chromium.org/1424933013 . --- fpdfsdk/src/jsapi/fxjs_v8.cpp | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'fpdfsdk/src/jsapi/fxjs_v8.cpp') diff --git a/fpdfsdk/src/jsapi/fxjs_v8.cpp b/fpdfsdk/src/jsapi/fxjs_v8.cpp index 0c58a1b01f..d21970e595 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; @@ -289,14 +290,9 @@ void FXJS_InitializeRuntime(v8::Isolate* pIsolate, v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, 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() @@ -310,23 +306,27 @@ 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::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); @@ -340,8 +340,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()) { @@ -352,6 +354,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; } -- cgit v1.2.3