Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2010 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 // Flags: --expose-debug-as debug
     29 // Get the Debug object exposed from the debug context global object.
     30 
     31 // Scenario: some function is being edited; the outer function has to have its
     32 // positions patched. Accoring to a special markup of function text
     33 // corresponding byte-code PCs should coincide before change and after it.
     34 
     35 Debug = debug.Debug
     36 Debug.setListener(function() {});
     37 
     38 eval(
     39     "function F1() {  return 5; }\n" +
     40     "function ChooseAnimal(/*$*/ ) {\n" +
     41     "/*$*/ var x = F1(/*$*/ );\n" +
     42     "/*$*/ var res/*$*/  =/*$*/ (function() { return 'Cat'; } )();\n" +
     43     "/*$*/ var y/*$*/  = F2(/*$*/ F1()/*$*/ , F1(/*$*/ )/*$*/ );\n" +
     44     "/*$*/ if (/*$*/ x.toString(/*$*/ )) { /*$*/ y = 3;/*$*/  } else {/*$*/  y = 8;/*$*/  }\n" +
     45     "/*$*/ var z = /*$*/ x * y;\n" +
     46     "/*$*/ return/*$*/  res/*$*/  + z;/*$*/  }\n" +
     47     "function F2(x, y) { return x + y; }"
     48 );
     49 
     50 // Find all *$* markers in text of the function and read corresponding statement
     51 // PCs.
     52 function ReadMarkerPositions(func) {
     53   var text = func.toString();
     54   var positions = new Array();
     55   var match;
     56   var pattern = /\/\*\$\*\//g;
     57   while ((match = pattern.exec(text)) != null) {
     58     positions.push(match.index);
     59   }
     60   return positions;
     61 }
     62 
     63 function ReadPCMap(func, positions) {
     64   var res = new Array();
     65   for (var i = 0; i < positions.length; i++) {
     66     var pc = Debug.LiveEdit.GetPcFromSourcePos(func, positions[i]);
     67 
     68     if (typeof pc === 'undefined') {
     69       // Function was marked for recompilation and it's code was replaced with a
     70       // stub. This can happen at any time especially if we are running with
     71       // --stress-opt. There is no way to get PCs now.
     72       return;
     73     }
     74 
     75     res.push(pc);
     76   }
     77 
     78   return res;
     79 }
     80 
     81 function ApplyPatch(orig_animal, new_animal) {
     82   var res = ChooseAnimal();
     83   assertEquals(orig_animal + "15", res);
     84 
     85   var script = Debug.findScript(ChooseAnimal);
     86 
     87   var orig_string = "'" + orig_animal + "'";
     88   var patch_string = "'" + new_animal + "'";
     89   var patch_pos = script.source.indexOf(orig_string);
     90 
     91   var change_log = new Array();
     92 
     93   Debug.LiveEdit.TestApi.ApplySingleChunkPatch(script,
     94                                                patch_pos,
     95                                                orig_string.length,
     96                                                patch_string,
     97                                                change_log);
     98 
     99   print("Change log: " + JSON.stringify(change_log) + "\n");
    100 
    101   var markerPositions = ReadMarkerPositions(ChooseAnimal);
    102   var pcArray = ReadPCMap(ChooseAnimal, markerPositions);
    103 
    104   var res = ChooseAnimal();
    105   assertEquals(new_animal + "15", res);
    106 
    107   return pcArray;
    108 }
    109 
    110 var pcArray1 = ApplyPatch('Cat', 'Dog');
    111 
    112 // When we patched function for the first time it was deoptimized.
    113 // Check that after the second patch maping between sources position and
    114 // pcs will not change.
    115 
    116 var pcArray2 = ApplyPatch('Dog', 'Capybara');
    117 
    118 print(pcArray1);
    119 print(pcArray2);
    120 
    121 // Function can be marked for recompilation at any point (especially if we are
    122 // running with --stress-opt). When we mark function for recompilation we
    123 // replace it's code with stub. So there is no reliable way to get PCs for
    124 // function.
    125 if (pcArray1 && pcArray2) {
    126   assertArrayEquals(pcArray1, pcArray2);
    127 }
    128 
    129 Debug.setListener(null);
    130