thin-strings.js 2.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --allow-natives-syntax

function get_thin_string(a, b) {
  var str = a + b;  // Make a ConsString.
  var o = {};
  o[str];  // Turn it into a ThinString.
  return str;
}

var str = get_thin_string("foo", "bar");

var re = /.o+ba./;
assertEquals(["foobar"], re.exec(str));
assertEquals(["foobar"], re.exec(str));
assertEquals(["foobar"], re.exec(str));

function CheckCS() {
  assertEquals("o", str.substring(1, 2));
  assertEquals("f".charCodeAt(0), str.charCodeAt(0));
  assertEquals("f", str.split(/oo/)[0]);
25 26
};
%PrepareFunctionForOptimization(CheckCS);
27 28 29 30 31
CheckCS();
%OptimizeFunctionOnNextCall(CheckCS);
CheckCS();

function CheckTF() {
32 33 34
  try {
  } catch (e) {
  }  // Turbofan.
35 36 37
  assertEquals("o", str.substring(1, 2));
  assertEquals("f".charCodeAt(0), str.charCodeAt(0));
  assertEquals("f", str.split(/oo/)[0]);
38 39
};
%PrepareFunctionForOptimization(CheckTF);
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
CheckTF();
%OptimizeFunctionOnNextCall(CheckTF);
CheckTF();

// Flat cons strings can point to a thin string.

function get_cons_thin_string(a, b) {
  // Make a ConsString.
  var s = a + b;
  // Flatten it.
  s.endsWith("a");
  // Internalize the first part.
  var o = {};
  o[s];
  return s;
}

var s = get_cons_thin_string("__________", "@@@@@@@@@@a");
s.match(/.*a/);
assertEquals("________", s.substring(0, 8));

function cc1(s) {
  assertEquals(95, s.charCodeAt(0));
  assertEquals(95, s.codePointAt(0));
64 65
};
%PrepareFunctionForOptimization(cc1);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
cc1(s);
cc1(s);
%OptimizeFunctionOnNextCall(cc1);
cc1(s);

// Sliced strings can have a thin string as their parent.

function get_sliced_thin_string(a, b) {
  // Make a long thin string.
  var s = a + b;
  // Slice a substring out of it.
  var slice = s.substring(0, 20);
  // Make the original string thin.
  var o = {};
  o[s];
  return slice;
}

84 85
var t = get_sliced_thin_string(
    'abcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvwxyz');
86 87 88 89 90
assertEquals("abcdefghijklmnopqrst", decodeURI(t));

function cc2(s) {
  assertEquals(97, s.charCodeAt(0));
  assertEquals(97, s.codePointAt(0));
91 92
};
%PrepareFunctionForOptimization(cc2);
93 94 95 96
cc2(t);
cc2(t);
%OptimizeFunctionOnNextCall(cc2);
cc2(t);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

function string_table_lookup_sliced_thin_string(a, b) {
  // Make a ConsString.
  var s = a + b;
  // Slice a substring out of it.
  var slice = s.substring(0, 20);
  // Make the original string thin.
  var o = {};
  o[s];
  // Try to internalize the SlicedString.
  o[slice];
}

string_table_lookup_sliced_thin_string(
    'abcdefghijklmnopqrstuvwxyz', '0123456789');