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