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