Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 1 | // 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 | |
| 7 | function 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 | |
| 14 | var str = get_thin_string("foo", "bar"); |
| 15 | |
| 16 | var re = /.o+ba./; |
| 17 | assertEquals(["foobar"], re.exec(str)); |
| 18 | assertEquals(["foobar"], re.exec(str)); |
| 19 | assertEquals(["foobar"], re.exec(str)); |
| 20 | |
| 21 | function CheckCS() { |
| 22 | assertEquals("o", str.substring(1, 2)); |
| 23 | assertEquals("f".charCodeAt(0), str.charCodeAt(0)); |
| 24 | assertEquals("f", str.split(/oo/)[0]); |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 25 | }; |
| 26 | %PrepareFunctionForOptimization(CheckCS); |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 27 | CheckCS(); |
| 28 | %OptimizeFunctionOnNextCall(CheckCS); |
| 29 | CheckCS(); |
| 30 | |
| 31 | function CheckTF() { |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 32 | try { |
| 33 | } catch (e) { |
| 34 | } // Turbofan. |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 35 | assertEquals("o", str.substring(1, 2)); |
| 36 | assertEquals("f".charCodeAt(0), str.charCodeAt(0)); |
| 37 | assertEquals("f", str.split(/oo/)[0]); |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 38 | }; |
| 39 | %PrepareFunctionForOptimization(CheckTF); |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 40 | CheckTF(); |
| 41 | %OptimizeFunctionOnNextCall(CheckTF); |
| 42 | CheckTF(); |
| 43 | |
| 44 | // Flat cons strings can point to a thin string. |
| 45 | |
| 46 | function 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 | |
| 57 | var s = get_cons_thin_string("__________", "@@@@@@@@@@a"); |
| 58 | s.match(/.*a/); |
| 59 | assertEquals("________", s.substring(0, 8)); |
| 60 | |
| 61 | function cc1(s) { |
| 62 | assertEquals(95, s.charCodeAt(0)); |
| 63 | assertEquals(95, s.codePointAt(0)); |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 64 | }; |
| 65 | %PrepareFunctionForOptimization(cc1); |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 66 | cc1(s); |
| 67 | cc1(s); |
| 68 | %OptimizeFunctionOnNextCall(cc1); |
| 69 | cc1(s); |
| 70 | |
| 71 | // Sliced strings can have a thin string as their parent. |
| 72 | |
| 73 | function 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 Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 84 | var t = get_sliced_thin_string( |
| 85 | 'abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz'); |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 86 | assertEquals("abcdefghijklmnopqrst", decodeURI(t)); |
| 87 | |
| 88 | function cc2(s) { |
| 89 | assertEquals(97, s.charCodeAt(0)); |
| 90 | assertEquals(97, s.codePointAt(0)); |
Andrew Top | 63c7ad4 | 2019-11-25 16:10:13 -0800 | [diff] [blame] | 91 | }; |
| 92 | %PrepareFunctionForOptimization(cc2); |
Andrew Top | ef837fa | 2017-10-04 22:44:25 -0700 | [diff] [blame] | 93 | cc2(t); |
| 94 | cc2(t); |
| 95 | %OptimizeFunctionOnNextCall(cc2); |
| 96 | cc2(t); |