From f2d7656f335cf38dfafb97370ffac38b9d4e3fc0 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Mon, 4 Dec 2017 15:42:36 +0000 Subject: XFA JS method cleanup Change-Id: I182388970e422bd31d011628a4f1b905d12b0da0 Reviewed-on: https://pdfium-review.googlesource.com/20193 Reviewed-by: Ryan Harrison Commit-Queue: dsinclair --- fxjs/cfxjse_context.cpp | 94 ++++++++++++++++++++++++------------------------- fxjs/cfxjse_context.h | 6 ---- fxjs/cfxjse_value.cpp | 73 -------------------------------------- fxjs/cfxjse_value.h | 4 --- 4 files changed, 46 insertions(+), 131 deletions(-) diff --git a/fxjs/cfxjse_context.cpp b/fxjs/cfxjse_context.cpp index ad37ca2c0a..bf9bee8652 100644 --- a/fxjs/cfxjse_context.cpp +++ b/fxjs/cfxjse_context.cpp @@ -43,6 +43,48 @@ const char szCompatibleModeScript[] = wchar_t g_FXJSETagString[] = L"FXJSE_HostObject"; +v8::Local CreateReturnValue(v8::Isolate* pIsolate, + v8::TryCatch& trycatch) { + v8::Local hReturnValue = v8::Object::New(pIsolate); + if (trycatch.HasCaught()) { + v8::Local hException = trycatch.Exception(); + v8::Local hMessage = trycatch.Message(); + if (hException->IsObject()) { + v8::Local hValue; + hValue = hException.As()->Get( + v8::String::NewFromUtf8(pIsolate, "name")); + if (hValue->IsString() || hValue->IsStringObject()) + hReturnValue->Set(0, hValue); + else + hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); + + hValue = hException.As()->Get( + v8::String::NewFromUtf8(pIsolate, "message")); + if (hValue->IsString() || hValue->IsStringObject()) + hReturnValue->Set(1, hValue); + else + hReturnValue->Set(1, hMessage->Get()); + } else { + hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); + hReturnValue->Set(1, hMessage->Get()); + } + hReturnValue->Set(2, hException); + hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber())); + hReturnValue->Set(4, hMessage->GetSourceLine()); + v8::Maybe maybe_int = + hMessage->GetStartColumn(pIsolate->GetCurrentContext()); + hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); + maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext()); + hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); + } + return hReturnValue; +} + +v8::Local GetGlobalObjectFromContext( + v8::Local hContext) { + return hContext->Global()->GetPrototype().As(); +} + } // namespace // Note, not in the anonymous namespace due to the friend call @@ -73,11 +115,6 @@ class CFXJSE_ScopeUtil_IsolateHandleContext { v8::Context::Scope m_cscope; }; -v8::Local FXJSE_GetGlobalObjectFromContext( - v8::Local hContext) { - return hContext->Global()->GetPrototype().As(); -} - void FXJSE_UpdateObjectBinding(v8::Local& hObject, CFXJSE_HostObject* lpNewBinding) { ASSERT(!hObject.IsEmpty()); @@ -115,43 +152,6 @@ CFXJSE_HostObject* FXJSE_RetrieveObjectBinding(v8::Local hJSObject, hObject->GetAlignedPointerFromInternalField(1)); } -v8::Local FXJSE_CreateReturnValue(v8::Isolate* pIsolate, - v8::TryCatch& trycatch) { - v8::Local hReturnValue = v8::Object::New(pIsolate); - if (trycatch.HasCaught()) { - v8::Local hException = trycatch.Exception(); - v8::Local hMessage = trycatch.Message(); - if (hException->IsObject()) { - v8::Local hValue; - hValue = hException.As()->Get( - v8::String::NewFromUtf8(pIsolate, "name")); - if (hValue->IsString() || hValue->IsStringObject()) - hReturnValue->Set(0, hValue); - else - hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); - - hValue = hException.As()->Get( - v8::String::NewFromUtf8(pIsolate, "message")); - if (hValue->IsString() || hValue->IsStringObject()) - hReturnValue->Set(1, hValue); - else - hReturnValue->Set(1, hMessage->Get()); - } else { - hReturnValue->Set(0, v8::String::NewFromUtf8(pIsolate, "Error")); - hReturnValue->Set(1, hMessage->Get()); - } - hReturnValue->Set(2, hException); - hReturnValue->Set(3, v8::Integer::New(pIsolate, hMessage->GetLineNumber())); - hReturnValue->Set(4, hMessage->GetSourceLine()); - v8::Maybe maybe_int = - hMessage->GetStartColumn(pIsolate->GetCurrentContext()); - hReturnValue->Set(5, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); - maybe_int = hMessage->GetEndColumn(pIsolate->GetCurrentContext()); - hReturnValue->Set(6, v8::Integer::New(pIsolate, maybe_int.FromMaybe(0))); - } - return hReturnValue; -} - // static std::unique_ptr CFXJSE_Context::Create( v8::Isolate* pIsolate, @@ -182,8 +182,7 @@ std::unique_ptr CFXJSE_Context::Create( v8::Local hRootContext = v8::Local::New( pIsolate, CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext); hNewContext->SetSecurityToken(hRootContext->GetSecurityToken()); - v8::Local hGlobalObject = - FXJSE_GetGlobalObjectFromContext(hNewContext); + v8::Local hGlobalObject = GetGlobalObjectFromContext(hNewContext); FXJSE_UpdateObjectBinding(hGlobalObject, pGlobalObject); pContext->m_hContext.Reset(pIsolate, hNewContext); return pContext; @@ -198,8 +197,7 @@ std::unique_ptr CFXJSE_Context::GetGlobalObject() { CFXJSE_ScopeUtil_IsolateHandleContext scope(this); v8::Local hContext = v8::Local::New(m_pIsolate, m_hContext); - v8::Local hGlobalObject = - FXJSE_GetGlobalObjectFromContext(hContext); + v8::Local hGlobalObject = GetGlobalObjectFromContext(hContext); pValue->ForceSetValue(hGlobalObject); return pValue; } @@ -245,7 +243,7 @@ bool CFXJSE_Context::ExecuteScript(const char* szScript, } if (lpRetValue) { lpRetValue->m_hValue.Reset(m_pIsolate, - FXJSE_CreateReturnValue(m_pIsolate, trycatch)); + CreateReturnValue(m_pIsolate, trycatch)); } return false; } @@ -270,7 +268,7 @@ bool CFXJSE_Context::ExecuteScript(const char* szScript, } if (lpRetValue) { lpRetValue->m_hValue.Reset(m_pIsolate, - FXJSE_CreateReturnValue(m_pIsolate, trycatch)); + CreateReturnValue(m_pIsolate, trycatch)); } return false; } diff --git a/fxjs/cfxjse_context.h b/fxjs/cfxjse_context.h index f93469bc4d..48816eb629 100644 --- a/fxjs/cfxjse_context.h +++ b/fxjs/cfxjse_context.h @@ -49,12 +49,6 @@ class CFXJSE_Context { std::vector> m_rgClasses; }; -v8::Local FXJSE_CreateReturnValue(v8::Isolate* pIsolate, - v8::TryCatch& trycatch); - -v8::Local FXJSE_GetGlobalObjectFromContext( - v8::Local hContext); - void FXJSE_UpdateObjectBinding(v8::Local& hObject, CFXJSE_HostObject* lpNewBinding = nullptr); diff --git a/fxjs/cfxjse_value.cpp b/fxjs/cfxjse_value.cpp index 0a135951d9..f5bdd54653 100644 --- a/fxjs/cfxjse_value.cpp +++ b/fxjs/cfxjse_value.cpp @@ -261,79 +261,6 @@ bool CFXJSE_Value::SetFunctionBind(CFXJSE_Value* lpOldFunction, return true; } -#define FXJSE_INVALID_PTR ((void*)(intptr_t)-1) -bool CFXJSE_Value::Call(CFXJSE_Value* lpReceiver, - CFXJSE_Value* lpRetValue, - uint32_t nArgCount, - CFXJSE_Value** lpArgs) { - CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate); - v8::Local hFunctionValue = - v8::Local::New(m_pIsolate, DirectGetValue()); - v8::Local hFunctionObject = - !hFunctionValue.IsEmpty() && hFunctionValue->IsObject() - ? hFunctionValue.As() - : v8::Local(); - - v8::TryCatch trycatch(m_pIsolate); - if (hFunctionObject.IsEmpty() || !hFunctionObject->IsCallable()) { - if (lpRetValue) - lpRetValue->ForceSetValue(FXJSE_CreateReturnValue(m_pIsolate, trycatch)); - return false; - } - - v8::Local hReturnValue; - v8::Local* lpLocalArgs = NULL; - if (nArgCount) { - lpLocalArgs = FX_Alloc(v8::Local, nArgCount); - for (uint32_t i = 0; i < nArgCount; i++) { - new (lpLocalArgs + i) v8::Local; - CFXJSE_Value* lpArg = lpArgs[i]; - if (lpArg) { - lpLocalArgs[i] = - v8::Local::New(m_pIsolate, lpArg->DirectGetValue()); - } - if (lpLocalArgs[i].IsEmpty()) { - lpLocalArgs[i] = v8::Undefined(m_pIsolate); - } - } - } - - bool bRetValue = true; - if (lpReceiver == FXJSE_INVALID_PTR) { - v8::MaybeLocal maybe_retvalue = - hFunctionObject->CallAsConstructor(m_pIsolate->GetCurrentContext(), - nArgCount, lpLocalArgs); - hReturnValue = maybe_retvalue.FromMaybe(v8::Local()); - } else { - v8::Local hReceiver; - if (lpReceiver) { - hReceiver = - v8::Local::New(m_pIsolate, lpReceiver->DirectGetValue()); - } - if (hReceiver.IsEmpty() || !hReceiver->IsObject()) - hReceiver = v8::Object::New(m_pIsolate); - - v8::MaybeLocal maybe_retvalue = hFunctionObject->CallAsFunction( - m_pIsolate->GetCurrentContext(), hReceiver, nArgCount, lpLocalArgs); - hReturnValue = maybe_retvalue.FromMaybe(v8::Local()); - } - - if (trycatch.HasCaught()) { - hReturnValue = FXJSE_CreateReturnValue(m_pIsolate, trycatch); - bRetValue = false; - } - - if (lpRetValue) - lpRetValue->ForceSetValue(hReturnValue); - - if (lpLocalArgs) { - for (uint32_t i = 0; i < nArgCount; i++) - lpLocalArgs[i].~Local(); - FX_Free(lpLocalArgs); - } - return bRetValue; -} - bool CFXJSE_Value::IsUndefined() const { if (m_hValue.IsEmpty()) return false; diff --git a/fxjs/cfxjse_value.h b/fxjs/cfxjse_value.h index 52905db90f..52bb036b95 100644 --- a/fxjs/cfxjse_value.h +++ b/fxjs/cfxjse_value.h @@ -68,10 +68,6 @@ class CFXJSE_Value { bool SetObjectOwnProperty(const ByteStringView& szPropName, CFXJSE_Value* lpPropValue); bool SetFunctionBind(CFXJSE_Value* lpOldFunction, CFXJSE_Value* lpNewThis); - bool Call(CFXJSE_Value* lpReceiver, - CFXJSE_Value* lpRetValue, - uint32_t nArgCount, - CFXJSE_Value** lpArgs); v8::Isolate* GetIsolate() const { return m_pIsolate; } const v8::Global& DirectGetValue() const { return m_hValue; } -- cgit v1.2.3