1 // Copyright 2017 PDFium Authors. All rights reserved.
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 #define GLOBAL_ARRAY(rt, name, ...) \
10 { \
11 static const wchar_t* const values[] = {__VA_ARGS__}; \
12 v8::Local<v8::Array> array = (rt)->NewArray(); \
13 v8::Local<v8::Context> ctx = (rt)->GetIsolate()->GetCurrentContext(); \
14 for (size_t i = 0; i < FX_ArraySize(values); ++i) \
15 array->Set(ctx, i, (rt)->NewString(values[i])).FromJust(); \
16 (rt)->SetConstArray((name), array); \
17 (rt)->DefineGlobalConst( \
18 (name), [](const v8::FunctionCallbackInfo<v8::Value>& info) { \
19 CJS_Object* pObj = CFXJS_Engine::GetObjectPrivate(info.Holder()); \
20 CJS_Runtime* pCurrentRuntime = pObj->GetRuntime(); \
21 if (pCurrentRuntime) \
22 info.GetReturnValue().Set(pCurrentRuntime->GetConstArray(name)); \
23 }); \
24 }
25
26 // static
DefineJSObjects(CJS_Runtime * pRuntime)27 void CJS_GlobalArrays::DefineJSObjects(CJS_Runtime* pRuntime) {
28 GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_ENTRY_DOT_SEP", L"[+-]?\\d*\\.?\\d*");
29 GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_COMMIT_DOT_SEP",
30 L"[+-]?\\d+(\\.\\d+)?", // -1.0 or -1
31 L"[+-]?\\.\\d+", // -.1
32 L"[+-]?\\d+\\."); // -1.
33
34 GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_ENTRY_COMMA_SEP", L"[+-]?\\d*,?\\d*");
35 GLOBAL_ARRAY(pRuntime, L"RE_NUMBER_COMMIT_COMMA_SEP",
36 L"[+-]?\\d+([.,]\\d+)?", // -1,0 or -1
37 L"[+-]?[.,]\\d+", // -,1
38 L"[+-]?\\d+[.,]"); // -1,
39
40 GLOBAL_ARRAY(pRuntime, L"RE_ZIP_ENTRY", L"\\d{0,5}");
41 GLOBAL_ARRAY(pRuntime, L"RE_ZIP_COMMIT", L"\\d{5}");
42 GLOBAL_ARRAY(pRuntime, L"RE_ZIP4_ENTRY", L"\\d{0,5}(\\.|[- ])?\\d{0,4}");
43 GLOBAL_ARRAY(pRuntime, L"RE_ZIP4_COMMIT", L"\\d{5}(\\.|[- ])?\\d{4}");
44 GLOBAL_ARRAY(pRuntime, L"RE_PHONE_ENTRY",
45 // 555-1234 or 408 555-1234
46 L"\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",
47
48 // (408
49 L"\\(\\d{0,3}",
50
51 // (408) 555-1234
52 // (allow the addition of parens as an afterthought)
53 L"\\(\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",
54
55 // (408 555-1234
56 L"\\(\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",
57
58 // 408) 555-1234
59 L"\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",
60
61 // international
62 L"011(\\.|[- \\d])*");
63
64 GLOBAL_ARRAY(
65 pRuntime, L"RE_PHONE_COMMIT", L"\\d{3}(\\.|[- ])?\\d{4}", // 555-1234
66 L"\\d{3}(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}", // 408 555-1234
67 L"\\(\\d{3}\\)(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}", // (408) 555-1234
68 L"011(\\.|[- \\d])*"); // international
69
70 GLOBAL_ARRAY(pRuntime, L"RE_SSN_ENTRY",
71 L"\\d{0,3}(\\.|[- ])?\\d{0,2}(\\.|[- ])?\\d{0,4}");
72
73 GLOBAL_ARRAY(pRuntime, L"RE_SSN_COMMIT",
74 L"\\d{3}(\\.|[- ])?\\d{2}(\\.|[- ])?\\d{4}");
75 }
76