From a4941914bb4411dc4e9053cb344e0349db388007 Mon Sep 17 00:00:00 2001 From: tsepez Date: Mon, 15 Aug 2016 11:40:12 -0700 Subject: Move some v8 objects from CJS back into FXJS Create a new class to hold these, CFXJS_Engine (could have been called Runtime, but there are too many "Runtimes" already). In a subsequent patch, all the FXJS_*() functions that take an isolate as the first argument can become methods on the engine. CJS_ must still manage the isolates; this happens outside the engine. The IJS_Runtime abstraction moves up to fpdfsdk/javascript; it remains to allow for either a real JS library or a stubb one to be linked (for non-js builds). Review-Url: https://codereview.chromium.org/2241483004 --- fxjs/fxjs_v8.cpp | 39 +++++++++++++++++++++------------------ fxjs/fxjs_v8_embeddertest.cpp | 12 +++++------- fxjs/include/fxjs_v8.h | 42 ++++++++++++++++++++++++++++-------------- 3 files changed, 54 insertions(+), 39 deletions(-) (limited to 'fxjs') diff --git a/fxjs/fxjs_v8.cpp b/fxjs/fxjs_v8.cpp index 92207fdd47..42fc021c83 100644 --- a/fxjs/fxjs_v8.cpp +++ b/fxjs/fxjs_v8.cpp @@ -222,6 +222,9 @@ FXJS_PerIsolateData::FXJS_PerIsolateData() : m_pFXJSERuntimeData(nullptr), m_pDynamicObjsMap(nullptr) {} #endif // PDF_ENABLE_XFA +CFXJS_Engine::CFXJS_Engine() : m_isolate(nullptr) {} +CFXJS_Engine::~CFXJS_Engine() {} + int FXJS_DefineObj(v8::Isolate* pIsolate, const wchar_t* sObjName, FXJSOBJTYPE eObjType, @@ -330,9 +333,9 @@ void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, fun); } -void FXJS_InitializeRuntime( +void FXJS_InitializeEngine( v8::Isolate* pIsolate, - IJS_Runtime* pIRuntime, + CFXJS_Engine* pEngine, v8::Global* pV8PersistentContext, std::vector*>* pStaticObjects) { if (pIsolate == g_isolate) @@ -353,7 +356,7 @@ void FXJS_InitializeRuntime( if (!pData) return; pData->CreateDynamicObjsMap(pIsolate); - v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); + v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pEngine); int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); pStaticObjects->resize(maxID + 1); @@ -367,10 +370,10 @@ void FXJS_InitializeRuntime( ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); if (pObjDef->m_pConstructor) - pObjDef->m_pConstructor(pIRuntime, v8Context->Global() - ->GetPrototype() - ->ToObject(v8Context) - .ToLocalChecked()); + pObjDef->m_pConstructor(pEngine, v8Context->Global() + ->GetPrototype() + ->ToObject(v8Context) + .ToLocalChecked()); } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); v8::Local m_ObjName = @@ -379,7 +382,7 @@ void FXJS_InitializeRuntime( .ToLocalChecked(); v8::Local obj = - FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i, true); + FXJS_NewFxDynamicObj(pIsolate, pEngine, i, true); v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); pStaticObjects->at(i) = new v8::Global(pIsolate, obj); } @@ -387,9 +390,9 @@ void FXJS_InitializeRuntime( pV8PersistentContext->Reset(pIsolate, v8Context); } -void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, - v8::Global* pV8PersistentContext, - std::vector*>* pStaticObjects) { +void FXJS_ReleaseEngine(v8::Isolate* pIsolate, + v8::Global* pV8PersistentContext, + std::vector*>* pStaticObjects) { v8::Isolate::Scope isolate_scope(pIsolate); v8::HandleScope handle_scope(pIsolate); v8::Local context = @@ -431,16 +434,16 @@ void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, delete pData; } -IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate) { +CFXJS_Engine* FXJS_GetCurrentEngineFromIsolate(v8::Isolate* pIsolate) { v8::Local context = pIsolate->GetCurrentContext(); - return static_cast( + return static_cast( context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); } #ifdef PDF_ENABLE_XFA -void FXJS_SetRuntimeForV8Context(v8::Local v8Context, - IJS_Runtime* pIRuntime) { - v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); +void FXJS_SetEngineForV8Context(v8::Local v8Context, + CFXJS_Engine* pEngine) { + v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pEngine); } #endif // PDF_ENABLE_XFA @@ -473,7 +476,7 @@ int FXJS_Execute(v8::Isolate* pIsolate, } v8::Local FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, - IJS_Runtime* pIRuntime, + CFXJS_Engine* pEngine, int nObjDefnID, bool bStatic) { v8::Isolate::Scope isolate_scope(pIsolate); @@ -502,7 +505,7 @@ v8::Local FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, CFXJS_PerObjectData* pPerObjData = new CFXJS_PerObjectData(nObjDefnID); obj->SetAlignedPointerInInternalField(0, pPerObjData); if (pObjDef->m_pConstructor) - pObjDef->m_pConstructor(pIRuntime, obj); + pObjDef->m_pConstructor(pEngine, obj); if (!bStatic && FXJS_PerIsolateData::Get(pIsolate)->m_pDynamicObjsMap) { FXJS_PerIsolateData::Get(pIsolate)->m_pDynamicObjsMap->set(pPerObjData, diff --git a/fxjs/fxjs_v8_embeddertest.cpp b/fxjs/fxjs_v8_embeddertest.cpp index 71b004c144..b573939b14 100644 --- a/fxjs/fxjs_v8_embeddertest.cpp +++ b/fxjs/fxjs_v8_embeddertest.cpp @@ -44,19 +44,17 @@ TEST_F(FXJSV8EmbedderTest, Getters) { CheckAssignmentInCurrentContext(kExpected1); } -TEST_F(FXJSV8EmbedderTest, MultipleRutimes) { +TEST_F(FXJSV8EmbedderTest, MultipleEngines) { v8::Isolate::Scope isolate_scope(isolate()); v8::HandleScope handle_scope(isolate()); v8::Global global_context1; std::vector*> static_objects1; - FXJS_InitializeRuntime(isolate(), nullptr, &global_context1, - &static_objects1); + FXJS_InitializeEngine(isolate(), nullptr, &global_context1, &static_objects1); v8::Global global_context2; std::vector*> static_objects2; - FXJS_InitializeRuntime(isolate(), nullptr, &global_context2, - &static_objects2); + FXJS_InitializeEngine(isolate(), nullptr, &global_context2, &static_objects2); v8::Context::Scope context_scope(GetV8Context()); ExecuteInCurrentContext(CFX_WideString(kScript0)); @@ -69,7 +67,7 @@ TEST_F(FXJSV8EmbedderTest, MultipleRutimes) { ExecuteInCurrentContext(CFX_WideString(kScript1)); CheckAssignmentInCurrentContext(kExpected1); } - FXJS_ReleaseRuntime(isolate(), &global_context1, &static_objects1); + FXJS_ReleaseEngine(isolate(), &global_context1, &static_objects1); { v8::Local context2 = @@ -78,7 +76,7 @@ TEST_F(FXJSV8EmbedderTest, MultipleRutimes) { ExecuteInCurrentContext(CFX_WideString(kScript2)); CheckAssignmentInCurrentContext(kExpected2); } - FXJS_ReleaseRuntime(isolate(), &global_context2, &static_objects2); + FXJS_ReleaseEngine(isolate(), &global_context2, &static_objects2); CheckAssignmentInCurrentContext(kExpected0); } diff --git a/fxjs/include/fxjs_v8.h b/fxjs/include/fxjs_v8.h index 5e12b082be..52afc491b2 100644 --- a/fxjs/include/fxjs_v8.h +++ b/fxjs/include/fxjs_v8.h @@ -17,16 +17,17 @@ #include #include +#include #include #include "core/fxcrt/include/fx_string.h" +class CFXJS_Engine; class CFXJS_ObjDefinition; -// FXJS_V8 places no restrictions on these two classes; it merely passes them +// FXJS_V8 places no restrictions on this class; it merely passes it // on to caller-provided methods. class IJS_Context; // A description of the event that caused JS execution. -class IJS_Runtime; // A native runtime, typically owns the v8::Context. #ifdef PDF_ENABLE_XFA // FXJS_V8 places no interpreation on this calass; it merely passes it @@ -124,12 +125,25 @@ class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { void Free(void* data, size_t length) override; }; -using FXJS_CONSTRUCTOR = void (*)(IJS_Runtime* cc, v8::Local obj); +using FXJS_CONSTRUCTOR = void (*)(CFXJS_Engine* fxjs, + v8::Local obj); using FXJS_DESTRUCTOR = void (*)(v8::Local obj); void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate); void FXJS_Release(); +class CFXJS_Engine { + public: + CFXJS_Engine(); + ~CFXJS_Engine(); + + protected: + v8::Isolate* m_isolate; + v8::Global m_context; + std::vector*> m_StaticObjects; + std::map> m_ConstArrays; +}; + // Gets the global isolate set by FXJS_Initialize(), or makes a new one each // time if there is no such isolate. Returns true if a new isolate had to be // created. @@ -172,30 +186,30 @@ void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, v8::FunctionCallback pConstGetter); // Called after FXJS_Define* calls made. -void FXJS_InitializeRuntime( +void FXJS_InitializeEngine( v8::Isolate* pIsolate, - IJS_Runtime* pIRuntime, + CFXJS_Engine* pEngine, v8::Global* pV8PersistentContext, std::vector*>* pStaticObjects); -void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, - v8::Global* pV8PersistentContext, - std::vector*>* pStaticObjects); -IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate); +void FXJS_ReleaseEngine(v8::Isolate* pIsolate, + v8::Global* pV8PersistentContext, + std::vector*>* pStaticObjects); +CFXJS_Engine* FXJS_GetCurrentEngineFromIsolate(v8::Isolate* pIsolate); #ifdef PDF_ENABLE_XFA -// Called as part of FXJS_InitializeRuntime, exposed so PDF can make its +// Called as part of FXJS_InitializeEngine, exposed so PDF can make its // own contexts compatible with XFA or vice versa. -void FXJS_SetRuntimeForV8Context(v8::Local v8Context, - IJS_Runtime* pIRuntime); +void FXJS_SetEngineForV8Context(v8::Local v8Context, + CFXJS_Engine* pEngine); #endif // PDF_ENABLE_XFA -// Called after FXJS_InitializeRuntime call made. +// Called after FXJS_InitializeEngine call made. int FXJS_Execute(v8::Isolate* pIsolate, const CFX_WideString& script, FXJSErr* perror); v8::Local FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, - IJS_Runtime* pJSContext, + CFXJS_Engine* pEngine, int nObjDefnID, bool bStatic = false); v8::Local FXJS_GetThisObj(v8::Isolate* pIsolate); -- cgit v1.2.3