blob: eaa23834c964d22737de4b954a4142edd53a9cc9 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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 Block6ded16b2010-05-10 14:33:55 +01004
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 Bernierd0a1eb72015-03-24 16:35:39 -040022
23"use strict";
24
Steve Block6ded16b2010-05-10 14:33:55 +010025Debug.LiveEdit = new function() {
26
27 // Forward declaration for minifier.
28 var FunctionStatus;
Ben Murdochf87a2032010-10-22 12:50:53 +010029
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030 var NEEDS_STEP_IN_PROPERTY_NAME = "stack_update_needs_step_in";
31
Steve Block6ded16b2010-05-10 14:33:55 +010032 // 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 Block8defd9f2010-07-08 12:39:36 +010035 function ApplyPatchMultiChunk(script, diff_array, new_source, preview_only,
36 change_log) {
Steve Block6ded16b2010-05-10 14:33:55 +010037
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 Murdochf87a2032010-10-22 12:50:53 +010042
Steve Block6ded16b2010-05-10 14:33:55 +010043 // 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 Murdochf87a2032010-10-22 12:50:53 +010053
Steve Block6ded16b2010-05-10 14:33:55 +010054 // 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 Murdochb8a8cc12014-11-26 15:28:44 +000059 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 Block6ded16b2010-05-10 14:33:55 +010070 }
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 Murdochf87a2032010-10-22 12:50:53 +010075
Steve Block6ded16b2010-05-10 14:33:55 +010076 // 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 Block8defd9f2010-07-08 12:39:36 +010091 // business and should have link to the actual script updated.
Steve Block6ded16b2010-05-10 14:33:55 +010092 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 Murdochf87a2032010-10-22 12:50:53 +010098
Steve Block6ded16b2010-05-10 14:33:55 +010099 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 Block8defd9f2010-07-08 12:39:36 +0100116 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 Murdochf87a2032010-10-22 12:50:53 +0100125
Steve Block8defd9f2010-07-08 12:39:36 +0100126 if (preview_only) {
127 return preview_description;
128 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100129
Steve Block6ded16b2010-05-10 14:33:55 +0100130 HarvestTodo(root_old_node);
Ben Murdochf87a2032010-10-22 12:50:53 +0100131
Steve Block6ded16b2010-05-10 14:33:55 +0100132 // 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 Murdochb0fe1622011-05-05 13:52:32 +0100135 var live_shared_function_infos =
136 replace_code_list[i].live_shared_function_infos;
137
138 if (live_shared_function_infos) {
Steve Block9fac8402011-05-12 15:51:54 +0100139 for (var j = 0; j < live_shared_function_infos.length; j++) {
140 replaced_function_infos.push(live_shared_function_infos[j]);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100141 }
Steve Block6ded16b2010-05-10 14:33:55 +0100142 }
143 }
144
Steve Block6ded16b2010-05-10 14:33:55 +0100145 // We haven't changed anything before this line yet.
146 // Committing all changes.
Ben Murdochf87a2032010-10-22 12:50:53 +0100147
Steve Block8defd9f2010-07-08 12:39:36 +0100148 // 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 Murdochf87a2032010-10-22 12:50:53 +0100151
152 preview_description.stack_modified = dropped_functions_number != 0;
153
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154 // 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 Murdochf87a2032010-10-22 12:50:53 +0100159 // Start with breakpoints. Convert their line/column positions and
Steve Block6ded16b2010-05-10 14:33:55 +0100160 // 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 Murdochb8a8cc12014-11-26 15:28:44 +0000169 old_script = UNDEFINED;
Steve Block6ded16b2010-05-10 14:33:55 +0100170 } else {
171 var old_script_name = CreateNameForOldScript(script);
Ben Murdochf87a2032010-10-22 12:50:53 +0100172
Steve Block6ded16b2010-05-10 14:33:55 +0100173 // 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 Murdochf87a2032010-10-22 12:50:53 +0100177
Steve Block6ded16b2010-05-10 14:33:55 +0100178 var link_to_old_script_report = new Array();
179 change_log.push( { linked_to_old_script: link_to_old_script_report } );
Ben Murdochf87a2032010-10-22 12:50:53 +0100180
Steve Block6ded16b2010-05-10 14:33:55 +0100181 // 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 Murdochf87a2032010-10-22 12:50:53 +0100186
Steve Block8defd9f2010-07-08 12:39:36 +0100187 preview_description.created_script_name = old_script_name;
Steve Block6ded16b2010-05-10 14:33:55 +0100188 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100189
Steve Block6ded16b2010-05-10 14:33:55 +0100190 // 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 Murdochf87a2032010-10-22 12:50:53 +0100199
Steve Block6ded16b2010-05-10 14:33:55 +0100200 var position_patch_report = new Array();
201 change_log.push( {position_patched: position_patch_report} );
Ben Murdochf87a2032010-10-22 12:50:53 +0100202
Steve Block6ded16b2010-05-10 14:33:55 +0100203 for (var i = 0; i < update_positions_list.length; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000204 // TODO(LiveEdit): take into account whether it's source_changed or
Steve Block6ded16b2010-05-10 14:33:55 +0100205 // unchanged and whether positions changed at all.
206 PatchPositions(update_positions_list[i], diff_array,
207 position_patch_report);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100208
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 Block6ded16b2010-05-10 14:33:55 +0100215 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100216
Steve Block6ded16b2010-05-10 14:33:55 +0100217 break_points_restorer(pos_translator, old_script);
Ben Murdochf87a2032010-10-22 12:50:53 +0100218
Steve Block8defd9f2010-07-08 12:39:36 +0100219 preview_description.updated = true;
220 return preview_description;
Steve Block6ded16b2010-05-10 14:33:55 +0100221 }
222 // Function is public.
223 this.ApplyPatchMultiChunk = ApplyPatchMultiChunk;
224
Ben Murdochf87a2032010-10-22 12:50:53 +0100225
Steve Block6ded16b2010-05-10 14:33:55 +0100226 // 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 Murdochf87a2032010-10-22 12:50:53 +0100234 // we have to manually erase all links right after compile.
Steve Block6ded16b2010-05-10 14:33:55 +0100235 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 Murdochb8a8cc12014-11-26 15:28:44 +0000249 %LiveEditFunctionSetScript(info.shared_function_info, UNDEFINED);
Steve Block6ded16b2010-05-10 14:33:55 +0100250 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 Murdochb8a8cc12014-11-26 15:28:44 +0000271 // After sorting update outer_index field using old_index_map. Also
Steve Block6ded16b2010-05-10 14:33:55 +0100272 // 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 Murdochf87a2032010-10-22 12:50:53 +0100301
Steve Block6ded16b2010-05-10 14:33:55 +0100302 // Replaces function's Code.
303 function PatchFunctionCode(old_node, change_log) {
304 var new_info = old_node.corresponding_node.info;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100305 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 Block6ded16b2010-05-10 14:33:55 +0100309
Ben Murdochb0fe1622011-05-05 13:52:32 +0100310 // 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 Murdoch3ef787d2012-04-12 10:51:47 +0100325 %LiveEditReplaceRefToNestedFunction(
326 old_info.info,
327 corresponding_child_info,
328 old_child_info.info);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100329 });
330 }
Steve Block6ded16b2010-05-10 14:33:55 +0100331 }
332 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100333 });
Ben Murdochf87a2032010-10-22 12:50:53 +0100334
Steve Block6ded16b2010-05-10 14:33:55 +0100335 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 Murdochf87a2032010-10-22 12:50:53 +0100342
Steve Block6ded16b2010-05-10 14:33:55 +0100343 // 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 Murdochb0fe1622011-05-05 13:52:32 +0100347 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 Block6ded16b2010-05-10 14:33:55 +0100354 } else {
355 report_array.push(
356 { name: old_info_node.info.function_name, not_found: true } );
357 }
358 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100359
Steve Block6ded16b2010-05-10 14:33:55 +0100360
361 // Returns function that restores breakpoints.
362 function TemporaryRemoveBreakPoints(original_script, change_log) {
363 var script_break_points = GetScriptBreakPoints(original_script);
Ben Murdochf87a2032010-10-22 12:50:53 +0100364
Steve Block6ded16b2010-05-10 14:33:55 +0100365 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 Murdochf87a2032010-10-22 12:50:53 +0100373
374 // TODO(LiveEdit): be careful with resource offset here.
Steve Block6ded16b2010-05-10 14:33:55 +0100375 var break_point_position = Debug.findScriptSourcePosition(original_script,
376 break_point.line(), break_point.column());
Ben Murdochf87a2032010-10-22 12:50:53 +0100377
Steve Block6ded16b2010-05-10 14:33:55 +0100378 var old_position_description = {
379 position: break_point_position,
380 line: break_point.line(),
381 column: break_point.column()
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100382 };
Steve Block6ded16b2010-05-10 14:33:55 +0100383 break_point_old_positions.push(old_position_description);
384 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100385
386
Steve Block6ded16b2010-05-10 14:33:55 +0100387 // 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 Murdochf87a2032010-10-22 12:50:53 +0100397
Steve Block6ded16b2010-05-10 14:33:55 +0100398 break_points_update_report.push( {
399 type: "copied_to_old",
400 id: break_point.number(),
Ben Murdochf87a2032010-10-22 12:50:53 +0100401 new_id: clone.number(),
Steve Block6ded16b2010-05-10 14:33:55 +0100402 positions: break_point_old_positions[i]
403 } );
404 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100405
Steve Block6ded16b2010-05-10 14:33:55 +0100406 var updated_position = pos_translator.Translate(
407 break_point_old_positions[i].position,
408 PosTranslator.ShiftWithTopInsideChunkHandler);
Ben Murdochf87a2032010-10-22 12:50:53 +0100409
Steve Block6ded16b2010-05-10 14:33:55 +0100410 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 Murdoch3ef787d2012-04-12 10:51:47 +0100419 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100420
Steve Block6ded16b2010-05-10 14:33:55 +0100421 break_point.set(original_script);
Ben Murdochf87a2032010-10-22 12:50:53 +0100422
Steve Block6ded16b2010-05-10 14:33:55 +0100423 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 Murdoch3ef787d2012-04-12 10:51:47 +0100429 };
Steve Block6ded16b2010-05-10 14:33:55 +0100430 }
431
Ben Murdochf87a2032010-10-22 12:50:53 +0100432
Steve Block6ded16b2010-05-10 14:33:55 +0100433 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 Murdochf87a2032010-10-22 12:50:53 +0100449
Steve Block6ded16b2010-05-10 14:33:55 +0100450 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 Murdochf87a2032010-10-22 12:50:53 +0100460 current_diff = pos2_end - pos1_end;
Steve Block6ded16b2010-05-10 14:33:55 +0100461 }
462 this.chunks = chunks;
463 }
464 PosTranslator.prototype.GetChunks = function() {
465 return this.chunks;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100466 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100467
Steve Block6ded16b2010-05-10 14:33:55 +0100468 PosTranslator.prototype.Translate = function(pos, inside_chunk_handler) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100469 var array = this.chunks;
Steve Block6ded16b2010-05-10 14:33:55 +0100470 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 Murdochf87a2032010-10-22 12:50:53 +0100486 return pos + chunk.pos2 + chunk.len2 - chunk.pos1 - chunk.len1;
Steve Block6ded16b2010-05-10 14:33:55 +0100487 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100488
Steve Block6ded16b2010-05-10 14:33:55 +0100489 if (!inside_chunk_handler) {
490 inside_chunk_handler = PosTranslator.DefaultInsideChunkHandler;
491 }
492 return inside_chunk_handler(pos, chunk);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100493 };
Steve Block6ded16b2010-05-10 14:33:55 +0100494
495 PosTranslator.DefaultInsideChunkHandler = function(pos, diff_chunk) {
496 Assert(false, "Cannot translate position in changed area");
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100497 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100498
Steve Block6ded16b2010-05-10 14:33:55 +0100499 PosTranslator.ShiftWithTopInsideChunkHandler =
500 function(pos, diff_chunk) {
501 // We carelessly do not check whether we stay inside the chunk after
502 // translation.
Ben Murdochf87a2032010-10-22 12:50:53 +0100503 return pos - diff_chunk.pos1 + diff_chunk.pos2;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100504 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100505
Steve Block6ded16b2010-05-10 14:33:55 +0100506 var FunctionStatus = {
507 // No change to function or its inner functions; however its positions
Ben Murdochf87a2032010-10-22 12:50:53 +0100508 // in script may have been shifted.
Steve Block6ded16b2010-05-10 14:33:55 +0100509 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 Murdoch3ef787d2012-04-12 10:51:47 +0100518 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100519
Steve Block6ded16b2010-05-10 14:33:55 +0100520 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 Murdochf87a2032010-10-22 12:50:53 +0100524 this.array_index = array_index;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000525 this.parent = UNDEFINED;
Ben Murdochf87a2032010-10-22 12:50:53 +0100526
Steve Block6ded16b2010-05-10 14:33:55 +0100527 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 Murdochb8a8cc12014-11-26 15:28:44 +0000530 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 Murdochf87a2032010-10-22 12:50:53 +0100535
Steve Block8defd9f2010-07-08 12:39:36 +0100536 // '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 Murdochf87a2032010-10-22 12:50:53 +0100541 // as an old function deleted and new function created.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000542 this.textual_corresponding_node = UNDEFINED;
543 this.textually_unmatched_new_nodes = UNDEFINED;
Ben Murdochf87a2032010-10-22 12:50:53 +0100544
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000545 this.live_shared_function_infos = UNDEFINED;
Steve Block6ded16b2010-05-10 14:33:55 +0100546 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100547
Steve Block6ded16b2010-05-10 14:33:55 +0100548 // 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 Murdochf87a2032010-10-22 12:50:53 +0100554 // Recursive function that builds a branch of tree.
Steve Block6ded16b2010-05-10 14:33:55 +0100555 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 Murdochf87a2032010-10-22 12:50:53 +0100570
Steve Block6ded16b2010-05-10 14:33:55 +0100571 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 Murdoch3ef787d2012-04-12 10:51:47 +0100581 // A convenient iterator over diff chunks that also translates
Steve Block6ded16b2010-05-10 14:33:55 +0100582 // 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 Murdoch3ef787d2012-04-12 10:51:47 +0100586 this.current = function() { return chunks[chunk_index]; };
Steve Block6ded16b2010-05-10 14:33:55 +0100587 this.next = function() {
588 var chunk = chunks[chunk_index];
Ben Murdochf87a2032010-10-22 12:50:53 +0100589 pos_diff = chunk.pos2 + chunk.len2 - (chunk.pos1 + chunk.len1);
Steve Block6ded16b2010-05-10 14:33:55 +0100590 chunk_index++;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100591 };
592 this.done = function() { return chunk_index >= chunks.length; };
593 this.TranslatePos = function(pos) { return pos + pos_diff; };
Steve Block6ded16b2010-05-10 14:33:55 +0100594 };
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 Murdochf87a2032010-10-22 12:50:53 +0100601 info_node.info.start_position);
Steve Block6ded16b2010-05-10 14:33:55 +0100602 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 Murdochf87a2032010-10-22 12:50:53 +0100610
Steve Block6ded16b2010-05-10 14:33:55 +0100611 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 Murdochf87a2032010-10-22 12:50:53 +0100639 if (chunk_it.current().pos1 + chunk_it.current().len1 <=
Steve Block6ded16b2010-05-10 14:33:55 +0100640 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 Murdochf87a2032010-10-22 12:50:53 +0100664 chunk_it.TranslatePos(info_node.info.end_position);
Steve Block6ded16b2010-05-10 14:33:55 +0100665 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100666
Steve Block6ded16b2010-05-10 14:33:55 +0100667 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 Murdochf87a2032010-10-22 12:50:53 +0100671
Steve Block6ded16b2010-05-10 14:33:55 +0100672 ProcessInternals(code_info_tree);
673 }
674
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000675 // For each old function (if it is not damaged) tries to find a corresponding
Steve Block6ded16b2010-05-10 14:33:55 +0100676 // 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 Murdochb8a8cc12014-11-26 15:28:44 +0000678 // reasons for correspondence not to be found; function with unmodified text
Steve Block6ded16b2010-05-10 14:33:55 +0100679 // 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 Murdochb8a8cc12014-11-26 15:28:44 +0000686 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 Block6ded16b2010-05-10 14:33:55 +0100693 var old_children = old_node.children;
694 var new_children = new_node.children;
Ben Murdochf87a2032010-10-22 12:50:53 +0100695
Steve Block6ded16b2010-05-10 14:33:55 +0100696 var unmatched_new_nodes_list = [];
Steve Block8defd9f2010-07-08 12:39:36 +0100697 var textually_unmatched_new_nodes_list = [];
Steve Block6ded16b2010-05-10 14:33:55 +0100698
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 Block8defd9f2010-07-08 12:39:36 +0100708 textually_unmatched_new_nodes_list.push(new_children[new_index]);
Steve Block6ded16b2010-05-10 14:33:55 +0100709 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 Block8defd9f2010-07-08 12:39:36 +0100716 old_children[old_index].textual_corresponding_node =
717 new_children[new_index];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000718 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 Block6ded16b2010-05-10 14:33:55 +0100727 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 Murdochb8a8cc12014-11-26 15:28:44 +0000731 old_children[old_index].corresponding_node = UNDEFINED;
Steve Block6ded16b2010-05-10 14:33:55 +0100732 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 Block8defd9f2010-07-08 12:39:36 +0100741 textually_unmatched_new_nodes_list.push(new_children[new_index]);
Steve Block6ded16b2010-05-10 14:33:55 +0100742 }
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 Murdochf87a2032010-10-22 12:50:53 +0100760
Steve Block6ded16b2010-05-10 14:33:55 +0100761 while (new_index < new_children.length) {
762 unmatched_new_nodes_list.push(new_children[new_index]);
Steve Block8defd9f2010-07-08 12:39:36 +0100763 textually_unmatched_new_nodes_list.push(new_children[new_index]);
Steve Block6ded16b2010-05-10 14:33:55 +0100764 new_index++;
765 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100766
Steve Block6ded16b2010-05-10 14:33:55 +0100767 if (old_node.status == FunctionStatus.CHANGED) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000768 if (old_node.info.param_num != new_node.info.param_num) {
Steve Block6ded16b2010-05-10 14:33:55 +0100769 old_node.status = FunctionStatus.DAMAGED;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000770 old_node.status_explanation = "Changed parameter number: " +
771 old_node.info.param_num + " and " + new_node.info.param_num;
Steve Block6ded16b2010-05-10 14:33:55 +0100772 }
773 }
774 old_node.unmatched_new_nodes = unmatched_new_nodes_list;
Steve Block8defd9f2010-07-08 12:39:36 +0100775 old_node.textually_unmatched_new_nodes =
776 textually_unmatched_new_nodes_list;
Steve Block6ded16b2010-05-10 14:33:55 +0100777 }
778
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000779 ProcessNode(old_code_tree, new_code_tree);
Ben Murdochf87a2032010-10-22 12:50:53 +0100780
Steve Block6ded16b2010-05-10 14:33:55 +0100781 old_code_tree.corresponding_node = new_code_tree;
Steve Block8defd9f2010-07-08 12:39:36 +0100782 old_code_tree.textual_corresponding_node = new_code_tree;
783
Steve Block6ded16b2010-05-10 14:33:55 +0100784 Assert(old_code_tree.status != FunctionStatus.DAMAGED,
785 "Script became damaged");
786 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100787
Steve Block6ded16b2010-05-10 14:33:55 +0100788 function FindLiveSharedInfos(old_code_tree, script) {
789 var shared_raw_list = %LiveEditFindSharedFunctionInfosForScript(script);
Ben Murdochf87a2032010-10-22 12:50:53 +0100790
Steve Block6ded16b2010-05-10 14:33:55 +0100791 var shared_infos = new Array();
Ben Murdochf87a2032010-10-22 12:50:53 +0100792
Steve Block6ded16b2010-05-10 14:33:55 +0100793 for (var i = 0; i < shared_raw_list.length; i++) {
794 shared_infos.push(new SharedInfoWrapper(shared_raw_list[i]));
795 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100796
Ben Murdochb0fe1622011-05-05 13:52:32 +0100797 // Finds all SharedFunctionInfos that corresponds to compile info
Steve Block6ded16b2010-05-10 14:33:55 +0100798 // in old version of the script.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100799 function FindFunctionInfos(compile_info) {
800 var wrappers = [];
801
Steve Block6ded16b2010-05-10 14:33:55 +0100802 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 Murdochb0fe1622011-05-05 13:52:32 +0100806 wrappers.push(wrapper);
Steve Block6ded16b2010-05-10 14:33:55 +0100807 }
808 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100809
810 if (wrappers.length > 0) {
811 return wrappers;
812 }
Steve Block6ded16b2010-05-10 14:33:55 +0100813 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100814
Steve Block6ded16b2010-05-10 14:33:55 +0100815 function TraverseTree(node) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100816 node.live_shared_function_infos = FindFunctionInfos(node.info);
817
Steve Block6ded16b2010-05-10 14:33:55 +0100818 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 Murdochf87a2032010-10-22 12:50:53 +0100826
Steve Block6ded16b2010-05-10 14:33:55 +0100827 // 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 Merrick75681382010-08-19 15:07:18 +0100835 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 Block6ded16b2010-05-10 14:33:55 +0100839 this.next_sibling_index = null;
840 this.raw_array = raw_array;
841 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100842
Steve Block6ded16b2010-05-10 14:33:55 +0100843 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 Murdochb8a8cc12014-11-26 15:28:44 +0000851 // Changes positions (including all statements) in function.
Steve Block6ded16b2010-05-10 14:33:55 +0100852 function PatchPositions(old_info_node, diff_array, report_array) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100853 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 Block6ded16b2010-05-10 14:33:55 +0100861 // TODO(LiveEdit): function is not compiled yet or is already collected.
Ben Murdochf87a2032010-10-22 12:50:53 +0100862 report_array.push(
Steve Block6ded16b2010-05-10 14:33:55 +0100863 { name: old_info_node.info.function_name, info_not_found: true } );
Steve Block6ded16b2010-05-10 14:33:55 +0100864 }
Steve Block6ded16b2010-05-10 14:33:55 +0100865 }
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 Murdochf87a2032010-10-22 12:50:53 +0100872
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000873 // Compares a function scope heap structure, old and new version, whether it
Steve Block8defd9f2010-07-08 12:39:36 +0100874 // changed or not. Returns explanation if they differ.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000875 function IsFunctionContextLocalsChanged(function_info1, function_info2) {
Steve Block6ded16b2010-05-10 14:33:55 +0100876 var scope_info1 = function_info1.scope_info;
877 var scope_info2 = function_info2.scope_info;
Steve Block8defd9f2010-07-08 12:39:36 +0100878
Ben Murdochf87a2032010-10-22 12:50:53 +0100879 var scope_info1_text;
880 var scope_info2_text;
881
Steve Block8defd9f2010-07-08 12:39:36 +0100882 if (scope_info1) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100883 scope_info1_text = scope_info1.toString();
Steve Block8defd9f2010-07-08 12:39:36 +0100884 } else {
885 scope_info1_text = "";
Steve Block6ded16b2010-05-10 14:33:55 +0100886 }
Steve Block8defd9f2010-07-08 12:39:36 +0100887 if (scope_info2) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100888 scope_info2_text = scope_info2.toString();
Steve Block8defd9f2010-07-08 12:39:36 +0100889 } else {
890 scope_info2_text = "";
Steve Block6ded16b2010-05-10 14:33:55 +0100891 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100892
Steve Block8defd9f2010-07-08 12:39:36 +0100893 if (scope_info1_text != scope_info2_text) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000894 return "Variable map changed: [" + scope_info1_text +
895 "] => [" + scope_info2_text + "]";
Steve Block8defd9f2010-07-08 12:39:36 +0100896 }
897 // No differences. Return undefined.
898 return;
Steve Block6ded16b2010-05-10 14:33:55 +0100899 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100900
Steve Block6ded16b2010-05-10 14:33:55 +0100901 // Minifier forward declaration.
902 var FunctionPatchabilityStatus;
Ben Murdochf87a2032010-10-22 12:50:53 +0100903
Steve Block6ded16b2010-05-10 14:33:55 +0100904 // 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 Murdochf87a2032010-10-22 12:50:53 +0100917
Steve Block6ded16b2010-05-10 14:33:55 +0100918 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 Murdochf87a2032010-10-22 12:50:53 +0100927 start_pos: shared.start_position,
Steve Block6ded16b2010-05-10 14:33:55 +0100928 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 Murdochf87a2032010-10-22 12:50:53 +0100942
Steve Block8defd9f2010-07-08 12:39:36 +0100943 return dropped.length;
Steve Block6ded16b2010-05-10 14:33:55 +0100944 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100945
Steve Block6ded16b2010-05-10 14:33:55 +0100946 // 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 Murdochb8a8cc12014-11-26 15:28:44 +0000952 REPLACED_ON_ACTIVE_STACK: 5,
953 BLOCKED_UNDER_GENERATOR: 6,
954 BLOCKED_ACTIVE_GENERATOR: 7
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100955 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100956
Steve Block6ded16b2010-05-10 14:33:55 +0100957 FunctionPatchabilityStatus.SymbolName = function(code) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100958 var enumeration = FunctionPatchabilityStatus;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400959 for (var name in enumeration) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100960 if (enumeration[name] == code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100961 return name;
962 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100963 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100964 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100965
966
Steve Block6ded16b2010-05-10 14:33:55 +0100967 // 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 Murdochf87a2032010-10-22 12:50:53 +0100974
Steve Block6ded16b2010-05-10 14:33:55 +0100975 Failure.prototype.toString = function() {
976 return "LiveEdit Failure: " + this.message;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100977 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100978
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000979 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 Block6ded16b2010-05-10 14:33:55 +01001004 // 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 Block8defd9f2010-07-08 12:39:36 +01001012 function SetScriptSource(script, new_source, preview_only, change_log) {
Steve Block6ded16b2010-05-10 14:33:55 +01001013 var old_source = script.source;
Ben Murdochb8e0da22011-05-16 14:20:40 +01001014 var diff = CompareStrings(old_source, new_source);
Steve Block8defd9f2010-07-08 12:39:36 +01001015 return ApplyPatchMultiChunk(script, diff, new_source, preview_only,
1016 change_log);
Steve Block6ded16b2010-05-10 14:33:55 +01001017 }
1018 // Function is public.
1019 this.SetScriptSource = SetScriptSource;
Ben Murdochf87a2032010-10-22 12:50:53 +01001020
Ben Murdochb8e0da22011-05-16 14:20:40 +01001021 function CompareStrings(s1, s2) {
1022 return %LiveEditCompareStrings(s1, s2);
Steve Block6ded16b2010-05-10 14:33:55 +01001023 }
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 Murdochf87a2032010-10-22 12:50:53 +01001036
Steve Block6ded16b2010-05-10 14:33:55 +01001037 // 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 Murdochf87a2032010-10-22 12:50:53 +01001040
Steve Block6ded16b2010-05-10 14:33:55 +01001041 return ApplyPatchMultiChunk(script,
1042 [ change_pos, change_pos + change_len, change_pos + new_str.length],
Steve Block8defd9f2010-07-08 12:39:36 +01001043 new_source, false, change_log);
1044 }
Ben Murdochf87a2032010-10-22 12:50:53 +01001045
Steve Block8defd9f2010-07-08 12:39:36 +01001046 // Creates JSON description for a change tree.
1047 function DescribeChangeTree(old_code_tree) {
Ben Murdochf87a2032010-10-22 12:50:53 +01001048
Steve Block8defd9f2010-07-08 12:39:36 +01001049 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 Murdochf87a2032010-10-22 12:50:53 +01001069 new_children: new_child_infos
Steve Block8defd9f2010-07-08 12:39:36 +01001070 };
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 Murdochf87a2032010-10-22 12:50:53 +01001079
Steve Block8defd9f2010-07-08 12:39:36 +01001080 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 Murdochf87a2032010-10-22 12:50:53 +01001095
Steve Block8defd9f2010-07-08 12:39:36 +01001096 function DescribePositions(node) {
1097 return {
1098 start_position: node.info.start_position,
1099 end_position: node.info.end_position
1100 };
1101 }
Ben Murdochf87a2032010-10-22 12:50:53 +01001102
Steve Block8defd9f2010-07-08 12:39:36 +01001103 return ProcessOldNode(old_code_tree);
Steve Block6ded16b2010-05-10 14:33:55 +01001104 }
1105
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001106 // 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 Murdochf87a2032010-10-22 12:50:53 +01001118
Steve Block6ded16b2010-05-10 14:33:55 +01001119 // Functions are public for tests.
1120 this.TestApi = {
1121 PosTranslator: PosTranslator,
Ben Murdochb8e0da22011-05-16 14:20:40 +01001122 CompareStrings: CompareStrings,
Steve Block6ded16b2010-05-10 14:33:55 +01001123 ApplySingleChunkPatch: ApplySingleChunkPatch
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001124 };
1125};