forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegexPattern.cpp
More file actions
246 lines (217 loc) · 7.86 KB
/
RegexPattern.cpp
File metadata and controls
246 lines (217 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "ParserPch.h"
namespace UnifiedRegex
{
RegexPattern::RegexPattern(Js::JavascriptLibrary *const library, Program* program, bool isLiteral)
: library(library), isLiteral(isLiteral), isShallowClone(false), testCache(nullptr)
{
rep.unified.program = program;
rep.unified.matcher = nullptr;
rep.unified.trigramInfo = nullptr;
}
RegexPattern *RegexPattern::New(Js::ScriptContext *scriptContext, Program* program, bool isLiteral)
{
return
RecyclerNewFinalized(
scriptContext->GetRecycler(),
RegexPattern,
scriptContext->GetLibrary(),
program,
isLiteral);
}
void RegexPattern::Finalize(bool isShutdown)
{
if (isShutdown)
{
return;
}
const auto scriptContext = GetScriptContext();
if (!scriptContext)
{
return;
}
#if DBG
// In JSRT or ChakraEngine, we might not have a chance to close at finalize time
if (!isLiteral && !scriptContext->IsClosed() &&
!scriptContext->GetThreadContext()->IsJSRT() &&
!scriptContext->GetLibrary()->IsChakraEngine())
{
const auto source = GetSource();
RegexPattern *p = nullptr;
bool hasRegexPatternForSourceKey = GetScriptContext()->GetDynamicRegexMap()->TryGetValue(
RegexKey(source.GetBuffer(), source.GetLength(), GetFlags()), &p);
bool isSourceLengthZero = source.GetLength() == 0;
bool isUniquePattern = p != this;
Assert(!hasRegexPatternForSourceKey || isSourceLengthZero || isUniquePattern);
}
#endif
if (isShallowClone)
{
return;
}
rep.unified.program->FreeBody(scriptContext->RegexAllocator());
}
void RegexPattern::Dispose(bool isShutdown)
{
}
Js::ScriptContext *RegexPattern::GetScriptContext() const
{
return library->GetScriptContext();
}
Js::InternalString RegexPattern::GetSource() const
{
return Js::InternalString(rep.unified.program->source, rep.unified.program->sourceLen);
}
RegexFlags RegexPattern::GetFlags() const
{
return rep.unified.program->flags;
}
uint16 RegexPattern::NumGroups() const
{
return rep.unified.program->numGroups;
}
bool RegexPattern::IsIgnoreCase() const
{
return (rep.unified.program->flags & IgnoreCaseRegexFlag) != 0;
}
bool RegexPattern::IsDotAll() const
{
return GetScriptContext()->GetConfig()->IsES2018RegExDotAllEnabled() && (rep.unified.program->flags & DotAllRegexFlag) != 0;
}
bool RegexPattern::IsGlobal() const
{
return (rep.unified.program->flags & GlobalRegexFlag) != 0;
}
bool RegexPattern::IsMultiline() const
{
return (rep.unified.program->flags & MultilineRegexFlag) != 0;
}
bool RegexPattern::IsUnicode() const
{
return GetScriptContext()->GetConfig()->IsES6UnicodeExtensionsEnabled() && (rep.unified.program->flags & UnicodeRegexFlag) != 0;
}
bool RegexPattern::IsSticky() const
{
return GetScriptContext()->GetConfig()->IsES6RegExStickyEnabled() && (rep.unified.program->flags & StickyRegexFlag) != 0;
}
bool RegexPattern::WasLastMatchSuccessful() const
{
return rep.unified.matcher != 0 && rep.unified.matcher->WasLastMatchSuccessful();
}
GroupInfo RegexPattern::GetGroup(int groupId) const
{
Assert(groupId == 0 || WasLastMatchSuccessful());
Assert(groupId >= 0 && groupId < NumGroups());
return rep.unified.matcher->GetGroup(groupId);
}
RegexPattern *RegexPattern::CopyToScriptContext(Js::ScriptContext *scriptContext)
{
// This routine assumes that this instance will outlive the copy, which is the case for copy-on-write,
// and therefore doesn't copy the immutable parts of the pattern. This should not be confused with a
// would be CloneToScriptContext which will would clone the immutable parts as well because the lifetime
// of a clone might be longer than the original.
RegexPattern *result = UnifiedRegex::RegexPattern::New(scriptContext, rep.unified.program, isLiteral);
Matcher *matcherClone = rep.unified.matcher ? rep.unified.matcher->CloneToScriptContext(scriptContext, result) : nullptr;
result->rep.unified.matcher = matcherClone;
result->isShallowClone = true;
return result;
}
Field(RegExpTestCache*) RegexPattern::EnsureTestCache()
{
if (this->testCache == nullptr)
{
this->testCache = RecyclerNewPlusZ(this->library->GetRecycler(), TestCacheSize * sizeof(void*), RegExpTestCache);
}
return this->testCache;
}
/* static */
uint RegexPattern::GetTestCacheIndex(Js::JavascriptString* str)
{
return (uint)(((uintptr_t)str) >> PolymorphicInlineCacheShift) & (TestCacheSize - 1);
}
#if ENABLE_REGEX_CONFIG_OPTIONS
void RegexPattern::Print(DebugWriter* w)
{
w->Print(_u("/"));
Js::InternalString str = GetSource();
if (str.GetLength() == 0)
w->Print(_u("(?:)"));
else
{
for (charcount_t i = 0; i < str.GetLength(); ++i)
{
const char16 c = str.GetBuffer()[i];
switch(c)
{
case _u('/'):
w->Print(_u("\\%lc"), c);
break;
case _u('\n'):
case _u('\r'):
case _u('\x2028'):
case _u('\x2029'):
w->PrintEscapedChar(c);
break;
case _u('-'):
w->Print(_u("-"));
break;
case _u('\\'):
Assert(i + 1 < str.GetLength()); // cannot end in a '\'
w->Print(_u("\\%lc"), str.GetBuffer()[++i]);
break;
default:
w->PrintEscapedChar(c);
break;
}
}
}
w->Print(_u("/"));
if (IsIgnoreCase())
w->Print(_u("i"));
if (IsGlobal())
w->Print(_u("g"));
if (IsMultiline())
w->Print(_u("m"));
if (IsDotAll())
w->Print(_u("s"));
if (IsUnicode())
w->Print(_u("u"));
if (IsSticky())
w->Print(_u("y"));
w->Print(_u(" /* "));
w->Print(_u(", "));
w->Print(isLiteral ? _u("literal") : _u("dynamic"));
w->Print(_u(" */"));
}
/* static */
void RegexPattern::TraceTestCache(bool cacheHit, Js::JavascriptString* input, Js::JavascriptString* cachedValue, bool disabled)
{
if (REGEX_CONFIG_FLAG(RegexTracing))
{
if (disabled)
{
Output::Print(_u("Regexp Test Cache Disabled.\n"));
}
else if (cacheHit)
{
Output::Print(_u("Regexp Test Cache Hit.\n"));
}
else
{
Output::Print(_u("Regexp Test Cache Miss. "));
if (cachedValue != nullptr)
{
Output::Print(_u("Input: (%p); Cached String: (%p) '%s'\n"), input, cachedValue, cachedValue->GetString());
}
else
{
Output::Print(_u("Cache was empty\n"));
}
}
}
}
#endif
}