blob: 0c50cf6971dd3f8ebd89769e335164fb83efaba1 [file] [log] [blame]
Andrew Topef837fa2017-10-04 22:44:25 -07001// Copyright 2016 the V8 project 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// Flags: --allow-natives-syntax
6
7function get_thin_string(a, b) {
8 var str = a + b; // Make a ConsString.
9 var o = {};
10 o[str]; // Turn it into a ThinString.
11 return str;
12}
13
14var str = get_thin_string("foo", "bar");
15
16var re = /.o+ba./;
17assertEquals(["foobar"], re.exec(str));
18assertEquals(["foobar"], re.exec(str));
19assertEquals(["foobar"], re.exec(str));
20
21function CheckCS() {
22 assertEquals("o", str.substring(1, 2));
23 assertEquals("f".charCodeAt(0), str.charCodeAt(0));
24 assertEquals("f", str.split(/oo/)[0]);
Andrew Top63c7ad42019-11-25 16:10:13 -080025};
26%PrepareFunctionForOptimization(CheckCS);
Andrew Topef837fa2017-10-04 22:44:25 -070027CheckCS();
28%OptimizeFunctionOnNextCall(CheckCS);
29CheckCS();
30
31function CheckTF() {
Andrew Top63c7ad42019-11-25 16:10:13 -080032 try {
33 } catch (e) {
34 } // Turbofan.
Andrew Topef837fa2017-10-04 22:44:25 -070035 assertEquals("o", str.substring(1, 2));
36 assertEquals("f".charCodeAt(0), str.charCodeAt(0));
37 assertEquals("f", str.split(/oo/)[0]);
Andrew Top63c7ad42019-11-25 16:10:13 -080038};
39%PrepareFunctionForOptimization(CheckTF);
Andrew Topef837fa2017-10-04 22:44:25 -070040CheckTF();
41%OptimizeFunctionOnNextCall(CheckTF);
42CheckTF();
43
44// Flat cons strings can point to a thin string.
45
46function get_cons_thin_string(a, b) {
47 // Make a ConsString.
48 var s = a + b;
49 // Flatten it.
50 s.endsWith("a");
51 // Internalize the first part.
52 var o = {};
53 o[s];
54 return s;
55}
56
57var s = get_cons_thin_string("__________", "@@@@@@@@@@a");
58s.match(/.*a/);
59assertEquals("________", s.substring(0, 8));
60
61function cc1(s) {
62 assertEquals(95, s.charCodeAt(0));
63 assertEquals(95, s.codePointAt(0));
Andrew Top63c7ad42019-11-25 16:10:13 -080064};
65%PrepareFunctionForOptimization(cc1);
Andrew Topef837fa2017-10-04 22:44:25 -070066cc1(s);
67cc1(s);
68%OptimizeFunctionOnNextCall(cc1);
69cc1(s);
70
71// Sliced strings can have a thin string as their parent.
72
73function get_sliced_thin_string(a, b) {
74 // Make a long thin string.
75 var s = a + b;
76 // Slice a substring out of it.
77 var slice = s.substring(0, 20);
78 // Make the original string thin.
79 var o = {};
80 o[s];
81 return slice;
82}
83
Andrew Top63c7ad42019-11-25 16:10:13 -080084var t = get_sliced_thin_string(
85 'abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz');
Andrew Topef837fa2017-10-04 22:44:25 -070086assertEquals("abcdefghijklmnopqrst", decodeURI(t));
87
88function cc2(s) {
89 assertEquals(97, s.charCodeAt(0));
90 assertEquals(97, s.codePointAt(0));
Andrew Top63c7ad42019-11-25 16:10:13 -080091};
92%PrepareFunctionForOptimization(cc2);
Andrew Topef837fa2017-10-04 22:44:25 -070093cc2(t);
94cc2(t);
95%OptimizeFunctionOnNextCall(cc2);
96cc2(t);