-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParserAtom.cpp
442 lines (369 loc) · 13.2 KB
/
ParserAtom.cpp
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "frontend/ParserAtom.h"
#include <type_traits>
#include "jsnum.h"
#include "frontend/NameCollections.h"
#include "vm/JSContext.h"
#include "vm/Printer.h"
#include "vm/Runtime.h"
#include "vm/StringType.h"
//
// Parser-Atoms should be disabled for now. This check ensures that.
// NOTE: This will be removed when the final transition patches from
// JS-atoms to parser-atoms lands.
//
#ifdef JS_PARSER_ATOMS
# error "Parser atoms define should remain disabled until this is removed."
#endif
using namespace js;
using namespace js::frontend;
namespace js {
namespace frontend {
static JS::OOM PARSER_ATOMS_OOM;
mozilla::GenericErrorResult<OOM&> RaiseParserAtomsOOMError(JSContext* cx) {
js::ReportOutOfMemory(cx);
return mozilla::Err(PARSER_ATOMS_OOM);
}
bool ParserAtomEntry::equalsJSAtom(JSAtom* other) const {
// Compare hashes first.
if (hash_ != other->hash()) {
return false;
}
if (length_ != other->length()) {
return false;
}
JS::AutoCheckCannotGC nogc;
if (hasTwoByteChars()) {
// Compare heap-allocated 16-bit chars to atom.
return other->hasLatin1Chars()
? EqualChars(twoByteChars(), other->latin1Chars(nogc), length_)
: EqualChars(twoByteChars(), other->twoByteChars(nogc), length_);
}
MOZ_ASSERT(hasLatin1Chars());
return other->hasLatin1Chars()
? EqualChars(latin1Chars(), other->latin1Chars(nogc), length_)
: EqualChars(latin1Chars(), other->twoByteChars(nogc), length_);
}
UniqueChars ParserAtomToPrintableString(JSContext* cx, ParserAtomId atom) {
const ParserAtomEntry* entry = atom.entry();
Sprinter sprinter(cx);
if (!sprinter.init()) {
return nullptr;
}
size_t length = entry->length();
if (entry->hasLatin1Chars()) {
if (!QuoteString<QuoteTarget::String>(
&sprinter, mozilla::Range(entry->latin1Chars(), length))) {
return nullptr;
}
} else {
if (!QuoteString<QuoteTarget::String>(
&sprinter, mozilla::Range(entry->twoByteChars(), length))) {
return nullptr;
}
}
return sprinter.release();
}
bool ParserAtomEntry::isIndex(uint32_t* indexp) const {
if (hasLatin1Chars()) {
return js::CheckStringIsIndex(latin1Chars(), length(), indexp);
}
return js::CheckStringIsIndex(twoByteChars(), length(), indexp);
}
JS::Result<JSAtom*, OOM&> ParserAtomEntry::toJSAtom(JSContext* cx) const {
if (jsatom_) {
return jsatom_;
}
if (hasLatin1Chars()) {
jsatom_ = AtomizeChars(cx, latin1Chars(), length());
} else {
jsatom_ = AtomizeChars(cx, twoByteChars(), length());
}
if (!jsatom_) {
return RaiseParserAtomsOOMError(cx);
}
return jsatom_;
}
bool ParserAtomEntry::toNumber(JSContext* cx, double* result) const {
return hasLatin1Chars() ? CharsToNumber(cx, latin1Chars(), length(), result)
: CharsToNumber(cx, twoByteChars(), length(), result);
}
ParserAtomsTable::ParserAtomsTable(JSContext* cx)
: entrySet_(cx), wellKnownTable_(*cx->runtime()->commonParserNames) {}
JS::Result<ParserAtomId, OOM&> ParserAtomsTable::addEntry(
JSContext* cx, EntrySet::AddPtr addPtr, ParserAtomEntry&& entry) {
UniquePtr<ParserAtomEntry> uniqueEntry(
cx->new_<ParserAtomEntry>(std::move(entry)));
if (!uniqueEntry) {
return RaiseParserAtomsOOMError(cx);
}
ParserAtomEntry* entryPtr = uniqueEntry.get();
if (!entrySet_.add(addPtr, std::move(uniqueEntry))) {
return RaiseParserAtomsOOMError(cx);
}
ParserAtomId id(entryPtr);
return id;
}
static const uint16_t MAX_LATIN1_CHAR = 0xff;
template <typename CharT, typename InCharT>
static void DrainChar16Seq(CharT* buf, InflatedChar16Sequence<InCharT> seq) {
static_assert(
std::is_same_v<CharT, char16_t> || std::is_same_v<CharT, Latin1Char>,
"Invalid target buffer type.");
CharT* cur = buf;
while (seq.hasMore()) {
char16_t ch = seq.next();
if constexpr (std::is_same_v<CharT, Latin1Char>) {
MOZ_ASSERT(ch <= MAX_LATIN1_CHAR);
}
*cur = ch;
cur++;
}
}
template <typename AtomCharT, typename SeqCharT>
JS::Result<ParserAtomId, OOM&> ParserAtomsTable::internChar16Seq(
JSContext* cx, EntrySet::AddPtr add, InflatedChar16Sequence<SeqCharT> seq,
uint32_t length, HashNumber hash) {
using UniqueCharsT = mozilla::UniquePtr<AtomCharT[], JS::FreePolicy>;
UniqueCharsT copy(cx->pod_malloc<AtomCharT>(length));
if (!copy) {
return RaiseParserAtomsOOMError(cx);
}
DrainChar16Seq<AtomCharT, SeqCharT>(copy.get(), seq);
ParserAtomEntry ent = ParserAtomEntry::make(std::move(copy), length, hash);
return addEntry(cx, add, std::move(ent));
}
template <typename CharT>
JS::Result<ParserAtomId, OOM&> ParserAtomsTable::lookupOrInternChar16Seq(
JSContext* cx, InflatedChar16Sequence<CharT> seq) {
// Check against well-known.
ParserAtomId wk = wellKnownTable_.lookupChar16Seq(seq);
if (wk) {
return wk;
}
// Check for existing atom.
SpecificParserAtomLookup<CharT> lookup(seq);
EntrySet::AddPtr add = entrySet_.lookupForAdd(lookup);
if (add) {
return ParserAtomId(add->get());
}
// Compute the total length and the storage requirements.
bool wide = false;
uint32_t length = 0;
InflatedChar16Sequence<CharT> seqCopy = seq;
while (seqCopy.hasMore()) {
char16_t ch = seqCopy.next();
wide = wide || (ch > MAX_LATIN1_CHAR);
length += 1;
}
HashNumber hash = lookup.hash();
return wide ? internChar16Seq<char16_t>(cx, add, seq, length, hash)
: internChar16Seq<Latin1Char>(cx, add, seq, length, hash);
}
JS::Result<ParserAtomId, OOM&> ParserAtomsTable::internChar16(
JSContext* cx, const char16_t* char16Ptr, uint32_t length) {
InflatedChar16Sequence<char16_t> seq(char16Ptr, length);
return lookupOrInternChar16Seq(cx, seq);
}
JS::Result<ParserAtomId, OOM&> ParserAtomsTable::internAscii(
JSContext* cx, const char* asciiPtr, uint32_t length) {
const Latin1Char* latin1Ptr = reinterpret_cast<const Latin1Char*>(asciiPtr);
return internLatin1(cx, latin1Ptr, length);
}
JS::Result<ParserAtomId, OOM&> ParserAtomsTable::internLatin1(
JSContext* cx, const Latin1Char* latin1Ptr, uint32_t length) {
// ASCII strings are strict subsets of Latin1 strings, an so can be used
// in the same (const) ways.
InflatedChar16Sequence<Latin1Char> seq(latin1Ptr, length);
// Check against well-known.
ParserAtomId wk = wellKnownTable_.lookupChar16Seq(seq);
if (wk) {
return wk;
}
// Look up.
SpecificParserAtomLookup<Latin1Char> lookup(seq);
EntrySet::AddPtr add = entrySet_.lookupForAdd(lookup);
if (add) {
return ParserAtomId(add->get());
}
// Existing entry not found, heap-allocate a copy and add it to the table.
UniqueLatin1Chars copy = js::DuplicateString(cx, latin1Ptr, length);
if (!copy) {
return RaiseParserAtomsOOMError(cx);
}
ParserAtomEntry ent =
ParserAtomEntry::make(std::move(copy), length, lookup.hash());
return addEntry(cx, add, std::move(ent));
}
JS::Result<ParserAtomId, OOM&> ParserAtomsTable::internUtf8(
JSContext* cx, const mozilla::Utf8Unit* utf8Ptr, uint32_t length) {
// If source text is ASCII, then the length of the target char buffer
// is the same as the length of the UTF8 input. Convert it to a Latin1
// encoded string on the heap.
UTF8Chars utf8(utf8Ptr, length);
if (FindSmallestEncoding(utf8) == JS::SmallestEncoding::ASCII) {
// As ascii strings are a subset of Latin1 strings, and each encoding
// unit is the same size, we can reliably cast this `Utf8Unit*`
// to a `Latin1Char*`.
const Latin1Char* latin1Ptr = reinterpret_cast<const Latin1Char*>(utf8Ptr);
return internLatin1(cx, latin1Ptr, length);
}
InflatedChar16Sequence<mozilla::Utf8Unit> seq(utf8Ptr, length);
// Otherwise, slowpath lookup/interning path that identifies the
// proper target encoding.
return lookupOrInternChar16Seq(cx, seq);
}
JS::Result<ParserAtomId, OOM&> ParserAtomsTable::internJSAtom(JSContext* cx,
JSAtom* atom) {
JS::AutoCheckCannotGC nogc;
auto result =
atom->hasLatin1Chars()
? internLatin1(cx, atom->latin1Chars(nogc), atom->length())
: internChar16(cx, atom->twoByteChars(nogc), atom->length());
if (result.isErr()) {
return result;
}
ParserAtomId id = result.unwrap();
id.entry()->setAtom(atom);
return id;
}
static void FillChar16Buffer(char16_t* buf, const ParserAtomEntry* ent) {
if (ent->hasLatin1Chars()) {
std::copy(ent->latin1Chars(), ent->latin1Chars() + ent->length(), buf);
} else {
std::copy(ent->twoByteChars(), ent->twoByteChars() + ent->length(), buf);
}
}
JS::Result<ParserAtomId, OOM&> ParserAtomsTable::concatAtoms(
JSContext* cx, ParserAtomId prefix, ParserAtomId suffix) {
const ParserAtomEntry* prefixEntry = prefix.entry();
const ParserAtomEntry* suffixEntry = suffix.entry();
bool latin1 = prefixEntry->hasLatin1Chars() && suffixEntry->hasLatin1Chars();
size_t prefixLength = prefixEntry->length();
size_t suffixLength = suffixEntry->length();
size_t concatLength = prefixLength + suffixLength;
if (latin1) {
// Concatenate a latin1 string and add it to the table.
UniqueLatin1Chars copy(cx->pod_malloc<Latin1Char>(concatLength));
if (!copy) {
return RaiseParserAtomsOOMError(cx);
}
mozilla::PodCopy(copy.get(), prefixEntry->latin1Chars(), prefixLength);
mozilla::PodCopy(copy.get() + prefixLength, suffixEntry->latin1Chars(),
suffixLength);
InflatedChar16Sequence<Latin1Char> seq(copy.get(), concatLength);
// Check against well-known.
ParserAtomId wk = wellKnownTable_.lookupChar16Seq(seq);
if (wk) {
return wk;
}
SpecificParserAtomLookup<Latin1Char> lookup(seq);
EntrySet::AddPtr add = entrySet_.lookupForAdd(lookup);
if (add) {
return ParserAtomId(add->get());
}
ParserAtomEntry ent =
ParserAtomEntry::make(std::move(copy), concatLength, lookup.hash());
return addEntry(cx, add, std::move(ent));
}
// Concatenate a char16 string and add it to the table.
UniqueTwoByteChars copy(cx->pod_malloc<char16_t>(concatLength));
if (!copy) {
return RaiseParserAtomsOOMError(cx);
}
FillChar16Buffer(copy.get(), prefixEntry);
FillChar16Buffer(copy.get() + prefixLength, suffixEntry);
InflatedChar16Sequence<char16_t> seq(copy.get(), concatLength);
// Check against well-known.
ParserAtomId wk = wellKnownTable_.lookupChar16Seq(seq);
if (wk) {
return wk;
}
SpecificParserAtomLookup<char16_t> lookup(seq);
EntrySet::AddPtr add = entrySet_.lookupForAdd(lookup);
if (add) {
return ParserAtomId(add->get());
}
ParserAtomEntry ent =
ParserAtomEntry::make(std::move(copy), concatLength, lookup.hash());
return addEntry(cx, add, std::move(ent));
}
template <typename CharT>
ParserAtomId WellKnownParserAtoms::lookupChar16Seq(
InflatedChar16Sequence<CharT> seq) const {
SpecificParserAtomLookup<CharT> lookup(seq);
EntrySet::Ptr get = entrySet_.readonlyThreadsafeLookup(lookup);
if (get) {
return ParserAtomId(get->get());
}
return ParserAtomId::Invalid();
}
bool WellKnownParserAtoms::initSingle(JSContext* cx, ParserNameId* name,
const char* str) {
MOZ_ASSERT(name != nullptr);
unsigned int len = strlen(str);
MOZ_ASSERT(FindSmallestEncoding(UTF8Chars(str, len)) ==
JS::SmallestEncoding::ASCII);
UniqueLatin1Chars copy(cx->pod_malloc<Latin1Char>(len));
if (!copy) {
return false;
}
mozilla::PodCopy(copy.get(), reinterpret_cast<const Latin1Char*>(str), len);
InflatedChar16Sequence<Latin1Char> seq(copy.get(), len);
SpecificParserAtomLookup<Latin1Char> lookup(seq);
ParserAtomEntry ent =
ParserAtomEntry::make(std::move(copy), len, lookup.hash());
UniquePtr<ParserAtomEntry> uniqueEntry(
cx->new_<ParserAtomEntry>(std::move(ent)));
if (!uniqueEntry) {
return false;
}
ParserNameId nm(uniqueEntry.get());
if (!entrySet_.putNew(lookup, std::move(uniqueEntry))) {
return false;
}
*name = nm;
return true;
}
bool WellKnownParserAtoms::init(JSContext* cx) {
#define COMMON_NAME_INIT(idpart, id, text) \
if (!initSingle(cx, &(id), text)) { \
return false; \
}
FOR_EACH_COMMON_PROPERTYNAME(COMMON_NAME_INIT)
#undef COMMON_NAME_INIT
return true;
}
} /* namespace frontend */
} /* namespace js */
bool JSRuntime::initializeParserAtoms(JSContext* cx) {
#ifdef JS_PARSER_ATOMS
MOZ_ASSERT(!commonParserNames);
if (parentRuntime) {
commonParserNames = parentRuntime->commonParserNames;
return true;
}
UniquePtr<js::frontend::WellKnownParserAtoms> names(
js_new<js::frontend::WellKnownParserAtoms>(cx));
if (!names || !names->init(cx)) {
return false;
}
commonParserNames = names.release();
#else
commonParserNames = nullptr;
#endif // JS_PARSER_ATOMS
return true;
}
void JSRuntime::finishParserAtoms() {
#ifdef JS_PARSER_ATOMS
if (!parentRuntime) {
js_delete(commonParserNames.ref());
}
#else
MOZ_ASSERT(!commonParserNames);
#endif // JS_PARSER_ATOMS
}