From 3cac3602f24f56413cc2ca312731675fc080b7ef Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 24 Oct 2017 15:15:27 -0400 Subject: Remove GetType from CJS_Value This CL removes the GetType method from CJS_Value and, instead, retrieves the value from the v8 object directly. Change-Id: Ia8390f3ead163c09a39cae493e75fccdd41a0961 Reviewed-on: https://pdfium-review.googlesource.com/16615 Reviewed-by: Tom Sepez Commit-Queue: dsinclair --- fpdfsdk/javascript/Document.cpp | 22 +++--- fpdfsdk/javascript/Field.cpp | 2 +- fpdfsdk/javascript/JS_Value.cpp | 25 +------ fpdfsdk/javascript/JS_Value.h | 15 ---- fpdfsdk/javascript/PublicMethods.cpp | 4 +- fpdfsdk/javascript/app.cpp | 45 +++++++----- fpdfsdk/javascript/event.cpp | 2 +- fpdfsdk/javascript/global.cpp | 128 ++++++++++++++++++----------------- fpdfsdk/javascript/util.cpp | 4 +- 9 files changed, 110 insertions(+), 137 deletions(-) (limited to 'fpdfsdk') diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp index 68023d02b0..22fe8b4c80 100644 --- a/fpdfsdk/javascript/Document.cpp +++ b/fpdfsdk/javascript/Document.cpp @@ -447,7 +447,7 @@ bool Document::print(CJS_Runtime* pRuntime, bool bAnnotations = false; int nlength = params.size(); if (nlength == 9) { - if (params[8].GetType() == CJS_Value::VT_object) { + if (params[8].ToV8Value()->IsObject()) { v8::Local pObj = pRuntime->ToObject(params[8].ToV8Value()); if (CFXJS_Engine::GetObjDefnID(pObj) == CJS_PrintParamsObj::g_nObjDefnID) { @@ -584,14 +584,10 @@ bool Document::resetForm(CJS_Runtime* pRuntime, return true; } - switch (params[0].GetType()) { - default: - aName = CJS_Array(pRuntime->ToArray(params[0].ToV8Value())); - break; - case CJS_Value::VT_string: - aName.SetElement(pRuntime, 0, params[0]); - break; - } + if (params[0].ToV8Value()->IsString()) + aName.SetElement(pRuntime, 0, params[0]); + else + aName = CJS_Array(pRuntime->ToArray(params[0].ToV8Value())); std::vector aFields; for (int i = 0, isz = aName.GetLength(pRuntime); i < isz; ++i) { @@ -643,7 +639,7 @@ bool Document::submitForm(CJS_Runtime* pRuntime, bool bFDF = true; bool bEmpty = false; CJS_Value v(params[0]); - if (v.GetType() == CJS_Value::VT_string) { + if (v.ToV8Value()->IsString()) { strURL = pRuntime->ToWideString(params[0].ToV8Value()); if (nSize > 1) bFDF = pRuntime->ToBoolean(params[1].ToV8Value()); @@ -651,7 +647,7 @@ bool Document::submitForm(CJS_Runtime* pRuntime, bEmpty = pRuntime->ToBoolean(params[2].ToV8Value()); if (nSize > 3) aFields = CJS_Array(pRuntime->ToArray(params[3].ToV8Value())); - } else if (v.GetType() == CJS_Value::VT_object) { + } else if (v.ToV8Value()->IsObject()) { v8::Local pObj = pRuntime->ToObject(params[0].ToV8Value()); v8::Local pValue = pRuntime->GetObjectProperty(pObj, L"cURL"); if (!pValue.IsEmpty()) @@ -737,7 +733,7 @@ bool Document::mailDoc(CJS_Runtime* pRuntime, if (params.size() >= 6) cMsg = pRuntime->ToWideString(params[5].ToV8Value()); - if (params.size() >= 1 && params[0].GetType() == CJS_Value::VT_object) { + if (params.size() >= 1 && params[0].ToV8Value()->IsObject()) { v8::Local pObj = pRuntime->ToObject(params[0].ToV8Value()); bUI = pRuntime->ToBoolean(pRuntime->GetObjectProperty(pObj, L"bUI")); cTo = pRuntime->ToWideString(pRuntime->GetObjectProperty(pObj, L"cTo")); @@ -1371,7 +1367,7 @@ bool Document::addIcon(CJS_Runtime* pRuntime, } WideString swIconName = pRuntime->ToWideString(params[0].ToV8Value()); - if (params[1].GetType() != CJS_Value::VT_object) { + if (!params[1].ToV8Value()->IsObject()) { sError = JSGetStringFromID(IDS_STRING_JSTYPEERROR); return false; } diff --git a/fpdfsdk/javascript/Field.cpp b/fpdfsdk/javascript/Field.cpp index 5bcc66b7e9..0ed0594b3b 100644 --- a/fpdfsdk/javascript/Field.cpp +++ b/fpdfsdk/javascript/Field.cpp @@ -877,7 +877,7 @@ bool Field::set_current_value_indices(CJS_Runtime* pRuntime, return false; std::vector array; - if (vp.GetType() == CJS_Value::VT_number) { + if (vp.ToV8Value()->IsNumber()) { array.push_back(pRuntime->ToInt32(vp.ToV8Value())); } else if (vp.IsArrayObject()) { CJS_Array SelArray(pRuntime->ToArray(vp.ToV8Value())); diff --git a/fpdfsdk/javascript/JS_Value.cpp b/fpdfsdk/javascript/JS_Value.cpp index 35a5b1c682..f535f69f94 100644 --- a/fpdfsdk/javascript/JS_Value.cpp +++ b/fpdfsdk/javascript/JS_Value.cpp @@ -196,7 +196,7 @@ v8::Local CJS_Value::ToV8Value() const { void CJS_Value::MaybeCoerceToNumber(CJS_Runtime* pRuntime) { bool bAllowNaN = false; - if (GetType() == VT_string) { + if (ToV8Value()->IsString()) { ByteString bstr = ByteString::FromUnicode(pRuntime->ToWideString(ToV8Value())); if (bstr.GetLength() == 0) @@ -216,27 +216,6 @@ void CJS_Value::MaybeCoerceToNumber(CJS_Runtime* pRuntime) { m_pValue = num; } -// static -CJS_Value::Type CJS_Value::GetValueType(v8::Local value) { - if (value.IsEmpty()) - return VT_unknown; - if (value->IsString()) - return VT_string; - if (value->IsNumber()) - return VT_number; - if (value->IsBoolean()) - return VT_boolean; - if (value->IsDate()) - return VT_date; - if (value->IsObject()) - return VT_object; - if (value->IsNull()) - return VT_null; - if (value->IsUndefined()) - return VT_undefined; - return VT_unknown; -} - bool CJS_Value::IsArrayObject() const { return !m_pValue.IsEmpty() && m_pValue->IsArray(); } @@ -456,7 +435,7 @@ std::vector ExpandKeywordParams( for (size_t i = 0; i < size; ++i) result[i] = originals[i]; - if (originals.size() != 1 || originals[0].GetType() != CJS_Value::VT_object || + if (originals.size() != 1 || !originals[0].ToV8Value()->IsObject() || originals[0].IsArrayObject()) { return result; } diff --git a/fpdfsdk/javascript/JS_Value.h b/fpdfsdk/javascript/JS_Value.h index 07e5986527..5e77e0beaa 100644 --- a/fpdfsdk/javascript/JS_Value.h +++ b/fpdfsdk/javascript/JS_Value.h @@ -19,19 +19,6 @@ class CJS_Runtime; class CJS_Value { public: - enum Type { - VT_unknown, - VT_string, - VT_number, - VT_boolean, - VT_date, - VT_object, - VT_null, - VT_undefined - }; - - static Type GetValueType(v8::Local value); - CJS_Value(); explicit CJS_Value(v8::Local pValue); CJS_Value(const CJS_Value& other); @@ -40,8 +27,6 @@ class CJS_Value { // These calls may re-enter JS (and hence invalidate objects). void Set(v8::Local pValue); - Type GetType() const { return GetValueType(m_pValue); } - v8::Local ToV8Value() const; // Replace the current |m_pValue| with a v8::Number if possible diff --git a/fpdfsdk/javascript/PublicMethods.cpp b/fpdfsdk/javascript/PublicMethods.cpp index 462398012d..68fc30572b 100644 --- a/fpdfsdk/javascript/PublicMethods.cpp +++ b/fpdfsdk/javascript/PublicMethods.cpp @@ -1660,7 +1660,7 @@ bool CJS_PublicMethods::AFMakeNumber(CJS_Runtime* pRuntime, ws.Replace(L",", L"."); vRet = CJS_Value(pRuntime->NewString(ws.c_str())); vRet.MaybeCoerceToNumber(pRuntime); - if (vRet.GetType() != CJS_Value::VT_number) + if (!vRet.ToV8Value()->IsNumber()) vRet = CJS_Value(pRuntime->NewNumber(0)); return true; } @@ -1675,7 +1675,7 @@ bool CJS_PublicMethods::AFSimple_Calculate(CJS_Runtime* pRuntime, } CJS_Value params1(params[1]); - if (!params1.IsArrayObject() && params1.GetType() != CJS_Value::VT_string) { + if (!params1.IsArrayObject() && !params1.ToV8Value()->IsString()) { sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); return false; } diff --git a/fpdfsdk/javascript/app.cpp b/fpdfsdk/javascript/app.cpp index 5345d25334..26f28f5b48 100644 --- a/fpdfsdk/javascript/app.cpp +++ b/fpdfsdk/javascript/app.cpp @@ -22,6 +22,17 @@ #include "fpdfsdk/javascript/resource.h" #include "third_party/base/stl_util.h" +namespace { + +bool IsTypeKnown(v8::Local value) { + return !value.IsEmpty() && + (value->IsString() || value->IsNumber() || value->IsBoolean() || + value->IsDate() || value->IsObject() || value->IsNull() || + value->IsUndefined()); +} + +} // namespace + class GlobalTimer { public: GlobalTimer(app* pObj, @@ -391,7 +402,7 @@ bool app::alert(CJS_Runtime* pRuntime, std::vector newParams = ExpandKeywordParams( pRuntime, params, 4, L"cMsg", L"nIcon", L"nType", L"cTitle"); - if (newParams[0].GetType() == CJS_Value::VT_unknown) { + if (!IsTypeKnown(newParams[0].ToV8Value())) { sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); return false; } @@ -403,7 +414,7 @@ bool app::alert(CJS_Runtime* pRuntime, } WideString swMsg; - if (newParams[0].GetType() == CJS_Value::VT_object) { + if (newParams[0].ToV8Value()->IsObject()) { if (newParams[0].IsArrayObject()) { CJS_Array carray(pRuntime->ToArray(newParams[0].ToV8Value())); swMsg = L"["; @@ -423,15 +434,15 @@ bool app::alert(CJS_Runtime* pRuntime, } int iIcon = 0; - if (newParams[1].GetType() != CJS_Value::VT_unknown) + if (IsTypeKnown(newParams[1].ToV8Value())) iIcon = pRuntime->ToInt32(newParams[1].ToV8Value()); int iType = 0; - if (newParams[2].GetType() != CJS_Value::VT_unknown) + if (IsTypeKnown(newParams[2].ToV8Value())) iType = pRuntime->ToInt32(newParams[2].ToV8Value()); WideString swTitle; - if (newParams[3].GetType() != CJS_Value::VT_unknown) + if (IsTypeKnown(newParams[3].ToV8Value())) swTitle = pRuntime->ToWideString(newParams[3].ToV8Value()); else swTitle = JSGetStringFromID(IDS_STRING_JSALERT); @@ -582,7 +593,7 @@ bool app::clearInterval(CJS_Runtime* pRuntime, } void app::ClearTimerCommon(CJS_Runtime* pRuntime, const CJS_Value& param) { - if (param.GetType() != CJS_Value::VT_object) + if (!param.ToV8Value()->IsObject()) return; v8::Local pObj = pRuntime->ToObject(param.ToV8Value()); @@ -652,14 +663,14 @@ bool app::mailMsg(CJS_Runtime* pRuntime, ExpandKeywordParams(pRuntime, params, 6, L"bUI", L"cTo", L"cCc", L"cBcc", L"cSubject", L"cMsg"); - if (newParams[0].GetType() == CJS_Value::VT_unknown) { + if (!IsTypeKnown(newParams[0].ToV8Value())) { sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); return false; } bool bUI = pRuntime->ToBoolean(newParams[0].ToV8Value()); WideString cTo; - if (newParams[1].GetType() != CJS_Value::VT_unknown) { + if (IsTypeKnown(newParams[1].ToV8Value())) { cTo = pRuntime->ToWideString(newParams[1].ToV8Value()); } else { if (!bUI) { @@ -670,19 +681,19 @@ bool app::mailMsg(CJS_Runtime* pRuntime, } WideString cCc; - if (newParams[2].GetType() != CJS_Value::VT_unknown) + if (IsTypeKnown(newParams[2].ToV8Value())) cCc = pRuntime->ToWideString(newParams[2].ToV8Value()); WideString cBcc; - if (newParams[3].GetType() != CJS_Value::VT_unknown) + if (IsTypeKnown(newParams[3].ToV8Value())) cBcc = pRuntime->ToWideString(newParams[3].ToV8Value()); WideString cSubject; - if (newParams[4].GetType() != CJS_Value::VT_unknown) + if (IsTypeKnown(newParams[4].ToV8Value())) cSubject = pRuntime->ToWideString(newParams[4].ToV8Value()); WideString cMsg; - if (newParams[5].GetType() != CJS_Value::VT_unknown) + if (IsTypeKnown(newParams[5].ToV8Value())) cMsg = pRuntime->ToWideString(newParams[5].ToV8Value()); pRuntime->BeginBlock(); @@ -773,26 +784,26 @@ bool app::response(CJS_Runtime* pRuntime, ExpandKeywordParams(pRuntime, params, 5, L"cQuestion", L"cTitle", L"cDefault", L"bPassword", L"cLabel"); - if (newParams[0].GetType() == CJS_Value::VT_unknown) { + if (!IsTypeKnown(newParams[0].ToV8Value())) { sError = JSGetStringFromID(IDS_STRING_JSPARAMERROR); return false; } WideString swQuestion = pRuntime->ToWideString(newParams[0].ToV8Value()); WideString swTitle = L"PDF"; - if (newParams[1].GetType() != CJS_Value::VT_unknown) + if (IsTypeKnown(newParams[1].ToV8Value())) swTitle = pRuntime->ToWideString(newParams[1].ToV8Value()); WideString swDefault; - if (newParams[2].GetType() != CJS_Value::VT_unknown) + if (IsTypeKnown(newParams[2].ToV8Value())) swDefault = pRuntime->ToWideString(newParams[2].ToV8Value()); bool bPassword = false; - if (newParams[3].GetType() != CJS_Value::VT_unknown) + if (IsTypeKnown(newParams[3].ToV8Value())) bPassword = pRuntime->ToBoolean(newParams[3].ToV8Value()); WideString swLabel; - if (newParams[4].GetType() != CJS_Value::VT_unknown) + if (IsTypeKnown(newParams[4].ToV8Value())) swLabel = pRuntime->ToWideString(newParams[4].ToV8Value()); const int MAX_INPUT_BYTES = 2048; diff --git a/fpdfsdk/javascript/event.cpp b/fpdfsdk/javascript/event.cpp index 337ce40cd8..da81aac45a 100644 --- a/fpdfsdk/javascript/event.cpp +++ b/fpdfsdk/javascript/event.cpp @@ -61,7 +61,7 @@ bool event::set_change(CJS_Runtime* pRuntime, CJS_EventHandler* pEvent = pRuntime->GetCurrentEventContext()->GetEventHandler(); - if (vp.GetType() == CJS_Value::VT_string) { + if (vp.ToV8Value()->IsString()) { WideString& wChange = pEvent->Change(); wChange = pRuntime->ToWideString(vp.ToV8Value()); } diff --git a/fpdfsdk/javascript/global.cpp b/fpdfsdk/javascript/global.cpp index f095440413..aae7c93160 100644 --- a/fpdfsdk/javascript/global.cpp +++ b/fpdfsdk/javascript/global.cpp @@ -325,31 +325,33 @@ bool JSGlobalAlternate::SetProperty(CJS_Runtime* pRuntime, const wchar_t* propname, const CJS_Value& vp) { ByteString sPropName = ByteString::FromUnicode(propname); - switch (vp.GetType()) { - case CJS_Value::VT_number: - return SetGlobalVariables(sPropName, JS_GlobalDataType::NUMBER, - pRuntime->ToDouble(vp.ToV8Value()), false, "", - v8::Local(), false); - case CJS_Value::VT_boolean: - return SetGlobalVariables(sPropName, JS_GlobalDataType::BOOLEAN, 0, - pRuntime->ToBoolean(vp.ToV8Value()), "", - v8::Local(), false); - case CJS_Value::VT_string: - return SetGlobalVariables( - sPropName, JS_GlobalDataType::STRING, 0, false, - ByteString::FromUnicode(pRuntime->ToWideString(vp.ToV8Value())), - v8::Local(), false); - case CJS_Value::VT_object: - return SetGlobalVariables(sPropName, JS_GlobalDataType::OBJECT, 0, false, - "", pRuntime->ToObject(vp.ToV8Value()), false); - case CJS_Value::VT_null: - return SetGlobalVariables(sPropName, JS_GlobalDataType::NULLOBJ, 0, false, - "", v8::Local(), false); - case CJS_Value::VT_undefined: - DelProperty(pRuntime, propname); - return true; - default: - break; + if (vp.ToV8Value()->IsNumber()) { + return SetGlobalVariables(sPropName, JS_GlobalDataType::NUMBER, + pRuntime->ToDouble(vp.ToV8Value()), false, "", + v8::Local(), false); + } + if (vp.ToV8Value()->IsBoolean()) { + return SetGlobalVariables(sPropName, JS_GlobalDataType::BOOLEAN, 0, + pRuntime->ToBoolean(vp.ToV8Value()), "", + v8::Local(), false); + } + if (vp.ToV8Value()->IsString()) { + return SetGlobalVariables( + sPropName, JS_GlobalDataType::STRING, 0, false, + ByteString::FromUnicode(pRuntime->ToWideString(vp.ToV8Value())), + v8::Local(), false); + } + if (vp.ToV8Value()->IsObject()) { + return SetGlobalVariables(sPropName, JS_GlobalDataType::OBJECT, 0, false, + "", pRuntime->ToObject(vp.ToV8Value()), false); + } + if (vp.ToV8Value()->IsNull()) { + return SetGlobalVariables(sPropName, JS_GlobalDataType::NULLOBJ, 0, false, + "", v8::Local(), false); + } + if (vp.ToV8Value()->IsUndefined()) { + DelProperty(pRuntime, propname); + return true; } return false; } @@ -471,44 +473,44 @@ void JSGlobalAlternate::ObjectToArray(CJS_Runtime* pRuntime, for (const auto& ws : pKeyList) { ByteString sKey = ws.UTF8Encode(); v8::Local v = pRuntime->GetObjectProperty(pObj, ws); - switch (CJS_Value::GetValueType(v)) { - case CJS_Value::VT_number: { - CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GlobalDataType::NUMBER; - pObjElement->sKey = sKey; - pObjElement->dData = pRuntime->ToDouble(v); - array.Add(pObjElement); - } break; - case CJS_Value::VT_boolean: { - CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GlobalDataType::BOOLEAN; - pObjElement->sKey = sKey; - pObjElement->dData = pRuntime->ToBoolean(v); - array.Add(pObjElement); - } break; - case CJS_Value::VT_string: { - ByteString sValue = ByteString::FromUnicode(pRuntime->ToWideString(v)); - CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GlobalDataType::STRING; - pObjElement->sKey = sKey; - pObjElement->sData = sValue; - array.Add(pObjElement); - } break; - case CJS_Value::VT_object: { - CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GlobalDataType::OBJECT; - pObjElement->sKey = sKey; - ObjectToArray(pRuntime, pRuntime->ToObject(v), pObjElement->objData); - array.Add(pObjElement); - } break; - case CJS_Value::VT_null: { - CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GlobalDataType::NULLOBJ; - pObjElement->sKey = sKey; - array.Add(pObjElement); - } break; - default: - break; + if (v->IsNumber()) { + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::NUMBER; + pObjElement->sKey = sKey; + pObjElement->dData = pRuntime->ToDouble(v); + array.Add(pObjElement); + continue; + } + if (v->IsBoolean()) { + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::BOOLEAN; + pObjElement->sKey = sKey; + pObjElement->dData = pRuntime->ToBoolean(v); + array.Add(pObjElement); + continue; + } + if (v->IsString()) { + ByteString sValue = ByteString::FromUnicode(pRuntime->ToWideString(v)); + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::STRING; + pObjElement->sKey = sKey; + pObjElement->sData = sValue; + array.Add(pObjElement); + continue; + } + if (v->IsObject()) { + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::OBJECT; + pObjElement->sKey = sKey; + ObjectToArray(pRuntime, pRuntime->ToObject(v), pObjElement->objData); + array.Add(pObjElement); + continue; + } + if (v->IsNull()) { + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::NULLOBJ; + pObjElement->sKey = sKey; + array.Add(pObjElement); } } } diff --git a/fpdfsdk/javascript/util.cpp b/fpdfsdk/javascript/util.cpp index 96b2c8a6ef..28ab0bb95c 100644 --- a/fpdfsdk/javascript/util.cpp +++ b/fpdfsdk/javascript/util.cpp @@ -162,7 +162,7 @@ bool util::printd(CJS_Runtime* pRuntime, return false; } - if (p1.GetType() == CJS_Value::VT_number) { + if (p1.ToV8Value()->IsNumber()) { WideString swResult; switch (pRuntime->ToInt32(p1.ToV8Value())) { case 0: @@ -194,7 +194,7 @@ bool util::printd(CJS_Runtime* pRuntime, return true; } - if (p1.GetType() == CJS_Value::VT_string) { + if (p1.ToV8Value()->IsString()) { if (iSize > 2 && pRuntime->ToBoolean(params[2].ToV8Value())) { sError = JSGetStringFromID(IDS_STRING_JSNOTSUPPORT); return false; // currently, it doesn't support XFAPicture. -- cgit v1.2.3