From c6dc69fb69e5d9974aa451d590194d568b78131b Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Thu, 23 Feb 2017 09:53:09 -0800 Subject: Store JS string constants as single-byte strings. Save some space since none contain non-ascii characters. Avoid allocating C++ WideStrings just to convert back to UTF8 when defining properties. Change-Id: Id94db21b32ee7a96856c35a09f7550b54599ae13 Reviewed-on: https://pdfium-review.googlesource.com/2826 Reviewed-by: dsinclair Commit-Queue: dsinclair --- fpdfsdk/javascript/Consts.cpp | 72 ++++++++++++++++++++--------------------- fpdfsdk/javascript/Document.cpp | 27 ++++++++++------ fpdfsdk/javascript/JS_Define.h | 2 +- fpdfsdk/javascript/JS_Value.cpp | 5 +-- fpdfsdk/javascript/global.cpp | 4 +-- 5 files changed, 59 insertions(+), 51 deletions(-) (limited to 'fpdfsdk') diff --git a/fpdfsdk/javascript/Consts.cpp b/fpdfsdk/javascript/Consts.cpp index 59bb9c6336..82f9b4c945 100644 --- a/fpdfsdk/javascript/Consts.cpp +++ b/fpdfsdk/javascript/Consts.cpp @@ -11,11 +11,11 @@ #include "fpdfsdk/javascript/JS_Value.h" JSConstSpec CJS_Border::ConstSpecs[] = { - {"s", JSConstSpec::String, 0, L"solid"}, - {"b", JSConstSpec::String, 0, L"beveled"}, - {"d", JSConstSpec::String, 0, L"dashed"}, - {"i", JSConstSpec::String, 0, L"inset"}, - {"u", JSConstSpec::String, 0, L"underline"}, + {"s", JSConstSpec::String, 0, "solid"}, + {"b", JSConstSpec::String, 0, "beveled"}, + {"d", JSConstSpec::String, 0, "dashed"}, + {"i", JSConstSpec::String, 0, "inset"}, + {"u", JSConstSpec::String, 0, "underline"}, {0, JSConstSpec::Number, 0, 0}}; IMPLEMENT_JS_CLASS_CONST(CJS_Border, border) @@ -27,28 +27,28 @@ JSConstSpec CJS_Display::ConstSpecs[] = {{"visible", JSConstSpec::Number, 0, 0}, IMPLEMENT_JS_CLASS_CONST(CJS_Display, display) JSConstSpec CJS_Font::ConstSpecs[] = { - {"Times", JSConstSpec::String, 0, L"Times-Roman"}, - {"TimesB", JSConstSpec::String, 0, L"Times-Bold"}, - {"TimesI", JSConstSpec::String, 0, L"Times-Italic"}, - {"TimesBI", JSConstSpec::String, 0, L"Times-BoldItalic"}, - {"Helv", JSConstSpec::String, 0, L"Helvetica"}, - {"HelvB", JSConstSpec::String, 0, L"Helvetica-Bold"}, - {"HelvI", JSConstSpec::String, 0, L"Helvetica-Oblique"}, - {"HelvBI", JSConstSpec::String, 0, L"Helvetica-BoldOblique"}, - {"Cour", JSConstSpec::String, 0, L"Courier"}, - {"CourB", JSConstSpec::String, 0, L"Courier-Bold"}, - {"CourI", JSConstSpec::String, 0, L"Courier-Oblique"}, - {"CourBI", JSConstSpec::String, 0, L"Courier-BoldOblique"}, - {"Symbol", JSConstSpec::String, 0, L"Symbol"}, - {"ZapfD", JSConstSpec::String, 0, L"ZapfDingbats"}, + {"Times", JSConstSpec::String, 0, "Times-Roman"}, + {"TimesB", JSConstSpec::String, 0, "Times-Bold"}, + {"TimesI", JSConstSpec::String, 0, "Times-Italic"}, + {"TimesBI", JSConstSpec::String, 0, "Times-BoldItalic"}, + {"Helv", JSConstSpec::String, 0, "Helvetica"}, + {"HelvB", JSConstSpec::String, 0, "Helvetica-Bold"}, + {"HelvI", JSConstSpec::String, 0, "Helvetica-Oblique"}, + {"HelvBI", JSConstSpec::String, 0, "Helvetica-BoldOblique"}, + {"Cour", JSConstSpec::String, 0, "Courier"}, + {"CourB", JSConstSpec::String, 0, "Courier-Bold"}, + {"CourI", JSConstSpec::String, 0, "Courier-Oblique"}, + {"CourBI", JSConstSpec::String, 0, "Courier-BoldOblique"}, + {"Symbol", JSConstSpec::String, 0, "Symbol"}, + {"ZapfD", JSConstSpec::String, 0, "ZapfDingbats"}, {0, JSConstSpec::Number, 0, 0}}; IMPLEMENT_JS_CLASS_CONST(CJS_Font, font) JSConstSpec CJS_Highlight::ConstSpecs[] = { - {"n", JSConstSpec::String, 0, L"none"}, - {"i", JSConstSpec::String, 0, L"invert"}, - {"p", JSConstSpec::String, 0, L"push"}, - {"o", JSConstSpec::String, 0, L"outline"}, + {"n", JSConstSpec::String, 0, "none"}, + {"i", JSConstSpec::String, 0, "invert"}, + {"p", JSConstSpec::String, 0, "push"}, + {"o", JSConstSpec::String, 0, "outline"}, {0, JSConstSpec::Number, 0, 0}}; IMPLEMENT_JS_CLASS_CONST(CJS_Highlight, highlight) @@ -78,23 +78,23 @@ JSConstSpec CJS_ScaleWhen::ConstSpecs[] = { IMPLEMENT_JS_CLASS_CONST(CJS_ScaleWhen, scaleWhen) JSConstSpec CJS_Style::ConstSpecs[] = { - {"ch", JSConstSpec::String, 0, L"check"}, - {"cr", JSConstSpec::String, 0, L"cross"}, - {"di", JSConstSpec::String, 0, L"diamond"}, - {"ci", JSConstSpec::String, 0, L"circle"}, - {"st", JSConstSpec::String, 0, L"star"}, - {"sq", JSConstSpec::String, 0, L"square"}, + {"ch", JSConstSpec::String, 0, "check"}, + {"cr", JSConstSpec::String, 0, "cross"}, + {"di", JSConstSpec::String, 0, "diamond"}, + {"ci", JSConstSpec::String, 0, "circle"}, + {"st", JSConstSpec::String, 0, "star"}, + {"sq", JSConstSpec::String, 0, "square"}, {0, JSConstSpec::Number, 0, 0}}; IMPLEMENT_JS_CLASS_CONST(CJS_Style, style) JSConstSpec CJS_Zoomtype::ConstSpecs[] = { - {"none", JSConstSpec::String, 0, L"NoVary"}, - {"fitP", JSConstSpec::String, 0, L"FitPage"}, - {"fitW", JSConstSpec::String, 0, L"FitWidth"}, - {"fitH", JSConstSpec::String, 0, L"FitHeight"}, - {"fitV", JSConstSpec::String, 0, L"FitVisibleWidth"}, - {"pref", JSConstSpec::String, 0, L"Preferred"}, - {"refW", JSConstSpec::String, 0, L"ReflowWidth"}, + {"none", JSConstSpec::String, 0, "NoVary"}, + {"fitP", JSConstSpec::String, 0, "FitPage"}, + {"fitW", JSConstSpec::String, 0, "FitWidth"}, + {"fitH", JSConstSpec::String, 0, "FitHeight"}, + {"fitV", JSConstSpec::String, 0, "FitVisibleWidth"}, + {"pref", JSConstSpec::String, 0, "Preferred"}, + {"refW", JSConstSpec::String, 0, "ReflowWidth"}, {0, JSConstSpec::Number, 0, 0}}; IMPLEMENT_JS_CLASS_CONST(CJS_Zoomtype, zoomtype) diff --git a/fpdfsdk/javascript/Document.cpp b/fpdfsdk/javascript/Document.cpp index 3fdb906108..fbcd53f35a 100644 --- a/fpdfsdk/javascript/Document.cpp +++ b/fpdfsdk/javascript/Document.cpp @@ -766,18 +766,24 @@ bool Document::info(CJS_Runtime* pRuntime, CFX_WideString cwTrapped = pDictionary->GetUnicodeTextFor("Trapped"); v8::Local pObj = pRuntime->NewFxDynamicObj(-1); - pRuntime->PutObjectProperty(pObj, L"Author", pRuntime->NewString(cwAuthor)); - pRuntime->PutObjectProperty(pObj, L"Title", pRuntime->NewString(cwTitle)); - pRuntime->PutObjectProperty(pObj, L"Subject", pRuntime->NewString(cwSubject)); + pRuntime->PutObjectProperty(pObj, L"Author", + pRuntime->NewString(cwAuthor.AsStringC())); + pRuntime->PutObjectProperty(pObj, L"Title", + pRuntime->NewString(cwTitle.AsStringC())); + pRuntime->PutObjectProperty(pObj, L"Subject", + pRuntime->NewString(cwSubject.AsStringC())); pRuntime->PutObjectProperty(pObj, L"Keywords", - pRuntime->NewString(cwKeywords)); - pRuntime->PutObjectProperty(pObj, L"Creator", pRuntime->NewString(cwCreator)); + pRuntime->NewString(cwKeywords.AsStringC())); + pRuntime->PutObjectProperty(pObj, L"Creator", + pRuntime->NewString(cwCreator.AsStringC())); pRuntime->PutObjectProperty(pObj, L"Producer", - pRuntime->NewString(cwProducer)); + pRuntime->NewString(cwProducer.AsStringC())); pRuntime->PutObjectProperty(pObj, L"CreationDate", - pRuntime->NewString(cwCreationDate)); - pRuntime->PutObjectProperty(pObj, L"ModDate", pRuntime->NewString(cwModDate)); - pRuntime->PutObjectProperty(pObj, L"Trapped", pRuntime->NewString(cwTrapped)); + pRuntime->NewString(cwCreationDate.AsStringC())); + pRuntime->PutObjectProperty(pObj, L"ModDate", + pRuntime->NewString(cwModDate.AsStringC())); + pRuntime->PutObjectProperty(pObj, L"Trapped", + pRuntime->NewString(cwTrapped.AsStringC())); // It's to be compatible to non-standard info dictionary. for (const auto& it : *pDictionary) { @@ -786,7 +792,8 @@ bool Document::info(CJS_Runtime* pRuntime, CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey.AsStringC()); if (pValueObj->IsString() || pValueObj->IsName()) { pRuntime->PutObjectProperty( - pObj, wsKey, pRuntime->NewString(pValueObj->GetUnicodeText())); + pObj, wsKey, + pRuntime->NewString(pValueObj->GetUnicodeText().AsStringC())); } else if (pValueObj->IsNumber()) { pRuntime->PutObjectProperty(pObj, wsKey, pRuntime->NewNumber(pValueObj->GetNumber())); diff --git a/fpdfsdk/javascript/JS_Define.h b/fpdfsdk/javascript/JS_Define.h index 0bd3a5e722..ee7448c649 100644 --- a/fpdfsdk/javascript/JS_Define.h +++ b/fpdfsdk/javascript/JS_Define.h @@ -20,7 +20,7 @@ struct JSConstSpec { const char* pName; Type eType; double number; - const wchar_t* pStr; + const char* pStr; }; struct JSPropertySpec { diff --git a/fpdfsdk/javascript/JS_Value.cpp b/fpdfsdk/javascript/JS_Value.cpp index 7900914a8e..b058847564 100644 --- a/fpdfsdk/javascript/JS_Value.cpp +++ b/fpdfsdk/javascript/JS_Value.cpp @@ -677,8 +677,9 @@ double JS_DateParse(const CFX_WideString& str) { if (v->IsFunction()) { v8::Local funC = v8::Local::Cast(v); const int argc = 1; - v8::Local timeStr = - CJS_Runtime::CurrentRuntimeFromIsolate(pIsolate)->WSToJSString(str); + v8::Local timeStr = + CJS_Runtime::CurrentRuntimeFromIsolate(pIsolate)->NewString( + str.AsStringC()); v8::Local argv[argc] = {timeStr}; v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked(); if (v->IsNumber()) { diff --git a/fpdfsdk/javascript/global.cpp b/fpdfsdk/javascript/global.cpp index eb7afb1332..7dd8ab57be 100644 --- a/fpdfsdk/javascript/global.cpp +++ b/fpdfsdk/javascript/global.cpp @@ -206,7 +206,7 @@ void JSGlobalAlternate::UpdateGlobalPersistentVariables() { pData->bPersistent == 1); pRuntime->PutObjectProperty( m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), - pRuntime->NewString(pData->data.sData.UTF8Decode())); + pRuntime->NewString(pData->data.sData.UTF8Decode().AsStringC())); break; case JS_GlobalDataType::OBJECT: { v8::Local pObj = pRuntime->NewFxDynamicObj(-1); @@ -335,7 +335,7 @@ void JSGlobalAlternate::PutObjectProperty(v8::Local pObj, case JS_GlobalDataType::STRING: pRuntime->PutObjectProperty( pObj, pObjData->sKey.UTF8Decode(), - pRuntime->NewString(pObjData->sData.UTF8Decode())); + pRuntime->NewString(pObjData->sData.UTF8Decode().AsStringC())); break; case JS_GlobalDataType::OBJECT: { v8::Local pNewObj = pRuntime->NewFxDynamicObj(-1); -- cgit v1.2.3