xref: /aosp_15_r20/external/pdfium/fxjs/cjs_globalarrays.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2017 The PDFium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "fxjs/cjs_globalarrays.h"
8 
9 #include <iterator>
10 
11 #include "third_party/base/numerics/safe_conversions.h"
12 #include "v8/include/v8-container.h"
13 #include "v8/include/v8-isolate.h"
14 
15 #define GLOBAL_ARRAY(rt, name, ...)                                            \
16   {                                                                            \
17     static const wchar_t* const values[] = {__VA_ARGS__};                      \
18     v8::Local<v8::Array> array = (rt)->NewArray();                             \
19     v8::Local<v8::Context> ctx = (rt)->GetIsolate()->GetCurrentContext();      \
20     for (size_t i = 0; i < std::size(values); ++i) {                           \
21       array                                                                    \
22           ->Set(ctx, pdfium::base::checked_cast<uint32_t>(i),                  \
23                 (rt)->NewString(values[i]))                                    \
24           .FromJust();                                                         \
25     }                                                                          \
26     (rt)->SetConstArray((name), array);                                        \
27     (rt)->DefineGlobalConst(                                                   \
28         (name), [](const v8::FunctionCallbackInfo<v8::Value>& info) {          \
29           CJS_Object* pObj = CFXJS_Engine::GetObjectPrivate(info.GetIsolate(), \
30                                                             info.Holder());    \
31           CJS_Runtime* pCurrentRuntime = pObj->GetRuntime();                   \
32           if (pCurrentRuntime)                                                 \
33             info.GetReturnValue().Set(pCurrentRuntime->GetConstArray(name));   \
34         });                                                                    \
35   }
36 
37 // static
DefineJSObjects(CJS_Runtime * pRuntime)38 void CJS_GlobalArrays::DefineJSObjects(CJS_Runtime* pRuntime) {
39   GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_ENTRY_DOT_SEP", L"[+-]?\\d*\\.?\\d*");
40   GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_COMMIT_DOT_SEP",
41                L"[+-]?\\d+(\\.\\d+)?",  // -1.0 or -1
42                L"[+-]?\\.\\d+",         // -.1
43                L"[+-]?\\d+\\.");        // -1.
44 
45   GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_ENTRY_COMMA_SEP", L"[+-]?\\d*,?\\d*");
46   GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_COMMIT_COMMA_SEP",
47                L"[+-]?\\d+([.,]\\d+)?",  // -1,0 or -1
48                L"[+-]?[.,]\\d+",         // -,1
49                L"[+-]?\\d+[.,]");        // -1,
50 
51   GLOBAL_ARRAY(pRuntime, L"RE_ZIP_ENTRY", L"\\d{0,5}");
52   GLOBAL_ARRAY(pRuntime, L"RE_ZIP_COMMIT", L"\\d{5}");
53   GLOBAL_ARRAY(pRuntime, L"RE_ZIP4_ENTRY", L"\\d{0,5}(\\.|[- ])?\\d{0,4}");
54   GLOBAL_ARRAY(pRuntime, L"RE_ZIP4_COMMIT", L"\\d{5}(\\.|[- ])?\\d{4}");
55   GLOBAL_ARRAY(pRuntime, L"RE_PHONE_ENTRY",
56                // 555-1234 or 408 555-1234
57                L"\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",
58 
59                // (408
60                L"\\(\\d{0,3}",
61 
62                // (408) 555-1234
63                // (allow the addition of parens as an afterthought)
64                L"\\(\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",
65 
66                // (408 555-1234
67                L"\\(\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",
68 
69                // 408) 555-1234
70                L"\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",
71 
72                // international
73                L"011(\\.|[- \\d])*");
74 
75   GLOBAL_ARRAY(
76       pRuntime, L"RE_PHONE_COMMIT", L"\\d{3}(\\.|[- ])?\\d{4}",  // 555-1234
77       L"\\d{3}(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}",               // 408 555-1234
78       L"\\(\\d{3}\\)(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}",  // (408) 555-1234
79       L"011(\\.|[- \\d])*");                              // international
80 
81   GLOBAL_ARRAY(pRuntime, L"RE_SSN_ENTRY",
82                L"\\d{0,3}(\\.|[- ])?\\d{0,2}(\\.|[- ])?\\d{0,4}");
83 
84   GLOBAL_ARRAY(pRuntime, L"RE_SSN_COMMIT",
85                L"\\d{3}(\\.|[- ])?\\d{2}(\\.|[- ])?\\d{4}");
86 }
87