From 3e44a7bd2b60ace1ee8bc8f48c709056f65c3dc1 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Fri, 4 Mar 2016 12:25:33 -0800 Subject: Only place primitive objects on the V8 global template. Kill some crummy comments along the way. BUG=pdfium:419 R=jochen@chromium.org Review URL: https://codereview.chromium.org/1759893002 . --- fpdfsdk/src/javascript/Consts.cpp | 273 ++++++++++++---------------------- fpdfsdk/src/javascript/JS_Runtime.cpp | 9 ++ fpdfsdk/src/javascript/JS_Runtime.h | 4 + fpdfsdk/src/jsapi/fxjs_v8.cpp | 11 +- 4 files changed, 111 insertions(+), 186 deletions(-) (limited to 'fpdfsdk/src') diff --git a/fpdfsdk/src/javascript/Consts.cpp b/fpdfsdk/src/javascript/Consts.cpp index 132f214d2d..b7cc2d0a46 100644 --- a/fpdfsdk/src/javascript/Consts.cpp +++ b/fpdfsdk/src/javascript/Consts.cpp @@ -11,8 +11,6 @@ #include "fpdfsdk/src/javascript/JS_Object.h" #include "fpdfsdk/src/javascript/JS_Value.h" -/* ------------------------------ border ------------------------------ */ - BEGIN_JS_STATIC_CONST(CJS_Border) JS_STATIC_CONST_ENTRY_STRING(L"s", L"solid") JS_STATIC_CONST_ENTRY_STRING(L"b", L"beveled") @@ -20,22 +18,16 @@ JS_STATIC_CONST_ENTRY_STRING(L"d", L"dashed") JS_STATIC_CONST_ENTRY_STRING(L"i", L"inset") JS_STATIC_CONST_ENTRY_STRING(L"u", L"underline") END_JS_STATIC_CONST() - IMPLEMENT_JS_CLASS_CONST(CJS_Border, border) -/* ------------------------------ display ------------------------------ */ - BEGIN_JS_STATIC_CONST(CJS_Display) JS_STATIC_CONST_ENTRY_NUMBER(L"visible", 0) JS_STATIC_CONST_ENTRY_NUMBER(L"hidden", 1) JS_STATIC_CONST_ENTRY_NUMBER(L"noPrint", 2) JS_STATIC_CONST_ENTRY_NUMBER(L"noView", 3) END_JS_STATIC_CONST() - IMPLEMENT_JS_CLASS_CONST(CJS_Display, display) -/* ------------------------------ font ------------------------------ */ - BEGIN_JS_STATIC_CONST(CJS_Font) JS_STATIC_CONST_ENTRY_STRING(L"Times", L"Times-Roman") JS_STATIC_CONST_ENTRY_STRING(L"TimesB", L"Times-Bold") @@ -52,22 +44,16 @@ JS_STATIC_CONST_ENTRY_STRING(L"CourBI", L"Courier-BoldOblique") JS_STATIC_CONST_ENTRY_STRING(L"Symbol", L"Symbol") JS_STATIC_CONST_ENTRY_STRING(L"ZapfD", L"ZapfDingbats") END_JS_STATIC_CONST() - IMPLEMENT_JS_CLASS_CONST(CJS_Font, font) -/* ------------------------------ highlight ------------------------------ */ - BEGIN_JS_STATIC_CONST(CJS_Highlight) JS_STATIC_CONST_ENTRY_STRING(L"n", L"none") JS_STATIC_CONST_ENTRY_STRING(L"i", L"invert") JS_STATIC_CONST_ENTRY_STRING(L"p", L"push") JS_STATIC_CONST_ENTRY_STRING(L"o", L"outline") END_JS_STATIC_CONST() - IMPLEMENT_JS_CLASS_CONST(CJS_Highlight, highlight) -/* ------------------------------ position ------------------------------ */ - BEGIN_JS_STATIC_CONST(CJS_Position) JS_STATIC_CONST_ENTRY_NUMBER(L"textOnly", 0) JS_STATIC_CONST_ENTRY_NUMBER(L"iconOnly", 1) @@ -77,31 +63,22 @@ JS_STATIC_CONST_ENTRY_NUMBER(L"iconTextH", 4) JS_STATIC_CONST_ENTRY_NUMBER(L"textIconH", 5) JS_STATIC_CONST_ENTRY_NUMBER(L"overlay", 6) END_JS_STATIC_CONST() - IMPLEMENT_JS_CLASS_CONST(CJS_Position, position) -/* ------------------------------ scaleHow ------------------------------ */ - BEGIN_JS_STATIC_CONST(CJS_ScaleHow) JS_STATIC_CONST_ENTRY_NUMBER(L"proportional", 0) JS_STATIC_CONST_ENTRY_NUMBER(L"anamorphic", 1) END_JS_STATIC_CONST() - IMPLEMENT_JS_CLASS_CONST(CJS_ScaleHow, scaleHow) -/* ------------------------------ scaleWhen ------------------------------ */ - BEGIN_JS_STATIC_CONST(CJS_ScaleWhen) JS_STATIC_CONST_ENTRY_NUMBER(L"always", 0) JS_STATIC_CONST_ENTRY_NUMBER(L"never", 1) JS_STATIC_CONST_ENTRY_NUMBER(L"tooBig", 2) JS_STATIC_CONST_ENTRY_NUMBER(L"tooSmall", 3) END_JS_STATIC_CONST() - IMPLEMENT_JS_CLASS_CONST(CJS_ScaleWhen, scaleWhen) -/* ------------------------------ style ------------------------------ */ - BEGIN_JS_STATIC_CONST(CJS_Style) JS_STATIC_CONST_ENTRY_STRING(L"ch", L"check") JS_STATIC_CONST_ENTRY_STRING(L"cr", L"cross") @@ -110,11 +87,8 @@ JS_STATIC_CONST_ENTRY_STRING(L"ci", L"circle") JS_STATIC_CONST_ENTRY_STRING(L"st", L"star") JS_STATIC_CONST_ENTRY_STRING(L"sq", L"square") END_JS_STATIC_CONST() - IMPLEMENT_JS_CLASS_CONST(CJS_Style, style) -/* ------------------------------ zoomtype ------------------------------ */ - BEGIN_JS_STATIC_CONST(CJS_Zoomtype) JS_STATIC_CONST_ENTRY_STRING(L"none", L"NoVary") JS_STATIC_CONST_ENTRY_STRING(L"fitP", L"FitPage") @@ -124,172 +98,109 @@ JS_STATIC_CONST_ENTRY_STRING(L"fitV", L"FitVisibleWidth") JS_STATIC_CONST_ENTRY_STRING(L"pref", L"Preferred") JS_STATIC_CONST_ENTRY_STRING(L"refW", L"ReflowWidth") END_JS_STATIC_CONST() - IMPLEMENT_JS_CLASS_CONST(CJS_Zoomtype, zoomtype) -/* ------------------------------ CJS_GlobalConsts ------------------------- */ - -static void DefineGlobalConstString(CJS_Runtime* pRuntime, - const wchar_t* pConstName, - const wchar_t* pValue) { - FXJS_DefineGlobalConst(pRuntime->GetIsolate(), pConstName, - FXJS_NewString(pRuntime->GetIsolate(), pValue)); -} +#define GLOBAL_STRING(rt, name, value) \ + FXJS_DefineGlobalConst( \ + (rt)->GetIsolate(), (name), \ + [](const v8::FunctionCallbackInfo& info) { \ + info.GetReturnValue().Set(FXJS_NewString(info.GetIsolate(), (value))); \ + }) void CJS_GlobalConsts::DefineJSObjects(CJS_Runtime* pRuntime) { - DefineGlobalConstString( - pRuntime, L"IDS_GREATER_THAN", - L"Invalid value: must be greater than or equal to % s."); - DefineGlobalConstString( - pRuntime, L"IDS_GT_AND_LT", - L"Invalid value: must be greater than or equal to % s " - L"and less than or equal to % s."); - DefineGlobalConstString(pRuntime, L"IDS_LESS_THAN", - L"Invalid value: must be less than or equal to % s."); - DefineGlobalConstString(pRuntime, L"IDS_INVALID_MONTH", L"**Invalid**"); - DefineGlobalConstString( - pRuntime, L"IDS_INVALID_DATE", - L"Invalid date / time: please ensure that the date / time exists.Field"); - DefineGlobalConstString( - pRuntime, L"IDS_INVALID_VALUE", - L"The value entered does not match the format of the field"); - DefineGlobalConstString(pRuntime, L"IDS_AM", L"am"); - DefineGlobalConstString(pRuntime, L"IDS_PM", L"pm"); - DefineGlobalConstString( - pRuntime, L"IDS_MONTH_INFO", - L"January[1] February[2] March[3] April[4] May[5] " - L"June[6] July[7] August[8] September[9] October[10] " - L"November[11] December[12] Sept[9] Jan[1] Feb[2] Mar[3] " - L"Apr[4] Jun[6] Jul[7] Aug[8] Sep[9] Oct[10] Nov[11] " - L"Dec[12]"); - DefineGlobalConstString(pRuntime, L"IDS_STARTUP_CONSOLE_MSG", L"** ^ _ ^ **"); -} - -/* ------------------------------ CJS_GlobalArrays ------------------------ */ + GLOBAL_STRING(pRuntime, L"IDS_GREATER_THAN", + L"Invalid value: must be greater than or equal to % s."); -void DefineGlobalConstStringArray(CJS_Runtime* pRuntime, - const wchar_t* sConstName, - const wchar_t** pValues, - size_t nValues) { - CJS_Array array(pRuntime); - for (size_t i = 0; i < nValues; ++i) { - array.SetElement(i, CJS_Value(pRuntime, pValues[i])); - } - CJS_PropValue prop(pRuntime); - prop << array; - FXJS_DefineGlobalConst(pRuntime->GetIsolate(), sConstName, prop.ToV8Value()); -} - -void CJS_GlobalArrays::DefineJSObjects(CJS_Runtime* pRuntime) { - { - const FX_WCHAR* ArrayName = L"RE_NUMBER_ENTRY_DOT_SEP"; - const FX_WCHAR* ArrayContent[] = {L"[+-]?\\d*\\.?\\d*"}; - DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent, - FX_ArraySize(ArrayContent)); - } - - { - const FX_WCHAR* ArrayName = L"RE_NUMBER_COMMIT_DOT_SEP"; - const FX_WCHAR* ArrayContent[] = { - L"[+-]?\\d+(\\.\\d+)?", /* -1.0 or -1 */ - L"[+-]?\\.\\d+", /* -.1 */ - L"[+-]?\\d+\\." /* -1. */ - }; - DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent, - FX_ArraySize(ArrayContent)); - } - - { - const FX_WCHAR* ArrayName = L"RE_NUMBER_ENTRY_COMMA_SEP"; - const FX_WCHAR* ArrayContent[] = {L"[+-]?\\d*,?\\d*"}; - - DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent, - FX_ArraySize(ArrayContent)); - } - - { - const FX_WCHAR* ArrayName = L"RE_NUMBER_COMMIT_COMMA_SEP"; - const FX_WCHAR* ArrayContent[] = { - L"[+-]?\\d+([.,]\\d+)?", /* -1,0 or -1 */ - L"[+-]?[.,]\\d+", /* -,1 */ - L"[+-]?\\d+[.,]" /* -1, */ - }; - DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent, - FX_ArraySize(ArrayContent)); - } + GLOBAL_STRING(pRuntime, L"IDS_GT_AND_LT", + L"Invalid value: must be greater than or equal to % s " + L"and less than or equal to % s."); - { - const FX_WCHAR* ArrayName = L"RE_ZIP_ENTRY"; - const FX_WCHAR* ArrayContent[] = {L"\\d{0,5}"}; - DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent, - FX_ArraySize(ArrayContent)); - } - - { - const FX_WCHAR* ArrayName = L"RE_ZIP_COMMIT"; - const FX_WCHAR* ArrayContent[] = {L"\\d{5}"}; - DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent, - FX_ArraySize(ArrayContent)); - } + GLOBAL_STRING(pRuntime, L"IDS_LESS_THAN", + L"Invalid value: must be less than or equal to % s."); - { - const FX_WCHAR* ArrayName = L"RE_ZIP4_ENTRY"; - const FX_WCHAR* ArrayContent[] = {L"\\d{0,5}(\\.|[- ])?\\d{0,4}"}; - DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent, - FX_ArraySize(ArrayContent)); - } + GLOBAL_STRING(pRuntime, L"IDS_INVALID_MONTH", L"**Invalid**"); + GLOBAL_STRING( + pRuntime, L"IDS_INVALID_DATE", + L"Invalid date / time: please ensure that the date / time exists.Field"); - { - const FX_WCHAR* ArrayName = L"RE_ZIP4_COMMIT"; - const FX_WCHAR* ArrayContent[] = {L"\\d{5}(\\.|[- ])?\\d{4}"}; - DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent, - FX_ArraySize(ArrayContent)); - } + GLOBAL_STRING(pRuntime, L"IDS_INVALID_VALUE", + L"The value entered does not match the format of the field"); - { - const FX_WCHAR* ArrayName = L"RE_PHONE_ENTRY"; - const FX_WCHAR* ArrayContent[] = { - L"\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", // 555-1234 or 408 - // 555-1234 - L"\\(\\d{0,3}", // (408 - L"\\(\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", // (408) - // 555-1234 - // (allow the addition of parens as an afterthought) - L"\\(\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", // (408 555-1234 - - L"\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", // 408) 555-1234 - - L"011(\\.|[- \\d])*" // international - }; - DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent, - FX_ArraySize(ArrayContent)); - } + GLOBAL_STRING(pRuntime, L"IDS_AM", L"am"); + GLOBAL_STRING(pRuntime, L"IDS_PM", L"pm"); + GLOBAL_STRING(pRuntime, L"IDS_MONTH_INFO", + L"January[1] February[2] March[3] April[4] May[5] " + L"June[6] July[7] August[8] September[9] October[10] " + L"November[11] December[12] Sept[9] Jan[1] Feb[2] Mar[3] " + L"Apr[4] Jun[6] Jul[7] Aug[8] Sep[9] Oct[10] Nov[11] " + L"Dec[12]"); - { - const FX_WCHAR* ArrayName = L"RE_PHONE_COMMIT"; - const FX_WCHAR* ArrayContent[] = { - L"\\d{3}(\\.|[- ])?\\d{4}", // 555-1234 - L"\\d{3}(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}", // 408 555-1234 - L"\\(\\d{3}\\)(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}", // (408) 555-1234 - L"011(\\.|[- \\d])*" // international - }; - DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent, - FX_ArraySize(ArrayContent)); - } + GLOBAL_STRING(pRuntime, L"IDS_STARTUP_CONSOLE_MSG", L"** ^ _ ^ **"); +} - { - const FX_WCHAR* ArrayName = L"RE_SSN_ENTRY"; - const FX_WCHAR* ArrayContent[] = { - L"\\d{0,3}(\\.|[- ])?\\d{0,2}(\\.|[- ])?\\d{0,4}"}; - DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent, - FX_ArraySize(ArrayContent)); +#define GLOBAL_ARRAY(rt, name, ...) \ + { \ + const FX_WCHAR* values[] = {__VA_ARGS__}; \ + v8::Local array = FXJS_NewArray((rt)->GetIsolate()); \ + for (size_t i = 0; i < FX_ArraySize(values); ++i) \ + array->Set(i, FXJS_NewString((rt)->GetIsolate(), values[i])); \ + rt->SetConstArray(name, array); \ + FXJS_DefineGlobalConst( \ + (rt)->GetIsolate(), (name), \ + [](const v8::FunctionCallbackInfo& info) { \ + CJS_Runtime* pRuntime = static_cast( \ + FXJS_GetRuntimeFromIsolate(info.GetIsolate())); \ + if (pRuntime) \ + info.GetReturnValue().Set(pRuntime->GetConstArray(name)); \ + }); \ } - { - const FX_WCHAR* ArrayName = L"RE_SSN_COMMIT"; - const FX_WCHAR* ArrayContent[] = { - L"\\d{3}(\\.|[- ])?\\d{2}(\\.|[- ])?\\d{4}"}; - DefineGlobalConstStringArray(pRuntime, ArrayName, ArrayContent, - FX_ArraySize(ArrayContent)); - } +void CJS_GlobalArrays::DefineJSObjects(CJS_Runtime* pRuntime) { + GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_ENTRY_DOT_SEP", L"[+-]?\\d*\\.?\\d*"); + GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_COMMIT_DOT_SEP", + L"[+-]?\\d+(\\.\\d+)?", // -1.0 or -1 + L"[+-]?\\.\\d+", // -.1 + L"[+-]?\\d+\\."); // -1. + + GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_ENTRY_COMMA_SEP", L"[+-]?\\d*,?\\d*"); + GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_COMMIT_COMMA_SEP", + L"[+-]?\\d+([.,]\\d+)?", // -1,0 or -1 + L"[+-]?[.,]\\d+", // -,1 + L"[+-]?\\d+[.,]"); // -1, + + GLOBAL_ARRAY(pRuntime, L"RE_ZIP_ENTRY", L"\\d{0,5}"); + GLOBAL_ARRAY(pRuntime, L"RE_ZIP_COMMIT", L"\\d{5}"); + GLOBAL_ARRAY(pRuntime, L"RE_ZIP4_ENTRY", L"\\d{0,5}(\\.|[- ])?\\d{0,4}"); + GLOBAL_ARRAY(pRuntime, L"RE_ZIP4_COMMIT", L"\\d{5}(\\.|[- ])?\\d{4}"); + GLOBAL_ARRAY(pRuntime, L"RE_PHONE_ENTRY", + // 555-1234 or 408 555-1234 + L"\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", + + // (408 + L"\\(\\d{0,3}", + + // (408) 555-1234 + // (allow the addition of parens as an afterthought) + L"\\(\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", + + // (408 555-1234 + L"\\(\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", + + // 408) 555-1234 + L"\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}", + + // international + L"011(\\.|[- \\d])*"); + + GLOBAL_ARRAY( + pRuntime, L"RE_PHONE_COMMIT", L"\\d{3}(\\.|[- ])?\\d{4}", // 555-1234 + L"\\d{3}(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}", // 408 555-1234 + L"\\(\\d{3}\\)(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}", // (408) 555-1234 + L"011(\\.|[- \\d])*"); // international + + GLOBAL_ARRAY(pRuntime, L"RE_SSN_ENTRY", + L"\\d{0,3}(\\.|[- ])?\\d{0,2}(\\.|[- ])?\\d{0,4}"); + + GLOBAL_ARRAY(pRuntime, L"RE_SSN_COMMIT", + L"\\d{3}(\\.|[- ])?\\d{2}(\\.|[- ])?\\d{4}"); } diff --git a/fpdfsdk/src/javascript/JS_Runtime.cpp b/fpdfsdk/src/javascript/JS_Runtime.cpp index 76221db3f2..342b43e267 100644 --- a/fpdfsdk/src/javascript/JS_Runtime.cpp +++ b/fpdfsdk/src/javascript/JS_Runtime.cpp @@ -256,6 +256,15 @@ v8::Local CJS_Runtime::NewJSContext() { return v8::Local::New(m_isolate, m_context); } +void CJS_Runtime::SetConstArray(const CFX_WideString& name, + v8::Local array) { + m_ConstArrays[name] = v8::Global(m_isolate, array); +} + +v8::Local CJS_Runtime::GetConstArray(const CFX_WideString& name) { + return v8::Local::New(m_isolate, m_ConstArrays[name]); +} + #ifdef PDF_ENABLE_XFA CFX_WideString ChangeObjName(const CFX_WideString& str) { CFX_WideString sRet = str; diff --git a/fpdfsdk/src/javascript/JS_Runtime.h b/fpdfsdk/src/javascript/JS_Runtime.h index 6fc3894f44..8215b88994 100644 --- a/fpdfsdk/src/javascript/JS_Runtime.h +++ b/fpdfsdk/src/javascript/JS_Runtime.h @@ -58,6 +58,9 @@ class CJS_Runtime : public IJS_Runtime { v8::Isolate* GetIsolate() const { return m_isolate; } v8::Local NewJSContext(); + void SetConstArray(const CFX_WideString& name, v8::Local array); + v8::Local GetConstArray(const CFX_WideString& name); + #ifdef PDF_ENABLE_XFA FX_BOOL GetHValueByName(const CFX_ByteStringC& utf8Name, FXJSE_HVALUE hValue) override; @@ -80,6 +83,7 @@ class CJS_Runtime : public IJS_Runtime { bool m_isolateManaged; v8::Global m_context; std::vector*> m_StaticObjects; + std::map> m_ConstArrays; std::set m_observers; }; diff --git a/fpdfsdk/src/jsapi/fxjs_v8.cpp b/fpdfsdk/src/jsapi/fxjs_v8.cpp index f015f87136..5631ab6258 100644 --- a/fpdfsdk/src/jsapi/fxjs_v8.cpp +++ b/fpdfsdk/src/jsapi/fxjs_v8.cpp @@ -287,14 +287,15 @@ void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate, void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, const wchar_t* sConstName, - v8::Local pDefault) { + v8::FunctionCallback pConstGetter) { v8::Isolate::Scope isolate_scope(pIsolate); v8::HandleScope handle_scope(pIsolate); CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode(); - GetGlobalObjectTemplate(pIsolate)->Set( - v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), - v8::NewStringType::kNormal).ToLocalChecked(), - pDefault, v8::ReadOnly); + GetGlobalObjectTemplate(pIsolate) + ->SetAccessorProperty(v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), + v8::NewStringType::kNormal) + .ToLocalChecked(), + v8::FunctionTemplate::New(pIsolate, pConstGetter)); } void FXJS_InitializeRuntime( -- cgit v1.2.3