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
diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp
index 4c361b1..551d649 100644
--- a/fpdfsdk/javascript/Document.cpp
+++ b/fpdfsdk/javascript/Document.cpp
@@ -769,16 +769,15 @@
CJS_Runtime* pRuntime = pContext->GetJSRuntime();
v8::Local<v8::Object> 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 @@
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 0b31494..e2c990c 100644
--- a/fpdfsdk/javascript/JS_Value.cpp
+++ b/fpdfsdk/javascript/JS_Value.cpp
@@ -787,7 +787,7 @@
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 @@
.ToLocalChecked();
if (v->IsFunction()) {
v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v);
-
const int argc = 1;
v8::Local<v8::String> timeStr = FXJS_WSToJSString(pIsolate, str);
v8::Local<v8::Value> argv[argc] = {timeStr};
diff --git a/fpdfsdk/javascript/JS_Value.h b/fpdfsdk/javascript/JS_Value.h
index 423d7f6..8adec4b 100644
--- a/fpdfsdk/javascript/JS_Value.h
+++ b/fpdfsdk/javascript/JS_Value.h
@@ -208,7 +208,7 @@
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 9f7b60f..a84c093 100644
--- a/fpdfsdk/javascript/PublicMethods.cpp
+++ b/fpdfsdk/javascript/PublicMethods.cpp
@@ -306,7 +306,7 @@
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 @@
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 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 7242a14..380713b 100644
--- a/fpdfsdk/javascript/global.cpp
+++ b/fpdfsdk/javascript/global.cpp
@@ -233,15 +233,14 @@
pData->data.dData, false, "",
v8::Local<v8::Object>(), 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<v8::Object>(), 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 @@
false, pData->data.sData, v8::Local<v8::Object>(),
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 @@
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<v8::Object>(),
pData->bPersistent == 1);
FXJS_PutObjectNull(NULL, m_pJSObject->ToV8Object(),
- pData->data.sKey.UTF8Decode().c_str());
+ pData->data.sKey.UTF8Decode());
break;
}
}
@@ -324,7 +323,7 @@
FXJS_ToString(isolate, FXJS_GetArrayElement(isolate, pKeyList, i));
CFX_ByteString sKey = ws.UTF8Encode();
- v8::Local<v8::Value> v = FXJS_GetObjectElement(isolate, pObj, ws.c_str());
+ v8::Local<v8::Value> 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 @@
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<v8::Object> 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 c66cf91..a471d56 100644
--- a/fpdfsdk/jsapi/fxjs_v8.cpp
+++ b/fpdfsdk/jsapi/fxjs_v8.cpp
@@ -583,25 +583,24 @@
}
v8::Local<v8::String> 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<v8::Value> FXJS_GetObjectElement(v8::Isolate* pIsolate,
- v8::Local<v8::Object> pObj,
- const wchar_t* PropertyName) {
+v8::Local<v8::Value> FXJS_GetObjectElement(
+ v8::Isolate* pIsolate,
+ v8::Local<v8::Object> pObj,
+ const CFX_WideString& wsPropertyName) {
if (pObj.IsEmpty())
return v8::Local<v8::Value>();
v8::Local<v8::Value> val;
if (!pObj->Get(pIsolate->GetCurrentContext(),
- FXJS_WSToJSString(pIsolate, PropertyName))
+ FXJS_WSToJSString(pIsolate, wsPropertyName))
.ToLocal(&val))
return v8::Local<v8::Value>();
return val;
@@ -619,82 +618,83 @@
void FXJS_PutObjectString(v8::Isolate* pIsolate,
v8::Local<v8::Object> 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<v8::Object> 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<v8::Object> 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<v8::Object> 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<v8::Object> 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<v8::Object> pObj,
- const wchar_t* PropertyName,
+ const CFX_WideString& wsPropertyName,
v8::Local<v8::Object> 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<v8::Object> pObj,
- const wchar_t* PropertyName) {
+ const CFX_WideString& wsPropertyName) {
if (pObj.IsEmpty())
return;
pObj->Set(pIsolate->GetCurrentContext(),
- FXJS_WSToJSString(pIsolate, PropertyName), v8::Local<v8::Object>())
+ FXJS_WSToJSString(pIsolate, wsPropertyName),
+ v8::Local<v8::Object>())
.FromJust();
}
diff --git a/fpdfsdk/jsapi/include/fxjs_v8.h b/fpdfsdk/jsapi/include/fxjs_v8.h
index 34ff377..235f3bc 100644
--- a/fpdfsdk/jsapi/include/fxjs_v8.h
+++ b/fpdfsdk/jsapi/include/fxjs_v8.h
@@ -229,50 +229,47 @@
void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj);
void FXJS_FreePrivate(void* p);
void FXJS_FreePrivate(v8::Local<v8::Object> pObj);
-
void FXJS_Error(v8::Isolate* isolate, const CFX_WideString& message);
-v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate,
- const wchar_t* PropertyName,
- int Len = -1);
+v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate,
+ const CFX_WideString& wsPropertyName);
v8::Local<v8::Value> FXJS_GetObjectElement(v8::Isolate* pIsolate,
v8::Local<v8::Object> pObj,
- const wchar_t* PropertyName);
+ const CFX_WideString& PropertyName);
v8::Local<v8::Array> FXJS_GetObjectElementNames(v8::Isolate* pIsolate,
v8::Local<v8::Object> pObj);
-
v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate,
v8::Local<v8::Array> pArray,
unsigned index);
-unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray);
+unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray);
void FXJS_PutObjectString(v8::Isolate* pIsolate,
v8::Local<v8::Object> 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<v8::Object> pObj,
- const wchar_t* PropertyName,
+ const CFX_WideString& PropertyName,
int nValue);
void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
v8::Local<v8::Object> pObj,
- const wchar_t* PropertyName,
+ const CFX_WideString& PropertyName,
float fValue);
void FXJS_PutObjectNumber(v8::Isolate* pIsolate,
v8::Local<v8::Object> pObj,
- const wchar_t* PropertyName,
+ const CFX_WideString& PropertyName,
double dValue);
void FXJS_PutObjectBoolean(v8::Isolate* pIsolate,
v8::Local<v8::Object> pObj,
- const wchar_t* PropertyName,
+ const CFX_WideString& PropertyName,
bool bValue);
void FXJS_PutObjectObject(v8::Isolate* pIsolate,
v8::Local<v8::Object> pObj,
- const wchar_t* PropertyName,
+ const CFX_WideString& PropertyName,
v8::Local<v8::Object> pPut);
void FXJS_PutObjectNull(v8::Isolate* pIsolate,
v8::Local<v8::Object> pObj,
- const wchar_t* PropertyName);
+ const CFX_WideString& PropertyName);
unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate,
v8::Local<v8::Array> pArray,
unsigned index,