Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Flags: --expose-debug-as debug --harmony-generators | |
| 6 | |
| 7 var Debug = debug.Debug; | |
| 8 var LiveEdit = Debug.LiveEdit; | |
| 9 | |
| 10 unique_id = 0; | |
| 11 | |
| 12 var Generator = (function*(){}).constructor; | |
| 13 | |
| 14 function assertIteratorResult(value, done, result) { | |
| 15 assertEquals({value: value, done: done}, result); | |
| 16 } | |
| 17 | |
| 18 function CatGenerator() { | |
| 19 // Prevents eval script caching. | |
| 20 unique_id++; | |
| 21 return Generator('callback', | |
| 22 "/* " + unique_id + "*/\n" + | |
| 23 "yield callback();\n" + | |
| 24 "return 'Cat';\n"); | |
| 25 } | |
| 26 | |
| 27 function CatFetcher() { | |
| 28 // Prevents eval script caching. | |
| 29 unique_id++; | |
| 30 return Function('callback', | |
| 31 "/* " + unique_id + "*/\n" + | |
| 32 "callback();\n" + | |
| 33 "return 'Cat';\n"); | |
| 34 } | |
| 35 | |
| 36 // First, try CatGenerator with no perturbations. | |
| 37 (function(){ | |
| 38 var generator = CatGenerator(); | |
| 39 function callback() {}; | |
| 40 var iter = generator(callback); | |
| 41 assertIteratorResult(undefined, false, iter.next()); | |
| 42 assertIteratorResult("Cat", true, iter.next()); | |
| 43 })(); | |
| 44 | |
| 45 function patch(fun, from, to) { | |
| 46 function debug() { | |
| 47 var log = new Array(); | |
| 48 var script = Debug.findScript(fun); | |
| 49 var pos = script.source.indexOf(from); | |
| 50 try { | |
| 51 LiveEdit.TestApi.ApplySingleChunkPatch(script, pos, from.length, to, | |
| 52 log); | |
| 53 } finally { | |
| 54 print("Change log: " + JSON.stringify(log) + "\n"); | |
| 55 } | |
| 56 } | |
| 57 Debug.ExecuteInDebugContext(debug, false); | |
| 58 } | |
| 59 | |
| 60 // Try to edit a CatGenerator while it's running, then again while it's stopped. | |
| 61 (function(){ | |
| 62 var generator = CatGenerator(); | |
| 63 | |
| 64 var gen_patch_attempted = false; | |
| 65 function attempt_gen_patch() { | |
| 66 assertFalse(gen_patch_attempted); | |
| 67 gen_patch_attempted = true; | |
| 68 assertThrows(function() { patch(generator, "'Cat'", "'Capybara'") }, | |
| 69 LiveEdit.Failure); | |
| 70 }; | |
| 71 var iter = generator(attempt_gen_patch); | |
| 72 assertIteratorResult(undefined, false, iter.next()); | |
| 73 // Patch should not succeed because there is a live generator activation on | |
| 74 // the stack. | |
| 75 assertIteratorResult("Cat", true, iter.next()); | |
| 76 assertTrue(gen_patch_attempted); | |
| 77 | |
| 78 // At this point one iterator is live, but closed, so the patch will succeed. | |
| 79 patch(generator, "'Cat'", "'Capybara'"); | |
| 80 iter = generator(function(){}); | |
| 81 assertIteratorResult(undefined, false, iter.next()); | |
| 82 // Patch successful. | |
| 83 assertIteratorResult("Capybara", true, iter.next()); | |
| 84 | |
| 85 // Patching will fail however when a live iterator is suspended. | |
| 86 iter = generator(function(){}); | |
| 87 assertIteratorResult(undefined, false, iter.next()); | |
| 88 assertThrows(function() { patch(generator, "'Capybara'", "'Tapir'") }, LiveEdi t.Failure); | |
|
Yang
2014/05/06 13:05:03
line break somewhere for the 80-char limit would b
| |
| 89 assertIteratorResult("Capybara", true, iter.next()); | |
| 90 | |
| 91 // Try to patch functions with activations inside and outside generator | |
| 92 // function activations. We should succeed in the former case, but not in the | |
| 93 // latter. | |
| 94 var fetch_outside = CatFetcher(); | |
| 95 var fetch_inside = CatFetcher(); | |
| 96 var fun_patch_attempted = false; | |
| 97 var fun_patch_restarted = false; | |
| 98 function attempt_fun_patches() { | |
| 99 if (fun_patch_attempted) { | |
| 100 assertFalse(fun_patch_restarted); | |
| 101 fun_patch_restarted = true; | |
| 102 return; | |
| 103 } | |
| 104 fun_patch_attempted = true; | |
| 105 // Patching outside a generator activation must fail. | |
| 106 assertThrows(function() { patch(fetch_outside, "'Cat'", "'Cobra'") }, | |
| 107 LiveEdit.Failure); | |
| 108 // Patching inside a generator activation may succeed. | |
| 109 patch(fetch_inside, "'Cat'", "'Koala'"); | |
| 110 } | |
| 111 iter = generator(function() { return fetch_inside(attempt_fun_patches) }); | |
| 112 assertEquals('Cat', | |
| 113 fetch_outside(function () { | |
| 114 assertIteratorResult('Koala', false, iter.next()); | |
| 115 assertTrue(fun_patch_restarted); | |
| 116 })); | |
| 117 })(); | |
| OLD | NEW |