OLD | NEW |
(Empty) | |
| 1 // Flags: --expose-debug-as debug |
| 2 // Get the Debug object exposed from the debug context global object. |
| 3 |
| 4 Debug = debug.Debug |
| 5 |
| 6 eval( |
| 7 "function ChooseAnimal(p) { " + |
| 8 " if (p == 7) {" + // Use p |
| 9 " return;" + |
| 10 " }" + |
| 11 " return function Chooser() {" + |
| 12 " return 'Cat';" + |
| 13 " };" + |
| 14 "}" |
| 15 ); |
| 16 |
| 17 var old_closure = ChooseAnimal(19); |
| 18 |
| 19 assertEquals("Cat", old_closure()); |
| 20 |
| 21 var script = Debug.findScript(ChooseAnimal); |
| 22 |
| 23 var orig_animal = "'Cat'"; |
| 24 var patch_pos = script.source.indexOf(orig_animal); |
| 25 var new_animal_patch = "'Capybara' + p"; |
| 26 |
| 27 // We patch innermost function "Chooser". |
| 28 // However, this does not actually patch existing "Chooser" instances, |
| 29 // because old value of parameter "p" was not saved. |
| 30 // Instead it patches ChooseAnimal. |
| 31 Debug.change_script_live(script, patch_pos, orig_animal.length, new_animal_patch
); |
| 32 |
| 33 var new_closure = ChooseAnimal(19); |
| 34 // New instance of closure is patched. |
| 35 assertEquals("Capybara19", new_closure()); |
| 36 |
| 37 // Old instance of closure is not patched. |
| 38 assertEquals("Cat", old_closure()); |
| 39 |
OLD | NEW |