debug-liveedit-patch-positions.js revision 6ded16be15dd865a9b21ea304d5273c8be299c87
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 conicide 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    res.push(Debug.LiveEdit.GetPcFromSourcePos(func, positions[i]));
66  }
67  return res;
68}
69
70var res = ChooseAnimal();
71assertEquals("Cat15", res);
72
73var markerPositionsBefore = ReadMarkerPositions(ChooseAnimal);
74var pcArrayBefore = ReadPCMap(ChooseAnimal, markerPositionsBefore);
75
76var script = Debug.findScript(ChooseAnimal);
77
78var orig_animal = "'Cat'";
79var patch_pos = script.source.indexOf(orig_animal);
80var new_animal_patch = "'Capybara'";
81
82var change_log = new Array();
83Debug.LiveEdit.TestApi.ApplySingleChunkPatch(script, patch_pos, orig_animal.length, new_animal_patch, change_log);
84print("Change log: " + JSON.stringify(change_log) + "\n");
85
86var res = ChooseAnimal();
87assertEquals("Capybara15", res);
88
89var markerPositionsAfter = ReadMarkerPositions(ChooseAnimal);
90var pcArrayAfter = ReadPCMap(ChooseAnimal, markerPositionsAfter);
91
92assertArrayEquals(pcArrayBefore, pcArrayAfter);
93
94