Debian Codegraph @

js/src/jit-test/tests/warp/guard-specific-atom-with-short-atom.js

raw
1 // Test case to cover constant atom guards.
2 //
3 // GuardSpecificAtom for short (≤32 characters) constant atoms is optimised.
4
5 function* characters(...ranges) {
6 for (let [start, end] of ranges) {
7 for (let i = start; i <= end; ++i) {
8 yield i;
9 }
10 }
11 }
12
13 const ascii = [...characters(
14 [0x41, 0x5A], // A..Z
15 [0x61, 0x7A], // a..z
16 [0x30, 0x39], // 0..9
17 )];
18
19 const latin1 = [...characters(
20 [0xC0, 0xFF], // À..ÿ
21 )];
22
23 const twoByte = [...characters(
24 [0x100, 0x17E], // Ā..ž
25 )];
26
27 function toRope(s) {
28 try {
29 return newRope(s[0], s.substring(1));
30 } catch {}
31 // newRope can fail when |s| fits into an inline string. In that case simply
32 // return the input.
33 return s;
34 }
35
36 function atomize(s) {
37 return Object.keys({[s]: 0})[0];
38 }
39
40 for (let i = 1; i <= 32; ++i) {
41 let strings = [ascii, latin1, twoByte].flatMap(codePoints => [
42 // Same string as the input.
43 String.fromCodePoint(...codePoints.slice(0, i)),
44
45 // Same length as the input, but a different string.
46 String.fromCodePoint(...codePoints.slice(1, i + 1)),
47
48 // Shorter string than the input.
49 String.fromCodePoint(...codePoints.slice(0, i - 1)),
50
51 // Longer string than the input.
52 String.fromCodePoint(...codePoints.slice(0, i + 1)),
53 ]).flatMap(x => [
54 x,
55 toRope(x),
56 newString(x, {twoByte: true}),
57 atomize(x),
58 ]);
59
60 // Must be small enough to transition to megamorphic ICs.
61 const stringsPerLoop = 4;
62
63 for (let codePoints of [ascii, latin1, twoByte]) {
64 let str = String.fromCodePoint(...codePoints.slice(0, i));
65
66 for (let i = 0; i < strings.length; i += stringsPerLoop) {
67 let fn = Function("strings", `
68 var obj = {["${str}"]: 0};
69
70 for (let i = 0; i < 250; ++i) {
71 let idx = i % strings.length;
72 let str = strings[idx];
73
74 let actual = str in obj;
75 let expected = str === "${str}";
76 if (actual !== expected) throw new Error();
77 }
78 `);
79 fn(strings.slice(i, stringsPerLoop));
80 }
81 }
82 }