From 018935c9304bebf13fbad20b124d775ccae87fae Mon Sep 17 00:00:00 2001 From: tsepez Date: Fri, 15 Apr 2016 13:15:12 -0700 Subject: Pass WideStrings without narrowing to c_str in javascript/ Avoid string duplication when the caller already has one. Review URL: https://codereview.chromium.org/1883273003 --- fpdfsdk/javascript/Document.cpp | 27 +++++++++---------- fpdfsdk/javascript/JS_Value.cpp | 3 +-- fpdfsdk/javascript/JS_Value.h | 2 +- fpdfsdk/javascript/PublicMethods.cpp | 15 +++++------ fpdfsdk/javascript/global.cpp | 28 ++++++++++---------- fpdfsdk/jsapi/fxjs_v8.cpp | 50 ++++++++++++++++++------------------ fpdfsdk/jsapi/include/fxjs_v8.h | 27 +++++++++---------- 7 files changed, 70 insertions(+), 82 deletions(-) diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp index 4c361b197e..551d649830 100644 --- a/fpdfsdk/javascript/Document.cpp +++ b/fpdfsdk/javascript/Document.cpp @@ -769,16 +769,15 @@ FX_BOOL Document::info(IJS_Context* cc, CJS_Runtime* pRuntime = pContext->GetJSRuntime(); v8::Local pObj = 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()); - FXJS_PutObjectString(isolate, pObj, L"Keywords", cwKeywords.c_str()); - FXJS_PutObjectString(isolate, pObj, L"Creator", cwCreator.c_str()); - FXJS_PutObjectString(isolate, pObj, L"Producer", cwProducer.c_str()); - FXJS_PutObjectString(isolate, pObj, L"CreationDate", - cwCreationDate.c_str()); - FXJS_PutObjectString(isolate, pObj, L"ModDate", cwModDate.c_str()); - FXJS_PutObjectString(isolate, pObj, L"Trapped", cwTrapped.c_str()); + FXJS_PutObjectString(isolate, pObj, L"Author", cwAuthor); + FXJS_PutObjectString(isolate, pObj, L"Title", cwTitle); + FXJS_PutObjectString(isolate, pObj, L"Subject", cwSubject); + FXJS_PutObjectString(isolate, pObj, L"Keywords", cwKeywords); + FXJS_PutObjectString(isolate, pObj, L"Creator", cwCreator); + FXJS_PutObjectString(isolate, pObj, L"Producer", cwProducer); + FXJS_PutObjectString(isolate, pObj, L"CreationDate", cwCreationDate); + FXJS_PutObjectString(isolate, pObj, L"ModDate", cwModDate); + FXJS_PutObjectString(isolate, pObj, L"Trapped", cwTrapped); // It's to be compatible to non-standard info dictionary. for (const auto& it : *pDictionary) { @@ -786,14 +785,12 @@ FX_BOOL Document::info(IJS_Context* cc, CPDF_Object* pValueObj = it.second; CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC()); if (pValueObj->IsString() || pValueObj->IsName()) { - FXJS_PutObjectString(isolate, pObj, wsKey.c_str(), - pValueObj->GetUnicodeText().c_str()); + FXJS_PutObjectString(isolate, pObj, wsKey, pValueObj->GetUnicodeText()); } else if (pValueObj->IsNumber()) { - FXJS_PutObjectNumber(isolate, pObj, wsKey.c_str(), + FXJS_PutObjectNumber(isolate, pObj, wsKey, (float)pValueObj->GetNumber()); } else if (pValueObj->IsBoolean()) { - FXJS_PutObjectBoolean(isolate, pObj, wsKey.c_str(), - !!pValueObj->GetInteger()); + FXJS_PutObjectBoolean(isolate, pObj, wsKey, !!pValueObj->GetInteger()); } } vp << pObj; diff --git a/fpdfsdk/javascript/JS_Value.cpp b/fpdfsdk/javascript/JS_Value.cpp index 0b31494b75..e2c990c51c 100644 --- a/fpdfsdk/javascript/JS_Value.cpp +++ b/fpdfsdk/javascript/JS_Value.cpp @@ -787,7 +787,7 @@ int JS_GetSecFromTime(double dt) { return (int)_Mod(floor(dt / 1000), 60); } -double JS_DateParse(const wchar_t* str) { +double JS_DateParse(const CFX_WideString& str) { v8::Isolate* pIsolate = v8::Isolate::GetCurrent(); v8::Isolate::Scope isolate_scope(pIsolate); v8::HandleScope scope(pIsolate); @@ -809,7 +809,6 @@ double JS_DateParse(const wchar_t* str) { .ToLocalChecked(); if (v->IsFunction()) { v8::Local funC = v8::Local::Cast(v); - const int argc = 1; v8::Local timeStr = FXJS_WSToJSString(pIsolate, str); v8::Local argv[argc] = {timeStr}; diff --git a/fpdfsdk/javascript/JS_Value.h b/fpdfsdk/javascript/JS_Value.h index 423d7f62e1..8adec4b1d1 100644 --- a/fpdfsdk/javascript/JS_Value.h +++ b/fpdfsdk/javascript/JS_Value.h @@ -208,7 +208,7 @@ int JS_GetDayFromTime(double dt); int JS_GetHourFromTime(double dt); int JS_GetMinFromTime(double dt); int JS_GetSecFromTime(double dt); -double JS_DateParse(const wchar_t* str); +double JS_DateParse(const CFX_WideString& str); double JS_MakeDay(int nYear, int nMonth, int nDay); double JS_MakeTime(int nHour, int nMin, int nSec, int nMs); double JS_MakeDate(double day, double time); diff --git a/fpdfsdk/javascript/PublicMethods.cpp b/fpdfsdk/javascript/PublicMethods.cpp index 9f7b60f70d..a84c0935bc 100644 --- a/fpdfsdk/javascript/PublicMethods.cpp +++ b/fpdfsdk/javascript/PublicMethods.cpp @@ -306,7 +306,7 @@ double CJS_PublicMethods::ParseNormalDate(const CFX_WideString& value, CFX_WideString swTemp; swTemp.Format(L"%d/%d/%d %d:%d:%d", nMonth, nDay, nYear, nHour, nMin, nSec); - return JS_DateParse(swTemp.c_str()); + return JS_DateParse(swTemp); } double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value, @@ -563,24 +563,21 @@ double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value, bBadFormat = true; double dRet = 0; - if (bBadFormat) { dRet = ParseNormalDate(value, &bBadFormat); } else { dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay), JS_MakeTime(nHour, nMin, nSec, 0)); - - if (JS_PortIsNan(dRet)) { - dRet = JS_DateParse(value.c_str()); - } + if (JS_PortIsNan(dRet)) + dRet = JS_DateParse(value); } - if (JS_PortIsNan(dRet)) { + if (JS_PortIsNan(dRet)) dRet = ParseNormalDate(value, &bBadFormat); - } if (bWrongFormat) *bWrongFormat = bBadFormat; + return dRet; } @@ -1204,7 +1201,7 @@ double CJS_PublicMethods::MakeInterDate(const CFX_WideString& strValue) { double dRet = JS_MakeDate(JS_MakeDay(nYear, nMonth - 1, nDay), JS_MakeTime(nHour, nMin, nSec, 0)); if (JS_PortIsNan(dRet)) - dRet = JS_DateParse(strValue.c_str()); + dRet = JS_DateParse(strValue); return dRet; } diff --git a/fpdfsdk/javascript/global.cpp b/fpdfsdk/javascript/global.cpp index 7242a144c9..380713b425 100644 --- a/fpdfsdk/javascript/global.cpp +++ b/fpdfsdk/javascript/global.cpp @@ -233,15 +233,14 @@ void JSGlobalAlternate::UpdateGlobalPersistentVariables() { pData->data.dData, false, "", v8::Local(), pData->bPersistent == 1); FXJS_PutObjectNumber(NULL, m_pJSObject->ToV8Object(), - pData->data.sKey.UTF8Decode().c_str(), - pData->data.dData); + pData->data.sKey.UTF8Decode(), pData->data.dData); break; case JS_GLOBALDATA_TYPE_BOOLEAN: SetGlobalVariables(pData->data.sKey, JS_GLOBALDATA_TYPE_BOOLEAN, 0, (bool)(pData->data.bData == 1), "", v8::Local(), pData->bPersistent == 1); FXJS_PutObjectBoolean(NULL, m_pJSObject->ToV8Object(), - pData->data.sKey.UTF8Decode().c_str(), + pData->data.sKey.UTF8Decode(), (bool)(pData->data.bData == 1)); break; case JS_GLOBALDATA_TYPE_STRING: @@ -249,8 +248,8 @@ void JSGlobalAlternate::UpdateGlobalPersistentVariables() { false, pData->data.sData, v8::Local(), pData->bPersistent == 1); FXJS_PutObjectString(NULL, m_pJSObject->ToV8Object(), - pData->data.sKey.UTF8Decode().c_str(), - pData->data.sData.UTF8Decode().c_str()); + pData->data.sKey.UTF8Decode(), + pData->data.sData.UTF8Decode()); break; case JS_GLOBALDATA_TYPE_OBJECT: { v8::Isolate* pRuntime = m_pJSObject->ToV8Object()->GetIsolate(); @@ -261,14 +260,14 @@ void JSGlobalAlternate::UpdateGlobalPersistentVariables() { SetGlobalVariables(pData->data.sKey, JS_GLOBALDATA_TYPE_OBJECT, 0, false, "", pObj, pData->bPersistent == 1); FXJS_PutObjectObject(NULL, m_pJSObject->ToV8Object(), - pData->data.sKey.UTF8Decode().c_str(), pObj); + pData->data.sKey.UTF8Decode(), pObj); } break; case JS_GLOBALDATA_TYPE_NULL: SetGlobalVariables(pData->data.sKey, JS_GLOBALDATA_TYPE_NULL, 0, false, "", v8::Local(), pData->bPersistent == 1); FXJS_PutObjectNull(NULL, m_pJSObject->ToV8Object(), - pData->data.sKey.UTF8Decode().c_str()); + pData->data.sKey.UTF8Decode()); break; } } @@ -324,7 +323,7 @@ void JSGlobalAlternate::ObjectToArray(IJS_Context* cc, FXJS_ToString(isolate, FXJS_GetArrayElement(isolate, pKeyList, i)); CFX_ByteString sKey = ws.UTF8Encode(); - v8::Local v = FXJS_GetObjectElement(isolate, pObj, ws.c_str()); + v8::Local v = FXJS_GetObjectElement(isolate, pObj, ws); switch (GET_VALUE_TYPE(v)) { case CJS_Value::VT_number: { CJS_KeyValue* pObjElement = new CJS_KeyValue; @@ -374,27 +373,26 @@ void JSGlobalAlternate::PutObjectProperty(v8::Local pObj, CJS_KeyValue* pObjData = pData->objData.GetAt(i); switch (pObjData->nType) { case JS_GLOBALDATA_TYPE_NUMBER: - FXJS_PutObjectNumber(NULL, pObj, pObjData->sKey.UTF8Decode().c_str(), + FXJS_PutObjectNumber(NULL, pObj, pObjData->sKey.UTF8Decode(), pObjData->dData); break; case JS_GLOBALDATA_TYPE_BOOLEAN: - FXJS_PutObjectBoolean(NULL, pObj, pObjData->sKey.UTF8Decode().c_str(), + FXJS_PutObjectBoolean(NULL, pObj, pObjData->sKey.UTF8Decode(), pObjData->bData == 1); break; case JS_GLOBALDATA_TYPE_STRING: - FXJS_PutObjectString(NULL, pObj, pObjData->sKey.UTF8Decode().c_str(), - pObjData->sData.UTF8Decode().c_str()); + FXJS_PutObjectString(NULL, pObj, pObjData->sKey.UTF8Decode(), + pObjData->sData.UTF8Decode()); break; case JS_GLOBALDATA_TYPE_OBJECT: { v8::Isolate* pRuntime = m_pJSObject->ToV8Object()->GetIsolate(); v8::Local pNewObj = FXJS_NewFxDynamicObj(pRuntime, NULL, -1); PutObjectProperty(pNewObj, pObjData); - FXJS_PutObjectObject(NULL, pObj, pObjData->sKey.UTF8Decode().c_str(), - pNewObj); + FXJS_PutObjectObject(NULL, pObj, pObjData->sKey.UTF8Decode(), pNewObj); } break; case JS_GLOBALDATA_TYPE_NULL: - FXJS_PutObjectNull(NULL, pObj, pObjData->sKey.UTF8Decode().c_str()); + FXJS_PutObjectNull(NULL, pObj, pObjData->sKey.UTF8Decode()); break; } } diff --git a/fpdfsdk/jsapi/fxjs_v8.cpp b/fpdfsdk/jsapi/fxjs_v8.cpp index c66cf91b56..a471d5613d 100644 --- a/fpdfsdk/jsapi/fxjs_v8.cpp +++ b/fpdfsdk/jsapi/fxjs_v8.cpp @@ -583,25 +583,24 @@ void FXJS_FreePrivate(v8::Local pObj) { } v8::Local FXJS_WSToJSString(v8::Isolate* pIsolate, - const wchar_t* PropertyName, - int Len) { - CFX_WideString ws = CFX_WideString(PropertyName, Len); - CFX_ByteString bs = ws.UTF8Encode(); + const CFX_WideString& wsPropertyName) { + CFX_ByteString bs = wsPropertyName.UTF8Encode(); if (!pIsolate) pIsolate = v8::Isolate::GetCurrent(); return v8::String::NewFromUtf8(pIsolate, bs.c_str(), - v8::NewStringType::kNormal) + v8::NewStringType::kNormal, bs.GetLength()) .ToLocalChecked(); } -v8::Local FXJS_GetObjectElement(v8::Isolate* pIsolate, - v8::Local pObj, - const wchar_t* PropertyName) { +v8::Local FXJS_GetObjectElement( + v8::Isolate* pIsolate, + v8::Local pObj, + const CFX_WideString& wsPropertyName) { if (pObj.IsEmpty()) return v8::Local(); v8::Local val; if (!pObj->Get(pIsolate->GetCurrentContext(), - FXJS_WSToJSString(pIsolate, PropertyName)) + FXJS_WSToJSString(pIsolate, wsPropertyName)) .ToLocal(&val)) return v8::Local(); return val; @@ -619,82 +618,83 @@ v8::Local FXJS_GetObjectElementNames(v8::Isolate* pIsolate, void FXJS_PutObjectString(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName, - const wchar_t* sValue) { + const CFX_WideString& wsPropertyName, + const CFX_WideString& wsValue) { if (pObj.IsEmpty()) return; pObj->Set(pIsolate->GetCurrentContext(), - FXJS_WSToJSString(pIsolate, PropertyName), - FXJS_WSToJSString(pIsolate, sValue)) + FXJS_WSToJSString(pIsolate, wsPropertyName), + FXJS_WSToJSString(pIsolate, wsValue)) .FromJust(); } void FXJS_PutObjectNumber(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName, + const CFX_WideString& wsPropertyName, int nValue) { if (pObj.IsEmpty()) return; pObj->Set(pIsolate->GetCurrentContext(), - FXJS_WSToJSString(pIsolate, PropertyName), + FXJS_WSToJSString(pIsolate, wsPropertyName), v8::Int32::New(pIsolate, nValue)) .FromJust(); } void FXJS_PutObjectNumber(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName, + const CFX_WideString& wsPropertyName, float fValue) { if (pObj.IsEmpty()) return; pObj->Set(pIsolate->GetCurrentContext(), - FXJS_WSToJSString(pIsolate, PropertyName), + FXJS_WSToJSString(pIsolate, wsPropertyName), v8::Number::New(pIsolate, (double)fValue)) .FromJust(); } void FXJS_PutObjectNumber(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName, + const CFX_WideString& wsPropertyName, double dValue) { if (pObj.IsEmpty()) return; pObj->Set(pIsolate->GetCurrentContext(), - FXJS_WSToJSString(pIsolate, PropertyName), + FXJS_WSToJSString(pIsolate, wsPropertyName), v8::Number::New(pIsolate, (double)dValue)) .FromJust(); } void FXJS_PutObjectBoolean(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName, + const CFX_WideString& wsPropertyName, bool bValue) { if (pObj.IsEmpty()) return; pObj->Set(pIsolate->GetCurrentContext(), - FXJS_WSToJSString(pIsolate, PropertyName), + FXJS_WSToJSString(pIsolate, wsPropertyName), v8::Boolean::New(pIsolate, bValue)) .FromJust(); } void FXJS_PutObjectObject(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName, + const CFX_WideString& wsPropertyName, v8::Local pPut) { if (pObj.IsEmpty()) return; pObj->Set(pIsolate->GetCurrentContext(), - FXJS_WSToJSString(pIsolate, PropertyName), pPut) + FXJS_WSToJSString(pIsolate, wsPropertyName), pPut) .FromJust(); } void FXJS_PutObjectNull(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName) { + const CFX_WideString& wsPropertyName) { if (pObj.IsEmpty()) return; pObj->Set(pIsolate->GetCurrentContext(), - FXJS_WSToJSString(pIsolate, PropertyName), v8::Local()) + FXJS_WSToJSString(pIsolate, wsPropertyName), + v8::Local()) .FromJust(); } diff --git a/fpdfsdk/jsapi/include/fxjs_v8.h b/fpdfsdk/jsapi/include/fxjs_v8.h index 34ff377d92..235f3bc902 100644 --- a/fpdfsdk/jsapi/include/fxjs_v8.h +++ b/fpdfsdk/jsapi/include/fxjs_v8.h @@ -229,50 +229,47 @@ void FXJS_SetPrivate(v8::Isolate* pIsolate, void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local pObj); void FXJS_FreePrivate(void* p); void FXJS_FreePrivate(v8::Local pObj); - void FXJS_Error(v8::Isolate* isolate, const CFX_WideString& message); -v8::Local FXJS_WSToJSString(v8::Isolate* pIsolate, - const wchar_t* PropertyName, - int Len = -1); +v8::Local FXJS_WSToJSString(v8::Isolate* pIsolate, + const CFX_WideString& wsPropertyName); v8::Local FXJS_GetObjectElement(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName); + const CFX_WideString& PropertyName); v8::Local FXJS_GetObjectElementNames(v8::Isolate* pIsolate, v8::Local pObj); - v8::Local FXJS_GetArrayElement(v8::Isolate* pIsolate, v8::Local pArray, unsigned index); -unsigned FXJS_GetArrayLength(v8::Local pArray); +unsigned FXJS_GetArrayLength(v8::Local pArray); void FXJS_PutObjectString(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName, - const wchar_t* sValue); + const CFX_WideString& wsPropertyName, + const CFX_WideString& wsValue); void FXJS_PutObjectNumber(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName, + const CFX_WideString& PropertyName, int nValue); void FXJS_PutObjectNumber(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName, + const CFX_WideString& PropertyName, float fValue); void FXJS_PutObjectNumber(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName, + const CFX_WideString& PropertyName, double dValue); void FXJS_PutObjectBoolean(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName, + const CFX_WideString& PropertyName, bool bValue); void FXJS_PutObjectObject(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName, + const CFX_WideString& PropertyName, v8::Local pPut); void FXJS_PutObjectNull(v8::Isolate* pIsolate, v8::Local pObj, - const wchar_t* PropertyName); + const CFX_WideString& PropertyName); unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate, v8::Local pArray, unsigned index, -- cgit v1.2.3