string-wrapper.js 1.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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
// 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.

var limit = 10000;

function testStringWrapper(string) {
  assertEquals('a', string[0]);
  assertEquals('b', string[1]);
  assertEquals('c', string[2]);
}

(function testFastStringWrapperGrow() {
  var string = new String("abc");
  for (var i = 0; i < limit; i += 2) {
    string[i] = {};
  }
  testStringWrapper(string);

  for (var i = limit; i > 0; i -= 2) {
    delete string[i];
  }
  testStringWrapper(string);
})();

(function testSlowStringWrapperGrow() {
  var string = new String("abc");
  // Force Slow String Wrapper Elements Kind
  string[limit] = limit;
  for (var i = 0; i < limit; i += 2) {
    string[i] = {};
  }
  testStringWrapper(string);
  assertEquals(limit, string[limit]);

  for (var i = limit; i > 0; i -= 2) {
    delete string[i];
  }
  testStringWrapper(string);
  assertEquals(undefined, string[limit]);
})();
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62


(function testReconfigureStringWrapperElements() {
  var s = new String('abc');
  // Can't reconfigure string contents.
  assertThrows(() => Object.defineProperty(s, '1', {value: "value"}), TypeError);

  // Configure a property outside the string range
  var value = 'v1';
  Object.defineProperty(s, '3', {
    get: () => {return value},
    configurable:true
  });
  assertEquals('v1', s[3]);
  value = 'v2';
  assertEquals('v2', s[3]);

  Object.defineProperty(s, '3', {value: 'v3', configurable: false});
  assertEquals('v3', s[3]);
  assertThrows(() => Object.defineProperty(s, '3', {value:2}), TypeError);
})();