Home | History | Annotate | Download | only in mjsunit
      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 var limit = 10000;
      6 
      7 function testStringWrapper(string) {
      8   assertEquals('a', string[0]);
      9   assertEquals('b', string[1]);
     10   assertEquals('c', string[2]);
     11 }
     12 
     13 (function testFastStringWrapperGrow() {
     14   var string = new String("abc");
     15   for (var i = 0; i < limit; i += 2) {
     16     string[i] = {};
     17   }
     18   testStringWrapper(string);
     19 
     20   for (var i = limit; i > 0; i -= 2) {
     21     delete string[i];
     22   }
     23   testStringWrapper(string);
     24 })();
     25 
     26 (function testSlowStringWrapperGrow() {
     27   var string = new String("abc");
     28   // Force Slow String Wrapper Elements Kind
     29   string[limit] = limit;
     30   for (var i = 0; i < limit; i += 2) {
     31     string[i] = {};
     32   }
     33   testStringWrapper(string);
     34   assertEquals(limit, string[limit]);
     35 
     36   for (var i = limit; i > 0; i -= 2) {
     37     delete string[i];
     38   }
     39   testStringWrapper(string);
     40   assertEquals(undefined, string[limit]);
     41 })();
     42 
     43 
     44 (function testReconfigureStringWrapperElements() {
     45   var s = new String('abc');
     46   // Can't reconfigure string contents.
     47   assertThrows(() => Object.defineProperty(s, '1', {value: "value"}), TypeError);
     48 
     49   // Configure a property outside the string range
     50   var value = 'v1';
     51   Object.defineProperty(s, '3', {
     52     get: () => {return value},
     53     configurable:true
     54   });
     55   assertEquals('v1', s[3]);
     56   value = 'v2';
     57   assertEquals('v2', s[3]);
     58 
     59   Object.defineProperty(s, '3', {value: 'v3', configurable: false});
     60   assertEquals('v3', s[3]);
     61   assertThrows(() => Object.defineProperty(s, '3', {value:2}), TypeError);
     62 })();
     63