ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 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 | // LiveEdit feature implementation. The script should be executed after |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 29 | // debug-debugger.js. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 30 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 31 | // A LiveEdit namespace. It contains functions that modifies JavaScript code |
| 32 | // according to changes of script source (if possible). |
| 33 | // |
| 34 | // When new script source is put in, the difference is calculated textually, |
| 35 | // in form of list of delete/add/change chunks. The functions that include |
| 36 | // change chunk(s) get recompiled, or their enclosing functions are |
| 37 | // recompiled instead. |
| 38 | // If the function may not be recompiled (e.g. it was completely erased in new |
| 39 | // version of the script) it remains unchanged, but the code that could |
| 40 | // create a new instance of this function goes away. An old version of script |
| 41 | // is created to back up this obsolete function. |
| 42 | // All unchanged functions have their positions updated accordingly. |
| 43 | // |
| 44 | // LiveEdit namespace is declared inside a single function constructor. |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 45 | Debug.LiveEdit = new function() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 46 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 47 | // Forward declaration for minifier. |
| 48 | var FunctionStatus; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 49 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 50 | // Applies the change to the script. |
| 51 | // The change is in form of list of chunks encoded in a single array as |
| 52 | // a series of triplets (pos1_start, pos1_end, pos2_end) |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 53 | function ApplyPatchMultiChunk(script, diff_array, new_source, preview_only, |
| 54 | change_log) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 55 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 56 | var old_source = script.source; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 57 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 58 | // Gather compile information about old version of script. |
| 59 | var old_compile_info = GatherCompileInfo(old_source, script); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 60 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 61 | // Build tree structures for old and new versions of the script. |
| 62 | var root_old_node = BuildCodeInfoTree(old_compile_info); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 63 | |
| 64 | var pos_translator = new PosTranslator(diff_array); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 65 | |
| 66 | // Analyze changes. |
| 67 | MarkChangedFunctions(root_old_node, pos_translator.GetChunks()); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 68 | |
| 69 | // Find all SharedFunctionInfo's that were compiled from this script. |
| 70 | FindLiveSharedInfos(root_old_node, script); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 71 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 72 | // Gather compile information about new version of script. |
| 73 | var new_compile_info; |
| 74 | try { |
| 75 | new_compile_info = GatherCompileInfo(new_source, script); |
| 76 | } catch (e) { |
| 77 | throw new Failure("Failed to compile new version of script: " + e); |
| 78 | } |
| 79 | var root_new_node = BuildCodeInfoTree(new_compile_info); |
| 80 | |
| 81 | // Link recompiled script data with other data. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 82 | FindCorrespondingFunctions(root_old_node, root_new_node); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 83 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 84 | // Prepare to-do lists. |
| 85 | var replace_code_list = new Array(); |
| 86 | var link_to_old_script_list = new Array(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 87 | var link_to_original_script_list = new Array(); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 88 | var update_positions_list = new Array(); |
| 89 | |
| 90 | function HarvestTodo(old_node) { |
| 91 | function CollectDamaged(node) { |
| 92 | link_to_old_script_list.push(node); |
| 93 | for (var i = 0; i < node.children.length; i++) { |
| 94 | CollectDamaged(node.children[i]); |
| 95 | } |
| 96 | } |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 97 | |
| 98 | // Recursively collects all newly compiled functions that are going into |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 99 | // business and should have link to the actual script updated. |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 100 | function CollectNew(node_list) { |
| 101 | for (var i = 0; i < node_list.length; i++) { |
| 102 | link_to_original_script_list.push(node_list[i]); |
| 103 | CollectNew(node_list[i].children); |
| 104 | } |
| 105 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 106 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 107 | if (old_node.status == FunctionStatus.DAMAGED) { |
| 108 | CollectDamaged(old_node); |
| 109 | return; |
| 110 | } |
| 111 | if (old_node.status == FunctionStatus.UNCHANGED) { |
| 112 | update_positions_list.push(old_node); |
| 113 | } else if (old_node.status == FunctionStatus.SOURCE_CHANGED) { |
| 114 | update_positions_list.push(old_node); |
| 115 | } else if (old_node.status == FunctionStatus.CHANGED) { |
| 116 | replace_code_list.push(old_node); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 117 | CollectNew(old_node.unmatched_new_nodes); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 118 | } |
| 119 | for (var i = 0; i < old_node.children.length; i++) { |
| 120 | HarvestTodo(old_node.children[i]); |
| 121 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 122 | } |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 123 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 124 | var preview_description = { |
| 125 | change_tree: DescribeChangeTree(root_old_node), |
| 126 | textual_diff: { |
| 127 | old_len: old_source.length, |
| 128 | new_len: new_source.length, |
| 129 | chunks: diff_array |
| 130 | }, |
| 131 | updated: false |
| 132 | }; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 133 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 134 | if (preview_only) { |
| 135 | return preview_description; |
| 136 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 137 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 138 | HarvestTodo(root_old_node); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 139 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 140 | // Collect shared infos for functions whose code need to be patched. |
| 141 | var replaced_function_infos = new Array(); |
| 142 | for (var i = 0; i < replace_code_list.length; i++) { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 143 | var info_wrapper = replace_code_list[i].live_shared_info_wrapper; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 144 | if (info_wrapper) { |
| 145 | replaced_function_infos.push(info_wrapper); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 149 | // We haven't changed anything before this line yet. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 150 | // Committing all changes. |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 151 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 152 | // Check that function being patched is not currently on stack or drop them. |
| 153 | var dropped_functions_number = |
| 154 | CheckStackActivations(replaced_function_infos, change_log); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 155 | |
| 156 | preview_description.stack_modified = dropped_functions_number != 0; |
| 157 | |
| 158 | // Start with breakpoints. Convert their line/column positions and |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 159 | // temporary remove. |
| 160 | var break_points_restorer = TemporaryRemoveBreakPoints(script, change_log); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 161 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 162 | var old_script; |
| 163 | |
| 164 | // Create an old script only if there are function that should be linked |
| 165 | // to old version. |
| 166 | if (link_to_old_script_list.length == 0) { |
| 167 | %LiveEditReplaceScript(script, new_source, null); |
| 168 | old_script = void 0; |
| 169 | } else { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 170 | var old_script_name = CreateNameForOldScript(script); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 171 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 172 | // Update the script text and create a new script representing an old |
| 173 | // version of the script. |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 174 | old_script = %LiveEditReplaceScript(script, new_source, |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 175 | old_script_name); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 176 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 177 | var link_to_old_script_report = new Array(); |
| 178 | change_log.push( { linked_to_old_script: link_to_old_script_report } ); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 179 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 180 | // We need to link to old script all former nested functions. |
| 181 | for (var i = 0; i < link_to_old_script_list.length; i++) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 182 | LinkToOldScript(link_to_old_script_list[i], old_script, |
| 183 | link_to_old_script_report); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 184 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 185 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 186 | preview_description.created_script_name = old_script_name; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 187 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 188 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 189 | // Link to an actual script all the functions that we are going to use. |
| 190 | for (var i = 0; i < link_to_original_script_list.length; i++) { |
| 191 | %LiveEditFunctionSetScript( |
| 192 | link_to_original_script_list[i].info.shared_function_info, script); |
| 193 | } |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 194 | |
| 195 | for (var i = 0; i < replace_code_list.length; i++) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 196 | PatchFunctionCode(replace_code_list[i], change_log); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 197 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 198 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 199 | var position_patch_report = new Array(); |
| 200 | change_log.push( {position_patched: position_patch_report} ); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 201 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 202 | for (var i = 0; i < update_positions_list.length; i++) { |
| 203 | // TODO(LiveEdit): take into account wether it's source_changed or |
| 204 | // unchanged and whether positions changed at all. |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 205 | PatchPositions(update_positions_list[i], diff_array, |
| 206 | position_patch_report); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 207 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 208 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 209 | break_points_restorer(pos_translator, old_script); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 210 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 211 | preview_description.updated = true; |
| 212 | return preview_description; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 213 | } |
| 214 | // Function is public. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 215 | this.ApplyPatchMultiChunk = ApplyPatchMultiChunk; |
| 216 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 217 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 218 | // Fully compiles source string as a script. Returns Array of |
| 219 | // FunctionCompileInfo -- a descriptions of all functions of the script. |
| 220 | // Elements of array are ordered by start positions of functions (from top |
| 221 | // to bottom) in the source. Fields outer_index and next_sibling_index help |
| 222 | // to navigate the nesting structure of functions. |
| 223 | // |
| 224 | // All functions get compiled linked to script provided as parameter script. |
| 225 | // TODO(LiveEdit): consider not using actual scripts as script, because |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 226 | // we have to manually erase all links right after compile. |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 227 | function GatherCompileInfo(source, script) { |
| 228 | // Get function info, elements are partially sorted (it is a tree of |
| 229 | // nested functions serialized as parent followed by serialized children. |
| 230 | var raw_compile_info = %LiveEditGatherCompileInfo(script, source); |
| 231 | |
| 232 | // Sort function infos by start position field. |
| 233 | var compile_info = new Array(); |
| 234 | var old_index_map = new Array(); |
| 235 | for (var i = 0; i < raw_compile_info.length; i++) { |
| 236 | var info = new FunctionCompileInfo(raw_compile_info[i]); |
| 237 | // Remove all links to the actual script. Breakpoints system and |
| 238 | // LiveEdit itself believe that any function in heap that points to a |
| 239 | // particular script is a regular function. |
| 240 | // For some functions we will restore this link later. |
| 241 | %LiveEditFunctionSetScript(info.shared_function_info, void 0); |
| 242 | compile_info.push(info); |
| 243 | old_index_map.push(i); |
| 244 | } |
| 245 | |
| 246 | for (var i = 0; i < compile_info.length; i++) { |
| 247 | var k = i; |
| 248 | for (var j = i + 1; j < compile_info.length; j++) { |
| 249 | if (compile_info[k].start_position > compile_info[j].start_position) { |
| 250 | k = j; |
| 251 | } |
| 252 | } |
| 253 | if (k != i) { |
| 254 | var temp_info = compile_info[k]; |
| 255 | var temp_index = old_index_map[k]; |
| 256 | compile_info[k] = compile_info[i]; |
| 257 | old_index_map[k] = old_index_map[i]; |
| 258 | compile_info[i] = temp_info; |
| 259 | old_index_map[i] = temp_index; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // After sorting update outer_inder field using old_index_map. Also |
| 264 | // set next_sibling_index field. |
| 265 | var current_index = 0; |
| 266 | |
| 267 | // The recursive function, that goes over all children of a particular |
| 268 | // node (i.e. function info). |
| 269 | function ResetIndexes(new_parent_index, old_parent_index) { |
| 270 | var previous_sibling = -1; |
| 271 | while (current_index < compile_info.length && |
| 272 | compile_info[current_index].outer_index == old_parent_index) { |
| 273 | var saved_index = current_index; |
| 274 | compile_info[saved_index].outer_index = new_parent_index; |
| 275 | if (previous_sibling != -1) { |
| 276 | compile_info[previous_sibling].next_sibling_index = saved_index; |
| 277 | } |
| 278 | previous_sibling = saved_index; |
| 279 | current_index++; |
| 280 | ResetIndexes(saved_index, old_index_map[saved_index]); |
| 281 | } |
| 282 | if (previous_sibling != -1) { |
| 283 | compile_info[previous_sibling].next_sibling_index = -1; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | ResetIndexes(-1, -1); |
| 288 | Assert(current_index == compile_info.length); |
| 289 | |
| 290 | return compile_info; |
| 291 | } |
| 292 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 293 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 294 | // Replaces function's Code. |
| 295 | function PatchFunctionCode(old_node, change_log) { |
| 296 | var new_info = old_node.corresponding_node.info; |
| 297 | var shared_info_wrapper = old_node.live_shared_info_wrapper; |
| 298 | if (shared_info_wrapper) { |
| 299 | %LiveEditReplaceFunctionCode(new_info.raw_array, |
| 300 | shared_info_wrapper.raw_array); |
| 301 | |
| 302 | // The function got a new code. However, this new code brings all new |
| 303 | // instances of SharedFunctionInfo for nested functions. However, |
| 304 | // we want the original instances to be used wherever possible. |
| 305 | // (This is because old instances and new instances will be both |
| 306 | // linked to a script and breakpoints subsystem does not really |
| 307 | // expects this; neither does LiveEdit subsystem on next call). |
| 308 | for (var i = 0; i < old_node.children.length; i++) { |
| 309 | if (old_node.children[i].corresponding_node) { |
| 310 | var corresponding_child = old_node.children[i].corresponding_node; |
| 311 | var child_shared_info_wrapper = |
| 312 | old_node.children[i].live_shared_info_wrapper; |
| 313 | if (child_shared_info_wrapper) { |
| 314 | %LiveEditReplaceRefToNestedFunction(shared_info_wrapper.info, |
| 315 | corresponding_child.info.shared_function_info, |
| 316 | child_shared_info_wrapper.info); |
| 317 | } |
| 318 | } |
| 319 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 320 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 321 | change_log.push( {function_patched: new_info.function_name} ); |
| 322 | } else { |
| 323 | change_log.push( {function_patched: new_info.function_name, |
| 324 | function_info_not_found: true} ); |
| 325 | } |
| 326 | } |
| 327 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 328 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 329 | // Makes a function associated with another instance of a script (the |
| 330 | // one representing its old version). This way the function still |
| 331 | // may access its own text. |
| 332 | function LinkToOldScript(old_info_node, old_script, report_array) { |
| 333 | var shared_info = old_info_node.live_shared_info_wrapper; |
| 334 | if (shared_info) { |
| 335 | %LiveEditFunctionSetScript(shared_info.info, old_script); |
| 336 | report_array.push( { name: shared_info.function_name } ); |
| 337 | } else { |
| 338 | report_array.push( |
| 339 | { name: old_info_node.info.function_name, not_found: true } ); |
| 340 | } |
| 341 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 342 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 343 | |
| 344 | // Returns function that restores breakpoints. |
| 345 | function TemporaryRemoveBreakPoints(original_script, change_log) { |
| 346 | var script_break_points = GetScriptBreakPoints(original_script); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 347 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 348 | var break_points_update_report = []; |
| 349 | change_log.push( { break_points_update: break_points_update_report } ); |
| 350 | |
| 351 | var break_point_old_positions = []; |
| 352 | for (var i = 0; i < script_break_points.length; i++) { |
| 353 | var break_point = script_break_points[i]; |
| 354 | |
| 355 | break_point.clear(); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 356 | |
| 357 | // TODO(LiveEdit): be careful with resource offset here. |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 358 | var break_point_position = Debug.findScriptSourcePosition(original_script, |
| 359 | break_point.line(), break_point.column()); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 360 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 361 | var old_position_description = { |
| 362 | position: break_point_position, |
| 363 | line: break_point.line(), |
| 364 | column: break_point.column() |
| 365 | } |
| 366 | break_point_old_positions.push(old_position_description); |
| 367 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 368 | |
| 369 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 370 | // Restores breakpoints and creates their copies in the "old" copy of |
| 371 | // the script. |
| 372 | return function (pos_translator, old_script_copy_opt) { |
| 373 | // Update breakpoints (change positions and restore them in old version |
| 374 | // of script. |
| 375 | for (var i = 0; i < script_break_points.length; i++) { |
| 376 | var break_point = script_break_points[i]; |
| 377 | if (old_script_copy_opt) { |
| 378 | var clone = break_point.cloneForOtherScript(old_script_copy_opt); |
| 379 | clone.set(old_script_copy_opt); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 380 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 381 | break_points_update_report.push( { |
| 382 | type: "copied_to_old", |
| 383 | id: break_point.number(), |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 384 | new_id: clone.number(), |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 385 | positions: break_point_old_positions[i] |
| 386 | } ); |
| 387 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 388 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 389 | var updated_position = pos_translator.Translate( |
| 390 | break_point_old_positions[i].position, |
| 391 | PosTranslator.ShiftWithTopInsideChunkHandler); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 392 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 393 | var new_location = |
| 394 | original_script.locationFromPosition(updated_position, false); |
| 395 | |
| 396 | break_point.update_positions(new_location.line, new_location.column); |
| 397 | |
| 398 | var new_position_description = { |
| 399 | position: updated_position, |
| 400 | line: new_location.line, |
| 401 | column: new_location.column |
| 402 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 403 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 404 | break_point.set(original_script); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 405 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 406 | break_points_update_report.push( { type: "position_changed", |
| 407 | id: break_point.number(), |
| 408 | old_positions: break_point_old_positions[i], |
| 409 | new_positions: new_position_description |
| 410 | } ); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 415 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 416 | function Assert(condition, message) { |
| 417 | if (!condition) { |
| 418 | if (message) { |
| 419 | throw "Assert " + message; |
| 420 | } else { |
| 421 | throw "Assert"; |
| 422 | } |
| 423 | } |
| 424 | } |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 425 | |
| 426 | function DiffChunk(pos1, pos2, len1, len2) { |
| 427 | this.pos1 = pos1; |
| 428 | this.pos2 = pos2; |
| 429 | this.len1 = len1; |
| 430 | this.len2 = len2; |
| 431 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 432 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 433 | function PosTranslator(diff_array) { |
| 434 | var chunks = new Array(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 435 | var current_diff = 0; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 436 | for (var i = 0; i < diff_array.length; i += 3) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 437 | var pos1_begin = diff_array[i]; |
| 438 | var pos2_begin = pos1_begin + current_diff; |
| 439 | var pos1_end = diff_array[i + 1]; |
| 440 | var pos2_end = diff_array[i + 2]; |
| 441 | chunks.push(new DiffChunk(pos1_begin, pos2_begin, pos1_end - pos1_begin, |
| 442 | pos2_end - pos2_begin)); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 443 | current_diff = pos2_end - pos1_end; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 444 | } |
| 445 | this.chunks = chunks; |
| 446 | } |
| 447 | PosTranslator.prototype.GetChunks = function() { |
| 448 | return this.chunks; |
| 449 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 450 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 451 | PosTranslator.prototype.Translate = function(pos, inside_chunk_handler) { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 452 | var array = this.chunks; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 453 | if (array.length == 0 || pos < array[0].pos1) { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 454 | return pos; |
| 455 | } |
| 456 | var chunk_index1 = 0; |
| 457 | var chunk_index2 = array.length - 1; |
| 458 | |
| 459 | while (chunk_index1 < chunk_index2) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 460 | var middle_index = Math.floor((chunk_index1 + chunk_index2) / 2); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 461 | if (pos < array[middle_index + 1].pos1) { |
| 462 | chunk_index2 = middle_index; |
| 463 | } else { |
| 464 | chunk_index1 = middle_index + 1; |
| 465 | } |
| 466 | } |
| 467 | var chunk = array[chunk_index1]; |
| 468 | if (pos >= chunk.pos1 + chunk.len1) { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 469 | return pos + chunk.pos2 + chunk.len2 - chunk.pos1 - chunk.len1; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 470 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 471 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 472 | if (!inside_chunk_handler) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 473 | inside_chunk_handler = PosTranslator.DefaultInsideChunkHandler; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 474 | } |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 475 | return inside_chunk_handler(pos, chunk); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 476 | } |
| 477 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 478 | PosTranslator.DefaultInsideChunkHandler = function(pos, diff_chunk) { |
| 479 | Assert(false, "Cannot translate position in changed area"); |
| 480 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 481 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 482 | PosTranslator.ShiftWithTopInsideChunkHandler = |
| 483 | function(pos, diff_chunk) { |
| 484 | // We carelessly do not check whether we stay inside the chunk after |
| 485 | // translation. |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 486 | return pos - diff_chunk.pos1 + diff_chunk.pos2; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 487 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 488 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 489 | var FunctionStatus = { |
| 490 | // No change to function or its inner functions; however its positions |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 491 | // in script may have been shifted. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 492 | UNCHANGED: "unchanged", |
| 493 | // The code of a function remains unchanged, but something happened inside |
| 494 | // some inner functions. |
| 495 | SOURCE_CHANGED: "source changed", |
| 496 | // The code of a function is changed or some nested function cannot be |
| 497 | // properly patched so this function must be recompiled. |
| 498 | CHANGED: "changed", |
| 499 | // Function is changed but cannot be patched. |
| 500 | DAMAGED: "damaged" |
| 501 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 502 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 503 | function CodeInfoTreeNode(code_info, children, array_index) { |
| 504 | this.info = code_info; |
| 505 | this.children = children; |
| 506 | // an index in array of compile_info |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 507 | this.array_index = array_index; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 508 | this.parent = void 0; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 509 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 510 | this.status = FunctionStatus.UNCHANGED; |
| 511 | // Status explanation is used for debugging purposes and will be shown |
| 512 | // in user UI if some explanations are needed. |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 513 | this.status_explanation = void 0; |
| 514 | this.new_start_pos = void 0; |
| 515 | this.new_end_pos = void 0; |
| 516 | this.corresponding_node = void 0; |
| 517 | this.unmatched_new_nodes = void 0; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 518 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 519 | // 'Textual' correspondence/matching is weaker than 'pure' |
| 520 | // correspondence/matching. We need 'textual' level for visual presentation |
| 521 | // in UI, we use 'pure' level for actual code manipulation. |
| 522 | // Sometimes only function body is changed (functions in old and new script |
| 523 | // textually correspond), but we cannot patch the code, so we see them |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 524 | // as an old function deleted and new function created. |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 525 | this.textual_corresponding_node = void 0; |
| 526 | this.textually_unmatched_new_nodes = void 0; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 527 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 528 | this.live_shared_info_wrapper = void 0; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 529 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 530 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 531 | // From array of function infos that is implicitly a tree creates |
| 532 | // an actual tree of functions in script. |
| 533 | function BuildCodeInfoTree(code_info_array) { |
| 534 | // Throughtout all function we iterate over input array. |
| 535 | var index = 0; |
| 536 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 537 | // Recursive function that builds a branch of tree. |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 538 | function BuildNode() { |
| 539 | var my_index = index; |
| 540 | index++; |
| 541 | var child_array = new Array(); |
| 542 | while (index < code_info_array.length && |
| 543 | code_info_array[index].outer_index == my_index) { |
| 544 | child_array.push(BuildNode()); |
| 545 | } |
| 546 | var node = new CodeInfoTreeNode(code_info_array[my_index], child_array, |
| 547 | my_index); |
| 548 | for (var i = 0; i < child_array.length; i++) { |
| 549 | child_array[i].parent = node; |
| 550 | } |
| 551 | return node; |
| 552 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 553 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 554 | var root = BuildNode(); |
| 555 | Assert(index == code_info_array.length); |
| 556 | return root; |
| 557 | } |
| 558 | |
| 559 | // Applies a list of the textual diff chunks onto the tree of functions. |
| 560 | // Determines status of each function (from unchanged to damaged). However |
| 561 | // children of unchanged functions are ignored. |
| 562 | function MarkChangedFunctions(code_info_tree, chunks) { |
| 563 | |
| 564 | // A convenient interator over diff chunks that also translates |
| 565 | // positions from old to new in a current non-changed part of script. |
| 566 | var chunk_it = new function() { |
| 567 | var chunk_index = 0; |
| 568 | var pos_diff = 0; |
| 569 | this.current = function() { return chunks[chunk_index]; } |
| 570 | this.next = function() { |
| 571 | var chunk = chunks[chunk_index]; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 572 | pos_diff = chunk.pos2 + chunk.len2 - (chunk.pos1 + chunk.len1); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 573 | chunk_index++; |
| 574 | } |
| 575 | this.done = function() { return chunk_index >= chunks.length; } |
| 576 | this.TranslatePos = function(pos) { return pos + pos_diff; } |
| 577 | }; |
| 578 | |
| 579 | // A recursive function that processes internals of a function and all its |
| 580 | // inner functions. Iterator chunk_it initially points to a chunk that is |
| 581 | // below function start. |
| 582 | function ProcessInternals(info_node) { |
| 583 | info_node.new_start_pos = chunk_it.TranslatePos( |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 584 | info_node.info.start_position); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 585 | var child_index = 0; |
| 586 | var code_changed = false; |
| 587 | var source_changed = false; |
| 588 | // Simultaneously iterates over child functions and over chunks. |
| 589 | while (!chunk_it.done() && |
| 590 | chunk_it.current().pos1 < info_node.info.end_position) { |
| 591 | if (child_index < info_node.children.length) { |
| 592 | var child = info_node.children[child_index]; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 593 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 594 | if (child.info.end_position <= chunk_it.current().pos1) { |
| 595 | ProcessUnchangedChild(child); |
| 596 | child_index++; |
| 597 | continue; |
| 598 | } else if (child.info.start_position >= |
| 599 | chunk_it.current().pos1 + chunk_it.current().len1) { |
| 600 | code_changed = true; |
| 601 | chunk_it.next(); |
| 602 | continue; |
| 603 | } else if (child.info.start_position <= chunk_it.current().pos1 && |
| 604 | child.info.end_position >= chunk_it.current().pos1 + |
| 605 | chunk_it.current().len1) { |
| 606 | ProcessInternals(child); |
| 607 | source_changed = source_changed || |
| 608 | ( child.status != FunctionStatus.UNCHANGED ); |
| 609 | code_changed = code_changed || |
| 610 | ( child.status == FunctionStatus.DAMAGED ); |
| 611 | child_index++; |
| 612 | continue; |
| 613 | } else { |
| 614 | code_changed = true; |
| 615 | child.status = FunctionStatus.DAMAGED; |
| 616 | child.status_explanation = |
| 617 | "Text diff overlaps with function boundary"; |
| 618 | child_index++; |
| 619 | continue; |
| 620 | } |
| 621 | } else { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 622 | if (chunk_it.current().pos1 + chunk_it.current().len1 <= |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 623 | info_node.info.end_position) { |
| 624 | info_node.status = FunctionStatus.CHANGED; |
| 625 | chunk_it.next(); |
| 626 | continue; |
| 627 | } else { |
| 628 | info_node.status = FunctionStatus.DAMAGED; |
| 629 | info_node.status_explanation = |
| 630 | "Text diff overlaps with function boundary"; |
| 631 | return; |
| 632 | } |
| 633 | } |
| 634 | Assert("Unreachable", false); |
| 635 | } |
| 636 | while (child_index < info_node.children.length) { |
| 637 | var child = info_node.children[child_index]; |
| 638 | ProcessUnchangedChild(child); |
| 639 | child_index++; |
| 640 | } |
| 641 | if (code_changed) { |
| 642 | info_node.status = FunctionStatus.CHANGED; |
| 643 | } else if (source_changed) { |
| 644 | info_node.status = FunctionStatus.SOURCE_CHANGED; |
| 645 | } |
| 646 | info_node.new_end_pos = |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 647 | chunk_it.TranslatePos(info_node.info.end_position); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 648 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 649 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 650 | function ProcessUnchangedChild(node) { |
| 651 | node.new_start_pos = chunk_it.TranslatePos(node.info.start_position); |
| 652 | node.new_end_pos = chunk_it.TranslatePos(node.info.end_position); |
| 653 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 654 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 655 | ProcessInternals(code_info_tree); |
| 656 | } |
| 657 | |
| 658 | // For ecah old function (if it is not damaged) tries to find a corresponding |
| 659 | // function in new script. Typically it should succeed (non-damaged functions |
| 660 | // by definition may only have changes inside their bodies). However there are |
| 661 | // reasons for corresponence not to be found; function with unmodified text |
| 662 | // in new script may become enclosed into other function; the innocent change |
| 663 | // inside function body may in fact be something like "} function B() {" that |
| 664 | // splits a function into 2 functions. |
| 665 | function FindCorrespondingFunctions(old_code_tree, new_code_tree) { |
| 666 | |
| 667 | // A recursive function that tries to find a correspondence for all |
| 668 | // child functions and for their inner functions. |
| 669 | function ProcessChildren(old_node, new_node) { |
| 670 | var old_children = old_node.children; |
| 671 | var new_children = new_node.children; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 672 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 673 | var unmatched_new_nodes_list = []; |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 674 | var textually_unmatched_new_nodes_list = []; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 675 | |
| 676 | var old_index = 0; |
| 677 | var new_index = 0; |
| 678 | while (old_index < old_children.length) { |
| 679 | if (old_children[old_index].status == FunctionStatus.DAMAGED) { |
| 680 | old_index++; |
| 681 | } else if (new_index < new_children.length) { |
| 682 | if (new_children[new_index].info.start_position < |
| 683 | old_children[old_index].new_start_pos) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 684 | unmatched_new_nodes_list.push(new_children[new_index]); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 685 | textually_unmatched_new_nodes_list.push(new_children[new_index]); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 686 | new_index++; |
| 687 | } else if (new_children[new_index].info.start_position == |
| 688 | old_children[old_index].new_start_pos) { |
| 689 | if (new_children[new_index].info.end_position == |
| 690 | old_children[old_index].new_end_pos) { |
| 691 | old_children[old_index].corresponding_node = |
| 692 | new_children[new_index]; |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 693 | old_children[old_index].textual_corresponding_node = |
| 694 | new_children[new_index]; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 695 | if (old_children[old_index].status != FunctionStatus.UNCHANGED) { |
| 696 | ProcessChildren(old_children[old_index], |
| 697 | new_children[new_index]); |
| 698 | if (old_children[old_index].status == FunctionStatus.DAMAGED) { |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 699 | unmatched_new_nodes_list.push( |
| 700 | old_children[old_index].corresponding_node); |
| 701 | old_children[old_index].corresponding_node = void 0; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 702 | old_node.status = FunctionStatus.CHANGED; |
| 703 | } |
| 704 | } |
| 705 | } else { |
| 706 | old_children[old_index].status = FunctionStatus.DAMAGED; |
| 707 | old_children[old_index].status_explanation = |
| 708 | "No corresponding function in new script found"; |
| 709 | old_node.status = FunctionStatus.CHANGED; |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 710 | unmatched_new_nodes_list.push(new_children[new_index]); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 711 | textually_unmatched_new_nodes_list.push(new_children[new_index]); |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 712 | } |
| 713 | new_index++; |
| 714 | old_index++; |
| 715 | } else { |
| 716 | old_children[old_index].status = FunctionStatus.DAMAGED; |
| 717 | old_children[old_index].status_explanation = |
| 718 | "No corresponding function in new script found"; |
| 719 | old_node.status = FunctionStatus.CHANGED; |
| 720 | old_index++; |
| 721 | } |
| 722 | } else { |
| 723 | old_children[old_index].status = FunctionStatus.DAMAGED; |
| 724 | old_children[old_index].status_explanation = |
| 725 | "No corresponding function in new script found"; |
| 726 | old_node.status = FunctionStatus.CHANGED; |
| 727 | old_index++; |
| 728 | } |
| 729 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 730 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 731 | while (new_index < new_children.length) { |
| 732 | unmatched_new_nodes_list.push(new_children[new_index]); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 733 | textually_unmatched_new_nodes_list.push(new_children[new_index]); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 734 | new_index++; |
| 735 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 736 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 737 | if (old_node.status == FunctionStatus.CHANGED) { |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 738 | var why_wrong_expectations = |
| 739 | WhyFunctionExpectationsDiffer(old_node.info, new_node.info); |
| 740 | if (why_wrong_expectations) { |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 741 | old_node.status = FunctionStatus.DAMAGED; |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 742 | old_node.status_explanation = why_wrong_expectations; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 745 | old_node.unmatched_new_nodes = unmatched_new_nodes_list; |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 746 | old_node.textually_unmatched_new_nodes = |
| 747 | textually_unmatched_new_nodes_list; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | ProcessChildren(old_code_tree, new_code_tree); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 751 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 752 | old_code_tree.corresponding_node = new_code_tree; |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 753 | old_code_tree.textual_corresponding_node = new_code_tree; |
| 754 | |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 755 | Assert(old_code_tree.status != FunctionStatus.DAMAGED, |
| 756 | "Script became damaged"); |
| 757 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 758 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 759 | function FindLiveSharedInfos(old_code_tree, script) { |
| 760 | var shared_raw_list = %LiveEditFindSharedFunctionInfosForScript(script); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 761 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 762 | var shared_infos = new Array(); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 763 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 764 | for (var i = 0; i < shared_raw_list.length; i++) { |
| 765 | shared_infos.push(new SharedInfoWrapper(shared_raw_list[i])); |
| 766 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 767 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 768 | // Finds SharedFunctionInfo that corresponds compile info with index |
| 769 | // in old version of the script. |
| 770 | function FindFunctionInfo(compile_info) { |
| 771 | for (var i = 0; i < shared_infos.length; i++) { |
| 772 | var wrapper = shared_infos[i]; |
| 773 | if (wrapper.start_position == compile_info.start_position && |
| 774 | wrapper.end_position == compile_info.end_position) { |
| 775 | return wrapper; |
| 776 | } |
| 777 | } |
| 778 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 779 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 780 | function TraverseTree(node) { |
| 781 | var info_wrapper = FindFunctionInfo(node.info); |
| 782 | if (info_wrapper) { |
| 783 | node.live_shared_info_wrapper = info_wrapper; |
| 784 | } |
| 785 | for (var i = 0; i < node.children.length; i++) { |
| 786 | TraverseTree(node.children[i]); |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | TraverseTree(old_code_tree); |
| 791 | } |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 792 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 793 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 794 | // An object describing function compilation details. Its index fields |
| 795 | // apply to indexes inside array that stores these objects. |
| 796 | function FunctionCompileInfo(raw_array) { |
| 797 | this.function_name = raw_array[0]; |
| 798 | this.start_position = raw_array[1]; |
| 799 | this.end_position = raw_array[2]; |
| 800 | this.param_num = raw_array[3]; |
| 801 | this.code = raw_array[4]; |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 802 | this.code_scope_info = raw_array[5]; |
| 803 | this.scope_info = raw_array[6]; |
| 804 | this.outer_index = raw_array[7]; |
| 805 | this.shared_function_info = raw_array[8]; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 806 | this.next_sibling_index = null; |
| 807 | this.raw_array = raw_array; |
| 808 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 809 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 810 | function SharedInfoWrapper(raw_array) { |
| 811 | this.function_name = raw_array[0]; |
| 812 | this.start_position = raw_array[1]; |
| 813 | this.end_position = raw_array[2]; |
| 814 | this.info = raw_array[3]; |
| 815 | this.raw_array = raw_array; |
| 816 | } |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 817 | |
| 818 | // Changes positions (including all statments) in function. |
| 819 | function PatchPositions(old_info_node, diff_array, report_array) { |
| 820 | var shared_info_wrapper = old_info_node.live_shared_info_wrapper; |
| 821 | if (!shared_info_wrapper) { |
| 822 | // TODO(LiveEdit): function is not compiled yet or is already collected. |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 823 | report_array.push( |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 824 | { name: old_info_node.info.function_name, info_not_found: true } ); |
| 825 | return; |
| 826 | } |
| 827 | %LiveEditPatchFunctionPositions(shared_info_wrapper.raw_array, |
| 828 | diff_array); |
| 829 | report_array.push( { name: old_info_node.info.function_name } ); |
| 830 | } |
| 831 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 832 | // Adds a suffix to script name to mark that it is old version. |
| 833 | function CreateNameForOldScript(script) { |
| 834 | // TODO(635): try better than this; support several changes. |
| 835 | return script.name + " (old)"; |
| 836 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 837 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 838 | // Compares a function interface old and new version, whether it |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 839 | // changed or not. Returns explanation if they differ. |
| 840 | function WhyFunctionExpectationsDiffer(function_info1, function_info2) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 841 | // Check that function has the same number of parameters (there may exist |
| 842 | // an adapter, that won't survive function parameter number change). |
| 843 | if (function_info1.param_num != function_info2.param_num) { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 844 | return "Changed parameter number: " + function_info1.param_num + |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 845 | " and " + function_info2.param_num; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 846 | } |
| 847 | var scope_info1 = function_info1.scope_info; |
| 848 | var scope_info2 = function_info2.scope_info; |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 849 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 850 | var scope_info1_text; |
| 851 | var scope_info2_text; |
| 852 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 853 | if (scope_info1) { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 854 | scope_info1_text = scope_info1.toString(); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 855 | } else { |
| 856 | scope_info1_text = ""; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 857 | } |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 858 | if (scope_info2) { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 859 | scope_info2_text = scope_info2.toString(); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 860 | } else { |
| 861 | scope_info2_text = ""; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 862 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 863 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 864 | if (scope_info1_text != scope_info2_text) { |
| 865 | return "Incompatible variable maps: [" + scope_info1_text + |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 866 | "] and [" + scope_info2_text + "]"; |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 867 | } |
| 868 | // No differences. Return undefined. |
| 869 | return; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 870 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 871 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 872 | // Minifier forward declaration. |
| 873 | var FunctionPatchabilityStatus; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 874 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 875 | // For array of wrapped shared function infos checks that none of them |
| 876 | // have activations on stack (of any thread). Throws a Failure exception |
| 877 | // if this proves to be false. |
| 878 | function CheckStackActivations(shared_wrapper_list, change_log) { |
| 879 | var shared_list = new Array(); |
| 880 | for (var i = 0; i < shared_wrapper_list.length; i++) { |
| 881 | shared_list[i] = shared_wrapper_list[i].info; |
| 882 | } |
| 883 | var result = %LiveEditCheckAndDropActivations(shared_list, true); |
| 884 | if (result[shared_list.length]) { |
| 885 | // Extra array element may contain error message. |
| 886 | throw new Failure(result[shared_list.length]); |
| 887 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 888 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 889 | var problems = new Array(); |
| 890 | var dropped = new Array(); |
| 891 | for (var i = 0; i < shared_list.length; i++) { |
| 892 | var shared = shared_wrapper_list[i]; |
| 893 | if (result[i] == FunctionPatchabilityStatus.REPLACED_ON_ACTIVE_STACK) { |
| 894 | dropped.push({ name: shared.function_name } ); |
| 895 | } else if (result[i] != FunctionPatchabilityStatus.AVAILABLE_FOR_PATCH) { |
| 896 | var description = { |
| 897 | name: shared.function_name, |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 898 | start_pos: shared.start_position, |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 899 | end_pos: shared.end_position, |
| 900 | replace_problem: |
| 901 | FunctionPatchabilityStatus.SymbolName(result[i]) |
| 902 | }; |
| 903 | problems.push(description); |
| 904 | } |
| 905 | } |
| 906 | if (dropped.length > 0) { |
| 907 | change_log.push({ dropped_from_stack: dropped }); |
| 908 | } |
| 909 | if (problems.length > 0) { |
| 910 | change_log.push( { functions_on_stack: problems } ); |
| 911 | throw new Failure("Blocked by functions on stack"); |
| 912 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 913 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 914 | return dropped.length; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 915 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 916 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 917 | // A copy of the FunctionPatchabilityStatus enum from liveedit.h |
| 918 | var FunctionPatchabilityStatus = { |
| 919 | AVAILABLE_FOR_PATCH: 1, |
| 920 | BLOCKED_ON_ACTIVE_STACK: 2, |
| 921 | BLOCKED_ON_OTHER_STACK: 3, |
| 922 | BLOCKED_UNDER_NATIVE_CODE: 4, |
| 923 | REPLACED_ON_ACTIVE_STACK: 5 |
| 924 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 925 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 926 | FunctionPatchabilityStatus.SymbolName = function(code) { |
| 927 | var enum = FunctionPatchabilityStatus; |
| 928 | for (name in enum) { |
| 929 | if (enum[name] == code) { |
| 930 | return name; |
| 931 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 932 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 933 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 934 | |
| 935 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 936 | // A logical failure in liveedit process. This means that change_log |
| 937 | // is valid and consistent description of what happened. |
| 938 | function Failure(message) { |
| 939 | this.message = message; |
| 940 | } |
| 941 | // Function (constructor) is public. |
| 942 | this.Failure = Failure; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 943 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 944 | Failure.prototype.toString = function() { |
| 945 | return "LiveEdit Failure: " + this.message; |
| 946 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 947 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 948 | // A testing entry. |
| 949 | function GetPcFromSourcePos(func, source_pos) { |
| 950 | return %GetFunctionCodePositionFromSource(func, source_pos); |
| 951 | } |
| 952 | // Function is public. |
| 953 | this.GetPcFromSourcePos = GetPcFromSourcePos; |
| 954 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 955 | // LiveEdit main entry point: changes a script text to a new string. |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 956 | function SetScriptSource(script, new_source, preview_only, change_log) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 957 | var old_source = script.source; |
fschneider@chromium.org | 013f3e1 | 2010-04-26 13:27:52 +0000 | [diff] [blame] | 958 | var diff = CompareStringsLinewise(old_source, new_source); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 959 | return ApplyPatchMultiChunk(script, diff, new_source, preview_only, |
| 960 | change_log); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 961 | } |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 962 | // Function is public. |
| 963 | this.SetScriptSource = SetScriptSource; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 964 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 965 | function CompareStringsLinewise(s1, s2) { |
| 966 | return %LiveEditCompareStringsLinewise(s1, s2); |
| 967 | } |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 968 | |
| 969 | // Applies the change to the script. |
| 970 | // The change is always a substring (change_pos, change_pos + change_len) |
| 971 | // being replaced with a completely different string new_str. |
| 972 | // This API is a legacy and is obsolete. |
| 973 | // |
| 974 | // @param {Script} script that is being changed |
| 975 | // @param {Array} change_log a list that collects engineer-readable |
| 976 | // description of what happened. |
| 977 | function ApplySingleChunkPatch(script, change_pos, change_len, new_str, |
| 978 | change_log) { |
| 979 | var old_source = script.source; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 980 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 981 | // Prepare new source string. |
| 982 | var new_source = old_source.substring(0, change_pos) + |
| 983 | new_str + old_source.substring(change_pos + change_len); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 984 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 985 | return ApplyPatchMultiChunk(script, |
| 986 | [ change_pos, change_pos + change_len, change_pos + new_str.length], |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 987 | new_source, false, change_log); |
| 988 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 989 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 990 | // Creates JSON description for a change tree. |
| 991 | function DescribeChangeTree(old_code_tree) { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 992 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 993 | function ProcessOldNode(node) { |
| 994 | var child_infos = []; |
| 995 | for (var i = 0; i < node.children.length; i++) { |
| 996 | var child = node.children[i]; |
| 997 | if (child.status != FunctionStatus.UNCHANGED) { |
| 998 | child_infos.push(ProcessOldNode(child)); |
| 999 | } |
| 1000 | } |
| 1001 | var new_child_infos = []; |
| 1002 | if (node.textually_unmatched_new_nodes) { |
| 1003 | for (var i = 0; i < node.textually_unmatched_new_nodes.length; i++) { |
| 1004 | var child = node.textually_unmatched_new_nodes[i]; |
| 1005 | new_child_infos.push(ProcessNewNode(child)); |
| 1006 | } |
| 1007 | } |
| 1008 | var res = { |
| 1009 | name: node.info.function_name, |
| 1010 | positions: DescribePositions(node), |
| 1011 | status: node.status, |
| 1012 | children: child_infos, |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1013 | new_children: new_child_infos |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1014 | }; |
| 1015 | if (node.status_explanation) { |
| 1016 | res.status_explanation = node.status_explanation; |
| 1017 | } |
| 1018 | if (node.textual_corresponding_node) { |
| 1019 | res.new_positions = DescribePositions(node.textual_corresponding_node); |
| 1020 | } |
| 1021 | return res; |
| 1022 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1023 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1024 | function ProcessNewNode(node) { |
| 1025 | var child_infos = []; |
| 1026 | // Do not list ancestors. |
| 1027 | if (false) { |
| 1028 | for (var i = 0; i < node.children.length; i++) { |
| 1029 | child_infos.push(ProcessNewNode(node.children[i])); |
| 1030 | } |
| 1031 | } |
| 1032 | var res = { |
| 1033 | name: node.info.function_name, |
| 1034 | positions: DescribePositions(node), |
| 1035 | children: child_infos, |
| 1036 | }; |
| 1037 | return res; |
| 1038 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1039 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1040 | function DescribePositions(node) { |
| 1041 | return { |
| 1042 | start_position: node.info.start_position, |
| 1043 | end_position: node.info.end_position |
| 1044 | }; |
| 1045 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1046 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 1047 | return ProcessOldNode(old_code_tree); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1048 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1049 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1050 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 1051 | // Functions are public for tests. |
| 1052 | this.TestApi = { |
| 1053 | PosTranslator: PosTranslator, |
| 1054 | CompareStringsLinewise: CompareStringsLinewise, |
| 1055 | ApplySingleChunkPatch: ApplySingleChunkPatch |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 1056 | } |
| 1057 | } |