Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2014 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 --nostress-opt --track-field-types
      6 
      7 (function() {
      8   var o = { text: "Hello World!" };
      9   function A() {
     10     this.a = o;
     11   }
     12   function readA(x) {
     13     return x.a;
     14   }
     15   var a = new A();
     16   assertUnoptimized(readA);
     17   readA(a); readA(a); readA(a);
     18   %OptimizeFunctionOnNextCall(readA);
     19   assertEquals(readA(a), o);
     20   assertOptimized(readA);
     21 
     22   var b = new A();
     23   b.b = o;
     24   assertEquals(readA(b), o);
     25   assertUnoptimized(readA);
     26   %OptimizeFunctionOnNextCall(readA);
     27   assertEquals(readA(a), o);
     28   assertOptimized(readA);
     29   assertEquals(readA(a), o);
     30   assertEquals(readA(b), o);
     31   assertOptimized(readA);
     32 
     33   function readAFromB(x) {
     34     return x.a;
     35   }
     36   assertUnoptimized(readAFromB);
     37   readAFromB(b); readAFromB(b); readAFromB(b);
     38   %OptimizeFunctionOnNextCall(readAFromB);
     39   assertEquals(readAFromB(b), o);
     40   assertOptimized(readAFromB);
     41 
     42   var c = new A();
     43   c.c = o;
     44   assertOptimized(readA);
     45   assertOptimized(readAFromB);
     46   c.a = [1];
     47   assertUnoptimized(readA);
     48   assertUnoptimized(readAFromB);
     49   assertEquals(readA(a), o);
     50   assertEquals(readA(b), o);
     51   assertEquals(readA(c), [1]);
     52   assertEquals(readAFromB(b), o);
     53 
     54   %OptimizeFunctionOnNextCall(readA);
     55   assertEquals(readA(a), o);
     56   %OptimizeFunctionOnNextCall(readAFromB);
     57   assertEquals(readAFromB(b), o);
     58   assertOptimized(readA);
     59   a.a = [1];
     60   assertEquals(readA(a), [1]);
     61   assertEquals(readA(b), o);
     62   assertEquals(readA(c), [1]);
     63   assertOptimized(readA);
     64   b.a = [1];
     65   assertEquals(readA(a), [1]);
     66   assertEquals(readA(b), [1]);
     67   assertEquals(readA(c), [1]);
     68   assertOptimized(readA);
     69   assertOptimized(readAFromB);
     70 })();
     71 
     72 (function() {
     73   function A() { this.x = 0; }
     74   A.prototype = {y: 20};
     75   function B(o) { return o.a.y; }
     76   function C() { this.a = new A(); }
     77 
     78   B(new C());
     79   B(new C());
     80   %OptimizeFunctionOnNextCall(B);
     81   var c = new C();
     82   assertEquals(20, B(c));
     83   assertOptimized(B);
     84   c.a.y = 10;
     85   assertEquals(10, B(c));
     86   assertUnoptimized(B);
     87 
     88   var c = new C();
     89   %OptimizeFunctionOnNextCall(B);
     90   assertEquals(20, B(c));
     91   assertOptimized(B);
     92   c.a.y = 30;
     93   assertEquals(30, B(c));
     94   assertOptimized(B);
     95 })();
     96 
     97 (function() {
     98   var x = new Object();
     99   x.a = 1 + "Long string that results in a cons string";
    100   x = JSON.parse('{"a":"Short"}');
    101 })();
    102 
    103 (function() {
    104   var x = {y: {z: 1}};
    105   x.y.z = 1.1;
    106 })();
    107 
    108 (function() {
    109   function Foo(x) { this.x = x; }
    110   var f1 = new Foo({x: 1});
    111   var f2 = new Foo({x: 2});
    112   var f3 = new Foo({x: 3});
    113   function readX(f) { return f.x.x; }
    114   assertEquals(readX(f1), 1);
    115   assertEquals(readX(f2), 2);
    116   assertUnoptimized(readX);
    117   %OptimizeFunctionOnNextCall(readX);
    118   assertEquals(readX(f3), 3);
    119   assertOptimized(readX);
    120   function writeX(f, x) { f.x = x; }
    121   writeX(f1, {x: 11});
    122   writeX(f2, {x: 22});
    123   assertUnoptimized(writeX);
    124   assertEquals(readX(f1), 11);
    125   assertEquals(readX(f2), 22);
    126   assertOptimized(readX);
    127   %OptimizeFunctionOnNextCall(writeX);
    128   writeX(f3, {x: 33});
    129   assertEquals(readX(f3), 33);
    130   assertOptimized(readX);
    131   assertOptimized(writeX);
    132   function addY(f, y) { f.y = y; }
    133   writeX(f1, {a: "a"});
    134   assertUnoptimized(readX);
    135   assertUnoptimized(writeX);
    136 })();
    137 
    138 (function() {
    139   function Narf(x) { this.x = x; }
    140   var f1 = new Narf(1);
    141   var f2 = new Narf(2);
    142   var f3 = new Narf(3);
    143   function baz(f, y) { f.y = y; }
    144   baz(f1, {y: 9});
    145   baz(f2, {y: 9});
    146   %OptimizeFunctionOnNextCall(baz);
    147   baz(f3, {a: -1});
    148   assertUnoptimized(baz);
    149 })();
    150 
    151 (function() {
    152   function Foo(x) { this.x = x; this.a = x; }
    153   function Bar(x) { this.x = x; this.b = x; }
    154   function readA(o) { return o.x.a; }
    155   var f = new Foo({a:1});
    156   var b = new Bar({a:2});
    157   assertEquals(readA(f), 1);
    158   assertEquals(readA(b), 2);
    159   assertEquals(readA(f), 1);
    160   assertEquals(readA(b), 2);
    161   %OptimizeFunctionOnNextCall(readA);
    162   assertEquals(readA(f), 1);
    163   assertEquals(readA(b), 2);
    164   assertOptimized(readA);
    165   f.a.y = 0;
    166   assertUnoptimized(readA);
    167 })();
    168