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.
5 // LiveEdit feature implementation. The script should be executed after
8 // A LiveEdit namespace. It contains functions that modifies JavaScript code
9 // according to changes of script source (if possible).
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.
21 // LiveEdit namespace is declared inside a single function constructor.
22 Debug.LiveEdit = new function() {
24 // Forward declaration for minifier.
27 var NEEDS_STEP_IN_PROPERTY_NAME = "stack_update_needs_step_in";
29 // Applies the change to the script.
30 // The change is in form of list of chunks encoded in a single array as
31 // a series of triplets (pos1_start, pos1_end, pos2_end)
32 function ApplyPatchMultiChunk(script, diff_array, new_source, preview_only,
35 var old_source = script.source;
37 // Gather compile information about old version of script.
38 var old_compile_info = GatherCompileInfo(old_source, script);
40 // Build tree structures for old and new versions of the script.
41 var root_old_node = BuildCodeInfoTree(old_compile_info);
43 var pos_translator = new PosTranslator(diff_array);
46 MarkChangedFunctions(root_old_node, pos_translator.GetChunks());
48 // Find all SharedFunctionInfo's that were compiled from this script.
49 FindLiveSharedInfos(root_old_node, script);
51 // Gather compile information about new version of script.
54 new_compile_info = GatherCompileInfo(new_source, script);
57 new Failure("Failed to compile new version of script: " + e);
58 if (e instanceof SyntaxError) {
60 type: "liveedit_compile_error",
61 syntaxErrorMessage: e.message
63 CopyErrorPositionToDetails(e, details);
64 failure.details = details;
68 var root_new_node = BuildCodeInfoTree(new_compile_info);
70 // Link recompiled script data with other data.
71 FindCorrespondingFunctions(root_old_node, root_new_node);
73 // Prepare to-do lists.
74 var replace_code_list = new Array();
75 var link_to_old_script_list = new Array();
76 var link_to_original_script_list = new Array();
77 var update_positions_list = new Array();
79 function HarvestTodo(old_node) {
80 function CollectDamaged(node) {
81 link_to_old_script_list.push(node);
82 for (var i = 0; i < node.children.length; i++) {
83 CollectDamaged(node.children[i]);
87 // Recursively collects all newly compiled functions that are going into
88 // business and should have link to the actual script updated.
89 function CollectNew(node_list) {
90 for (var i = 0; i < node_list.length; i++) {
91 link_to_original_script_list.push(node_list[i]);
92 CollectNew(node_list[i].children);
96 if (old_node.status == FunctionStatus.DAMAGED) {
97 CollectDamaged(old_node);
100 if (old_node.status == FunctionStatus.UNCHANGED) {
101 update_positions_list.push(old_node);
102 } else if (old_node.status == FunctionStatus.SOURCE_CHANGED) {
103 update_positions_list.push(old_node);
104 } else if (old_node.status == FunctionStatus.CHANGED) {
105 replace_code_list.push(old_node);
106 CollectNew(old_node.unmatched_new_nodes);
108 for (var i = 0; i < old_node.children.length; i++) {
109 HarvestTodo(old_node.children[i]);
113 var preview_description = {
114 change_tree: DescribeChangeTree(root_old_node),
116 old_len: old_source.length,
117 new_len: new_source.length,
124 return preview_description;
127 HarvestTodo(root_old_node);
129 // Collect shared infos for functions whose code need to be patched.
130 var replaced_function_infos = new Array();
131 for (var i = 0; i < replace_code_list.length; i++) {
132 var live_shared_function_infos =
133 replace_code_list[i].live_shared_function_infos;
135 if (live_shared_function_infos) {
136 for (var j = 0; j < live_shared_function_infos.length; j++) {
137 replaced_function_infos.push(live_shared_function_infos[j]);
142 // We haven't changed anything before this line yet.
143 // Committing all changes.
145 // Check that function being patched is not currently on stack or drop them.
146 var dropped_functions_number =
147 CheckStackActivations(replaced_function_infos, change_log);
149 preview_description.stack_modified = dropped_functions_number != 0;
151 // Our current implementation requires client to manually issue "step in"
152 // command for correct stack state.
153 preview_description[NEEDS_STEP_IN_PROPERTY_NAME] =
154 preview_description.stack_modified;
156 // Start with breakpoints. Convert their line/column positions and
158 var break_points_restorer = TemporaryRemoveBreakPoints(script, change_log);
162 // Create an old script only if there are function that should be linked
164 if (link_to_old_script_list.length == 0) {
165 %LiveEditReplaceScript(script, new_source, null);
166 old_script = UNDEFINED;
168 var old_script_name = CreateNameForOldScript(script);
170 // Update the script text and create a new script representing an old
171 // version of the script.
172 old_script = %LiveEditReplaceScript(script, new_source,
175 var link_to_old_script_report = new Array();
176 change_log.push( { linked_to_old_script: link_to_old_script_report } );
178 // We need to link to old script all former nested functions.
179 for (var i = 0; i < link_to_old_script_list.length; i++) {
180 LinkToOldScript(link_to_old_script_list[i], old_script,
181 link_to_old_script_report);
184 preview_description.created_script_name = old_script_name;
187 // Link to an actual script all the functions that we are going to use.
188 for (var i = 0; i < link_to_original_script_list.length; i++) {
189 %LiveEditFunctionSetScript(
190 link_to_original_script_list[i].info.shared_function_info, script);
193 for (var i = 0; i < replace_code_list.length; i++) {
194 PatchFunctionCode(replace_code_list[i], change_log);
197 var position_patch_report = new Array();
198 change_log.push( {position_patched: position_patch_report} );
200 for (var i = 0; i < update_positions_list.length; i++) {
201 // TODO(LiveEdit): take into account whether it's source_changed or
202 // unchanged and whether positions changed at all.
203 PatchPositions(update_positions_list[i], diff_array,
204 position_patch_report);
206 if (update_positions_list[i].live_shared_function_infos) {
207 update_positions_list[i].live_shared_function_infos.
208 forEach(function (info) {
209 %LiveEditFunctionSourceUpdated(info.raw_array);
214 break_points_restorer(pos_translator, old_script);
216 preview_description.updated = true;
217 return preview_description;
219 // Function is public.
220 this.ApplyPatchMultiChunk = ApplyPatchMultiChunk;
223 // Fully compiles source string as a script. Returns Array of
224 // FunctionCompileInfo -- a descriptions of all functions of the script.
225 // Elements of array are ordered by start positions of functions (from top
226 // to bottom) in the source. Fields outer_index and next_sibling_index help
227 // to navigate the nesting structure of functions.
229 // All functions get compiled linked to script provided as parameter script.
230 // TODO(LiveEdit): consider not using actual scripts as script, because
231 // we have to manually erase all links right after compile.
232 function GatherCompileInfo(source, script) {
233 // Get function info, elements are partially sorted (it is a tree of
234 // nested functions serialized as parent followed by serialized children.
235 var raw_compile_info = %LiveEditGatherCompileInfo(script, source);
237 // Sort function infos by start position field.
238 var compile_info = new Array();
239 var old_index_map = new Array();
240 for (var i = 0; i < raw_compile_info.length; i++) {
241 var info = new FunctionCompileInfo(raw_compile_info[i]);
242 // Remove all links to the actual script. Breakpoints system and
243 // LiveEdit itself believe that any function in heap that points to a
244 // particular script is a regular function.
245 // For some functions we will restore this link later.
246 %LiveEditFunctionSetScript(info.shared_function_info, UNDEFINED);
247 compile_info.push(info);
248 old_index_map.push(i);
251 for (var i = 0; i < compile_info.length; i++) {
253 for (var j = i + 1; j < compile_info.length; j++) {
254 if (compile_info[k].start_position > compile_info[j].start_position) {
259 var temp_info = compile_info[k];
260 var temp_index = old_index_map[k];
261 compile_info[k] = compile_info[i];
262 old_index_map[k] = old_index_map[i];
263 compile_info[i] = temp_info;
264 old_index_map[i] = temp_index;
268 // After sorting update outer_index field using old_index_map. Also
269 // set next_sibling_index field.
270 var current_index = 0;
272 // The recursive function, that goes over all children of a particular
273 // node (i.e. function info).
274 function ResetIndexes(new_parent_index, old_parent_index) {
275 var previous_sibling = -1;
276 while (current_index < compile_info.length &&
277 compile_info[current_index].outer_index == old_parent_index) {
278 var saved_index = current_index;
279 compile_info[saved_index].outer_index = new_parent_index;
280 if (previous_sibling != -1) {
281 compile_info[previous_sibling].next_sibling_index = saved_index;
283 previous_sibling = saved_index;
285 ResetIndexes(saved_index, old_index_map[saved_index]);
287 if (previous_sibling != -1) {
288 compile_info[previous_sibling].next_sibling_index = -1;
292 ResetIndexes(-1, -1);
293 Assert(current_index == compile_info.length);
299 // Replaces function's Code.
300 function PatchFunctionCode(old_node, change_log) {
301 var new_info = old_node.corresponding_node.info;
302 if (old_node.live_shared_function_infos) {
303 old_node.live_shared_function_infos.forEach(function (old_info) {
304 %LiveEditReplaceFunctionCode(new_info.raw_array,
307 // The function got a new code. However, this new code brings all new
308 // instances of SharedFunctionInfo for nested functions. However,
309 // we want the original instances to be used wherever possible.
310 // (This is because old instances and new instances will be both
311 // linked to a script and breakpoints subsystem does not really
312 // expects this; neither does LiveEdit subsystem on next call).
313 for (var i = 0; i < old_node.children.length; i++) {
314 if (old_node.children[i].corresponding_node) {
315 var corresponding_child_info =
316 old_node.children[i].corresponding_node.info.
317 shared_function_info;
319 if (old_node.children[i].live_shared_function_infos) {
320 old_node.children[i].live_shared_function_infos.
321 forEach(function (old_child_info) {
322 %LiveEditReplaceRefToNestedFunction(
324 corresponding_child_info,
325 old_child_info.info);
332 change_log.push( {function_patched: new_info.function_name} );
334 change_log.push( {function_patched: new_info.function_name,
335 function_info_not_found: true} );
340 // Makes a function associated with another instance of a script (the
341 // one representing its old version). This way the function still
342 // may access its own text.
343 function LinkToOldScript(old_info_node, old_script, report_array) {
344 if (old_info_node.live_shared_function_infos) {
345 old_info_node.live_shared_function_infos.
346 forEach(function (info) {
347 %LiveEditFunctionSetScript(info.info, old_script);
350 report_array.push( { name: old_info_node.info.function_name } );
353 { name: old_info_node.info.function_name, not_found: true } );
358 // Returns function that restores breakpoints.
359 function TemporaryRemoveBreakPoints(original_script, change_log) {
360 var script_break_points = GetScriptBreakPoints(original_script);
362 var break_points_update_report = [];
363 change_log.push( { break_points_update: break_points_update_report } );
365 var break_point_old_positions = [];
366 for (var i = 0; i < script_break_points.length; i++) {
367 var break_point = script_break_points[i];
371 // TODO(LiveEdit): be careful with resource offset here.
372 var break_point_position = Debug.findScriptSourcePosition(original_script,
373 break_point.line(), break_point.column());
375 var old_position_description = {
376 position: break_point_position,
377 line: break_point.line(),
378 column: break_point.column()
380 break_point_old_positions.push(old_position_description);
384 // Restores breakpoints and creates their copies in the "old" copy of
386 return function (pos_translator, old_script_copy_opt) {
387 // Update breakpoints (change positions and restore them in old version
389 for (var i = 0; i < script_break_points.length; i++) {
390 var break_point = script_break_points[i];
391 if (old_script_copy_opt) {
392 var clone = break_point.cloneForOtherScript(old_script_copy_opt);
393 clone.set(old_script_copy_opt);
395 break_points_update_report.push( {
396 type: "copied_to_old",
397 id: break_point.number(),
398 new_id: clone.number(),
399 positions: break_point_old_positions[i]
403 var updated_position = pos_translator.Translate(
404 break_point_old_positions[i].position,
405 PosTranslator.ShiftWithTopInsideChunkHandler);
408 original_script.locationFromPosition(updated_position, false);
410 break_point.update_positions(new_location.line, new_location.column);
412 var new_position_description = {
413 position: updated_position,
414 line: new_location.line,
415 column: new_location.column
418 break_point.set(original_script);
420 break_points_update_report.push( { type: "position_changed",
421 id: break_point.number(),
422 old_positions: break_point_old_positions[i],
423 new_positions: new_position_description
430 function Assert(condition, message) {
433 throw "Assert " + message;
440 function DiffChunk(pos1, pos2, len1, len2) {
447 function PosTranslator(diff_array) {
448 var chunks = new Array();
449 var current_diff = 0;
450 for (var i = 0; i < diff_array.length; i += 3) {
451 var pos1_begin = diff_array[i];
452 var pos2_begin = pos1_begin + current_diff;
453 var pos1_end = diff_array[i + 1];
454 var pos2_end = diff_array[i + 2];
455 chunks.push(new DiffChunk(pos1_begin, pos2_begin, pos1_end - pos1_begin,
456 pos2_end - pos2_begin));
457 current_diff = pos2_end - pos1_end;
459 this.chunks = chunks;
461 PosTranslator.prototype.GetChunks = function() {
465 PosTranslator.prototype.Translate = function(pos, inside_chunk_handler) {
466 var array = this.chunks;
467 if (array.length == 0 || pos < array[0].pos1) {
470 var chunk_index1 = 0;
471 var chunk_index2 = array.length - 1;
473 while (chunk_index1 < chunk_index2) {
474 var middle_index = Math.floor((chunk_index1 + chunk_index2) / 2);
475 if (pos < array[middle_index + 1].pos1) {
476 chunk_index2 = middle_index;
478 chunk_index1 = middle_index + 1;
481 var chunk = array[chunk_index1];
482 if (pos >= chunk.pos1 + chunk.len1) {
483 return pos + chunk.pos2 + chunk.len2 - chunk.pos1 - chunk.len1;
486 if (!inside_chunk_handler) {
487 inside_chunk_handler = PosTranslator.DefaultInsideChunkHandler;
489 return inside_chunk_handler(pos, chunk);
492 PosTranslator.DefaultInsideChunkHandler = function(pos, diff_chunk) {
493 Assert(false, "Cannot translate position in changed area");
496 PosTranslator.ShiftWithTopInsideChunkHandler =
497 function(pos, diff_chunk) {
498 // We carelessly do not check whether we stay inside the chunk after
500 return pos - diff_chunk.pos1 + diff_chunk.pos2;
503 var FunctionStatus = {
504 // No change to function or its inner functions; however its positions
505 // in script may have been shifted.
506 UNCHANGED: "unchanged",
507 // The code of a function remains unchanged, but something happened inside
508 // some inner functions.
509 SOURCE_CHANGED: "source changed",
510 // The code of a function is changed or some nested function cannot be
511 // properly patched so this function must be recompiled.
513 // Function is changed but cannot be patched.
517 function CodeInfoTreeNode(code_info, children, array_index) {
518 this.info = code_info;
519 this.children = children;
520 // an index in array of compile_info
521 this.array_index = array_index;
522 this.parent = UNDEFINED;
524 this.status = FunctionStatus.UNCHANGED;
525 // Status explanation is used for debugging purposes and will be shown
526 // in user UI if some explanations are needed.
527 this.status_explanation = UNDEFINED;
528 this.new_start_pos = UNDEFINED;
529 this.new_end_pos = UNDEFINED;
530 this.corresponding_node = UNDEFINED;
531 this.unmatched_new_nodes = UNDEFINED;
533 // 'Textual' correspondence/matching is weaker than 'pure'
534 // correspondence/matching. We need 'textual' level for visual presentation
535 // in UI, we use 'pure' level for actual code manipulation.
536 // Sometimes only function body is changed (functions in old and new script
537 // textually correspond), but we cannot patch the code, so we see them
538 // as an old function deleted and new function created.
539 this.textual_corresponding_node = UNDEFINED;
540 this.textually_unmatched_new_nodes = UNDEFINED;
542 this.live_shared_function_infos = UNDEFINED;
545 // From array of function infos that is implicitly a tree creates
546 // an actual tree of functions in script.
547 function BuildCodeInfoTree(code_info_array) {
548 // Throughtout all function we iterate over input array.
551 // Recursive function that builds a branch of tree.
552 function BuildNode() {
553 var my_index = index;
555 var child_array = new Array();
556 while (index < code_info_array.length &&
557 code_info_array[index].outer_index == my_index) {
558 child_array.push(BuildNode());
560 var node = new CodeInfoTreeNode(code_info_array[my_index], child_array,
562 for (var i = 0; i < child_array.length; i++) {
563 child_array[i].parent = node;
568 var root = BuildNode();
569 Assert(index == code_info_array.length);
573 // Applies a list of the textual diff chunks onto the tree of functions.
574 // Determines status of each function (from unchanged to damaged). However
575 // children of unchanged functions are ignored.
576 function MarkChangedFunctions(code_info_tree, chunks) {
578 // A convenient iterator over diff chunks that also translates
579 // positions from old to new in a current non-changed part of script.
580 var chunk_it = new function() {
583 this.current = function() { return chunks[chunk_index]; };
584 this.next = function() {
585 var chunk = chunks[chunk_index];
586 pos_diff = chunk.pos2 + chunk.len2 - (chunk.pos1 + chunk.len1);
589 this.done = function() { return chunk_index >= chunks.length; };
590 this.TranslatePos = function(pos) { return pos + pos_diff; };
593 // A recursive function that processes internals of a function and all its
594 // inner functions. Iterator chunk_it initially points to a chunk that is
595 // below function start.
596 function ProcessInternals(info_node) {
597 info_node.new_start_pos = chunk_it.TranslatePos(
598 info_node.info.start_position);
600 var code_changed = false;
601 var source_changed = false;
602 // Simultaneously iterates over child functions and over chunks.
603 while (!chunk_it.done() &&
604 chunk_it.current().pos1 < info_node.info.end_position) {
605 if (child_index < info_node.children.length) {
606 var child = info_node.children[child_index];
608 if (child.info.end_position <= chunk_it.current().pos1) {
609 ProcessUnchangedChild(child);
612 } else if (child.info.start_position >=
613 chunk_it.current().pos1 + chunk_it.current().len1) {
617 } else if (child.info.start_position <= chunk_it.current().pos1 &&
618 child.info.end_position >= chunk_it.current().pos1 +
619 chunk_it.current().len1) {
620 ProcessInternals(child);
621 source_changed = source_changed ||
622 ( child.status != FunctionStatus.UNCHANGED );
623 code_changed = code_changed ||
624 ( child.status == FunctionStatus.DAMAGED );
629 child.status = FunctionStatus.DAMAGED;
630 child.status_explanation =
631 "Text diff overlaps with function boundary";
636 if (chunk_it.current().pos1 + chunk_it.current().len1 <=
637 info_node.info.end_position) {
638 info_node.status = FunctionStatus.CHANGED;
642 info_node.status = FunctionStatus.DAMAGED;
643 info_node.status_explanation =
644 "Text diff overlaps with function boundary";
648 Assert("Unreachable", false);
650 while (child_index < info_node.children.length) {
651 var child = info_node.children[child_index];
652 ProcessUnchangedChild(child);
656 info_node.status = FunctionStatus.CHANGED;
657 } else if (source_changed) {
658 info_node.status = FunctionStatus.SOURCE_CHANGED;
660 info_node.new_end_pos =
661 chunk_it.TranslatePos(info_node.info.end_position);
664 function ProcessUnchangedChild(node) {
665 node.new_start_pos = chunk_it.TranslatePos(node.info.start_position);
666 node.new_end_pos = chunk_it.TranslatePos(node.info.end_position);
669 ProcessInternals(code_info_tree);
672 // For each old function (if it is not damaged) tries to find a corresponding
673 // function in new script. Typically it should succeed (non-damaged functions
674 // by definition may only have changes inside their bodies). However there are
675 // reasons for correspondence not to be found; function with unmodified text
676 // in new script may become enclosed into other function; the innocent change
677 // inside function body may in fact be something like "} function B() {" that
678 // splits a function into 2 functions.
679 function FindCorrespondingFunctions(old_code_tree, new_code_tree) {
681 // A recursive function that tries to find a correspondence for all
682 // child functions and for their inner functions.
683 function ProcessNode(old_node, new_node) {
684 var scope_change_description =
685 IsFunctionContextLocalsChanged(old_node.info, new_node.info);
686 if (scope_change_description) {
687 old_node.status = FunctionStatus.CHANGED;
690 var old_children = old_node.children;
691 var new_children = new_node.children;
693 var unmatched_new_nodes_list = [];
694 var textually_unmatched_new_nodes_list = [];
698 while (old_index < old_children.length) {
699 if (old_children[old_index].status == FunctionStatus.DAMAGED) {
701 } else if (new_index < new_children.length) {
702 if (new_children[new_index].info.start_position <
703 old_children[old_index].new_start_pos) {
704 unmatched_new_nodes_list.push(new_children[new_index]);
705 textually_unmatched_new_nodes_list.push(new_children[new_index]);
707 } else if (new_children[new_index].info.start_position ==
708 old_children[old_index].new_start_pos) {
709 if (new_children[new_index].info.end_position ==
710 old_children[old_index].new_end_pos) {
711 old_children[old_index].corresponding_node =
712 new_children[new_index];
713 old_children[old_index].textual_corresponding_node =
714 new_children[new_index];
715 if (scope_change_description) {
716 old_children[old_index].status = FunctionStatus.DAMAGED;
717 old_children[old_index].status_explanation =
718 "Enclosing function is now incompatible. " +
719 scope_change_description;
720 old_children[old_index].corresponding_node = UNDEFINED;
721 } else if (old_children[old_index].status !=
722 FunctionStatus.UNCHANGED) {
723 ProcessNode(old_children[old_index],
724 new_children[new_index]);
725 if (old_children[old_index].status == FunctionStatus.DAMAGED) {
726 unmatched_new_nodes_list.push(
727 old_children[old_index].corresponding_node);
728 old_children[old_index].corresponding_node = UNDEFINED;
729 old_node.status = FunctionStatus.CHANGED;
733 old_children[old_index].status = FunctionStatus.DAMAGED;
734 old_children[old_index].status_explanation =
735 "No corresponding function in new script found";
736 old_node.status = FunctionStatus.CHANGED;
737 unmatched_new_nodes_list.push(new_children[new_index]);
738 textually_unmatched_new_nodes_list.push(new_children[new_index]);
743 old_children[old_index].status = FunctionStatus.DAMAGED;
744 old_children[old_index].status_explanation =
745 "No corresponding function in new script found";
746 old_node.status = FunctionStatus.CHANGED;
750 old_children[old_index].status = FunctionStatus.DAMAGED;
751 old_children[old_index].status_explanation =
752 "No corresponding function in new script found";
753 old_node.status = FunctionStatus.CHANGED;
758 while (new_index < new_children.length) {
759 unmatched_new_nodes_list.push(new_children[new_index]);
760 textually_unmatched_new_nodes_list.push(new_children[new_index]);
764 if (old_node.status == FunctionStatus.CHANGED) {
765 if (old_node.info.param_num != new_node.info.param_num) {
766 old_node.status = FunctionStatus.DAMAGED;
767 old_node.status_explanation = "Changed parameter number: " +
768 old_node.info.param_num + " and " + new_node.info.param_num;
771 old_node.unmatched_new_nodes = unmatched_new_nodes_list;
772 old_node.textually_unmatched_new_nodes =
773 textually_unmatched_new_nodes_list;
776 ProcessNode(old_code_tree, new_code_tree);
778 old_code_tree.corresponding_node = new_code_tree;
779 old_code_tree.textual_corresponding_node = new_code_tree;
781 Assert(old_code_tree.status != FunctionStatus.DAMAGED,
782 "Script became damaged");
785 function FindLiveSharedInfos(old_code_tree, script) {
786 var shared_raw_list = %LiveEditFindSharedFunctionInfosForScript(script);
788 var shared_infos = new Array();
790 for (var i = 0; i < shared_raw_list.length; i++) {
791 shared_infos.push(new SharedInfoWrapper(shared_raw_list[i]));
794 // Finds all SharedFunctionInfos that corresponds to compile info
795 // in old version of the script.
796 function FindFunctionInfos(compile_info) {
799 for (var i = 0; i < shared_infos.length; i++) {
800 var wrapper = shared_infos[i];
801 if (wrapper.start_position == compile_info.start_position &&
802 wrapper.end_position == compile_info.end_position) {
803 wrappers.push(wrapper);
807 if (wrappers.length > 0) {
812 function TraverseTree(node) {
813 node.live_shared_function_infos = FindFunctionInfos(node.info);
815 for (var i = 0; i < node.children.length; i++) {
816 TraverseTree(node.children[i]);
820 TraverseTree(old_code_tree);
824 // An object describing function compilation details. Its index fields
825 // apply to indexes inside array that stores these objects.
826 function FunctionCompileInfo(raw_array) {
827 this.function_name = raw_array[0];
828 this.start_position = raw_array[1];
829 this.end_position = raw_array[2];
830 this.param_num = raw_array[3];
831 this.code = raw_array[4];
832 this.code_scope_info = raw_array[5];
833 this.scope_info = raw_array[6];
834 this.outer_index = raw_array[7];
835 this.shared_function_info = raw_array[8];
836 this.next_sibling_index = null;
837 this.raw_array = raw_array;
840 function SharedInfoWrapper(raw_array) {
841 this.function_name = raw_array[0];
842 this.start_position = raw_array[1];
843 this.end_position = raw_array[2];
844 this.info = raw_array[3];
845 this.raw_array = raw_array;
848 // Changes positions (including all statements) in function.
849 function PatchPositions(old_info_node, diff_array, report_array) {
850 if (old_info_node.live_shared_function_infos) {
851 old_info_node.live_shared_function_infos.forEach(function (info) {
852 %LiveEditPatchFunctionPositions(info.raw_array,
856 report_array.push( { name: old_info_node.info.function_name } );
858 // TODO(LiveEdit): function is not compiled yet or is already collected.
860 { name: old_info_node.info.function_name, info_not_found: true } );
864 // Adds a suffix to script name to mark that it is old version.
865 function CreateNameForOldScript(script) {
866 // TODO(635): try better than this; support several changes.
867 return script.name + " (old)";
870 // Compares a function scope heap structure, old and new version, whether it
871 // changed or not. Returns explanation if they differ.
872 function IsFunctionContextLocalsChanged(function_info1, function_info2) {
873 var scope_info1 = function_info1.scope_info;
874 var scope_info2 = function_info2.scope_info;
876 var scope_info1_text;
877 var scope_info2_text;
880 scope_info1_text = scope_info1.toString();
882 scope_info1_text = "";
885 scope_info2_text = scope_info2.toString();
887 scope_info2_text = "";
890 if (scope_info1_text != scope_info2_text) {
891 return "Variable map changed: [" + scope_info1_text +
892 "] => [" + scope_info2_text + "]";
894 // No differences. Return undefined.
898 // Minifier forward declaration.
899 var FunctionPatchabilityStatus;
901 // For array of wrapped shared function infos checks that none of them
902 // have activations on stack (of any thread). Throws a Failure exception
903 // if this proves to be false.
904 function CheckStackActivations(shared_wrapper_list, change_log) {
905 var shared_list = new Array();
906 for (var i = 0; i < shared_wrapper_list.length; i++) {
907 shared_list[i] = shared_wrapper_list[i].info;
909 var result = %LiveEditCheckAndDropActivations(shared_list, true);
910 if (result[shared_list.length]) {
911 // Extra array element may contain error message.
912 throw new Failure(result[shared_list.length]);
915 var problems = new Array();
916 var dropped = new Array();
917 for (var i = 0; i < shared_list.length; i++) {
918 var shared = shared_wrapper_list[i];
919 if (result[i] == FunctionPatchabilityStatus.REPLACED_ON_ACTIVE_STACK) {
920 dropped.push({ name: shared.function_name } );
921 } else if (result[i] != FunctionPatchabilityStatus.AVAILABLE_FOR_PATCH) {
923 name: shared.function_name,
924 start_pos: shared.start_position,
925 end_pos: shared.end_position,
927 FunctionPatchabilityStatus.SymbolName(result[i])
929 problems.push(description);
932 if (dropped.length > 0) {
933 change_log.push({ dropped_from_stack: dropped });
935 if (problems.length > 0) {
936 change_log.push( { functions_on_stack: problems } );
937 throw new Failure("Blocked by functions on stack");
940 return dropped.length;
943 // A copy of the FunctionPatchabilityStatus enum from liveedit.h
944 var FunctionPatchabilityStatus = {
945 AVAILABLE_FOR_PATCH: 1,
946 BLOCKED_ON_ACTIVE_STACK: 2,
947 BLOCKED_ON_OTHER_STACK: 3,
948 BLOCKED_UNDER_NATIVE_CODE: 4,
949 REPLACED_ON_ACTIVE_STACK: 5
952 FunctionPatchabilityStatus.SymbolName = function(code) {
953 var enumeration = FunctionPatchabilityStatus;
954 for (name in enumeration) {
955 if (enumeration[name] == code) {
962 // A logical failure in liveedit process. This means that change_log
963 // is valid and consistent description of what happened.
964 function Failure(message) {
965 this.message = message;
967 // Function (constructor) is public.
968 this.Failure = Failure;
970 Failure.prototype.toString = function() {
971 return "LiveEdit Failure: " + this.message;
974 function CopyErrorPositionToDetails(e, details) {
975 function createPositionStruct(script, position) {
976 if (position == -1) return;
977 var location = script.locationFromPosition(position, true);
978 if (location == null) return;
980 line: location.line + 1,
981 column: location.column + 1,
986 if (!("scriptObject" in e) || !("startPosition" in e)) {
990 var script = e.scriptObject;
992 var position_struct = {
993 start: createPositionStruct(script, e.startPosition),
994 end: createPositionStruct(script, e.endPosition)
996 details.position = position_struct;
1000 function GetPcFromSourcePos(func, source_pos) {
1001 return %GetFunctionCodePositionFromSource(func, source_pos);
1003 // Function is public.
1004 this.GetPcFromSourcePos = GetPcFromSourcePos;
1006 // LiveEdit main entry point: changes a script text to a new string.
1007 function SetScriptSource(script, new_source, preview_only, change_log) {
1008 var old_source = script.source;
1009 var diff = CompareStrings(old_source, new_source);
1010 return ApplyPatchMultiChunk(script, diff, new_source, preview_only,
1013 // Function is public.
1014 this.SetScriptSource = SetScriptSource;
1016 function CompareStrings(s1, s2) {
1017 return %LiveEditCompareStrings(s1, s2);
1020 // Applies the change to the script.
1021 // The change is always a substring (change_pos, change_pos + change_len)
1022 // being replaced with a completely different string new_str.
1023 // This API is a legacy and is obsolete.
1025 // @param {Script} script that is being changed
1026 // @param {Array} change_log a list that collects engineer-readable
1027 // description of what happened.
1028 function ApplySingleChunkPatch(script, change_pos, change_len, new_str,
1030 var old_source = script.source;
1032 // Prepare new source string.
1033 var new_source = old_source.substring(0, change_pos) +
1034 new_str + old_source.substring(change_pos + change_len);
1036 return ApplyPatchMultiChunk(script,
1037 [ change_pos, change_pos + change_len, change_pos + new_str.length],
1038 new_source, false, change_log);
1041 // Creates JSON description for a change tree.
1042 function DescribeChangeTree(old_code_tree) {
1044 function ProcessOldNode(node) {
1045 var child_infos = [];
1046 for (var i = 0; i < node.children.length; i++) {
1047 var child = node.children[i];
1048 if (child.status != FunctionStatus.UNCHANGED) {
1049 child_infos.push(ProcessOldNode(child));
1052 var new_child_infos = [];
1053 if (node.textually_unmatched_new_nodes) {
1054 for (var i = 0; i < node.textually_unmatched_new_nodes.length; i++) {
1055 var child = node.textually_unmatched_new_nodes[i];
1056 new_child_infos.push(ProcessNewNode(child));
1060 name: node.info.function_name,
1061 positions: DescribePositions(node),
1062 status: node.status,
1063 children: child_infos,
1064 new_children: new_child_infos
1066 if (node.status_explanation) {
1067 res.status_explanation = node.status_explanation;
1069 if (node.textual_corresponding_node) {
1070 res.new_positions = DescribePositions(node.textual_corresponding_node);
1075 function ProcessNewNode(node) {
1076 var child_infos = [];
1077 // Do not list ancestors.
1079 for (var i = 0; i < node.children.length; i++) {
1080 child_infos.push(ProcessNewNode(node.children[i]));
1084 name: node.info.function_name,
1085 positions: DescribePositions(node),
1086 children: child_infos,
1091 function DescribePositions(node) {
1093 start_position: node.info.start_position,
1094 end_position: node.info.end_position
1098 return ProcessOldNode(old_code_tree);
1101 // Restarts call frame and returns value similar to what LiveEdit returns.
1102 function RestartFrame(frame_mirror) {
1103 var result = frame_mirror.restart();
1104 if (IS_STRING(result)) {
1105 throw new Failure("Failed to restart frame: " + result);
1108 result[NEEDS_STEP_IN_PROPERTY_NAME] = true;
1111 // Function is public.
1112 this.RestartFrame = RestartFrame;
1114 // Functions are public for tests.
1116 PosTranslator: PosTranslator,
1117 CompareStrings: CompareStrings,
1118 ApplySingleChunkPatch: ApplySingleChunkPatch