Home | History | Annotate | Download | only in harmony
      1 // Copyright 2008-2015 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 // This is adapted from mjsunit/for-in-special-cases.js.
     29 
     30 // Flags: --harmony-reflect
     31 
     32 
     33 function Accumulate(x) {
     34   var accumulator = "";
     35   for (var i of Reflect.enumerate(Object(x))) {
     36     accumulator += i;
     37   }
     38   return accumulator;
     39 }
     40 
     41 for (var i = 0; i < 3; ++i) {
     42   var elements = Accumulate("abcd");
     43   // We do not assume that enumerate enumerates elements in order.
     44   assertTrue(-1 != elements.indexOf("0"));
     45   assertTrue(-1 != elements.indexOf("1"));
     46   assertTrue(-1 != elements.indexOf("2"));
     47   assertTrue(-1 != elements.indexOf("3"));
     48   assertEquals(4, elements.length);
     49 }
     50 
     51 function for_in_string_prototype() {
     52 
     53   var x = new String("abc");
     54   x.foo = 19;
     55   function B() {
     56     this.bar = 5;
     57     this[7] = 4;
     58   }
     59   B.prototype = x;
     60 
     61   var y = new B();
     62   y.gub = 13;
     63 
     64   var elements = Accumulate(y);
     65   var elements1 = Accumulate(y);
     66   // If enumerate returns elements in a different order on multiple calls, this
     67   // assert will fail.  If that happens, consider if that behavior is OK.
     68   assertEquals(elements, elements1, "Enumeration not the same both times.");
     69   // We do not assume that enumerate enumerates elements in order.
     70   assertTrue(-1 != elements.indexOf("0"));
     71   assertTrue(-1 != elements.indexOf("1"));
     72   assertTrue(-1 != elements.indexOf("2"));
     73   assertTrue(-1 != elements.indexOf("7"));
     74   assertTrue(-1 != elements.indexOf("foo"));
     75   assertTrue(-1 != elements.indexOf("bar"));
     76   assertTrue(-1 != elements.indexOf("gub"));
     77   assertEquals(13, elements.length);
     78 
     79   elements = Accumulate(x);
     80   assertTrue(-1 != elements.indexOf("0"));
     81   assertTrue(-1 != elements.indexOf("1"));
     82   assertTrue(-1 != elements.indexOf("2"));
     83   assertTrue(-1 != elements.indexOf("foo"));
     84   assertEquals(6, elements.length);
     85 }
     86 
     87 for_in_string_prototype();
     88 for_in_string_prototype();
     89