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 +-- fxjs/fxjs_v8.cpp | 26 +++++++-------- fxjs/fxjs_v8.h | 4 +-- 7 files changed, 74 insertions(+), 66 deletions(-) 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); diff --git a/fxjs/fxjs_v8.cpp b/fxjs/fxjs_v8.cpp index 1f049a0dd3..053db07d3b 100644 --- a/fxjs/fxjs_v8.cpp +++ b/fxjs/fxjs_v8.cpp @@ -558,22 +558,14 @@ void* CFXJS_Engine::GetObjectPrivate(v8::Local pObj) { return pPerObjectData ? pPerObjectData->m_pPrivate : nullptr; } -v8::Local CFXJS_Engine::WSToJSString( - const CFX_WideString& wsPropertyName) { - v8::Isolate* pIsolate = m_isolate ? m_isolate : v8::Isolate::GetCurrent(); - CFX_ByteString bs = wsPropertyName.UTF8Encode(); - return v8::String::NewFromUtf8(pIsolate, bs.c_str(), - v8::NewStringType::kNormal, bs.GetLength()) - .ToLocalChecked(); -} - v8::Local CFXJS_Engine::GetObjectProperty( v8::Local pObj, const CFX_WideString& wsPropertyName) { if (pObj.IsEmpty()) return v8::Local(); v8::Local val; - if (!pObj->Get(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName)) + if (!pObj->Get(m_isolate->GetCurrentContext(), + NewString(wsPropertyName.AsStringC())) .ToLocal(&val)) return v8::Local(); return val; @@ -602,7 +594,8 @@ void CFXJS_Engine::PutObjectProperty(v8::Local pObj, v8::Local pPut) { if (pObj.IsEmpty()) return; - pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName), pPut) + pObj->Set(m_isolate->GetCurrentContext(), + NewString(wsPropertyName.AsStringC()), pPut) .FromJust(); } @@ -661,8 +654,15 @@ v8::Local CFXJS_Engine::NewBoolean(bool b) { return v8::Boolean::New(m_isolate, b); } -v8::Local CFXJS_Engine::NewString(const CFX_WideString& str) { - return WSToJSString(str.c_str()); +v8::Local CFXJS_Engine::NewString(const CFX_ByteStringC& str) { + v8::Isolate* pIsolate = m_isolate ? m_isolate : v8::Isolate::GetCurrent(); + return v8::String::NewFromUtf8(pIsolate, str.c_str(), + v8::NewStringType::kNormal, str.GetLength()) + .ToLocalChecked(); +} + +v8::Local CFXJS_Engine::NewString(const CFX_WideStringC& str) { + return NewString(FX_UTF8Encode(str).AsStringC()); } v8::Local CFXJS_Engine::NewNull() { diff --git a/fxjs/fxjs_v8.h b/fxjs/fxjs_v8.h index 8283184e47..50b0b2c6d0 100644 --- a/fxjs/fxjs_v8.h +++ b/fxjs/fxjs_v8.h @@ -184,7 +184,8 @@ class CFXJS_Engine { v8::Local NewNumber(double number); v8::Local NewNumber(float number); v8::Local NewBoolean(bool b); - v8::Local NewString(const CFX_WideString& str); + v8::Local NewString(const CFX_ByteStringC& str); + v8::Local NewString(const CFX_WideStringC& str); v8::Local NewDate(double d); v8::Local NewFxDynamicObj(int nObjDefnID, bool bStatic = false); @@ -221,7 +222,6 @@ class CFXJS_Engine { void SetConstArray(const CFX_WideString& name, v8::Local array); v8::Local GetConstArray(const CFX_WideString& name); - v8::Local WSToJSString(const CFX_WideString& wsPropertyName); void Error(const CFX_WideString& message); protected: -- cgit v1.2.3