From 848a13b6777cbff8cc4aea3ab3d69eaa0b82ae6c Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Fri, 9 Oct 2015 13:14:47 -0700 Subject: Pass IJS_Runtime, not IJS_Context, to native object constructors. This better separates the two IJS_ classes, with the IJS_Context taking on its proper role of describing an event. There's no need for the event details for object creation, so this gets much cleaner. Move some JS error reporting code from CJS_Context to CJS_Runtime. Make InitInstance() and ExitInstance() voids, they always return TRUE and we never check the result anyways. R=thestig@chromium.org Review URL: https://codereview.chromium.org/1389163007 . --- 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 | 14 ++++- 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 | 20 ++++--- fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp | 4 +- 21 files changed, 115 insertions(+), 180 deletions(-) diff --git a/fpdfsdk/include/javascript/IJavaScript.h b/fpdfsdk/include/javascript/IJavaScript.h index bf90210794..c4c5d0d053 100644 --- a/fpdfsdk/include/javascript/IJavaScript.h +++ b/fpdfsdk/include/javascript/IJavaScript.h @@ -16,10 +16,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; @@ -131,6 +132,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); @@ -142,6 +144,9 @@ class IJS_Runtime { virtual IJS_Context* GetCurrentContext() = 0; 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; protected: IJS_Runtime() {} diff --git a/fpdfsdk/include/jsapi/fxjs_v8.h b/fpdfsdk/include/jsapi/fxjs_v8.h index 651414b53b..13fbb02a72 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. enum FXJSOBJTYPE { FXJSOBJTYPE_DYNAMIC = 0, // Created by native method and returned to JS. @@ -61,7 +61,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. @@ -119,8 +119,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); @@ -130,11 +129,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 3d1a1571a1..874fcc7ff5 100644 --- a/fpdfsdk/src/fsdk_actionhandler.cpp +++ b/fpdfsdk/src/fsdk_actionhandler.cpp @@ -148,18 +148,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. } @@ -292,65 +288,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); @@ -388,20 +332,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); @@ -601,9 +540,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); @@ -626,9 +565,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); @@ -682,9 +621,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 ff71c2bcee..f6b6b14483 100644 --- a/fpdfsdk/src/fsdk_baseform.cpp +++ b/fpdfsdk/src/fsdk_baseform.cpp @@ -1418,8 +1418,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"); @@ -1766,7 +1765,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(); @@ -1784,7 +1782,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) { @@ -1846,7 +1844,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) { @@ -2144,7 +2142,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 7c1f5775d2..24f55b03f9 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); @@ -835,7 +830,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()); @@ -1394,7 +1389,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; @@ -1403,7 +1397,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; @@ -1446,7 +1440,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; @@ -1642,7 +1636,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 8f68769eb9..4e6fdee218 100644 --- a/fpdfsdk/src/javascript/Document.h +++ b/fpdfsdk/src/javascript/Document.h @@ -309,7 +309,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 56fc56b206..021a3a6a7b 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 b67096d9bc..cfca043333 100644 --- a/fpdfsdk/src/javascript/JS_Context.cpp +++ b/fpdfsdk/src/javascript/JS_Context.cpp @@ -33,14 +33,14 @@ 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::HandleScope handle_scope(m_pRuntime->GetIsolate()); v8::Local context = m_pRuntime->NewJSContext(); 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; @@ -49,24 +49,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 cb39841c53..3e5b797d07 100644 --- a/fpdfsdk/src/javascript/JS_Define.h +++ b/fpdfsdk/src/javascript/JS_Define.h @@ -247,22 +247,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 069edc5cd6..4331476bed 100644 --- a/fpdfsdk/src/javascript/JS_Runtime.cpp +++ b/fpdfsdk/src/javascript/JS_Runtime.cpp @@ -64,7 +64,7 @@ CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) DefineJSObjects(); CJS_Context* pContext = (CJS_Context*)NewContext(); - FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context); + FXJS_InitializeRuntime(GetIsolate(), this, m_context); ReleaseContext(pContext); } @@ -181,6 +181,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 8cbec38fc6..d90891eff0 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 2c5e625fdc..a7b081faf7 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; } @@ -136,6 +136,12 @@ class CJS_RuntimeStub final : public IJS_Runtime { } CPDFSDK_Document* GetReaderDocument() override { return m_pDoc; } + 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 93a627c97a..3db1e5b956 100644 --- a/fpdfsdk/src/javascript/app.cpp +++ b/fpdfsdk/src/javascript/app.cpp @@ -123,7 +123,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); @@ -402,7 +402,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(); @@ -443,7 +443,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(); @@ -553,14 +553,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 c8510d27dc..cd0048d95b 100644 --- a/fpdfsdk/src/javascript/global.cpp +++ b/fpdfsdk/src/javascript/global.cpp @@ -87,17 +87,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 e674a3f0b2..8a54475910 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; @@ -286,7 +285,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,12 +304,12 @@ void FXJS_InitializeRuntime(v8::Isolate* pIsolate, ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); if (pObjDef->m_pConstructor) - pObjDef->m_pConstructor(context, v8Context->Global() - ->GetPrototype() - ->ToObject(v8Context) - .ToLocalChecked()); + 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); } @@ -359,7 +358,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); @@ -386,7 +384,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(); @@ -413,7 +411,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 187d611770..b0f3f85167 100644 --- a/fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp +++ b/fpdfsdk/src/jsapi/fxjs_v8_embeddertest.cpp @@ -32,7 +32,7 @@ class FXJSV8Embeddertest : public EmbedderTest { v8::Isolate::Scope isolate_scope(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 { @@ -61,7 +61,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