From e99ee7f3e550f7706a3e2d8105ccc55553a9badb Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Thu, 1 Feb 2018 20:24:43 +0000 Subject: Introduce CXFJS_Engine::EngineFromContext/SetEngineInContext helpers No functional change, but expose helpers likely to be needed by next steps. Rename CXFJS_Engine::CurrentEngineFromIsolate to be more accurately named along the way. Change-Id: Iaf2d15a09cfed85705d24ee056e78edf0660ce59 Reviewed-on: https://pdfium-review.googlesource.com/25090 Reviewed-by: dsinclair Commit-Queue: Tom Sepez --- fxjs/JS_Define.cpp | 2 +- fxjs/JS_Define.h | 6 +++--- fxjs/cjs_global.cpp | 12 ++++++------ fxjs/cjs_globalarrays.cpp | 3 ++- fxjs/cjs_globalconsts.cpp | 12 ++++++------ fxjs/cjs_publicmethods.cpp | 2 +- fxjs/cjs_runtime.cpp | 5 +++-- fxjs/cjs_runtime.h | 2 +- fxjs/fxjs_v8.cpp | 22 +++++++++++++++------- fxjs/fxjs_v8.h | 6 +++++- 10 files changed, 43 insertions(+), 29 deletions(-) diff --git a/fxjs/JS_Define.cpp b/fxjs/JS_Define.cpp index 90f7557422..171e05c66e 100644 --- a/fxjs/JS_Define.cpp +++ b/fxjs/JS_Define.cpp @@ -232,7 +232,7 @@ double JS_DateParse(const WideString& str) { v8::Local funC = v8::Local::Cast(v); const int argc = 1; v8::Local timeStr = - CJS_Runtime::CurrentRuntimeFromIsolate(pIsolate)->NewString( + CJS_Runtime::RuntimeFromIsolateCurrentContext(pIsolate)->NewString( str.AsStringView()); v8::Local argv[argc] = {timeStr}; v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked(); diff --git a/fxjs/JS_Define.h b/fxjs/JS_Define.h index 08654b96e1..f45738e847 100644 --- a/fxjs/JS_Define.h +++ b/fxjs/JS_Define.h @@ -66,7 +66,7 @@ void JSPropGetter(const char* prop_name_string, v8::Local property, const v8::PropertyCallbackInfo& info) { CJS_Runtime* pRuntime = - CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); if (!pRuntime) return; @@ -94,7 +94,7 @@ void JSPropSetter(const char* prop_name_string, v8::Local value, const v8::PropertyCallbackInfo& info) { CJS_Runtime* pRuntime = - CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); if (!pRuntime) return; @@ -118,7 +118,7 @@ void JSMethod(const char* method_name_string, const char* class_name_string, const v8::FunctionCallbackInfo& info) { CJS_Runtime* pRuntime = - CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); if (!pRuntime) return; diff --git a/fxjs/cjs_global.cpp b/fxjs/cjs_global.cpp index eb66488d8e..3fc4bf0f0f 100644 --- a/fxjs/cjs_global.cpp +++ b/fxjs/cjs_global.cpp @@ -33,7 +33,7 @@ void JSSpecialPropQuery(const char*, v8::Local property, const v8::PropertyCallbackInfo& info) { CJS_Runtime* pRuntime = - CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); if (!pRuntime) return; @@ -53,7 +53,7 @@ void JSSpecialPropGet(const char* class_name, v8::Local property, const v8::PropertyCallbackInfo& info) { CJS_Runtime* pRuntime = - CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); if (!pRuntime) return; @@ -81,7 +81,7 @@ void JSSpecialPropPut(const char* class_name, v8::Local value, const v8::PropertyCallbackInfo& info) { CJS_Runtime* pRuntime = - CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); if (!pRuntime) return; @@ -104,7 +104,7 @@ void JSSpecialPropDel(const char* class_name, v8::Local property, const v8::PropertyCallbackInfo& info) { CJS_Runtime* pRuntime = - CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); if (!pRuntime) return; @@ -360,7 +360,7 @@ CJS_Return JSGlobalAlternate::setPersistent( void JSGlobalAlternate::UpdateGlobalPersistentVariables() { CJS_Runtime* pRuntime = - static_cast(CFXJS_Engine::CurrentEngineFromIsolate( + static_cast(CFXJS_Engine::EngineFromIsolateCurrentContext( m_pJSObject->ToV8Object()->GetIsolate())); for (int i = 0, sz = m_pGlobalData->GetSize(); i < sz; i++) { @@ -501,7 +501,7 @@ void JSGlobalAlternate::ObjectToArray(CJS_Runtime* pRuntime, void JSGlobalAlternate::PutObjectProperty(v8::Local pObj, CJS_KeyValue* pData) { - CJS_Runtime* pRuntime = CJS_Runtime::CurrentRuntimeFromIsolate( + CJS_Runtime* pRuntime = CJS_Runtime::RuntimeFromIsolateCurrentContext( m_pJSObject->ToV8Object()->GetIsolate()); for (int i = 0, sz = pData->objData.Count(); i < sz; i++) { diff --git a/fxjs/cjs_globalarrays.cpp b/fxjs/cjs_globalarrays.cpp index 7857161205..818b13f0e8 100644 --- a/fxjs/cjs_globalarrays.cpp +++ b/fxjs/cjs_globalarrays.cpp @@ -16,7 +16,8 @@ (rt)->DefineGlobalConst( \ (name), [](const v8::FunctionCallbackInfo& info) { \ CJS_Runtime* pCurrentRuntime = \ - CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); \ + CJS_Runtime::RuntimeFromIsolateCurrentContext( \ + info.GetIsolate()); \ if (pCurrentRuntime) \ info.GetReturnValue().Set(pCurrentRuntime->GetConstArray(name)); \ }); \ diff --git a/fxjs/cjs_globalconsts.cpp b/fxjs/cjs_globalconsts.cpp index 598479780a..a07bf60302 100644 --- a/fxjs/cjs_globalconsts.cpp +++ b/fxjs/cjs_globalconsts.cpp @@ -6,12 +6,12 @@ #include "fxjs/cjs_globalconsts.h" -#define GLOBAL_STRING(rt, name, value) \ - (rt)->DefineGlobalConst( \ - (name), [](const v8::FunctionCallbackInfo& info) { \ - info.GetReturnValue().Set( \ - CFXJS_Engine::CurrentEngineFromIsolate(info.GetIsolate()) \ - ->NewString(value)); \ +#define GLOBAL_STRING(rt, name, value) \ + (rt)->DefineGlobalConst( \ + (name), [](const v8::FunctionCallbackInfo& info) { \ + info.GetReturnValue().Set( \ + CFXJS_Engine::EngineFromIsolateCurrentContext(info.GetIsolate()) \ + ->NewString(value)); \ }) // static diff --git a/fxjs/cjs_publicmethods.cpp b/fxjs/cjs_publicmethods.cpp index 45eae7f8d2..9fcfc5f291 100644 --- a/fxjs/cjs_publicmethods.cpp +++ b/fxjs/cjs_publicmethods.cpp @@ -120,7 +120,7 @@ template & info) { CJS_Runtime* pRuntime = - CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + CJS_Runtime::RuntimeFromIsolateCurrentContext(info.GetIsolate()); if (!pRuntime) return; diff --git a/fxjs/cjs_runtime.cpp b/fxjs/cjs_runtime.cpp index 919c5a9b66..bffd77cba7 100644 --- a/fxjs/cjs_runtime.cpp +++ b/fxjs/cjs_runtime.cpp @@ -63,9 +63,10 @@ std::unique_ptr IJS_Runtime::Create( } // static -CJS_Runtime* CJS_Runtime::CurrentRuntimeFromIsolate(v8::Isolate* pIsolate) { +CJS_Runtime* CJS_Runtime::RuntimeFromIsolateCurrentContext( + v8::Isolate* pIsolate) { return static_cast( - CFXJS_Engine::CurrentEngineFromIsolate(pIsolate)); + CFXJS_Engine::EngineFromIsolateCurrentContext(pIsolate)); } CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv) diff --git a/fxjs/cjs_runtime.h b/fxjs/cjs_runtime.h index 7ecc0d5ce4..5d55b9a492 100644 --- a/fxjs/cjs_runtime.h +++ b/fxjs/cjs_runtime.h @@ -27,7 +27,7 @@ class CJS_Runtime : public IJS_Runtime, public: using FieldEvent = std::pair; - static CJS_Runtime* CurrentRuntimeFromIsolate(v8::Isolate* pIsolate); + static CJS_Runtime* RuntimeFromIsolateCurrentContext(v8::Isolate* pIsolate); explicit CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv); ~CJS_Runtime() override; diff --git a/fxjs/fxjs_v8.cpp b/fxjs/fxjs_v8.cpp index 50345cfcd1..e73ab0a8f0 100644 --- a/fxjs/fxjs_v8.cpp +++ b/fxjs/fxjs_v8.cpp @@ -177,8 +177,8 @@ void V8TemplateMapTraits::Dispose(v8::Isolate* isolate, if (!pObjDef) return; if (pObjDef->m_pDestructor) { - pObjDef->m_pDestructor(CFXJS_Engine::CurrentEngineFromIsolate(isolate), - obj); + pObjDef->m_pDestructor( + CFXJS_Engine::EngineFromIsolateCurrentContext(isolate), obj); } CFXJS_Engine::FreeObjectPrivate(obj); } @@ -261,10 +261,19 @@ CFXJS_Engine::CFXJS_Engine(v8::Isolate* pIsolate) : CJS_V8(pIsolate) {} CFXJS_Engine::~CFXJS_Engine() = default; // static -CFXJS_Engine* CFXJS_Engine::CurrentEngineFromIsolate(v8::Isolate* pIsolate) { +CFXJS_Engine* CFXJS_Engine::EngineFromIsolateCurrentContext( + v8::Isolate* pIsolate) { + return EngineFromContext(pIsolate->GetCurrentContext()); +} + +CFXJS_Engine* CFXJS_Engine::EngineFromContext(v8::Local pContext) { return static_cast( - pIsolate->GetCurrentContext()->GetAlignedPointerFromEmbedderData( - kPerContextDataIndex)); + pContext->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); +} + +void CFXJS_Engine::SetEngineInContext(CFXJS_Engine* pEngine, + v8::Local pContext) { + pContext->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pEngine); } // static @@ -385,8 +394,7 @@ void CFXJS_Engine::InitializeEngine() { v8::Local v8Context = v8::Context::New( GetIsolate(), nullptr, GetGlobalObjectTemplate(GetIsolate())); v8::Context::Scope context_scope(v8Context); - - v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, this); + SetEngineInContext(this, v8Context); int maxID = CFXJS_ObjDefinition::MaxID(GetIsolate()); m_StaticObjects.resize(maxID + 1); diff --git a/fxjs/fxjs_v8.h b/fxjs/fxjs_v8.h index 4575235755..0372ade9ce 100644 --- a/fxjs/fxjs_v8.h +++ b/fxjs/fxjs_v8.h @@ -137,7 +137,11 @@ class CFXJS_Engine : public CJS_V8 { v8::Local obj); using Destructor = void (*)(CFXJS_Engine* pEngine, v8::Local obj); - static CFXJS_Engine* CurrentEngineFromIsolate(v8::Isolate* pIsolate); + static CFXJS_Engine* EngineFromIsolateCurrentContext(v8::Isolate* pIsolate); + static CFXJS_Engine* EngineFromContext(v8::Local pContext); + static void SetEngineInContext(CFXJS_Engine* pEngine, + v8::Local pContext); + static int GetObjDefnID(v8::Local pObj); // Always returns a valid, newly-created objDefnID. -- cgit v1.2.3