From 3342090d65e852f3f37a8f04d3fbe3ff2467d05c Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Tue, 13 Oct 2015 15:00:10 -0700 Subject: Merge to XFA: Pass IJS_Runtime, not IJS_Context, to native object constructors. Original Review URL: https://codereview.chromium.org/1389163007 . (cherry picked from commit 848a13b6777cbff8cc4aea3ab3d69eaa0b82ae6c) Merge conflics in: fpdfsdk/src/javascript/JS_Runtime.cpp fpdfsdk/src/javascript/JS_Runtime_Stub.cpp fpdfsdk/src/jsapi/fxjs_v8.cpp R=thestig@chromium.org Review URL: https://codereview.chromium.org/1394993006 . --- fpdfsdk/include/javascript/IJavaScript.h | 7 ++- fpdfsdk/include/jsapi/fxjs_v8.h | 16 +++--- fpdfsdk/src/fsdk_actionhandler.cpp | 85 +++++------------------------- fpdfsdk/src/fsdk_baseform.cpp | 10 ++-- fpdfsdk/src/javascript/Document.cpp | 28 ++++------ fpdfsdk/src/javascript/Document.h | 2 +- fpdfsdk/src/javascript/Field.cpp | 18 +++---- fpdfsdk/src/javascript/Field.h | 2 +- fpdfsdk/src/javascript/JS_Context.cpp | 18 +++---- fpdfsdk/src/javascript/JS_Context.h | 2 +- fpdfsdk/src/javascript/JS_Define.h | 16 +++--- fpdfsdk/src/javascript/JS_EventHandler.cpp | 8 +-- fpdfsdk/src/javascript/JS_Object.h | 4 +- fpdfsdk/src/javascript/JS_Runtime.cpp | 16 +++++- fpdfsdk/src/javascript/JS_Runtime.h | 3 ++ fpdfsdk/src/javascript/JS_Runtime_Stub.cpp | 8 ++- fpdfsdk/src/javascript/app.cpp | 11 ++-- fpdfsdk/src/javascript/global.cpp | 17 +++--- fpdfsdk/src/javascript/global.h | 2 +- fpdfsdk/src/jsapi/fxjs_v8.cpp | 23 ++++---- fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp | 4 +- 21 files changed, 117 insertions(+), 183 deletions(-) diff --git a/fpdfsdk/include/javascript/IJavaScript.h b/fpdfsdk/include/javascript/IJavaScript.h index f78ee52793..c8962bd343 100644 --- a/fpdfsdk/include/javascript/IJavaScript.h +++ b/fpdfsdk/include/javascript/IJavaScript.h @@ -17,10 +17,11 @@ class CPDFSDK_Document; class CPDF_Bookmark; class CPDF_FormField; +// Records the details of an event and triggers JS execution for it. class IJS_Context { public: virtual FX_BOOL RunScript(const CFX_WideString& script, - CFX_WideString& info) = 0; + CFX_WideString* info) = 0; virtual void OnApp_Init() = 0; @@ -132,6 +133,7 @@ class IJS_Context { virtual ~IJS_Context() {} }; +// Owns the FJXS objects needed to actually execute JS. class IJS_Runtime { public: static void Initialize(unsigned int slot, void* isolate); @@ -144,6 +146,9 @@ class IJS_Runtime { virtual void SetReaderDocument(CPDFSDK_Document* pReaderDoc) = 0; virtual CPDFSDK_Document* GetReaderDocument() = 0; + virtual int Execute(IJS_Context* cc, + const wchar_t* script, + CFX_WideString* info) = 0; virtual FX_BOOL GetHValueByName(const CFX_ByteStringC& utf8Name, FXJSE_HVALUE hValue) = 0; diff --git a/fpdfsdk/include/jsapi/fxjs_v8.h b/fpdfsdk/include/jsapi/fxjs_v8.h index d3cd4ebe6e..7c6f49798d 100644 --- a/fpdfsdk/include/jsapi/fxjs_v8.h +++ b/fpdfsdk/include/jsapi/fxjs_v8.h @@ -17,10 +17,10 @@ #include #include "../../../core/include/fxcrt/fx_basic.h" -// FXJS_V8 places no interpretation on these two classes; it merely -// passes them on to the caller-provided FXJS_CONSTRUCTORs. -class IJS_Context; -class IJS_Runtime; +// FXJS_V8 places no restrictions on these two classes; it merely passes them +// 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. // FXJS_V8 places no interpreation on this calass; it merely passes it // along to XFA. @@ -66,7 +66,7 @@ class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { void Free(void* data, size_t length) override; }; -using FXJS_CONSTRUCTOR = void (*)(IJS_Context* cc, v8::Local obj); +using FXJS_CONSTRUCTOR = void (*)(IJS_Runtime* cc, v8::Local obj); using FXJS_DESTRUCTOR = void (*)(v8::Local obj); // Call before making FXJS_PrepareIsolate call. @@ -124,8 +124,7 @@ void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, // Called after FXJS_Define* calls made. void FXJS_InitializeRuntime(v8::Isolate* pIsolate, - IJS_Runtime* pFXRuntime, - IJS_Context* context, + IJS_Runtime* pIRuntime, v8::Global& v8PersistentContext); void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, v8::Global& v8PersistentContext); @@ -135,11 +134,10 @@ IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate); int FXJS_Execute(v8::Isolate* pIsolate, IJS_Context* pJSContext, const wchar_t* script, - long length, FXJSErr* perror); v8::Local FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, - IJS_Context* pJSContext, + IJS_Runtime* pJSContext, int nObjDefnID); v8::Local FXJS_GetThisObj(v8::Isolate* pIsolate); int FXJS_GetObjDefnID(v8::Local pObj); diff --git a/fpdfsdk/src/fsdk_actionhandler.cpp b/fpdfsdk/src/fsdk_actionhandler.cpp index 6cb37b9c80..feb9a85137 100644 --- a/fpdfsdk/src/fsdk_actionhandler.cpp +++ b/fpdfsdk/src/fsdk_actionhandler.cpp @@ -149,18 +149,14 @@ FX_BOOL CPDFSDK_ActionHandler::ExecuteLinkAction(const CPDF_Action& action, if (pEnv->IsJSInitiated()) { CFX_WideString swJS = action.GetJavaScript(); if (!swJS.IsEmpty()) { - IJS_Runtime* pRuntime = pDocument->GetJsRuntime(); //???? - ASSERT(pRuntime != NULL); - + IJS_Runtime* pRuntime = pDocument->GetJsRuntime(); pRuntime->SetReaderDocument(pDocument); IJS_Context* pContext = pRuntime->NewContext(); - ASSERT(pContext != NULL); - pContext->OnLink_MouseUp(pDocument); CFX_WideString csInfo; - FX_BOOL bRet = pContext->RunScript(swJS, csInfo); + FX_BOOL bRet = pContext->RunScript(swJS, &csInfo); if (!bRet) { // FIXME: return error. } @@ -293,65 +289,13 @@ FX_BOOL CPDFSDK_ActionHandler::ExecuteScreenAction( CFX_WideString swJS = action.GetJavaScript(); if (!swJS.IsEmpty()) { IJS_Runtime* pRuntime = pDocument->GetJsRuntime(); - ASSERT(pRuntime != NULL); - pRuntime->SetReaderDocument(pDocument); IJS_Context* pContext = pRuntime->NewContext(); - ASSERT(pContext != NULL); - - // switch (type) - // { - // case CPDF_AAction::CursorEnter: - // pContext->OnScreen_MouseEnter(IsCTRLpressed(), - // IsSHIFTpressed(), pScreen); - // break; - // case CPDF_AAction::CursorExit: - // pContext->OnScreen_MouseExit(IsCTRLpressed(), - // IsSHIFTpressed(), pScreen); - // break; - // case CPDF_AAction::ButtonDown: - // pContext->OnScreen_MouseDown(IsCTRLpressed(), - // IsSHIFTpressed(), pScreen); - // break; - // case CPDF_AAction::ButtonUp: - // pContext->OnScreen_MouseUp(IsCTRLpressed(), - // IsSHIFTpressed(), pScreen); - // break; - // case CPDF_AAction::GetFocus: - // pContext->OnScreen_Focus(IsCTRLpressed(), - // IsSHIFTpressed(), pScreen); - // break; - // case CPDF_AAction::LoseFocus: - // pContext->OnScreen_Blur(IsCTRLpressed(), - // IsSHIFTpressed(), pScreen); - // break; - // case CPDF_AAction::PageOpen: - // pContext->OnScreen_Open(IsCTRLpressed(), - // IsSHIFTpressed(), pScreen); - // break; - // case CPDF_AAction::PageClose: - // pContext->OnScreen_Close(IsCTRLpressed(), - // IsSHIFTpressed(), pScreen); - // break; - // case CPDF_AAction::PageVisible: - // pContext->OnScreen_InView(IsCTRLpressed(), - // IsSHIFTpressed(), pScreen); - // break; - // case CPDF_AAction::PageInvisible: - // pContext->OnScreen_OutView(IsCTRLpressed(), - // IsSHIFTpressed(), pScreen); - // break; - // default: - // ASSERT(FALSE); - // break; - // } - CFX_WideString csInfo; - FX_BOOL bRet = pContext->RunScript(swJS, csInfo); + FX_BOOL bRet = pContext->RunScript(swJS, &csInfo); if (!bRet) { - // CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(), - // csInfo); + // FIXME: return error. } pRuntime->ReleaseContext(pContext); @@ -389,20 +333,15 @@ FX_BOOL CPDFSDK_ActionHandler::ExecuteBookMark(const CPDF_Action& action, CFX_WideString swJS = action.GetJavaScript(); if (!swJS.IsEmpty()) { IJS_Runtime* pRuntime = pDocument->GetJsRuntime(); - ASSERT(pRuntime != NULL); - pRuntime->SetReaderDocument(pDocument); IJS_Context* pContext = pRuntime->NewContext(); - ASSERT(pContext != NULL); - pContext->OnBookmark_MouseUp(pBookmark); CFX_WideString csInfo; - FX_BOOL bRet = pContext->RunScript(swJS, csInfo); + FX_BOOL bRet = pContext->RunScript(swJS, &csInfo); if (!bRet) { - // CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(), - // csInfo); + // FIXME: return error. } pRuntime->ReleaseContext(pContext); @@ -602,9 +541,9 @@ void CPDFSDK_ActionHandler::RunFieldJavaScript(CPDFSDK_Document* pDocument, } CFX_WideString csInfo; - FX_BOOL bRet = pContext->RunScript(script, csInfo); + FX_BOOL bRet = pContext->RunScript(script, &csInfo); if (!bRet) { - // CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(), csInfo); + // FIXME: return error. } pRuntime->ReleaseContext(pContext); @@ -627,9 +566,9 @@ void CPDFSDK_ActionHandler::RunDocumentOpenJavaScript( pContext->OnDoc_Open(pDocument, sScriptName); CFX_WideString csInfo; - FX_BOOL bRet = pContext->RunScript(script, csInfo); + FX_BOOL bRet = pContext->RunScript(script, &csInfo); if (!bRet) { - // CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(), csInfo); + // FIXME: return error. } pRuntime->ReleaseContext(pContext); @@ -683,9 +622,9 @@ void CPDFSDK_ActionHandler::RunDocumentPageJavaScript( } CFX_WideString csInfo; - FX_BOOL bRet = pContext->RunScript(script, csInfo); + FX_BOOL bRet = pContext->RunScript(script, &csInfo); if (!bRet) { - // CBCL_FormNotify::MsgBoxJSError(pPageView->GetPageViewWnd(), csInfo); + // FIXME: return error. } pRuntime->ReleaseContext(pContext); diff --git a/fpdfsdk/src/fsdk_baseform.cpp b/fpdfsdk/src/fsdk_baseform.cpp index 4a6b98782c..fb79227df1 100644 --- a/fpdfsdk/src/fsdk_baseform.cpp +++ b/fpdfsdk/src/fsdk_baseform.cpp @@ -1996,8 +1996,7 @@ void CPDFSDK_Widget::AddImageToAppearance(const CFX_ByteString& sAPType, CPDF_Stream* pImage) { ASSERT(pImage != NULL); - CPDF_Document* pDoc = - m_pPageView->GetPDFDocument(); // pDocument->GetDocument(); + CPDF_Document* pDoc = m_pPageView->GetPDFDocument(); ASSERT(pDoc != NULL); CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDict("AP"); @@ -2453,7 +2452,6 @@ void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) { for (int i = 0; i < nSize; i++) { if (CPDF_FormField* pField = m_pInterForm->GetFieldInCalculationOrder(i)) { - // ASSERT(pField != NULL); int nType = pField->GetFieldType(); if (nType == FIELDTYPE_COMBOBOX || nType == FIELDTYPE_TEXTFIELD) { CPDF_AAction aAction = pField->GetAdditionalAction(); @@ -2471,7 +2469,7 @@ void CPDFSDK_InterForm::OnCalculate(CPDF_FormField* pFormField) { pContext->OnField_Calculate(pFormField, pField, sValue, bRC); CFX_WideString sInfo; - FX_BOOL bRet = pContext->RunScript(csJS, sInfo); + FX_BOOL bRet = pContext->RunScript(csJS, &sInfo); pRuntime->ReleaseContext(pContext); if (bRet) { @@ -2533,7 +2531,7 @@ CFX_WideString CPDFSDK_InterForm::OnFormat(CPDF_FormField* pFormField, pContext->OnField_Format(pFormField, Value, TRUE); CFX_WideString sInfo; - FX_BOOL bRet = pContext->RunScript(script, sInfo); + FX_BOOL bRet = pContext->RunScript(script, &sInfo); pRuntime->ReleaseContext(pContext); if (bRet) { @@ -2831,7 +2829,7 @@ FX_BOOL CPDFSDK_InterForm::ExportFieldsToFDFTextBuf(const CFX_PtrArray& fields, m_pDocument->GetPath(), (CFX_PtrArray&)fields, bIncludeOrExclude); if (!pFDF) return FALSE; - FX_BOOL bRet = pFDF->WriteBuf(textBuf); // = FALSE;// + FX_BOOL bRet = pFDF->WriteBuf(textBuf); delete pFDF; return bRet; diff --git a/fpdfsdk/src/javascript/Document.cpp b/fpdfsdk/src/javascript/Document.cpp index c1bd029351..bcf8af6595 100644 --- a/fpdfsdk/src/javascript/Document.cpp +++ b/fpdfsdk/src/javascript/Document.cpp @@ -140,17 +140,12 @@ END_JS_STATIC_METHOD() IMPLEMENT_JS_CLASS(CJS_Document, Document) -FX_BOOL CJS_Document::InitInstance(IJS_Context* cc) { - CJS_Context* pContext = (CJS_Context*)cc; - ASSERT(pContext != NULL); - - Document* pDoc = (Document*)GetEmbedObject(); - ASSERT(pDoc != NULL); - - pDoc->AttachDoc(pContext->GetReaderDocument()); - pDoc->SetIsolate(pContext->GetJSRuntime()->GetIsolate()); - return TRUE; -}; +void CJS_Document::InitInstance(IJS_Runtime* pIRuntime) { + CJS_Runtime* pRuntime = static_cast(pIRuntime); + Document* pDoc = static_cast(GetEmbedObject()); + pDoc->AttachDoc(pRuntime->GetReaderDocument()); + pDoc->SetIsolate(pRuntime->GetIsolate()); +} /* --------------------------------- Document --------------------------------- */ @@ -326,7 +321,7 @@ FX_BOOL Document::getField(IJS_Context* cc, CJS_Runtime* pRuntime = pContext->GetJSRuntime(); v8::Local pFieldObj = FXJS_NewFxDynamicObj( - pRuntime->GetIsolate(), pContext, CJS_Field::g_nObjDefnID); + pRuntime->GetIsolate(), pRuntime, CJS_Field::g_nObjDefnID); v8::Isolate* isolate = GetIsolate(cc); CJS_Field* pJSField = (CJS_Field*)FXJS_GetPrivate(isolate, pFieldObj); @@ -837,7 +832,7 @@ FX_BOOL Document::info(IJS_Context* cc, CJS_Context* pContext = (CJS_Context*)cc; CJS_Runtime* pRuntime = pContext->GetJSRuntime(); v8::Local pObj = - FXJS_NewFxDynamicObj(pRuntime->GetIsolate(), pContext, -1); + FXJS_NewFxDynamicObj(pRuntime->GetIsolate(), pRuntime, -1); FXJS_PutObjectString(isolate, pObj, L"Author", cwAuthor.c_str()); FXJS_PutObjectString(isolate, pObj, L"Title", cwTitle.c_str()); FXJS_PutObjectString(isolate, pObj, L"Subject", cwSubject.c_str()); @@ -1433,7 +1428,6 @@ FX_BOOL Document::icons(IJS_Context* cc, return TRUE; } - CJS_Context* pContext = static_cast(cc); CJS_Runtime* pRuntime = CJS_Runtime::FromContext(cc); CJS_Array Icons(pRuntime); IconElement* pIconElement = NULL; @@ -1442,7 +1436,7 @@ FX_BOOL Document::icons(IJS_Context* cc, pIconElement = (*m_pIconTree)[i]; v8::Local pObj = FXJS_NewFxDynamicObj( - pRuntime->GetIsolate(), pContext, CJS_Icon::g_nObjDefnID); + pRuntime->GetIsolate(), pRuntime, CJS_Icon::g_nObjDefnID); if (pObj.IsEmpty()) return FALSE; @@ -1485,7 +1479,7 @@ FX_BOOL Document::getIcon(IJS_Context* cc, Icon* pRetIcon = (*m_pIconTree)[i]->IconStream; v8::Local pObj = FXJS_NewFxDynamicObj( - pRuntime->GetIsolate(), pContext, CJS_Icon::g_nObjDefnID); + pRuntime->GetIsolate(), pRuntime, CJS_Icon::g_nObjDefnID); if (pObj.IsEmpty()) return FALSE; @@ -1686,7 +1680,7 @@ FX_BOOL Document::getPrintParams(IJS_Context* cc, CJS_Context* pContext = (CJS_Context*)cc; CJS_Runtime* pRuntime = pContext->GetJSRuntime(); v8::Local pRetObj = FXJS_NewFxDynamicObj( - pRuntime->GetIsolate(), pContext, CJS_PrintParamsObj::g_nObjDefnID); + pRuntime->GetIsolate(), pRuntime, CJS_PrintParamsObj::g_nObjDefnID); // Not implemented yet. diff --git a/fpdfsdk/src/javascript/Document.h b/fpdfsdk/src/javascript/Document.h index 516045e35b..149102d0d1 100644 --- a/fpdfsdk/src/javascript/Document.h +++ b/fpdfsdk/src/javascript/Document.h @@ -310,7 +310,7 @@ class CJS_Document : public CJS_Object { ~CJS_Document() override {} // CJS_Object - FX_BOOL InitInstance(IJS_Context* cc) override; + void InitInstance(IJS_Runtime* pIRuntime) override; DECLARE_JS_CLASS(); diff --git a/fpdfsdk/src/javascript/Field.cpp b/fpdfsdk/src/javascript/Field.cpp index f04c379d34..f3d12b479a 100644 --- a/fpdfsdk/src/javascript/Field.cpp +++ b/fpdfsdk/src/javascript/Field.cpp @@ -110,16 +110,10 @@ END_JS_STATIC_METHOD() IMPLEMENT_JS_CLASS(CJS_Field, Field) -FX_BOOL CJS_Field::InitInstance(IJS_Context* cc) { - CJS_Context* pContext = (CJS_Context*)cc; - ASSERT(pContext != NULL); - - Field* pField = (Field*)GetEmbedObject(); - ASSERT(pField != NULL); - - pField->SetIsolate(pContext->GetJSRuntime()->GetIsolate()); - - return TRUE; +void CJS_Field::InitInstance(IJS_Runtime* pIRuntime) { + CJS_Runtime* pRuntime = static_cast(pIRuntime); + Field* pField = static_cast(GetEmbedObject()); + pField->SetIsolate(pRuntime->GetIsolate()); }; Field::Field(CJS_Object* pJSObject) @@ -3279,7 +3273,7 @@ FX_BOOL Field::buttonGetIcon(IJS_Context* cc, CJS_Context* pContext = (CJS_Context*)cc; CJS_Runtime* pRuntime = pContext->GetJSRuntime(); v8::Local pObj = FXJS_NewFxDynamicObj( - pRuntime->GetIsolate(), pContext, CJS_Icon::g_nObjDefnID); + pRuntime->GetIsolate(), pRuntime, CJS_Icon::g_nObjDefnID); ASSERT(pObj.IsEmpty() == FALSE); CJS_Icon* pJS_Icon = (CJS_Icon*)FXJS_GetPrivate(pRuntime->GetIsolate(), pObj); @@ -3488,7 +3482,7 @@ FX_BOOL Field::getArray(IJS_Context* cc, for (int j = 0, jsz = swSort.GetSize(); j < jsz; j++) { nonstd::unique_ptr pStr(swSort.GetAt(j)); v8::Local pObj = FXJS_NewFxDynamicObj( - pRuntime->GetIsolate(), pContext, CJS_Field::g_nObjDefnID); + pRuntime->GetIsolate(), pRuntime, CJS_Field::g_nObjDefnID); ASSERT(!pObj.IsEmpty()); CJS_Field* pJSField = diff --git a/fpdfsdk/src/javascript/Field.h b/fpdfsdk/src/javascript/Field.h index b263241712..21ae04c960 100644 --- a/fpdfsdk/src/javascript/Field.h +++ b/fpdfsdk/src/javascript/Field.h @@ -501,7 +501,7 @@ class CJS_Field : public CJS_Object { CJS_Field(v8::Local pObject) : CJS_Object(pObject) {} ~CJS_Field(void) override {} - FX_BOOL InitInstance(IJS_Context* cc) override; + void InitInstance(IJS_Runtime* pIRuntime) override; DECLARE_JS_CLASS(); JS_STATIC_PROP(alignment, Field); diff --git a/fpdfsdk/src/javascript/JS_Context.cpp b/fpdfsdk/src/javascript/JS_Context.cpp index 4769dac4f7..a53e745786 100644 --- a/fpdfsdk/src/javascript/JS_Context.cpp +++ b/fpdfsdk/src/javascript/JS_Context.cpp @@ -33,7 +33,7 @@ CPDFDoc_Environment* CJS_Context::GetReaderApp() { } FX_BOOL CJS_Context::RunScript(const CFX_WideString& script, - CFX_WideString& info) { + CFX_WideString* info) { v8::Isolate::Scope isolate_scope(m_pRuntime->GetIsolate()); v8::Locker locker(m_pRuntime->GetIsolate()); v8::HandleScope handle_scope(m_pRuntime->GetIsolate()); @@ -41,7 +41,7 @@ FX_BOOL CJS_Context::RunScript(const CFX_WideString& script, v8::Context::Scope context_scope(context); if (m_bBusy) { - info = JSGetStringFromID(this, IDS_STRING_JSBUSY); + *info = JSGetStringFromID(this, IDS_STRING_JSBUSY); return FALSE; } m_bBusy = TRUE; @@ -50,24 +50,20 @@ FX_BOOL CJS_Context::RunScript(const CFX_WideString& script, CJS_Runtime::FieldEvent event(m_pEventHandler->TargetName(), m_pEventHandler->EventType()); if (!m_pRuntime->AddEventToSet(event)) { - info = JSGetStringFromID(this, IDS_STRING_JSEVENT); + *info = JSGetStringFromID(this, IDS_STRING_JSEVENT); return FALSE; } - FXJSErr error = {NULL, NULL, 0}; + CFX_WideString sErrorMessage; int nRet = 0; if (script.GetLength() > 0) { - nRet = FXJS_Execute(m_pRuntime->GetIsolate(), this, script.c_str(), - script.GetLength(), &error); + nRet = m_pRuntime->Execute(this, script.c_str(), &sErrorMessage); } if (nRet < 0) { - CFX_WideString sLine; - sLine.Format(L"[ Line: %05d { %s } ] : %s", error.linnum - 1, error.srcline, - error.message); - info += sLine; + *info += sErrorMessage; } else { - info = JSGetStringFromID(this, IDS_STRING_RUN); + *info = JSGetStringFromID(this, IDS_STRING_RUN); } m_pRuntime->RemoveEventFromSet(event); diff --git a/fpdfsdk/src/javascript/JS_Context.h b/fpdfsdk/src/javascript/JS_Context.h index 7fbcbec876..dc5d8339bf 100644 --- a/fpdfsdk/src/javascript/JS_Context.h +++ b/fpdfsdk/src/javascript/JS_Context.h @@ -21,7 +21,7 @@ class CJS_Context : public IJS_Context { // IJS_Context FX_BOOL RunScript(const CFX_WideString& script, - CFX_WideString& info) override; + CFX_WideString* info) override; void OnApp_Init() override; void OnDoc_Open(CPDFSDK_Document* pDoc, const CFX_WideString& strTargetName) override; diff --git a/fpdfsdk/src/javascript/JS_Define.h b/fpdfsdk/src/javascript/JS_Define.h index 178e28f3da..252c50ff1b 100644 --- a/fpdfsdk/src/javascript/JS_Define.h +++ b/fpdfsdk/src/javascript/JS_Define.h @@ -245,22 +245,22 @@ void JSMethod(const char* method_name_string, DefineMethods(pIsolate); \ } -#define DECLARE_JS_CLASS_RICH_PART() \ - static void JSConstructor(IJS_Context* cc, v8::Local obj); \ - static void JSDestructor(v8::Local obj); \ - static void DefineProps(v8::Isolate* pIsoalte); \ - static void DefineMethods(v8::Isolate* pIsoalte); \ - static JSPropertySpec JS_Class_Properties[]; \ +#define DECLARE_JS_CLASS_RICH_PART() \ + static void JSConstructor(IJS_Runtime* pRuntime, v8::Local obj); \ + static void JSDestructor(v8::Local obj); \ + static void DefineProps(v8::Isolate* pIsoalte); \ + static void DefineMethods(v8::Isolate* pIsoalte); \ + static JSPropertySpec JS_Class_Properties[]; \ static JSMethodSpec JS_Class_Methods[]; #define IMPLEMENT_JS_CLASS_RICH_PART(js_class_name, class_alternate, \ class_name) \ - void js_class_name::JSConstructor(IJS_Context* cc, \ + void js_class_name::JSConstructor(IJS_Runtime* pIRuntime, \ v8::Local obj) { \ CJS_Object* pObj = new js_class_name(obj); \ pObj->SetEmbedObject(new class_alternate(pObj)); \ FXJS_SetPrivate(nullptr, obj, (void*)pObj); \ - pObj->InitInstance(cc); \ + pObj->InitInstance(pIRuntime); \ } \ void js_class_name::JSDestructor(v8::Local obj) { \ js_class_name* pObj = (js_class_name*)FXJS_GetPrivate(nullptr, obj); \ diff --git a/fpdfsdk/src/javascript/JS_EventHandler.cpp b/fpdfsdk/src/javascript/JS_EventHandler.cpp index dd5c4427ce..cb86fde24d 100644 --- a/fpdfsdk/src/javascript/JS_EventHandler.cpp +++ b/fpdfsdk/src/javascript/JS_EventHandler.cpp @@ -613,11 +613,11 @@ FX_BOOL CJS_EventHandler::Shift() { Field* CJS_EventHandler::Source() { CJS_Runtime* pRuntime = m_pJSContext->GetJSRuntime(); v8::Local pDocObj = FXJS_NewFxDynamicObj( - pRuntime->GetIsolate(), m_pJSContext, CJS_Document::g_nObjDefnID); + pRuntime->GetIsolate(), pRuntime, CJS_Document::g_nObjDefnID); ASSERT(!pDocObj.IsEmpty()); v8::Local pFieldObj = FXJS_NewFxDynamicObj( - pRuntime->GetIsolate(), m_pJSContext, CJS_Field::g_nObjDefnID); + pRuntime->GetIsolate(), pRuntime, CJS_Field::g_nObjDefnID); ASSERT(!pFieldObj.IsEmpty()); CJS_Document* pJSDocument = @@ -636,11 +636,11 @@ Field* CJS_EventHandler::Source() { Field* CJS_EventHandler::Target_Field() { CJS_Runtime* pRuntime = m_pJSContext->GetJSRuntime(); v8::Local pDocObj = FXJS_NewFxDynamicObj( - pRuntime->GetIsolate(), m_pJSContext, CJS_Document::g_nObjDefnID); + pRuntime->GetIsolate(), pRuntime, CJS_Document::g_nObjDefnID); ASSERT(!pDocObj.IsEmpty()); v8::Local pFieldObj = FXJS_NewFxDynamicObj( - pRuntime->GetIsolate(), m_pJSContext, CJS_Field::g_nObjDefnID); + pRuntime->GetIsolate(), pRuntime, CJS_Field::g_nObjDefnID); ASSERT(!pFieldObj.IsEmpty()); CJS_Document* pJSDocument = diff --git a/fpdfsdk/src/javascript/JS_Object.h b/fpdfsdk/src/javascript/JS_Object.h index 563a316bd6..50751523f5 100644 --- a/fpdfsdk/src/javascript/JS_Object.h +++ b/fpdfsdk/src/javascript/JS_Object.h @@ -49,8 +49,8 @@ class CJS_Object { virtual FX_BOOL IsType(const FX_CHAR* sClassName) { return TRUE; } virtual CFX_ByteString GetClassName() { return ""; } - virtual FX_BOOL InitInstance(IJS_Context* cc) { return TRUE; } - virtual FX_BOOL ExitInstance() { return TRUE; } + virtual void InitInstance(IJS_Runtime* pIRuntime) {} + virtual void ExitInstance() {} v8::Local ToV8Object() { return m_pV8Object.Get(m_pIsolate); } diff --git a/fpdfsdk/src/javascript/JS_Runtime.cpp b/fpdfsdk/src/javascript/JS_Runtime.cpp index 1d2fa84323..8b0475b851 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, pContext, m_context); + FXJS_InitializeRuntime(GetIsolate(), this, m_context); 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, pContext, m_context); + FXJS_InitializeRuntime(GetIsolate(), this, m_context); ReleaseContext(pContext); } @@ -208,6 +208,18 @@ void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { } } +int CJS_Runtime::Execute(IJS_Context* cc, + const wchar_t* script, + CFX_WideString* info) { + FXJSErr error = {}; + int nRet = FXJS_Execute(m_isolate, cc, script, &error); + if (nRet < 0) { + info->Format(L"[ Line: %05d { %s } ] : %s", error.linnum - 1, error.srcline, + error.message); + } + return nRet; +} + bool CJS_Runtime::AddEventToSet(const FieldEvent& event) { return m_FieldEventSet.insert(event).second; } diff --git a/fpdfsdk/src/javascript/JS_Runtime.h b/fpdfsdk/src/javascript/JS_Runtime.h index c259a4188e..4772823d7f 100644 --- a/fpdfsdk/src/javascript/JS_Runtime.h +++ b/fpdfsdk/src/javascript/JS_Runtime.h @@ -40,6 +40,9 @@ class CJS_Runtime : public IJS_Runtime { IJS_Context* GetCurrentContext() override; void SetReaderDocument(CPDFSDK_Document* pReaderDoc) override; CPDFSDK_Document* GetReaderDocument() override { return m_pDocument; } + int Execute(IJS_Context* cc, + const wchar_t* script, + CFX_WideString* info) override; CPDFDoc_Environment* GetReaderApp() const { return m_pApp; } diff --git a/fpdfsdk/src/javascript/JS_Runtime_Stub.cpp b/fpdfsdk/src/javascript/JS_Runtime_Stub.cpp index ea1e6c1c55..7e3d78881a 100644 --- a/fpdfsdk/src/javascript/JS_Runtime_Stub.cpp +++ b/fpdfsdk/src/javascript/JS_Runtime_Stub.cpp @@ -15,7 +15,7 @@ class CJS_ContextStub final : public IJS_Context { // IJS_Context: FX_BOOL RunScript(const CFX_WideString& script, - CFX_WideString& info) override { + CFX_WideString* info) override { return FALSE; } @@ -146,6 +146,12 @@ class CJS_RuntimeStub final : public IJS_Runtime { return FALSE; } + int Execute(IJS_Context* cc, + const wchar_t* script, + CFX_WideString* info) override { + return 0; + } + protected: CPDFSDK_Document* m_pDoc; nonstd::unique_ptr m_pContext; diff --git a/fpdfsdk/src/javascript/app.cpp b/fpdfsdk/src/javascript/app.cpp index 80edaa6c93..80a9b17aa9 100644 --- a/fpdfsdk/src/javascript/app.cpp +++ b/fpdfsdk/src/javascript/app.cpp @@ -124,7 +124,7 @@ FX_BOOL app::activeDocs(IJS_Context* cc, (CJS_Document*)FXJS_GetPrivate(pRuntime->GetIsolate(), pObj); } else { v8::Local pObj = FXJS_NewFxDynamicObj( - pRuntime->GetIsolate(), pContext, CJS_Document::g_nObjDefnID); + pRuntime->GetIsolate(), pRuntime, CJS_Document::g_nObjDefnID); pJSDocument = (CJS_Document*)FXJS_GetPrivate(pRuntime->GetIsolate(), pObj); ASSERT(pJSDocument != NULL); @@ -409,7 +409,7 @@ FX_BOOL app::setInterval(IJS_Context* cc, m_aTimer.Add(pTimer); v8::Local pRetObj = FXJS_NewFxDynamicObj( - pRuntime->GetIsolate(), pContext, CJS_TimerObj::g_nObjDefnID); + pRuntime->GetIsolate(), pRuntime, CJS_TimerObj::g_nObjDefnID); CJS_TimerObj* pJS_TimerObj = (CJS_TimerObj*)FXJS_GetPrivate(pRuntime->GetIsolate(), pRetObj); TimerObj* pTimerObj = (TimerObj*)pJS_TimerObj->GetEmbedObject(); @@ -450,7 +450,7 @@ FX_BOOL app::setTimeOut(IJS_Context* cc, m_aTimer.Add(pTimer); v8::Local pRetObj = FXJS_NewFxDynamicObj( - pRuntime->GetIsolate(), pContext, CJS_TimerObj::g_nObjDefnID); + pRuntime->GetIsolate(), pRuntime, CJS_TimerObj::g_nObjDefnID); CJS_TimerObj* pJS_TimerObj = (CJS_TimerObj*)FXJS_GetPrivate(pRuntime->GetIsolate(), pRetObj); TimerObj* pTimerObj = (TimerObj*)pJS_TimerObj->GetEmbedObject(); @@ -560,14 +560,11 @@ void app::TimerProc(CJS_Timer* pTimer) { } void app::RunJsScript(CJS_Runtime* pRuntime, const CFX_WideString& wsScript) { - ASSERT(pRuntime != NULL); - if (!pRuntime->IsBlocking()) { IJS_Context* pContext = pRuntime->NewContext(); - ASSERT(pContext != NULL); pContext->OnExternal_Exec(); CFX_WideString wtInfo; - pContext->RunScript(wsScript, wtInfo); + pContext->RunScript(wsScript, &wtInfo); pRuntime->ReleaseContext(pContext); } } diff --git a/fpdfsdk/src/javascript/global.cpp b/fpdfsdk/src/javascript/global.cpp index 97a74e792d..403cf58ebf 100644 --- a/fpdfsdk/src/javascript/global.cpp +++ b/fpdfsdk/src/javascript/global.cpp @@ -88,17 +88,12 @@ END_JS_STATIC_METHOD() IMPLEMENT_SPECIAL_JS_CLASS(CJS_Global, JSGlobalAlternate, global); -FX_BOOL CJS_Global::InitInstance(IJS_Context* cc) { - CJS_Context* pContext = (CJS_Context*)cc; - ASSERT(pContext != NULL); - - JSGlobalAlternate* pGlobal = (JSGlobalAlternate*)GetEmbedObject(); - ASSERT(pGlobal != NULL); - - pGlobal->Initial(pContext->GetReaderApp()); - - return TRUE; -}; +void CJS_Global::InitInstance(IJS_Runtime* pIRuntime) { + CJS_Runtime* pRuntime = static_cast(pIRuntime); + JSGlobalAlternate* pGlobal = + static_cast(GetEmbedObject()); + pGlobal->Initial(pRuntime->GetReaderApp()); +} JSGlobalAlternate::JSGlobalAlternate(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject), m_pApp(NULL) { diff --git a/fpdfsdk/src/javascript/global.h b/fpdfsdk/src/javascript/global.h index 30f5d07b31..174e63a760 100644 --- a/fpdfsdk/src/javascript/global.h +++ b/fpdfsdk/src/javascript/global.h @@ -82,7 +82,7 @@ class CJS_Global : public CJS_Object { ~CJS_Global() override {} // CJS_Object - FX_BOOL InitInstance(IJS_Context* cc) override; + void InitInstance(IJS_Runtime* pIRuntime) override; DECLARE_SPECIAL_JS_CLASS(); JS_SPECIAL_STATIC_METHOD(setPersistent, JSGlobalAlternate, global); diff --git a/fpdfsdk/src/jsapi/fxjs_v8.cpp b/fpdfsdk/src/jsapi/fxjs_v8.cpp index f400efbcd5..1f8c5728b0 100644 --- a/fpdfsdk/src/jsapi/fxjs_v8.cpp +++ b/fpdfsdk/src/jsapi/fxjs_v8.cpp @@ -273,8 +273,7 @@ void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, } void FXJS_InitializeRuntime(v8::Isolate* pIsolate, - IJS_Runtime* pFXRuntime, - IJS_Context* context, + IJS_Runtime* pIRuntime, v8::Global& v8PersistentContext) { if (pIsolate == g_isolate) ++g_isolate_ref_count; @@ -287,7 +286,7 @@ void FXJS_InitializeRuntime(v8::Isolate* pIsolate, v8::Context::Scope context_scope(v8Context); FXJS_PerIsolateData::SetUp(pIsolate); - v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); + v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime); int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); for (int i = 0; i < maxID; ++i) { @@ -305,14 +304,13 @@ void FXJS_InitializeRuntime(v8::Isolate* pIsolate, .ToLocalChecked() ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); - if (pObjDef->m_pConstructor) { - pObjDef->m_pConstructor(context, v8Context->Global() - ->GetPrototype() - ->ToObject(v8Context) - .ToLocalChecked()); - } + if (pObjDef->m_pConstructor) + pObjDef->m_pConstructor(pIRuntime, v8Context->Global() + ->GetPrototype() + ->ToObject(v8Context) + .ToLocalChecked()); } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { - v8::Local obj = FXJS_NewFxDynamicObj(pIsolate, context, i); + v8::Local obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i); v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); pObjDef->m_StaticObj.Reset(pIsolate, obj); } @@ -365,7 +363,6 @@ IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate) { int FXJS_Execute(v8::Isolate* pIsolate, IJS_Context* pJSContext, const wchar_t* script, - long length, FXJSErr* pError) { v8::Isolate::Scope isolate_scope(pIsolate); v8::TryCatch try_catch(pIsolate); @@ -392,7 +389,7 @@ int FXJS_Execute(v8::Isolate* pIsolate, } v8::Local FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, - IJS_Context* pJSContext, + IJS_Runtime* pIRuntime, int nObjDefnID) { v8::Isolate::Scope isolate_scope(pIsolate); v8::Local context = pIsolate->GetCurrentContext(); @@ -419,7 +416,7 @@ v8::Local FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID)); if (pObjDef->m_pConstructor) - pObjDef->m_pConstructor(pJSContext, obj); + pObjDef->m_pConstructor(pIRuntime, obj); return obj; } diff --git a/fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp b/fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp index fc2324781a..1c10df7dbf 100644 --- a/fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp +++ b/fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp @@ -33,7 +33,7 @@ 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, nullptr, m_pPersistentContext); + FXJS_InitializeRuntime(m_pIsolate, nullptr, m_pPersistentContext); } void TearDown() override { @@ -63,7 +63,7 @@ TEST_F(FXJSV8Embeddertest, Getters) { FXJSErr error; CFX_WideString wsInfo; CFX_WideString wsScript(kScript); - int sts = FXJS_Execute(isolate(), nullptr, kScript, wcslen(kScript), &error); + int sts = FXJS_Execute(isolate(), nullptr, kScript, &error); EXPECT_EQ(0, sts); v8::Local This = FXJS_GetThisObj(isolate()); -- cgit v1.2.3