1402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// Copyright 2010 the V8 project authors. All rights reserved.
2402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// Redistribution and use in source and binary forms, with or without
3402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// modification, are permitted provided that the following conditions are
4402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// met:
5402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//
6402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//     * Redistributions of source code must retain the above copyright
7402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       notice, this list of conditions and the following disclaimer.
8402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//     * Redistributions in binary form must reproduce the above
9402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       copyright notice, this list of conditions and the following
10402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       disclaimer in the documentation and/or other materials provided
11402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       with the distribution.
12402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//     * Neither the name of Google Inc. nor the names of its
13402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       contributors may be used to endorse or promote products derived
14402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//       from this software without specific prior written permission.
15402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu//
16402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
286ded16be15dd865a9b21ea304d5273c8be299c87Steve Block// Flags: --expose-debug-as debug
296ded16be15dd865a9b21ea304d5273c8be299c87Steve Block// Get the Debug object exposed from the debug context global object.
30402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
31402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
326ded16be15dd865a9b21ea304d5273c8be299c87Steve BlockDebug = debug.Debug
33402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
34b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdocheval("function ChooseAnimal(p) {\n " +
35b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch     "  if (p == 7) {\n" + // Use p
36b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch     "    return;\n" +
37b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch     "  }\n" +
38b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch     "  return function Chooser() {\n" +
39b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch     "    return 'Cat';\n" +
40b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch     "  };\n" +
41b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch     "}\n");
42402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
436ded16be15dd865a9b21ea304d5273c8be299c87Steve Blockvar old_closure = ChooseAnimal(19);
44402d937239b0e2fd11bf2f4fe972ad78aa9fd481Andrei Popescu
456ded16be15dd865a9b21ea304d5273c8be299c87Steve BlockassertEquals("Cat", old_closure());
466ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
476ded16be15dd865a9b21ea304d5273c8be299c87Steve Blockvar script = Debug.findScript(ChooseAnimal);
486ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
496ded16be15dd865a9b21ea304d5273c8be299c87Steve Blockvar orig_animal = "'Cat'";
506ded16be15dd865a9b21ea304d5273c8be299c87Steve Blockvar patch_pos = script.source.indexOf(orig_animal);
516ded16be15dd865a9b21ea304d5273c8be299c87Steve Blockvar new_animal_patch = "'Capybara' + p";
526ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
536ded16be15dd865a9b21ea304d5273c8be299c87Steve Block// We patch innermost function "Chooser".
546ded16be15dd865a9b21ea304d5273c8be299c87Steve Block// However, this does not actually patch existing "Chooser" instances,
556ded16be15dd865a9b21ea304d5273c8be299c87Steve Block// because old value of parameter "p" was not saved.
566ded16be15dd865a9b21ea304d5273c8be299c87Steve Block// Instead it patches ChooseAnimal.
576ded16be15dd865a9b21ea304d5273c8be299c87Steve Blockvar change_log = new Array();
586ded16be15dd865a9b21ea304d5273c8be299c87Steve BlockDebug.LiveEdit.TestApi.ApplySingleChunkPatch(script, patch_pos, orig_animal.length, new_animal_patch, change_log);
596ded16be15dd865a9b21ea304d5273c8be299c87Steve Blockprint("Change log: " + JSON.stringify(change_log) + "\n");
606ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
616ded16be15dd865a9b21ea304d5273c8be299c87Steve Blockvar new_closure = ChooseAnimal(19);
626ded16be15dd865a9b21ea304d5273c8be299c87Steve Block// New instance of closure is patched.
636ded16be15dd865a9b21ea304d5273c8be299c87Steve BlockassertEquals("Capybara19", new_closure());
646ded16be15dd865a9b21ea304d5273c8be299c87Steve Block
656ded16be15dd865a9b21ea304d5273c8be299c87Steve Block// Old instance of closure is not patched.
666ded16be15dd865a9b21ea304d5273c8be299c87Steve BlockassertEquals("Cat", old_closure());
67