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