1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // LiveEdit feature implementation. The script should be executed after
31 // A LiveEdit namespace. It contains functions that modifies JavaScript code
32 // according to changes of script source (if possible).
34 // When new script source is put in, the difference is calculated textually,
35 // in form of list of delete/add/change chunks. The functions that include
36 // change chunk(s) get recompiled, or their enclosing functions are
37 // recompiled instead.
38 // If the function may not be recompiled (e.g. it was completely erased in new
39 // version of the script) it remains unchanged, but the code that could
40 // create a new instance of this function goes away. An old version of script
41 // is created to back up this obsolete function.
42 // All unchanged functions have their positions updated accordingly.
44 // LiveEdit namespace is declared inside a single function constructor.
45 Debug.LiveEdit = new function() {
47 // Forward declaration for minifier.
50 // Applies the change to the script.
51 // The change is in form of list of chunks encoded in a single array as
52 // a series of triplets (pos1_start, pos1_end, pos2_end)
53 function ApplyPatchMultiChunk(script, diff_array, new_source, preview_only,
56 var old_source = script.source;
58 // Gather compile information about old version of script.
59 var old_compile_info = GatherCompileInfo(old_source, script);
61 // Build tree structures for old and new versions of the script.
62 var root_old_node = BuildCodeInfoTree(old_compile_info);
64 var pos_translator = new PosTranslator(diff_array);
67 MarkChangedFunctions(root_old_node, pos_translator.GetChunks());
69 // Find all SharedFunctionInfo's that were compiled from this script.
70 FindLiveSharedInfos(root_old_node, script);
72 // Gather compile information about new version of script.
75 new_compile_info = GatherCompileInfo(new_source, script);
77 throw new Failure("Failed to compile new version of script: " + e);
79 var root_new_node = BuildCodeInfoTree(new_compile_info);
81 // Link recompiled script data with other data.
82 FindCorrespondingFunctions(root_old_node, root_new_node);
84 // Prepare to-do lists.
85 var replace_code_list = new Array();
86 var link_to_old_script_list = new Array();
87 var link_to_original_script_list = new Array();
88 var update_positions_list = new Array();
90 function HarvestTodo(old_node) {
91 function CollectDamaged(node) {
92 link_to_old_script_list.push(node);
93 for (var i = 0; i < node.children.length; i++) {
94 CollectDamaged(node.children[i]);
98 // Recursively collects all newly compiled functions that are going into
99 // business and should have link to the actual script updated.
100 function CollectNew(node_list) {
101 for (var i = 0; i < node_list.length; i++) {
102 link_to_original_script_list.push(node_list[i]);
103 CollectNew(node_list[i].children);
107 if (old_node.status == FunctionStatus.DAMAGED) {
108 CollectDamaged(old_node);
111 if (old_node.status == FunctionStatus.UNCHANGED) {
112 update_positions_list.push(old_node);
113 } else if (old_node.status == FunctionStatus.SOURCE_CHANGED) {
114 update_positions_list.push(old_node);
115 } else if (old_node.status == FunctionStatus.CHANGED) {
116 replace_code_list.push(old_node);
117 CollectNew(old_node.unmatched_new_nodes);
119 for (var i = 0; i < old_node.children.length; i++) {
120 HarvestTodo(old_node.children[i]);
124 var preview_description = {
125 change_tree: DescribeChangeTree(root_old_node),
127 old_len: old_source.length,
128 new_len: new_source.length,
135 return preview_description;
138 HarvestTodo(root_old_node);
140 // Collect shared infos for functions whose code need to be patched.
141 var replaced_function_infos = new Array();
142 for (var i = 0; i < replace_code_list.length; i++) {
143 var live_shared_function_infos =
144 replace_code_list[i].live_shared_function_infos;
146 if (live_shared_function_infos) {
147 for (var j = 0; j < live_shared_function_infos.length; j++) {
148 replaced_function_infos.push(live_shared_function_infos[j]);
153 // We haven't changed anything before this line yet.
154 // Committing all changes.
156 // Check that function being patched is not currently on stack or drop them.
157 var dropped_functions_number =
158 CheckStackActivations(replaced_function_infos, change_log);
160 preview_description.stack_modified = dropped_functions_number != 0;
162 // Our current implementation requires client to manually issue "step in"
163 // command for correct stack state.
164 preview_description.stack_update_needs_step_in =
165 preview_description.stack_modified;
167 // Start with breakpoints. Convert their line/column positions and
169 var break_points_restorer = TemporaryRemoveBreakPoints(script, change_log);
173 // Create an old script only if there are function that should be linked
175 if (link_to_old_script_list.length == 0) {
176 %LiveEditReplaceScript(script, new_source, null);
179 var old_script_name = CreateNameForOldScript(script);
181 // Update the script text and create a new script representing an old
182 // version of the script.
183 old_script = %LiveEditReplaceScript(script, new_source,
186 var link_to_old_script_report = new Array();
187 change_log.push( { linked_to_old_script: link_to_old_script_report } );
189 // We need to link to old script all former nested functions.
190 for (var i = 0; i < link_to_old_script_list.length; i++) {
191 LinkToOldScript(link_to_old_script_list[i], old_script,
192 link_to_old_script_report);
195 preview_description.created_script_name = old_script_name;
198 // Link to an actual script all the functions that we are going to use.
199 for (var i = 0; i < link_to_original_script_list.length; i++) {
200 %LiveEditFunctionSetScript(
201 link_to_original_script_list[i].info.shared_function_info, script);
204 for (var i = 0; i < replace_code_list.length; i++) {
205 PatchFunctionCode(replace_code_list[i], change_log);
208 var position_patch_report = new Array();
209 change_log.push( {position_patched: position_patch_report} );
211 for (var i = 0; i < update_positions_list.length; i++) {
212 // TODO(LiveEdit): take into account wether it's source_changed or
213 // unchanged and whether positions changed at all.
214 PatchPositions(update_positions_list[i], diff_array,
215 position_patch_report);
217 if (update_positions_list[i].live_shared_function_infos) {
218 update_positions_list[i].live_shared_function_infos.
219 forEach(function (info) {
220 %LiveEditFunctionSourceUpdated(info.raw_array);
225 break_points_restorer(pos_translator, old_script);
227 preview_description.updated = true;
228 return preview_description;
230 // Function is public.
231 this.ApplyPatchMultiChunk = ApplyPatchMultiChunk;
234 // Fully compiles source string as a script. Returns Array of
235 // FunctionCompileInfo -- a descriptions of all functions of the script.
236 // Elements of array are ordered by start positions of functions (from top
237 // to bottom) in the source. Fields outer_index and next_sibling_index help
238 // to navigate the nesting structure of functions.
240 // All functions get compiled linked to script provided as parameter script.
241 // TODO(LiveEdit): consider not using actual scripts as script, because
242 // we have to manually erase all links right after compile.
243 function GatherCompileInfo(source, script) {
244 // Get function info, elements are partially sorted (it is a tree of
245 // nested functions serialized as parent followed by serialized children.
246 var raw_compile_info = %LiveEditGatherCompileInfo(script, source);
248 // Sort function infos by start position field.
249 var compile_info = new Array();
250 var old_index_map = new Array();
251 for (var i = 0; i < raw_compile_info.length; i++) {
252 var info = new FunctionCompileInfo(raw_compile_info[i]);
253 // Remove all links to the actual script. Breakpoints system and
254 // LiveEdit itself believe that any function in heap that points to a
255 // particular script is a regular function.
256 // For some functions we will restore this link later.
257 %LiveEditFunctionSetScript(info.shared_function_info, void 0);
258 compile_info.push(info);
259 old_index_map.push(i);
262 for (var i = 0; i < compile_info.length; i++) {
264 for (var j = i + 1; j < compile_info.length; j++) {
265 if (compile_info[k].start_position > compile_info[j].start_position) {
270 var temp_info = compile_info[k];
271 var temp_index = old_index_map[k];
272 compile_info[k] = compile_info[i];
273 old_index_map[k] = old_index_map[i];
274 compile_info[i] = temp_info;
275 old_index_map[i] = temp_index;
279 // After sorting update outer_inder field using old_index_map. Also
280 // set next_sibling_index field.
281 var current_index = 0;
283 // The recursive function, that goes over all children of a particular
284 // node (i.e. function info).
285 function ResetIndexes(new_parent_index, old_parent_index) {
286 var previous_sibling = -1;
287 while (current_index < compile_info.length &&
288 compile_info[current_index].outer_index == old_parent_index) {
289 var saved_index = current_index;
290 compile_info[saved_index].outer_index = new_parent_index;
291 if (previous_sibling != -1) {
292 compile_info[previous_sibling].next_sibling_index = saved_index;
294 previous_sibling = saved_index;
296 ResetIndexes(saved_index, old_index_map[saved_index]);
298 if (previous_sibling != -1) {
299 compile_info[previous_sibling].next_sibling_index = -1;
303 ResetIndexes(-1, -1);
304 Assert(current_index == compile_info.length);
310 // Replaces function's Code.
311 function PatchFunctionCode(old_node, change_log) {
312 var new_info = old_node.corresponding_node.info;
313 if (old_node.live_shared_function_infos) {
314 old_node.live_shared_function_infos.forEach(function (old_info) {
315 %LiveEditReplaceFunctionCode(new_info.raw_array,
318 // The function got a new code. However, this new code brings all new
319 // instances of SharedFunctionInfo for nested functions. However,
320 // we want the original instances to be used wherever possible.
321 // (This is because old instances and new instances will be both
322 // linked to a script and breakpoints subsystem does not really
323 // expects this; neither does LiveEdit subsystem on next call).
324 for (var i = 0; i < old_node.children.length; i++) {
325 if (old_node.children[i].corresponding_node) {
326 var corresponding_child_info =
327 old_node.children[i].corresponding_node.info.
328 shared_function_info;
330 if (old_node.children[i].live_shared_function_infos) {
331 old_node.children[i].live_shared_function_infos.
332 forEach(function (old_child_info) {
333 %LiveEditReplaceRefToNestedFunction(
335 corresponding_child_info,
336 old_child_info.info);
343 change_log.push( {function_patched: new_info.function_name} );
345 change_log.push( {function_patched: new_info.function_name,
346 function_info_not_found: true} );
351 // Makes a function associated with another instance of a script (the
352 // one representing its old version). This way the function still
353 // may access its own text.
354 function LinkToOldScript(old_info_node, old_script, report_array) {
355 if (old_info_node.live_shared_function_infos) {
356 old_info_node.live_shared_function_infos.
357 forEach(function (info) {
358 %LiveEditFunctionSetScript(info.info, old_script);
361 report_array.push( { name: old_info_node.info.function_name } );
364 { name: old_info_node.info.function_name, not_found: true } );
369 // Returns function that restores breakpoints.
370 function TemporaryRemoveBreakPoints(original_script, change_log) {
371 var script_break_points = GetScriptBreakPoints(original_script);
373 var break_points_update_report = [];
374 change_log.push( { break_points_update: break_points_update_report } );
376 var break_point_old_positions = [];
377 for (var i = 0; i < script_break_points.length; i++) {
378 var break_point = script_break_points[i];
382 // TODO(LiveEdit): be careful with resource offset here.
383 var break_point_position = Debug.findScriptSourcePosition(original_script,
384 break_point.line(), break_point.column());
386 var old_position_description = {
387 position: break_point_position,
388 line: break_point.line(),
389 column: break_point.column()
391 break_point_old_positions.push(old_position_description);
395 // Restores breakpoints and creates their copies in the "old" copy of
397 return function (pos_translator, old_script_copy_opt) {
398 // Update breakpoints (change positions and restore them in old version
400 for (var i = 0; i < script_break_points.length; i++) {
401 var break_point = script_break_points[i];
402 if (old_script_copy_opt) {
403 var clone = break_point.cloneForOtherScript(old_script_copy_opt);
404 clone.set(old_script_copy_opt);
406 break_points_update_report.push( {
407 type: "copied_to_old",
408 id: break_point.number(),
409 new_id: clone.number(),
410 positions: break_point_old_positions[i]
414 var updated_position = pos_translator.Translate(
415 break_point_old_positions[i].position,
416 PosTranslator.ShiftWithTopInsideChunkHandler);
419 original_script.locationFromPosition(updated_position, false);
421 break_point.update_positions(new_location.line, new_location.column);
423 var new_position_description = {
424 position: updated_position,
425 line: new_location.line,
426 column: new_location.column
429 break_point.set(original_script);
431 break_points_update_report.push( { type: "position_changed",
432 id: break_point.number(),
433 old_positions: break_point_old_positions[i],
434 new_positions: new_position_description
441 function Assert(condition, message) {
444 throw "Assert " + message;
451 function DiffChunk(pos1, pos2, len1, len2) {
458 function PosTranslator(diff_array) {
459 var chunks = new Array();
460 var current_diff = 0;
461 for (var i = 0; i < diff_array.length; i += 3) {
462 var pos1_begin = diff_array[i];
463 var pos2_begin = pos1_begin + current_diff;
464 var pos1_end = diff_array[i + 1];
465 var pos2_end = diff_array[i + 2];
466 chunks.push(new DiffChunk(pos1_begin, pos2_begin, pos1_end - pos1_begin,
467 pos2_end - pos2_begin));
468 current_diff = pos2_end - pos1_end;
470 this.chunks = chunks;
472 PosTranslator.prototype.GetChunks = function() {
476 PosTranslator.prototype.Translate = function(pos, inside_chunk_handler) {
477 var array = this.chunks;
478 if (array.length == 0 || pos < array[0].pos1) {
481 var chunk_index1 = 0;
482 var chunk_index2 = array.length - 1;
484 while (chunk_index1 < chunk_index2) {
485 var middle_index = Math.floor((chunk_index1 + chunk_index2) / 2);
486 if (pos < array[middle_index + 1].pos1) {
487 chunk_index2 = middle_index;
489 chunk_index1 = middle_index + 1;
492 var chunk = array[chunk_index1];
493 if (pos >= chunk.pos1 + chunk.len1) {
494 return pos + chunk.pos2 + chunk.len2 - chunk.pos1 - chunk.len1;
497 if (!inside_chunk_handler) {
498 inside_chunk_handler = PosTranslator.DefaultInsideChunkHandler;
500 return inside_chunk_handler(pos, chunk);
503 PosTranslator.DefaultInsideChunkHandler = function(pos, diff_chunk) {
504 Assert(false, "Cannot translate position in changed area");
507 PosTranslator.ShiftWithTopInsideChunkHandler =
508 function(pos, diff_chunk) {
509 // We carelessly do not check whether we stay inside the chunk after
511 return pos - diff_chunk.pos1 + diff_chunk.pos2;
514 var FunctionStatus = {
515 // No change to function or its inner functions; however its positions
516 // in script may have been shifted.
517 UNCHANGED: "unchanged",
518 // The code of a function remains unchanged, but something happened inside
519 // some inner functions.
520 SOURCE_CHANGED: "source changed",
521 // The code of a function is changed or some nested function cannot be
522 // properly patched so this function must be recompiled.
524 // Function is changed but cannot be patched.
528 function CodeInfoTreeNode(code_info, children, array_index) {
529 this.info = code_info;
530 this.children = children;
531 // an index in array of compile_info
532 this.array_index = array_index;
533 this.parent = void 0;
535 this.status = FunctionStatus.UNCHANGED;
536 // Status explanation is used for debugging purposes and will be shown
537 // in user UI if some explanations are needed.
538 this.status_explanation = void 0;
539 this.new_start_pos = void 0;
540 this.new_end_pos = void 0;
541 this.corresponding_node = void 0;
542 this.unmatched_new_nodes = void 0;
544 // 'Textual' correspondence/matching is weaker than 'pure'
545 // correspondence/matching. We need 'textual' level for visual presentation
546 // in UI, we use 'pure' level for actual code manipulation.
547 // Sometimes only function body is changed (functions in old and new script
548 // textually correspond), but we cannot patch the code, so we see them
549 // as an old function deleted and new function created.
550 this.textual_corresponding_node = void 0;
551 this.textually_unmatched_new_nodes = void 0;
553 this.live_shared_function_infos = void 0;
556 // From array of function infos that is implicitly a tree creates
557 // an actual tree of functions in script.
558 function BuildCodeInfoTree(code_info_array) {
559 // Throughtout all function we iterate over input array.
562 // Recursive function that builds a branch of tree.
563 function BuildNode() {
564 var my_index = index;
566 var child_array = new Array();
567 while (index < code_info_array.length &&
568 code_info_array[index].outer_index == my_index) {
569 child_array.push(BuildNode());
571 var node = new CodeInfoTreeNode(code_info_array[my_index], child_array,
573 for (var i = 0; i < child_array.length; i++) {
574 child_array[i].parent = node;
579 var root = BuildNode();
580 Assert(index == code_info_array.length);
584 // Applies a list of the textual diff chunks onto the tree of functions.
585 // Determines status of each function (from unchanged to damaged). However
586 // children of unchanged functions are ignored.
587 function MarkChangedFunctions(code_info_tree, chunks) {
589 // A convenient iterator over diff chunks that also translates
590 // positions from old to new in a current non-changed part of script.
591 var chunk_it = new function() {
594 this.current = function() { return chunks[chunk_index]; };
595 this.next = function() {
596 var chunk = chunks[chunk_index];
597 pos_diff = chunk.pos2 + chunk.len2 - (chunk.pos1 + chunk.len1);
600 this.done = function() { return chunk_index >= chunks.length; };
601 this.TranslatePos = function(pos) { return pos + pos_diff; };
604 // A recursive function that processes internals of a function and all its
605 // inner functions. Iterator chunk_it initially points to a chunk that is
606 // below function start.
607 function ProcessInternals(info_node) {
608 info_node.new_start_pos = chunk_it.TranslatePos(
609 info_node.info.start_position);
611 var code_changed = false;
612 var source_changed = false;
613 // Simultaneously iterates over child functions and over chunks.
614 while (!chunk_it.done() &&
615 chunk_it.current().pos1 < info_node.info.end_position) {
616 if (child_index < info_node.children.length) {
617 var child = info_node.children[child_index];
619 if (child.info.end_position <= chunk_it.current().pos1) {
620 ProcessUnchangedChild(child);
623 } else if (child.info.start_position >=
624 chunk_it.current().pos1 + chunk_it.current().len1) {
628 } else if (child.info.start_position <= chunk_it.current().pos1 &&
629 child.info.end_position >= chunk_it.current().pos1 +
630 chunk_it.current().len1) {
631 ProcessInternals(child);
632 source_changed = source_changed ||
633 ( child.status != FunctionStatus.UNCHANGED );
634 code_changed = code_changed ||
635 ( child.status == FunctionStatus.DAMAGED );
640 child.status = FunctionStatus.DAMAGED;
641 child.status_explanation =
642 "Text diff overlaps with function boundary";
647 if (chunk_it.current().pos1 + chunk_it.current().len1 <=
648 info_node.info.end_position) {
649 info_node.status = FunctionStatus.CHANGED;
653 info_node.status = FunctionStatus.DAMAGED;
654 info_node.status_explanation =
655 "Text diff overlaps with function boundary";
659 Assert("Unreachable", false);
661 while (child_index < info_node.children.length) {
662 var child = info_node.children[child_index];
663 ProcessUnchangedChild(child);
667 info_node.status = FunctionStatus.CHANGED;
668 } else if (source_changed) {
669 info_node.status = FunctionStatus.SOURCE_CHANGED;
671 info_node.new_end_pos =
672 chunk_it.TranslatePos(info_node.info.end_position);
675 function ProcessUnchangedChild(node) {
676 node.new_start_pos = chunk_it.TranslatePos(node.info.start_position);
677 node.new_end_pos = chunk_it.TranslatePos(node.info.end_position);
680 ProcessInternals(code_info_tree);
683 // For ecah old function (if it is not damaged) tries to find a corresponding
684 // function in new script. Typically it should succeed (non-damaged functions
685 // by definition may only have changes inside their bodies). However there are
686 // reasons for corresponence not to be found; function with unmodified text
687 // in new script may become enclosed into other function; the innocent change
688 // inside function body may in fact be something like "} function B() {" that
689 // splits a function into 2 functions.
690 function FindCorrespondingFunctions(old_code_tree, new_code_tree) {
692 // A recursive function that tries to find a correspondence for all
693 // child functions and for their inner functions.
694 function ProcessChildren(old_node, new_node) {
695 var old_children = old_node.children;
696 var new_children = new_node.children;
698 var unmatched_new_nodes_list = [];
699 var textually_unmatched_new_nodes_list = [];
703 while (old_index < old_children.length) {
704 if (old_children[old_index].status == FunctionStatus.DAMAGED) {
706 } else if (new_index < new_children.length) {
707 if (new_children[new_index].info.start_position <
708 old_children[old_index].new_start_pos) {
709 unmatched_new_nodes_list.push(new_children[new_index]);
710 textually_unmatched_new_nodes_list.push(new_children[new_index]);
712 } else if (new_children[new_index].info.start_position ==
713 old_children[old_index].new_start_pos) {
714 if (new_children[new_index].info.end_position ==
715 old_children[old_index].new_end_pos) {
716 old_children[old_index].corresponding_node =
717 new_children[new_index];
718 old_children[old_index].textual_corresponding_node =
719 new_children[new_index];
720 if (old_children[old_index].status != FunctionStatus.UNCHANGED) {
721 ProcessChildren(old_children[old_index],
722 new_children[new_index]);
723 if (old_children[old_index].status == FunctionStatus.DAMAGED) {
724 unmatched_new_nodes_list.push(
725 old_children[old_index].corresponding_node);
726 old_children[old_index].corresponding_node = void 0;
727 old_node.status = FunctionStatus.CHANGED;
731 old_children[old_index].status = FunctionStatus.DAMAGED;
732 old_children[old_index].status_explanation =
733 "No corresponding function in new script found";
734 old_node.status = FunctionStatus.CHANGED;
735 unmatched_new_nodes_list.push(new_children[new_index]);
736 textually_unmatched_new_nodes_list.push(new_children[new_index]);
741 old_children[old_index].status = FunctionStatus.DAMAGED;
742 old_children[old_index].status_explanation =
743 "No corresponding function in new script found";
744 old_node.status = FunctionStatus.CHANGED;
748 old_children[old_index].status = FunctionStatus.DAMAGED;
749 old_children[old_index].status_explanation =
750 "No corresponding function in new script found";
751 old_node.status = FunctionStatus.CHANGED;
756 while (new_index < new_children.length) {
757 unmatched_new_nodes_list.push(new_children[new_index]);
758 textually_unmatched_new_nodes_list.push(new_children[new_index]);
762 if (old_node.status == FunctionStatus.CHANGED) {
763 var why_wrong_expectations =
764 WhyFunctionExpectationsDiffer(old_node.info, new_node.info);
765 if (why_wrong_expectations) {
766 old_node.status = FunctionStatus.DAMAGED;
767 old_node.status_explanation = why_wrong_expectations;
770 old_node.unmatched_new_nodes = unmatched_new_nodes_list;
771 old_node.textually_unmatched_new_nodes =
772 textually_unmatched_new_nodes_list;
775 ProcessChildren(old_code_tree, new_code_tree);
777 old_code_tree.corresponding_node = new_code_tree;
778 old_code_tree.textual_corresponding_node = new_code_tree;
780 Assert(old_code_tree.status != FunctionStatus.DAMAGED,
781 "Script became damaged");
784 function FindLiveSharedInfos(old_code_tree, script) {
785 var shared_raw_list = %LiveEditFindSharedFunctionInfosForScript(script);
787 var shared_infos = new Array();
789 for (var i = 0; i < shared_raw_list.length; i++) {
790 shared_infos.push(new SharedInfoWrapper(shared_raw_list[i]));
793 // Finds all SharedFunctionInfos that corresponds to compile info
794 // in old version of the script.
795 function FindFunctionInfos(compile_info) {
798 for (var i = 0; i < shared_infos.length; i++) {
799 var wrapper = shared_infos[i];
800 if (wrapper.start_position == compile_info.start_position &&
801 wrapper.end_position == compile_info.end_position) {
802 wrappers.push(wrapper);
806 if (wrappers.length > 0) {
811 function TraverseTree(node) {
812 node.live_shared_function_infos = FindFunctionInfos(node.info);
814 for (var i = 0; i < node.children.length; i++) {
815 TraverseTree(node.children[i]);
819 TraverseTree(old_code_tree);
823 // An object describing function compilation details. Its index fields
824 // apply to indexes inside array that stores these objects.
825 function FunctionCompileInfo(raw_array) {
826 this.function_name = raw_array[0];
827 this.start_position = raw_array[1];
828 this.end_position = raw_array[2];
829 this.param_num = raw_array[3];
830 this.code = raw_array[4];
831 this.code_scope_info = raw_array[5];
832 this.scope_info = raw_array[6];
833 this.outer_index = raw_array[7];
834 this.shared_function_info = raw_array[8];
835 this.next_sibling_index = null;
836 this.raw_array = raw_array;
839 function SharedInfoWrapper(raw_array) {
840 this.function_name = raw_array[0];
841 this.start_position = raw_array[1];
842 this.end_position = raw_array[2];
843 this.info = raw_array[3];
844 this.raw_array = raw_array;
847 // Changes positions (including all statments) in function.
848 function PatchPositions(old_info_node, diff_array, report_array) {
849 if (old_info_node.live_shared_function_infos) {
850 old_info_node.live_shared_function_infos.forEach(function (info) {
851 %LiveEditPatchFunctionPositions(info.raw_array,
855 report_array.push( { name: old_info_node.info.function_name } );
857 // TODO(LiveEdit): function is not compiled yet or is already collected.
859 { name: old_info_node.info.function_name, info_not_found: true } );
863 // Adds a suffix to script name to mark that it is old version.
864 function CreateNameForOldScript(script) {
865 // TODO(635): try better than this; support several changes.
866 return script.name + " (old)";
869 // Compares a function interface old and new version, whether it
870 // changed or not. Returns explanation if they differ.
871 function WhyFunctionExpectationsDiffer(function_info1, function_info2) {
872 // Check that function has the same number of parameters (there may exist
873 // an adapter, that won't survive function parameter number change).
874 if (function_info1.param_num != function_info2.param_num) {
875 return "Changed parameter number: " + function_info1.param_num +
876 " and " + function_info2.param_num;
878 var scope_info1 = function_info1.scope_info;
879 var scope_info2 = function_info2.scope_info;
881 var scope_info1_text;
882 var scope_info2_text;
885 scope_info1_text = scope_info1.toString();
887 scope_info1_text = "";
890 scope_info2_text = scope_info2.toString();
892 scope_info2_text = "";
895 if (scope_info1_text != scope_info2_text) {
896 return "Incompatible variable maps: [" + scope_info1_text +
897 "] and [" + scope_info2_text + "]";
899 // No differences. Return undefined.
903 // Minifier forward declaration.
904 var FunctionPatchabilityStatus;
906 // For array of wrapped shared function infos checks that none of them
907 // have activations on stack (of any thread). Throws a Failure exception
908 // if this proves to be false.
909 function CheckStackActivations(shared_wrapper_list, change_log) {
910 var shared_list = new Array();
911 for (var i = 0; i < shared_wrapper_list.length; i++) {
912 shared_list[i] = shared_wrapper_list[i].info;
914 var result = %LiveEditCheckAndDropActivations(shared_list, true);
915 if (result[shared_list.length]) {
916 // Extra array element may contain error message.
917 throw new Failure(result[shared_list.length]);
920 var problems = new Array();
921 var dropped = new Array();
922 for (var i = 0; i < shared_list.length; i++) {
923 var shared = shared_wrapper_list[i];
924 if (result[i] == FunctionPatchabilityStatus.REPLACED_ON_ACTIVE_STACK) {
925 dropped.push({ name: shared.function_name } );
926 } else if (result[i] != FunctionPatchabilityStatus.AVAILABLE_FOR_PATCH) {
928 name: shared.function_name,
929 start_pos: shared.start_position,
930 end_pos: shared.end_position,
932 FunctionPatchabilityStatus.SymbolName(result[i])
934 problems.push(description);
937 if (dropped.length > 0) {
938 change_log.push({ dropped_from_stack: dropped });
940 if (problems.length > 0) {
941 change_log.push( { functions_on_stack: problems } );
942 throw new Failure("Blocked by functions on stack");
945 return dropped.length;
948 // A copy of the FunctionPatchabilityStatus enum from liveedit.h
949 var FunctionPatchabilityStatus = {
950 AVAILABLE_FOR_PATCH: 1,
951 BLOCKED_ON_ACTIVE_STACK: 2,
952 BLOCKED_ON_OTHER_STACK: 3,
953 BLOCKED_UNDER_NATIVE_CODE: 4,
954 REPLACED_ON_ACTIVE_STACK: 5
957 FunctionPatchabilityStatus.SymbolName = function(code) {
958 var enumeration = FunctionPatchabilityStatus;
959 for (name in enumeration) {
960 if (enumeration[name] == code) {
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;
972 // Function (constructor) is public.
973 this.Failure = Failure;
975 Failure.prototype.toString = function() {
976 return "LiveEdit Failure: " + this.message;
980 function GetPcFromSourcePos(func, source_pos) {
981 return %GetFunctionCodePositionFromSource(func, source_pos);
983 // Function is public.
984 this.GetPcFromSourcePos = GetPcFromSourcePos;
986 // LiveEdit main entry point: changes a script text to a new string.
987 function SetScriptSource(script, new_source, preview_only, change_log) {
988 var old_source = script.source;
989 var diff = CompareStrings(old_source, new_source);
990 return ApplyPatchMultiChunk(script, diff, new_source, preview_only,
993 // Function is public.
994 this.SetScriptSource = SetScriptSource;
996 function CompareStrings(s1, s2) {
997 return %LiveEditCompareStrings(s1, s2);
1000 // Applies the change to the script.
1001 // The change is always a substring (change_pos, change_pos + change_len)
1002 // being replaced with a completely different string new_str.
1003 // This API is a legacy and is obsolete.
1005 // @param {Script} script that is being changed
1006 // @param {Array} change_log a list that collects engineer-readable
1007 // description of what happened.
1008 function ApplySingleChunkPatch(script, change_pos, change_len, new_str,
1010 var old_source = script.source;
1012 // Prepare new source string.
1013 var new_source = old_source.substring(0, change_pos) +
1014 new_str + old_source.substring(change_pos + change_len);
1016 return ApplyPatchMultiChunk(script,
1017 [ change_pos, change_pos + change_len, change_pos + new_str.length],
1018 new_source, false, change_log);
1021 // Creates JSON description for a change tree.
1022 function DescribeChangeTree(old_code_tree) {
1024 function ProcessOldNode(node) {
1025 var child_infos = [];
1026 for (var i = 0; i < node.children.length; i++) {
1027 var child = node.children[i];
1028 if (child.status != FunctionStatus.UNCHANGED) {
1029 child_infos.push(ProcessOldNode(child));
1032 var new_child_infos = [];
1033 if (node.textually_unmatched_new_nodes) {
1034 for (var i = 0; i < node.textually_unmatched_new_nodes.length; i++) {
1035 var child = node.textually_unmatched_new_nodes[i];
1036 new_child_infos.push(ProcessNewNode(child));
1040 name: node.info.function_name,
1041 positions: DescribePositions(node),
1042 status: node.status,
1043 children: child_infos,
1044 new_children: new_child_infos
1046 if (node.status_explanation) {
1047 res.status_explanation = node.status_explanation;
1049 if (node.textual_corresponding_node) {
1050 res.new_positions = DescribePositions(node.textual_corresponding_node);
1055 function ProcessNewNode(node) {
1056 var child_infos = [];
1057 // Do not list ancestors.
1059 for (var i = 0; i < node.children.length; i++) {
1060 child_infos.push(ProcessNewNode(node.children[i]));
1064 name: node.info.function_name,
1065 positions: DescribePositions(node),
1066 children: child_infos,
1071 function DescribePositions(node) {
1073 start_position: node.info.start_position,
1074 end_position: node.info.end_position
1078 return ProcessOldNode(old_code_tree);
1082 // Functions are public for tests.
1084 PosTranslator: PosTranslator,
1085 CompareStrings: CompareStrings,
1086 ApplySingleChunkPatch: ApplySingleChunkPatch