Debian Codegraph @

js/src/jsapi-tests/testParserAtom.cpp

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include "mozilla/Range.h" // mozilla::Range
6 #include "mozilla/Utf8.h" // mozilla::Utf8Unit
7
8 #include <string> // std::char_traits
9 #include <utility> // std::initializer_list
10 #include <vector> // std::vector
11
12 #include "frontend/FrontendContext.h" // AutoReportFrontendContext
13 #include "frontend/ParserAtom.h" // js::frontend::ParserAtomsTable, js::frontend::WellKnownParserAtoms
14 #include "js/TypeDecls.h" // JS::Latin1Char
15 #include "jsapi-tests/tests.h"
16
17 // Test empty strings behave consistently.
18 BEGIN_TEST(testParserAtom_empty) {
19 using js::frontend::ParserAtom;
20 using js::frontend::ParserAtomsTable;
21 using js::frontend::ParserAtomVector;
22 using js::frontend::TaggedParserAtomIndex;
23
24 js::AutoReportFrontendContext fc(cx);
25 js::LifoAlloc alloc(512, js::MallocArena);
26 ParserAtomsTable atomTable(alloc);
27
28 const char ascii[] = {};
29 const JS::Latin1Char latin1[] = {};
30 const mozilla::Utf8Unit utf8[] = {};
31 const char16_t char16[] = {};
32
33 // Check that the well-known empty atom matches for different entry points.
34 auto refIndex = TaggedParserAtomIndex::WellKnown::empty();
35 CHECK(atomTable.internAscii(&fc, ascii, 0) == refIndex);
36 CHECK(atomTable.internLatin1(&fc, latin1, 0) == refIndex);
37 CHECK(atomTable.internUtf8(&fc, utf8, 0) == refIndex);
38 CHECK(atomTable.internChar16(&fc, char16, 0) == refIndex);
39
40 return true;
41 }
42 END_TEST(testParserAtom_empty)
43
44 // Test length-1 fast-path is consistent across entry points for ASCII.
45 BEGIN_TEST(testParserAtom_tiny1_ASCII) {
46 using js::frontend::ParserAtom;
47 using js::frontend::ParserAtomsTable;
48 using js::frontend::ParserAtomVector;
49 using js::frontend::WellKnownParserAtoms;
50
51 js::AutoReportFrontendContext fc(cx);
52 js::LifoAlloc alloc(512, js::MallocArena);
53 ParserAtomsTable atomTable(alloc);
54
55 char16_t a = 'a';
56 const char ascii[] = {'a'};
57 JS::Latin1Char latin1[] = {'a'};
58 const mozilla::Utf8Unit utf8[] = {mozilla::Utf8Unit('a')};
59 char16_t char16[] = {'a'};
60
61 auto refIndex = WellKnownParserAtoms::getSingleton().lookupTinyIndex(&a, 1);
62 CHECK(refIndex);
63 CHECK(atomTable.internAscii(&fc, ascii, 1) == refIndex);
64 CHECK(atomTable.internLatin1(&fc, latin1, 1) == refIndex);
65 CHECK(atomTable.internUtf8(&fc, utf8, 1) == refIndex);
66 CHECK(atomTable.internChar16(&fc, char16, 1) == refIndex);
67
68 return true;
69 }
70 END_TEST(testParserAtom_tiny1_ASCII)
71
72 // Test length-1 fast-path is consistent across entry points for non-ASCII.
73 BEGIN_TEST(testParserAtom_tiny1_nonASCII) {
74 using js::frontend::ParserAtom;
75 using js::frontend::ParserAtomsTable;
76 using js::frontend::ParserAtomVector;
77 using js::frontend::WellKnownParserAtoms;
78
79 js::AutoReportFrontendContext fc(cx);
80 js::LifoAlloc alloc(512, js::MallocArena);
81 ParserAtomsTable atomTable(alloc);
82
83 {
84 char16_t euro = 0x0080;
85 JS::Latin1Char latin1[] = {0x80};
86 const mozilla::Utf8Unit utf8[] = {
87 mozilla::Utf8Unit(static_cast<unsigned char>(0xC2)),
88 mozilla::Utf8Unit(static_cast<unsigned char>(0x80))};
89 char16_t char16[] = {0x0080};
90
91 auto refIndex =
92 WellKnownParserAtoms::getSingleton().lookupTinyIndex(&euro, 1);
93 CHECK(refIndex);
94 CHECK(atomTable.internLatin1(&fc, latin1, 1) == refIndex);
95 CHECK(atomTable.internUtf8(&fc, utf8, 2) == refIndex);
96 CHECK(atomTable.internChar16(&fc, char16, 1) == refIndex);
97 }
98
99 {
100 char16_t frac12 = 0x00BD;
101 JS::Latin1Char latin1[] = {0xBD};
102 const mozilla::Utf8Unit utf8[] = {
103 mozilla::Utf8Unit(static_cast<unsigned char>(0xC2)),
104 mozilla::Utf8Unit(static_cast<unsigned char>(0xBD))};
105 char16_t char16[] = {0x00BD};
106
107 auto refIndex =
108 WellKnownParserAtoms::getSingleton().lookupTinyIndex(&frac12, 1);
109 CHECK(refIndex);
110 CHECK(atomTable.internLatin1(&fc, latin1, 1) == refIndex);
111 CHECK(atomTable.internUtf8(&fc, utf8, 2) == refIndex);
112 CHECK(atomTable.internChar16(&fc, char16, 1) == refIndex);
113 }
114
115 {
116 char16_t iquest = 0x00BF;
117 JS::Latin1Char latin1[] = {0xBF};
118 const mozilla::Utf8Unit utf8[] = {
119 mozilla::Utf8Unit(static_cast<unsigned char>(0xC2)),
120 mozilla::Utf8Unit(static_cast<unsigned char>(0xBF))};
121 char16_t char16[] = {0x00BF};
122
123 auto refIndex =
124 WellKnownParserAtoms::getSingleton().lookupTinyIndex(&iquest, 1);
125 CHECK(refIndex);
126 CHECK(atomTable.internLatin1(&fc, latin1, 1) == refIndex);
127 CHECK(atomTable.internUtf8(&fc, utf8, 2) == refIndex);
128 CHECK(atomTable.internChar16(&fc, char16, 1) == refIndex);
129 }
130
131 {
132 char16_t agrave = 0x00C0;
133 JS::Latin1Char latin1[] = {0xC0};
134 const mozilla::Utf8Unit utf8[] = {
135 mozilla::Utf8Unit(static_cast<unsigned char>(0xC3)),
136 mozilla::Utf8Unit(static_cast<unsigned char>(0x80))};
137 char16_t char16[] = {0x00C0};
138
139 auto refIndex =
140 WellKnownParserAtoms::getSingleton().lookupTinyIndex(&agrave, 1);
141 CHECK(refIndex);
142 CHECK(atomTable.internLatin1(&fc, latin1, 1) == refIndex);
143 CHECK(atomTable.internUtf8(&fc, utf8, 2) == refIndex);
144 CHECK(atomTable.internChar16(&fc, char16, 1) == refIndex);
145 }
146
147 {
148 char16_t ae = 0x00E6;
149 JS::Latin1Char latin1[] = {0xE6};
150 const mozilla::Utf8Unit utf8[] = {
151 mozilla::Utf8Unit(static_cast<unsigned char>(0xC3)),
152 mozilla::Utf8Unit(static_cast<unsigned char>(0xA6))};
153 char16_t char16[] = {0x00E6};
154
155 auto refIndex =
156 WellKnownParserAtoms::getSingleton().lookupTinyIndex(&ae, 1);
157 CHECK(refIndex);
158 CHECK(atomTable.internLatin1(&fc, latin1, 1) == refIndex);
159 CHECK(atomTable.internUtf8(&fc, utf8, 2) == refIndex);
160 CHECK(atomTable.internChar16(&fc, char16, 1) == refIndex);
161 }
162
163 {
164 char16_t yuml = 0x00FF;
165 JS::Latin1Char latin1[] = {0xFF};
166 const mozilla::Utf8Unit utf8[] = {
167 mozilla::Utf8Unit(static_cast<unsigned char>(0xC3)),
168 mozilla::Utf8Unit(static_cast<unsigned char>(0xBF))};
169 char16_t char16[] = {0x00FF};
170
171 auto refIndex =
172 WellKnownParserAtoms::getSingleton().lookupTinyIndex(&yuml, 1);
173 CHECK(refIndex);
174 CHECK(atomTable.internLatin1(&fc, latin1, 1) == refIndex);
175 CHECK(atomTable.internUtf8(&fc, utf8, 2) == refIndex);
176 CHECK(atomTable.internChar16(&fc, char16, 1) == refIndex);
177 }
178
179 return true;
180 }
181 END_TEST(testParserAtom_tiny1_nonASCII)
182
183 // Test for tiny1 UTF-8 with valid/invalid code units.
184 //
185 // NOTE: Passing invalid UTF-8 to internUtf8 hits assertion failure, so
186 // test in the opposite way.
187 // lookupTinyIndexUTF8 is used inside internUtf8.
188 BEGIN_TEST(testParserAtom_tiny1_invalidUTF8) {
189 using js::frontend::ParserAtom;
190 using js::frontend::ParserAtomsTable;
191 using js::frontend::WellKnownParserAtoms;
192
193 js::AutoReportFrontendContext fc(cx);
194 js::LifoAlloc alloc(512, js::MallocArena);
195 ParserAtomsTable atomTable(alloc);
196
197 {
198 const mozilla::Utf8Unit utf8[] = {
199 mozilla::Utf8Unit(static_cast<unsigned char>(0xC1)),
200 mozilla::Utf8Unit(static_cast<unsigned char>(0x80))};
201
202 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndexUTF8(utf8, 2));
203 }
204
205 {
206 const mozilla::Utf8Unit utf8[] = {
207 mozilla::Utf8Unit(static_cast<unsigned char>(0xC2)),
208 mozilla::Utf8Unit(static_cast<unsigned char>(0x7F))};
209
210 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndexUTF8(utf8, 2));
211 }
212
213 {
214 JS::Latin1Char latin1[] = {0x80};
215 const mozilla::Utf8Unit utf8[] = {
216 mozilla::Utf8Unit(static_cast<unsigned char>(0xC2)),
217 mozilla::Utf8Unit(static_cast<unsigned char>(0x80))};
218
219 auto refIndex =
220 WellKnownParserAtoms::getSingleton().lookupTinyIndexUTF8(utf8, 2);
221 CHECK(refIndex);
222 CHECK(atomTable.internLatin1(&fc, latin1, 1) == refIndex);
223 }
224
225 {
226 JS::Latin1Char latin1[] = {0xBF};
227 const mozilla::Utf8Unit utf8[] = {
228 mozilla::Utf8Unit(static_cast<unsigned char>(0xC2)),
229 mozilla::Utf8Unit(static_cast<unsigned char>(0xBF))};
230
231 auto refIndex =
232 WellKnownParserAtoms::getSingleton().lookupTinyIndexUTF8(utf8, 2);
233 CHECK(refIndex);
234 CHECK(atomTable.internLatin1(&fc, latin1, 1) == refIndex);
235 }
236
237 {
238 const mozilla::Utf8Unit utf8[] = {
239 mozilla::Utf8Unit(static_cast<unsigned char>(0xC2)),
240 mozilla::Utf8Unit(static_cast<unsigned char>(0xC0))};
241
242 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndexUTF8(utf8, 2));
243 }
244
245 {
246 const mozilla::Utf8Unit utf8[] = {
247 mozilla::Utf8Unit(static_cast<unsigned char>(0xC3)),
248 mozilla::Utf8Unit(static_cast<unsigned char>(0x7F))};
249
250 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndexUTF8(utf8, 2));
251 }
252
253 {
254 JS::Latin1Char latin1[] = {0xC0};
255 const mozilla::Utf8Unit utf8[] = {
256 mozilla::Utf8Unit(static_cast<unsigned char>(0xC3)),
257 mozilla::Utf8Unit(static_cast<unsigned char>(0x80))};
258
259 auto refIndex =
260 WellKnownParserAtoms::getSingleton().lookupTinyIndexUTF8(utf8, 2);
261 CHECK(refIndex);
262 CHECK(atomTable.internLatin1(&fc, latin1, 1) == refIndex);
263 }
264
265 {
266 JS::Latin1Char latin1[] = {0xFF};
267 const mozilla::Utf8Unit utf8[] = {
268 mozilla::Utf8Unit(static_cast<unsigned char>(0xC3)),
269 mozilla::Utf8Unit(static_cast<unsigned char>(0xBF))};
270
271 auto refIndex =
272 WellKnownParserAtoms::getSingleton().lookupTinyIndexUTF8(utf8, 2);
273 CHECK(refIndex);
274 CHECK(atomTable.internLatin1(&fc, latin1, 1) == refIndex);
275 }
276
277 {
278 const mozilla::Utf8Unit utf8[] = {
279 mozilla::Utf8Unit(static_cast<unsigned char>(0xC3)),
280 mozilla::Utf8Unit(static_cast<unsigned char>(0xC0))};
281
282 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndexUTF8(utf8, 2));
283 }
284
285 {
286 const mozilla::Utf8Unit utf8[] = {
287 mozilla::Utf8Unit(static_cast<unsigned char>(0xC4)),
288 mozilla::Utf8Unit(static_cast<unsigned char>(0x7F))};
289
290 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndexUTF8(utf8, 2));
291 }
292
293 {
294 const mozilla::Utf8Unit utf8[] = {
295 mozilla::Utf8Unit(static_cast<unsigned char>(0xC4)),
296 mozilla::Utf8Unit(static_cast<unsigned char>(0x80))};
297
298 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndexUTF8(utf8, 2));
299 }
300
301 {
302 const mozilla::Utf8Unit utf8[] = {
303 mozilla::Utf8Unit(static_cast<unsigned char>(0xC4)),
304 mozilla::Utf8Unit(static_cast<unsigned char>(0xBF))};
305
306 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndexUTF8(utf8, 2));
307 }
308
309 {
310 const mozilla::Utf8Unit utf8[] = {
311 mozilla::Utf8Unit(static_cast<unsigned char>(0xC4)),
312 mozilla::Utf8Unit(static_cast<unsigned char>(0xC0))};
313
314 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndexUTF8(utf8, 2));
315 }
316
317 return true;
318 }
319 END_TEST(testParserAtom_tiny1_invalidUTF8)
320
321 // Test length-2 fast-path is consistent across entry points.
322 BEGIN_TEST(testParserAtom_tiny2) {
323 using js::frontend::ParserAtom;
324 using js::frontend::ParserAtomsTable;
325 using js::frontend::ParserAtomVector;
326 using js::frontend::WellKnownParserAtoms;
327
328 js::AutoReportFrontendContext fc(cx);
329 js::LifoAlloc alloc(512, js::MallocArena);
330 ParserAtomsTable atomTable(alloc);
331
332 const char ascii[] = {'a', '0'};
333 JS::Latin1Char latin1[] = {'a', '0'};
334 const mozilla::Utf8Unit utf8[] = {mozilla::Utf8Unit('a'),
335 mozilla::Utf8Unit('0')};
336 char16_t char16[] = {'a', '0'};
337
338 auto refIndex =
339 WellKnownParserAtoms::getSingleton().lookupTinyIndex(ascii, 2);
340 CHECK(refIndex);
341 CHECK(atomTable.internAscii(&fc, ascii, 2) == refIndex);
342 CHECK(atomTable.internLatin1(&fc, latin1, 2) == refIndex);
343 CHECK(atomTable.internUtf8(&fc, utf8, 2) == refIndex);
344 CHECK(atomTable.internChar16(&fc, char16, 2) == refIndex);
345
346 // Note: If Latin1-Extended characters become supported, then UTF-8 behaviour
347 // should be tested.
348 char16_t ae0[] = {0x00E6, '0'};
349 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndex(ae0, 2));
350
351 return true;
352 }
353 END_TEST(testParserAtom_tiny2)
354
355 // Test length-3 fast-path is consistent across entry points.
356 BEGIN_TEST(testParserAtom_int) {
357 using js::frontend::ParserAtom;
358 using js::frontend::ParserAtomsTable;
359 using js::frontend::ParserAtomVector;
360 using js::frontend::WellKnownParserAtoms;
361
362 js::AutoReportFrontendContext fc(cx);
363 js::LifoAlloc alloc(512, js::MallocArena);
364 ParserAtomsTable atomTable(alloc);
365
366 {
367 const char ascii[] = {'1', '0', '0'};
368 JS::Latin1Char latin1[] = {'1', '0', '0'};
369 const mozilla::Utf8Unit utf8[] = {
370 mozilla::Utf8Unit('1'), mozilla::Utf8Unit('0'), mozilla::Utf8Unit('0')};
371 char16_t char16[] = {'1', '0', '0'};
372
373 auto refIndex =
374 WellKnownParserAtoms::getSingleton().lookupTinyIndex(ascii, 3);
375 CHECK(refIndex);
376 CHECK(atomTable.internAscii(&fc, ascii, 3) == refIndex);
377 CHECK(atomTable.internLatin1(&fc, latin1, 3) == refIndex);
378 CHECK(atomTable.internUtf8(&fc, utf8, 3) == refIndex);
379 CHECK(atomTable.internChar16(&fc, char16, 3) == refIndex);
380 }
381
382 {
383 const char ascii[] = {'2', '5', '5'};
384 JS::Latin1Char latin1[] = {'2', '5', '5'};
385 const mozilla::Utf8Unit utf8[] = {
386 mozilla::Utf8Unit('2'), mozilla::Utf8Unit('5'), mozilla::Utf8Unit('5')};
387 char16_t char16[] = {'2', '5', '5'};
388
389 auto refIndex =
390 WellKnownParserAtoms::getSingleton().lookupTinyIndex(ascii, 3);
391 CHECK(refIndex);
392 CHECK(atomTable.internAscii(&fc, ascii, 3) == refIndex);
393 CHECK(atomTable.internLatin1(&fc, latin1, 3) == refIndex);
394 CHECK(atomTable.internUtf8(&fc, utf8, 3) == refIndex);
395 CHECK(atomTable.internChar16(&fc, char16, 3) == refIndex);
396 }
397
398 {
399 const char ascii[] = {'0', '9', '9'};
400
401 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndex(ascii, 3));
402 }
403
404 {
405 const char ascii[] = {'0', 'F', 'F'};
406
407 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndex(ascii, 3));
408 }
409
410 {
411 const char ascii[] = {'1', '0', 'A'};
412
413 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndex(ascii, 3));
414 }
415
416 {
417 const char ascii[] = {'1', '0', 'a'};
418
419 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndex(ascii, 3));
420 }
421
422 {
423 const char ascii[] = {'2', '5', '6'};
424
425 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndex(ascii, 3));
426 }
427
428 {
429 const char ascii[] = {'3', '0', '0'};
430
431 CHECK(!WellKnownParserAtoms::getSingleton().lookupTinyIndex(ascii, 3));
432 }
433
434 return true;
435 }
436 END_TEST(testParserAtom_int)
437
438 // "€" U+0080
439 // "½" U+00BD
440 // "¿" U+00BF
441 // "À" U+00C0
442 // "æ" U+00E6
443 // "ÿ" U+00FF
444 // "π" U+03C0
445 // "🍕" U+1F355