blob: 07214f9657c0b3a99fa7809c41c28fe96234554b [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.
22Debug.LiveEdit = new function() {
23
24 // Forward declaration for minifier.
25 var FunctionStatus;
Ben Murdochf87a2032010-10-22 12:50:53 +010026
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027 var NEEDS_STEP_IN_PROPERTY_NAME = "stack_update_needs_step_in";
28
Steve Block6ded16b2010-05-10 14:33:55 +010029 // Applies the change to the script.
30 // The change is in form of list of chunks encoded in a single array as
31 // a series of triplets (pos1_start, pos1_end, pos2_end)
Steve Block8defd9f2010-07-08 12:39:36 +010032 function ApplyPatchMultiChunk(script, diff_array, new_source, preview_only,
33 change_log) {
Steve Block6ded16b2010-05-10 14:33:55 +010034
35 var old_source = script.source;
36
37 // Gather compile information about old version of script.
38 var old_compile_info = GatherCompileInfo(old_source, script);
Ben Murdochf87a2032010-10-22 12:50:53 +010039
Steve Block6ded16b2010-05-10 14:33:55 +010040 // Build tree structures for old and new versions of the script.
41 var root_old_node = BuildCodeInfoTree(old_compile_info);
42
43 var pos_translator = new PosTranslator(diff_array);
44
45 // Analyze changes.
46 MarkChangedFunctions(root_old_node, pos_translator.GetChunks());
47
48 // Find all SharedFunctionInfo's that were compiled from this script.
49 FindLiveSharedInfos(root_old_node, script);
Ben Murdochf87a2032010-10-22 12:50:53 +010050
Steve Block6ded16b2010-05-10 14:33:55 +010051 // Gather compile information about new version of script.
52 var new_compile_info;
53 try {
54 new_compile_info = GatherCompileInfo(new_source, script);
55 } catch (e) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056 var failure =
57 new Failure("Failed to compile new version of script: " + e);
58 if (e instanceof SyntaxError) {
59 var details = {
60 type: "liveedit_compile_error",
61 syntaxErrorMessage: e.message
62 };
63 CopyErrorPositionToDetails(e, details);
64 failure.details = details;
65 }
66 throw failure;
Steve Block6ded16b2010-05-10 14:33:55 +010067 }
68 var root_new_node = BuildCodeInfoTree(new_compile_info);
69
70 // Link recompiled script data with other data.
71 FindCorrespondingFunctions(root_old_node, root_new_node);
Ben Murdochf87a2032010-10-22 12:50:53 +010072
Steve Block6ded16b2010-05-10 14:33:55 +010073 // Prepare to-do lists.
74 var replace_code_list = new Array();
75 var link_to_old_script_list = new Array();
76 var link_to_original_script_list = new Array();
77 var update_positions_list = new Array();
78
79 function HarvestTodo(old_node) {
80 function CollectDamaged(node) {
81 link_to_old_script_list.push(node);
82 for (var i = 0; i < node.children.length; i++) {
83 CollectDamaged(node.children[i]);
84 }
85 }
86
87 // Recursively collects all newly compiled functions that are going into
Steve Block8defd9f2010-07-08 12:39:36 +010088 // business and should have link to the actual script updated.
Steve Block6ded16b2010-05-10 14:33:55 +010089 function CollectNew(node_list) {
90 for (var i = 0; i < node_list.length; i++) {
91 link_to_original_script_list.push(node_list[i]);
92 CollectNew(node_list[i].children);
93 }
94 }
Ben Murdochf87a2032010-10-22 12:50:53 +010095
Steve Block6ded16b2010-05-10 14:33:55 +010096 if (old_node.status == FunctionStatus.DAMAGED) {
97 CollectDamaged(old_node);
98 return;
99 }
100 if (old_node.status == FunctionStatus.UNCHANGED) {
101 update_positions_list.push(old_node);
102 } else if (old_node.status == FunctionStatus.SOURCE_CHANGED) {
103 update_positions_list.push(old_node);
104 } else if (old_node.status == FunctionStatus.CHANGED) {
105 replace_code_list.push(old_node);
106 CollectNew(old_node.unmatched_new_nodes);
107 }
108 for (var i = 0; i < old_node.children.length; i++) {
109 HarvestTodo(old_node.children[i]);
110 }
111 }
112
Steve Block8defd9f2010-07-08 12:39:36 +0100113 var preview_description = {
114 change_tree: DescribeChangeTree(root_old_node),
115 textual_diff: {
116 old_len: old_source.length,
117 new_len: new_source.length,
118 chunks: diff_array
119 },
120 updated: false
121 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100122
Steve Block8defd9f2010-07-08 12:39:36 +0100123 if (preview_only) {
124 return preview_description;
125 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100126
Steve Block6ded16b2010-05-10 14:33:55 +0100127 HarvestTodo(root_old_node);
Ben Murdochf87a2032010-10-22 12:50:53 +0100128
Steve Block6ded16b2010-05-10 14:33:55 +0100129 // Collect shared infos for functions whose code need to be patched.
130 var replaced_function_infos = new Array();
131 for (var i = 0; i < replace_code_list.length; i++) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100132 var live_shared_function_infos =
133 replace_code_list[i].live_shared_function_infos;
134
135 if (live_shared_function_infos) {
Steve Block9fac8402011-05-12 15:51:54 +0100136 for (var j = 0; j < live_shared_function_infos.length; j++) {
137 replaced_function_infos.push(live_shared_function_infos[j]);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100138 }
Steve Block6ded16b2010-05-10 14:33:55 +0100139 }
140 }
141
Steve Block6ded16b2010-05-10 14:33:55 +0100142 // We haven't changed anything before this line yet.
143 // Committing all changes.
Ben Murdochf87a2032010-10-22 12:50:53 +0100144
Steve Block8defd9f2010-07-08 12:39:36 +0100145 // Check that function being patched is not currently on stack or drop them.
146 var dropped_functions_number =
147 CheckStackActivations(replaced_function_infos, change_log);
Ben Murdochf87a2032010-10-22 12:50:53 +0100148
149 preview_description.stack_modified = dropped_functions_number != 0;
150
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151 // Our current implementation requires client to manually issue "step in"
152 // command for correct stack state.
153 preview_description[NEEDS_STEP_IN_PROPERTY_NAME] =
154 preview_description.stack_modified;
155
Ben Murdochf87a2032010-10-22 12:50:53 +0100156 // Start with breakpoints. Convert their line/column positions and
Steve Block6ded16b2010-05-10 14:33:55 +0100157 // temporary remove.
158 var break_points_restorer = TemporaryRemoveBreakPoints(script, change_log);
159
160 var old_script;
161
162 // Create an old script only if there are function that should be linked
163 // to old version.
164 if (link_to_old_script_list.length == 0) {
165 %LiveEditReplaceScript(script, new_source, null);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000166 old_script = UNDEFINED;
Steve Block6ded16b2010-05-10 14:33:55 +0100167 } else {
168 var old_script_name = CreateNameForOldScript(script);
Ben Murdochf87a2032010-10-22 12:50:53 +0100169
Steve Block6ded16b2010-05-10 14:33:55 +0100170 // Update the script text and create a new script representing an old
171 // version of the script.
172 old_script = %LiveEditReplaceScript(script, new_source,
173 old_script_name);
Ben Murdochf87a2032010-10-22 12:50:53 +0100174
Steve Block6ded16b2010-05-10 14:33:55 +0100175 var link_to_old_script_report = new Array();
176 change_log.push( { linked_to_old_script: link_to_old_script_report } );
Ben Murdochf87a2032010-10-22 12:50:53 +0100177
Steve Block6ded16b2010-05-10 14:33:55 +0100178 // We need to link to old script all former nested functions.
179 for (var i = 0; i < link_to_old_script_list.length; i++) {
180 LinkToOldScript(link_to_old_script_list[i], old_script,
181 link_to_old_script_report);
182 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100183
Steve Block8defd9f2010-07-08 12:39:36 +0100184 preview_description.created_script_name = old_script_name;
Steve Block6ded16b2010-05-10 14:33:55 +0100185 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100186
Steve Block6ded16b2010-05-10 14:33:55 +0100187 // Link to an actual script all the functions that we are going to use.
188 for (var i = 0; i < link_to_original_script_list.length; i++) {
189 %LiveEditFunctionSetScript(
190 link_to_original_script_list[i].info.shared_function_info, script);
191 }
192
193 for (var i = 0; i < replace_code_list.length; i++) {
194 PatchFunctionCode(replace_code_list[i], change_log);
195 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100196
Steve Block6ded16b2010-05-10 14:33:55 +0100197 var position_patch_report = new Array();
198 change_log.push( {position_patched: position_patch_report} );
Ben Murdochf87a2032010-10-22 12:50:53 +0100199
Steve Block6ded16b2010-05-10 14:33:55 +0100200 for (var i = 0; i < update_positions_list.length; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000201 // TODO(LiveEdit): take into account whether it's source_changed or
Steve Block6ded16b2010-05-10 14:33:55 +0100202 // unchanged and whether positions changed at all.
203 PatchPositions(update_positions_list[i], diff_array,
204 position_patch_report);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100205
206 if (update_positions_list[i].live_shared_function_infos) {
207 update_positions_list[i].live_shared_function_infos.
208 forEach(function (info) {
209 %LiveEditFunctionSourceUpdated(info.raw_array);
210 });
211 }
Steve Block6ded16b2010-05-10 14:33:55 +0100212 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100213
Steve Block6ded16b2010-05-10 14:33:55 +0100214 break_points_restorer(pos_translator, old_script);
Ben Murdochf87a2032010-10-22 12:50:53 +0100215
Steve Block8defd9f2010-07-08 12:39:36 +0100216 preview_description.updated = true;
217 return preview_description;
Steve Block6ded16b2010-05-10 14:33:55 +0100218 }
219 // Function is public.
220 this.ApplyPatchMultiChunk = ApplyPatchMultiChunk;
221
Ben Murdochf87a2032010-10-22 12:50:53 +0100222
Steve Block6ded16b2010-05-10 14:33:55 +0100223 // Fully compiles source string as a script. Returns Array of
224 // FunctionCompileInfo -- a descriptions of all functions of the script.
225 // Elements of array are ordered by start positions of functions (from top
226 // to bottom) in the source. Fields outer_index and next_sibling_index help
227 // to navigate the nesting structure of functions.
228 //
229 // All functions get compiled linked to script provided as parameter script.
230 // TODO(LiveEdit): consider not using actual scripts as script, because
Ben Murdochf87a2032010-10-22 12:50:53 +0100231 // we have to manually erase all links right after compile.
Steve Block6ded16b2010-05-10 14:33:55 +0100232 function GatherCompileInfo(source, script) {
233 // Get function info, elements are partially sorted (it is a tree of
234 // nested functions serialized as parent followed by serialized children.
235 var raw_compile_info = %LiveEditGatherCompileInfo(script, source);
236
237 // Sort function infos by start position field.
238 var compile_info = new Array();
239 var old_index_map = new Array();
240 for (var i = 0; i < raw_compile_info.length; i++) {
241 var info = new FunctionCompileInfo(raw_compile_info[i]);
242 // Remove all links to the actual script. Breakpoints system and
243 // LiveEdit itself believe that any function in heap that points to a
244 // particular script is a regular function.
245 // For some functions we will restore this link later.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000246 %LiveEditFunctionSetScript(info.shared_function_info, UNDEFINED);
Steve Block6ded16b2010-05-10 14:33:55 +0100247 compile_info.push(info);
248 old_index_map.push(i);
249 }
250
251 for (var i = 0; i < compile_info.length; i++) {
252 var k = i;
253 for (var j = i + 1; j < compile_info.length; j++) {
254 if (compile_info[k].start_position > compile_info[j].start_position) {
255 k = j;
256 }
257 }
258 if (k != i) {
259 var temp_info = compile_info[k];
260 var temp_index = old_index_map[k];
261 compile_info[k] = compile_info[i];
262 old_index_map[k] = old_index_map[i];
263 compile_info[i] = temp_info;
264 old_index_map[i] = temp_index;
265 }
266 }
267
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000268 // After sorting update outer_index field using old_index_map. Also
Steve Block6ded16b2010-05-10 14:33:55 +0100269 // set next_sibling_index field.
270 var current_index = 0;
271
272 // The recursive function, that goes over all children of a particular
273 // node (i.e. function info).
274 function ResetIndexes(new_parent_index, old_parent_index) {
275 var previous_sibling = -1;
276 while (current_index < compile_info.length &&
277 compile_info[current_index].outer_index == old_parent_index) {
278 var saved_index = current_index;
279 compile_info[saved_index].outer_index = new_parent_index;
280 if (previous_sibling != -1) {
281 compile_info[previous_sibling].next_sibling_index = saved_index;
282 }
283 previous_sibling = saved_index;
284 current_index++;
285 ResetIndexes(saved_index, old_index_map[saved_index]);
286 }
287 if (previous_sibling != -1) {
288 compile_info[previous_sibling].next_sibling_index = -1;
289 }
290 }
291
292 ResetIndexes(-1, -1);
293 Assert(current_index == compile_info.length);
294
295 return compile_info;
296 }
297
Ben Murdochf87a2032010-10-22 12:50:53 +0100298
Steve Block6ded16b2010-05-10 14:33:55 +0100299 // Replaces function's Code.
300 function PatchFunctionCode(old_node, change_log) {
301 var new_info = old_node.corresponding_node.info;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100302 if (old_node.live_shared_function_infos) {
303 old_node.live_shared_function_infos.forEach(function (old_info) {
304 %LiveEditReplaceFunctionCode(new_info.raw_array,
305 old_info.raw_array);
Steve Block6ded16b2010-05-10 14:33:55 +0100306
Ben Murdochb0fe1622011-05-05 13:52:32 +0100307 // The function got a new code. However, this new code brings all new
308 // instances of SharedFunctionInfo for nested functions. However,
309 // we want the original instances to be used wherever possible.
310 // (This is because old instances and new instances will be both
311 // linked to a script and breakpoints subsystem does not really
312 // expects this; neither does LiveEdit subsystem on next call).
313 for (var i = 0; i < old_node.children.length; i++) {
314 if (old_node.children[i].corresponding_node) {
315 var corresponding_child_info =
316 old_node.children[i].corresponding_node.info.
317 shared_function_info;
318
319 if (old_node.children[i].live_shared_function_infos) {
320 old_node.children[i].live_shared_function_infos.
321 forEach(function (old_child_info) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100322 %LiveEditReplaceRefToNestedFunction(
323 old_info.info,
324 corresponding_child_info,
325 old_child_info.info);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100326 });
327 }
Steve Block6ded16b2010-05-10 14:33:55 +0100328 }
329 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100330 });
Ben Murdochf87a2032010-10-22 12:50:53 +0100331
Steve Block6ded16b2010-05-10 14:33:55 +0100332 change_log.push( {function_patched: new_info.function_name} );
333 } else {
334 change_log.push( {function_patched: new_info.function_name,
335 function_info_not_found: true} );
336 }
337 }
338
Ben Murdochf87a2032010-10-22 12:50:53 +0100339
Steve Block6ded16b2010-05-10 14:33:55 +0100340 // Makes a function associated with another instance of a script (the
341 // one representing its old version). This way the function still
342 // may access its own text.
343 function LinkToOldScript(old_info_node, old_script, report_array) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100344 if (old_info_node.live_shared_function_infos) {
345 old_info_node.live_shared_function_infos.
346 forEach(function (info) {
347 %LiveEditFunctionSetScript(info.info, old_script);
348 });
349
350 report_array.push( { name: old_info_node.info.function_name } );
Steve Block6ded16b2010-05-10 14:33:55 +0100351 } else {
352 report_array.push(
353 { name: old_info_node.info.function_name, not_found: true } );
354 }
355 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100356
Steve Block6ded16b2010-05-10 14:33:55 +0100357
358 // Returns function that restores breakpoints.
359 function TemporaryRemoveBreakPoints(original_script, change_log) {
360 var script_break_points = GetScriptBreakPoints(original_script);
Ben Murdochf87a2032010-10-22 12:50:53 +0100361
Steve Block6ded16b2010-05-10 14:33:55 +0100362 var break_points_update_report = [];
363 change_log.push( { break_points_update: break_points_update_report } );
364
365 var break_point_old_positions = [];
366 for (var i = 0; i < script_break_points.length; i++) {
367 var break_point = script_break_points[i];
368
369 break_point.clear();
Ben Murdochf87a2032010-10-22 12:50:53 +0100370
371 // TODO(LiveEdit): be careful with resource offset here.
Steve Block6ded16b2010-05-10 14:33:55 +0100372 var break_point_position = Debug.findScriptSourcePosition(original_script,
373 break_point.line(), break_point.column());
Ben Murdochf87a2032010-10-22 12:50:53 +0100374
Steve Block6ded16b2010-05-10 14:33:55 +0100375 var old_position_description = {
376 position: break_point_position,
377 line: break_point.line(),
378 column: break_point.column()
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100379 };
Steve Block6ded16b2010-05-10 14:33:55 +0100380 break_point_old_positions.push(old_position_description);
381 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100382
383
Steve Block6ded16b2010-05-10 14:33:55 +0100384 // Restores breakpoints and creates their copies in the "old" copy of
385 // the script.
386 return function (pos_translator, old_script_copy_opt) {
387 // Update breakpoints (change positions and restore them in old version
388 // of script.
389 for (var i = 0; i < script_break_points.length; i++) {
390 var break_point = script_break_points[i];
391 if (old_script_copy_opt) {
392 var clone = break_point.cloneForOtherScript(old_script_copy_opt);
393 clone.set(old_script_copy_opt);
Ben Murdochf87a2032010-10-22 12:50:53 +0100394
Steve Block6ded16b2010-05-10 14:33:55 +0100395 break_points_update_report.push( {
396 type: "copied_to_old",
397 id: break_point.number(),
Ben Murdochf87a2032010-10-22 12:50:53 +0100398 new_id: clone.number(),
Steve Block6ded16b2010-05-10 14:33:55 +0100399 positions: break_point_old_positions[i]
400 } );
401 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100402
Steve Block6ded16b2010-05-10 14:33:55 +0100403 var updated_position = pos_translator.Translate(
404 break_point_old_positions[i].position,
405 PosTranslator.ShiftWithTopInsideChunkHandler);
Ben Murdochf87a2032010-10-22 12:50:53 +0100406
Steve Block6ded16b2010-05-10 14:33:55 +0100407 var new_location =
408 original_script.locationFromPosition(updated_position, false);
409
410 break_point.update_positions(new_location.line, new_location.column);
411
412 var new_position_description = {
413 position: updated_position,
414 line: new_location.line,
415 column: new_location.column
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100416 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100417
Steve Block6ded16b2010-05-10 14:33:55 +0100418 break_point.set(original_script);
Ben Murdochf87a2032010-10-22 12:50:53 +0100419
Steve Block6ded16b2010-05-10 14:33:55 +0100420 break_points_update_report.push( { type: "position_changed",
421 id: break_point.number(),
422 old_positions: break_point_old_positions[i],
423 new_positions: new_position_description
424 } );
425 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100426 };
Steve Block6ded16b2010-05-10 14:33:55 +0100427 }
428
Ben Murdochf87a2032010-10-22 12:50:53 +0100429
Steve Block6ded16b2010-05-10 14:33:55 +0100430 function Assert(condition, message) {
431 if (!condition) {
432 if (message) {
433 throw "Assert " + message;
434 } else {
435 throw "Assert";
436 }
437 }
438 }
439
440 function DiffChunk(pos1, pos2, len1, len2) {
441 this.pos1 = pos1;
442 this.pos2 = pos2;
443 this.len1 = len1;
444 this.len2 = len2;
445 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100446
Steve Block6ded16b2010-05-10 14:33:55 +0100447 function PosTranslator(diff_array) {
448 var chunks = new Array();
449 var current_diff = 0;
450 for (var i = 0; i < diff_array.length; i += 3) {
451 var pos1_begin = diff_array[i];
452 var pos2_begin = pos1_begin + current_diff;
453 var pos1_end = diff_array[i + 1];
454 var pos2_end = diff_array[i + 2];
455 chunks.push(new DiffChunk(pos1_begin, pos2_begin, pos1_end - pos1_begin,
456 pos2_end - pos2_begin));
Ben Murdochf87a2032010-10-22 12:50:53 +0100457 current_diff = pos2_end - pos1_end;
Steve Block6ded16b2010-05-10 14:33:55 +0100458 }
459 this.chunks = chunks;
460 }
461 PosTranslator.prototype.GetChunks = function() {
462 return this.chunks;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100463 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100464
Steve Block6ded16b2010-05-10 14:33:55 +0100465 PosTranslator.prototype.Translate = function(pos, inside_chunk_handler) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100466 var array = this.chunks;
Steve Block6ded16b2010-05-10 14:33:55 +0100467 if (array.length == 0 || pos < array[0].pos1) {
468 return pos;
469 }
470 var chunk_index1 = 0;
471 var chunk_index2 = array.length - 1;
472
473 while (chunk_index1 < chunk_index2) {
474 var middle_index = Math.floor((chunk_index1 + chunk_index2) / 2);
475 if (pos < array[middle_index + 1].pos1) {
476 chunk_index2 = middle_index;
477 } else {
478 chunk_index1 = middle_index + 1;
479 }
480 }
481 var chunk = array[chunk_index1];
482 if (pos >= chunk.pos1 + chunk.len1) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100483 return pos + chunk.pos2 + chunk.len2 - chunk.pos1 - chunk.len1;
Steve Block6ded16b2010-05-10 14:33:55 +0100484 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100485
Steve Block6ded16b2010-05-10 14:33:55 +0100486 if (!inside_chunk_handler) {
487 inside_chunk_handler = PosTranslator.DefaultInsideChunkHandler;
488 }
489 return inside_chunk_handler(pos, chunk);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100490 };
Steve Block6ded16b2010-05-10 14:33:55 +0100491
492 PosTranslator.DefaultInsideChunkHandler = function(pos, diff_chunk) {
493 Assert(false, "Cannot translate position in changed area");
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100494 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100495
Steve Block6ded16b2010-05-10 14:33:55 +0100496 PosTranslator.ShiftWithTopInsideChunkHandler =
497 function(pos, diff_chunk) {
498 // We carelessly do not check whether we stay inside the chunk after
499 // translation.
Ben Murdochf87a2032010-10-22 12:50:53 +0100500 return pos - diff_chunk.pos1 + diff_chunk.pos2;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100501 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100502
Steve Block6ded16b2010-05-10 14:33:55 +0100503 var FunctionStatus = {
504 // No change to function or its inner functions; however its positions
Ben Murdochf87a2032010-10-22 12:50:53 +0100505 // in script may have been shifted.
Steve Block6ded16b2010-05-10 14:33:55 +0100506 UNCHANGED: "unchanged",
507 // The code of a function remains unchanged, but something happened inside
508 // some inner functions.
509 SOURCE_CHANGED: "source changed",
510 // The code of a function is changed or some nested function cannot be
511 // properly patched so this function must be recompiled.
512 CHANGED: "changed",
513 // Function is changed but cannot be patched.
514 DAMAGED: "damaged"
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100515 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100516
Steve Block6ded16b2010-05-10 14:33:55 +0100517 function CodeInfoTreeNode(code_info, children, array_index) {
518 this.info = code_info;
519 this.children = children;
520 // an index in array of compile_info
Ben Murdochf87a2032010-10-22 12:50:53 +0100521 this.array_index = array_index;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000522 this.parent = UNDEFINED;
Ben Murdochf87a2032010-10-22 12:50:53 +0100523
Steve Block6ded16b2010-05-10 14:33:55 +0100524 this.status = FunctionStatus.UNCHANGED;
525 // Status explanation is used for debugging purposes and will be shown
526 // in user UI if some explanations are needed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000527 this.status_explanation = UNDEFINED;
528 this.new_start_pos = UNDEFINED;
529 this.new_end_pos = UNDEFINED;
530 this.corresponding_node = UNDEFINED;
531 this.unmatched_new_nodes = UNDEFINED;
Ben Murdochf87a2032010-10-22 12:50:53 +0100532
Steve Block8defd9f2010-07-08 12:39:36 +0100533 // 'Textual' correspondence/matching is weaker than 'pure'
534 // correspondence/matching. We need 'textual' level for visual presentation
535 // in UI, we use 'pure' level for actual code manipulation.
536 // Sometimes only function body is changed (functions in old and new script
537 // textually correspond), but we cannot patch the code, so we see them
Ben Murdochf87a2032010-10-22 12:50:53 +0100538 // as an old function deleted and new function created.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000539 this.textual_corresponding_node = UNDEFINED;
540 this.textually_unmatched_new_nodes = UNDEFINED;
Ben Murdochf87a2032010-10-22 12:50:53 +0100541
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000542 this.live_shared_function_infos = UNDEFINED;
Steve Block6ded16b2010-05-10 14:33:55 +0100543 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100544
Steve Block6ded16b2010-05-10 14:33:55 +0100545 // From array of function infos that is implicitly a tree creates
546 // an actual tree of functions in script.
547 function BuildCodeInfoTree(code_info_array) {
548 // Throughtout all function we iterate over input array.
549 var index = 0;
550
Ben Murdochf87a2032010-10-22 12:50:53 +0100551 // Recursive function that builds a branch of tree.
Steve Block6ded16b2010-05-10 14:33:55 +0100552 function BuildNode() {
553 var my_index = index;
554 index++;
555 var child_array = new Array();
556 while (index < code_info_array.length &&
557 code_info_array[index].outer_index == my_index) {
558 child_array.push(BuildNode());
559 }
560 var node = new CodeInfoTreeNode(code_info_array[my_index], child_array,
561 my_index);
562 for (var i = 0; i < child_array.length; i++) {
563 child_array[i].parent = node;
564 }
565 return node;
566 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100567
Steve Block6ded16b2010-05-10 14:33:55 +0100568 var root = BuildNode();
569 Assert(index == code_info_array.length);
570 return root;
571 }
572
573 // Applies a list of the textual diff chunks onto the tree of functions.
574 // Determines status of each function (from unchanged to damaged). However
575 // children of unchanged functions are ignored.
576 function MarkChangedFunctions(code_info_tree, chunks) {
577
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100578 // A convenient iterator over diff chunks that also translates
Steve Block6ded16b2010-05-10 14:33:55 +0100579 // positions from old to new in a current non-changed part of script.
580 var chunk_it = new function() {
581 var chunk_index = 0;
582 var pos_diff = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100583 this.current = function() { return chunks[chunk_index]; };
Steve Block6ded16b2010-05-10 14:33:55 +0100584 this.next = function() {
585 var chunk = chunks[chunk_index];
Ben Murdochf87a2032010-10-22 12:50:53 +0100586 pos_diff = chunk.pos2 + chunk.len2 - (chunk.pos1 + chunk.len1);
Steve Block6ded16b2010-05-10 14:33:55 +0100587 chunk_index++;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100588 };
589 this.done = function() { return chunk_index >= chunks.length; };
590 this.TranslatePos = function(pos) { return pos + pos_diff; };
Steve Block6ded16b2010-05-10 14:33:55 +0100591 };
592
593 // A recursive function that processes internals of a function and all its
594 // inner functions. Iterator chunk_it initially points to a chunk that is
595 // below function start.
596 function ProcessInternals(info_node) {
597 info_node.new_start_pos = chunk_it.TranslatePos(
Ben Murdochf87a2032010-10-22 12:50:53 +0100598 info_node.info.start_position);
Steve Block6ded16b2010-05-10 14:33:55 +0100599 var child_index = 0;
600 var code_changed = false;
601 var source_changed = false;
602 // Simultaneously iterates over child functions and over chunks.
603 while (!chunk_it.done() &&
604 chunk_it.current().pos1 < info_node.info.end_position) {
605 if (child_index < info_node.children.length) {
606 var child = info_node.children[child_index];
Ben Murdochf87a2032010-10-22 12:50:53 +0100607
Steve Block6ded16b2010-05-10 14:33:55 +0100608 if (child.info.end_position <= chunk_it.current().pos1) {
609 ProcessUnchangedChild(child);
610 child_index++;
611 continue;
612 } else if (child.info.start_position >=
613 chunk_it.current().pos1 + chunk_it.current().len1) {
614 code_changed = true;
615 chunk_it.next();
616 continue;
617 } else if (child.info.start_position <= chunk_it.current().pos1 &&
618 child.info.end_position >= chunk_it.current().pos1 +
619 chunk_it.current().len1) {
620 ProcessInternals(child);
621 source_changed = source_changed ||
622 ( child.status != FunctionStatus.UNCHANGED );
623 code_changed = code_changed ||
624 ( child.status == FunctionStatus.DAMAGED );
625 child_index++;
626 continue;
627 } else {
628 code_changed = true;
629 child.status = FunctionStatus.DAMAGED;
630 child.status_explanation =
631 "Text diff overlaps with function boundary";
632 child_index++;
633 continue;
634 }
635 } else {
Ben Murdochf87a2032010-10-22 12:50:53 +0100636 if (chunk_it.current().pos1 + chunk_it.current().len1 <=
Steve Block6ded16b2010-05-10 14:33:55 +0100637 info_node.info.end_position) {
638 info_node.status = FunctionStatus.CHANGED;
639 chunk_it.next();
640 continue;
641 } else {
642 info_node.status = FunctionStatus.DAMAGED;
643 info_node.status_explanation =
644 "Text diff overlaps with function boundary";
645 return;
646 }
647 }
648 Assert("Unreachable", false);
649 }
650 while (child_index < info_node.children.length) {
651 var child = info_node.children[child_index];
652 ProcessUnchangedChild(child);
653 child_index++;
654 }
655 if (code_changed) {
656 info_node.status = FunctionStatus.CHANGED;
657 } else if (source_changed) {
658 info_node.status = FunctionStatus.SOURCE_CHANGED;
659 }
660 info_node.new_end_pos =
Ben Murdochf87a2032010-10-22 12:50:53 +0100661 chunk_it.TranslatePos(info_node.info.end_position);
Steve Block6ded16b2010-05-10 14:33:55 +0100662 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100663
Steve Block6ded16b2010-05-10 14:33:55 +0100664 function ProcessUnchangedChild(node) {
665 node.new_start_pos = chunk_it.TranslatePos(node.info.start_position);
666 node.new_end_pos = chunk_it.TranslatePos(node.info.end_position);
667 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100668
Steve Block6ded16b2010-05-10 14:33:55 +0100669 ProcessInternals(code_info_tree);
670 }
671
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000672 // For each old function (if it is not damaged) tries to find a corresponding
Steve Block6ded16b2010-05-10 14:33:55 +0100673 // function in new script. Typically it should succeed (non-damaged functions
674 // by definition may only have changes inside their bodies). However there are
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000675 // reasons for correspondence not to be found; function with unmodified text
Steve Block6ded16b2010-05-10 14:33:55 +0100676 // in new script may become enclosed into other function; the innocent change
677 // inside function body may in fact be something like "} function B() {" that
678 // splits a function into 2 functions.
679 function FindCorrespondingFunctions(old_code_tree, new_code_tree) {
680
681 // A recursive function that tries to find a correspondence for all
682 // child functions and for their inner functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000683 function ProcessNode(old_node, new_node) {
684 var scope_change_description =
685 IsFunctionContextLocalsChanged(old_node.info, new_node.info);
686 if (scope_change_description) {
687 old_node.status = FunctionStatus.CHANGED;
688 }
689
Steve Block6ded16b2010-05-10 14:33:55 +0100690 var old_children = old_node.children;
691 var new_children = new_node.children;
Ben Murdochf87a2032010-10-22 12:50:53 +0100692
Steve Block6ded16b2010-05-10 14:33:55 +0100693 var unmatched_new_nodes_list = [];
Steve Block8defd9f2010-07-08 12:39:36 +0100694 var textually_unmatched_new_nodes_list = [];
Steve Block6ded16b2010-05-10 14:33:55 +0100695
696 var old_index = 0;
697 var new_index = 0;
698 while (old_index < old_children.length) {
699 if (old_children[old_index].status == FunctionStatus.DAMAGED) {
700 old_index++;
701 } else if (new_index < new_children.length) {
702 if (new_children[new_index].info.start_position <
703 old_children[old_index].new_start_pos) {
704 unmatched_new_nodes_list.push(new_children[new_index]);
Steve Block8defd9f2010-07-08 12:39:36 +0100705 textually_unmatched_new_nodes_list.push(new_children[new_index]);
Steve Block6ded16b2010-05-10 14:33:55 +0100706 new_index++;
707 } else if (new_children[new_index].info.start_position ==
708 old_children[old_index].new_start_pos) {
709 if (new_children[new_index].info.end_position ==
710 old_children[old_index].new_end_pos) {
711 old_children[old_index].corresponding_node =
712 new_children[new_index];
Steve Block8defd9f2010-07-08 12:39:36 +0100713 old_children[old_index].textual_corresponding_node =
714 new_children[new_index];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000715 if (scope_change_description) {
716 old_children[old_index].status = FunctionStatus.DAMAGED;
717 old_children[old_index].status_explanation =
718 "Enclosing function is now incompatible. " +
719 scope_change_description;
720 old_children[old_index].corresponding_node = UNDEFINED;
721 } else if (old_children[old_index].status !=
722 FunctionStatus.UNCHANGED) {
723 ProcessNode(old_children[old_index],
Steve Block6ded16b2010-05-10 14:33:55 +0100724 new_children[new_index]);
725 if (old_children[old_index].status == FunctionStatus.DAMAGED) {
726 unmatched_new_nodes_list.push(
727 old_children[old_index].corresponding_node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000728 old_children[old_index].corresponding_node = UNDEFINED;
Steve Block6ded16b2010-05-10 14:33:55 +0100729 old_node.status = FunctionStatus.CHANGED;
730 }
731 }
732 } else {
733 old_children[old_index].status = FunctionStatus.DAMAGED;
734 old_children[old_index].status_explanation =
735 "No corresponding function in new script found";
736 old_node.status = FunctionStatus.CHANGED;
737 unmatched_new_nodes_list.push(new_children[new_index]);
Steve Block8defd9f2010-07-08 12:39:36 +0100738 textually_unmatched_new_nodes_list.push(new_children[new_index]);
Steve Block6ded16b2010-05-10 14:33:55 +0100739 }
740 new_index++;
741 old_index++;
742 } else {
743 old_children[old_index].status = FunctionStatus.DAMAGED;
744 old_children[old_index].status_explanation =
745 "No corresponding function in new script found";
746 old_node.status = FunctionStatus.CHANGED;
747 old_index++;
748 }
749 } else {
750 old_children[old_index].status = FunctionStatus.DAMAGED;
751 old_children[old_index].status_explanation =
752 "No corresponding function in new script found";
753 old_node.status = FunctionStatus.CHANGED;
754 old_index++;
755 }
756 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100757
Steve Block6ded16b2010-05-10 14:33:55 +0100758 while (new_index < new_children.length) {
759 unmatched_new_nodes_list.push(new_children[new_index]);
Steve Block8defd9f2010-07-08 12:39:36 +0100760 textually_unmatched_new_nodes_list.push(new_children[new_index]);
Steve Block6ded16b2010-05-10 14:33:55 +0100761 new_index++;
762 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100763
Steve Block6ded16b2010-05-10 14:33:55 +0100764 if (old_node.status == FunctionStatus.CHANGED) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000765 if (old_node.info.param_num != new_node.info.param_num) {
Steve Block6ded16b2010-05-10 14:33:55 +0100766 old_node.status = FunctionStatus.DAMAGED;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000767 old_node.status_explanation = "Changed parameter number: " +
768 old_node.info.param_num + " and " + new_node.info.param_num;
Steve Block6ded16b2010-05-10 14:33:55 +0100769 }
770 }
771 old_node.unmatched_new_nodes = unmatched_new_nodes_list;
Steve Block8defd9f2010-07-08 12:39:36 +0100772 old_node.textually_unmatched_new_nodes =
773 textually_unmatched_new_nodes_list;
Steve Block6ded16b2010-05-10 14:33:55 +0100774 }
775
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000776 ProcessNode(old_code_tree, new_code_tree);
Ben Murdochf87a2032010-10-22 12:50:53 +0100777
Steve Block6ded16b2010-05-10 14:33:55 +0100778 old_code_tree.corresponding_node = new_code_tree;
Steve Block8defd9f2010-07-08 12:39:36 +0100779 old_code_tree.textual_corresponding_node = new_code_tree;
780
Steve Block6ded16b2010-05-10 14:33:55 +0100781 Assert(old_code_tree.status != FunctionStatus.DAMAGED,
782 "Script became damaged");
783 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100784
Steve Block6ded16b2010-05-10 14:33:55 +0100785 function FindLiveSharedInfos(old_code_tree, script) {
786 var shared_raw_list = %LiveEditFindSharedFunctionInfosForScript(script);
Ben Murdochf87a2032010-10-22 12:50:53 +0100787
Steve Block6ded16b2010-05-10 14:33:55 +0100788 var shared_infos = new Array();
Ben Murdochf87a2032010-10-22 12:50:53 +0100789
Steve Block6ded16b2010-05-10 14:33:55 +0100790 for (var i = 0; i < shared_raw_list.length; i++) {
791 shared_infos.push(new SharedInfoWrapper(shared_raw_list[i]));
792 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100793
Ben Murdochb0fe1622011-05-05 13:52:32 +0100794 // Finds all SharedFunctionInfos that corresponds to compile info
Steve Block6ded16b2010-05-10 14:33:55 +0100795 // in old version of the script.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100796 function FindFunctionInfos(compile_info) {
797 var wrappers = [];
798
Steve Block6ded16b2010-05-10 14:33:55 +0100799 for (var i = 0; i < shared_infos.length; i++) {
800 var wrapper = shared_infos[i];
801 if (wrapper.start_position == compile_info.start_position &&
802 wrapper.end_position == compile_info.end_position) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100803 wrappers.push(wrapper);
Steve Block6ded16b2010-05-10 14:33:55 +0100804 }
805 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100806
807 if (wrappers.length > 0) {
808 return wrappers;
809 }
Steve Block6ded16b2010-05-10 14:33:55 +0100810 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100811
Steve Block6ded16b2010-05-10 14:33:55 +0100812 function TraverseTree(node) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100813 node.live_shared_function_infos = FindFunctionInfos(node.info);
814
Steve Block6ded16b2010-05-10 14:33:55 +0100815 for (var i = 0; i < node.children.length; i++) {
816 TraverseTree(node.children[i]);
817 }
818 }
819
820 TraverseTree(old_code_tree);
821 }
822
Ben Murdochf87a2032010-10-22 12:50:53 +0100823
Steve Block6ded16b2010-05-10 14:33:55 +0100824 // An object describing function compilation details. Its index fields
825 // apply to indexes inside array that stores these objects.
826 function FunctionCompileInfo(raw_array) {
827 this.function_name = raw_array[0];
828 this.start_position = raw_array[1];
829 this.end_position = raw_array[2];
830 this.param_num = raw_array[3];
831 this.code = raw_array[4];
Iain Merrick75681382010-08-19 15:07:18 +0100832 this.code_scope_info = raw_array[5];
833 this.scope_info = raw_array[6];
834 this.outer_index = raw_array[7];
835 this.shared_function_info = raw_array[8];
Steve Block6ded16b2010-05-10 14:33:55 +0100836 this.next_sibling_index = null;
837 this.raw_array = raw_array;
838 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100839
Steve Block6ded16b2010-05-10 14:33:55 +0100840 function SharedInfoWrapper(raw_array) {
841 this.function_name = raw_array[0];
842 this.start_position = raw_array[1];
843 this.end_position = raw_array[2];
844 this.info = raw_array[3];
845 this.raw_array = raw_array;
846 }
847
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000848 // Changes positions (including all statements) in function.
Steve Block6ded16b2010-05-10 14:33:55 +0100849 function PatchPositions(old_info_node, diff_array, report_array) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100850 if (old_info_node.live_shared_function_infos) {
851 old_info_node.live_shared_function_infos.forEach(function (info) {
852 %LiveEditPatchFunctionPositions(info.raw_array,
853 diff_array);
854 });
855
856 report_array.push( { name: old_info_node.info.function_name } );
857 } else {
Steve Block6ded16b2010-05-10 14:33:55 +0100858 // TODO(LiveEdit): function is not compiled yet or is already collected.
Ben Murdochf87a2032010-10-22 12:50:53 +0100859 report_array.push(
Steve Block6ded16b2010-05-10 14:33:55 +0100860 { name: old_info_node.info.function_name, info_not_found: true } );
Steve Block6ded16b2010-05-10 14:33:55 +0100861 }
Steve Block6ded16b2010-05-10 14:33:55 +0100862 }
863
864 // Adds a suffix to script name to mark that it is old version.
865 function CreateNameForOldScript(script) {
866 // TODO(635): try better than this; support several changes.
867 return script.name + " (old)";
868 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100869
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000870 // Compares a function scope heap structure, old and new version, whether it
Steve Block8defd9f2010-07-08 12:39:36 +0100871 // changed or not. Returns explanation if they differ.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000872 function IsFunctionContextLocalsChanged(function_info1, function_info2) {
Steve Block6ded16b2010-05-10 14:33:55 +0100873 var scope_info1 = function_info1.scope_info;
874 var scope_info2 = function_info2.scope_info;
Steve Block8defd9f2010-07-08 12:39:36 +0100875
Ben Murdochf87a2032010-10-22 12:50:53 +0100876 var scope_info1_text;
877 var scope_info2_text;
878
Steve Block8defd9f2010-07-08 12:39:36 +0100879 if (scope_info1) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100880 scope_info1_text = scope_info1.toString();
Steve Block8defd9f2010-07-08 12:39:36 +0100881 } else {
882 scope_info1_text = "";
Steve Block6ded16b2010-05-10 14:33:55 +0100883 }
Steve Block8defd9f2010-07-08 12:39:36 +0100884 if (scope_info2) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100885 scope_info2_text = scope_info2.toString();
Steve Block8defd9f2010-07-08 12:39:36 +0100886 } else {
887 scope_info2_text = "";
Steve Block6ded16b2010-05-10 14:33:55 +0100888 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100889
Steve Block8defd9f2010-07-08 12:39:36 +0100890 if (scope_info1_text != scope_info2_text) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000891 return "Variable map changed: [" + scope_info1_text +
892 "] => [" + scope_info2_text + "]";
Steve Block8defd9f2010-07-08 12:39:36 +0100893 }
894 // No differences. Return undefined.
895 return;
Steve Block6ded16b2010-05-10 14:33:55 +0100896 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100897
Steve Block6ded16b2010-05-10 14:33:55 +0100898 // Minifier forward declaration.
899 var FunctionPatchabilityStatus;
Ben Murdochf87a2032010-10-22 12:50:53 +0100900
Steve Block6ded16b2010-05-10 14:33:55 +0100901 // For array of wrapped shared function infos checks that none of them
902 // have activations on stack (of any thread). Throws a Failure exception
903 // if this proves to be false.
904 function CheckStackActivations(shared_wrapper_list, change_log) {
905 var shared_list = new Array();
906 for (var i = 0; i < shared_wrapper_list.length; i++) {
907 shared_list[i] = shared_wrapper_list[i].info;
908 }
909 var result = %LiveEditCheckAndDropActivations(shared_list, true);
910 if (result[shared_list.length]) {
911 // Extra array element may contain error message.
912 throw new Failure(result[shared_list.length]);
913 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100914
Steve Block6ded16b2010-05-10 14:33:55 +0100915 var problems = new Array();
916 var dropped = new Array();
917 for (var i = 0; i < shared_list.length; i++) {
918 var shared = shared_wrapper_list[i];
919 if (result[i] == FunctionPatchabilityStatus.REPLACED_ON_ACTIVE_STACK) {
920 dropped.push({ name: shared.function_name } );
921 } else if (result[i] != FunctionPatchabilityStatus.AVAILABLE_FOR_PATCH) {
922 var description = {
923 name: shared.function_name,
Ben Murdochf87a2032010-10-22 12:50:53 +0100924 start_pos: shared.start_position,
Steve Block6ded16b2010-05-10 14:33:55 +0100925 end_pos: shared.end_position,
926 replace_problem:
927 FunctionPatchabilityStatus.SymbolName(result[i])
928 };
929 problems.push(description);
930 }
931 }
932 if (dropped.length > 0) {
933 change_log.push({ dropped_from_stack: dropped });
934 }
935 if (problems.length > 0) {
936 change_log.push( { functions_on_stack: problems } );
937 throw new Failure("Blocked by functions on stack");
938 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100939
Steve Block8defd9f2010-07-08 12:39:36 +0100940 return dropped.length;
Steve Block6ded16b2010-05-10 14:33:55 +0100941 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100942
Steve Block6ded16b2010-05-10 14:33:55 +0100943 // A copy of the FunctionPatchabilityStatus enum from liveedit.h
944 var FunctionPatchabilityStatus = {
945 AVAILABLE_FOR_PATCH: 1,
946 BLOCKED_ON_ACTIVE_STACK: 2,
947 BLOCKED_ON_OTHER_STACK: 3,
948 BLOCKED_UNDER_NATIVE_CODE: 4,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000949 REPLACED_ON_ACTIVE_STACK: 5,
950 BLOCKED_UNDER_GENERATOR: 6,
951 BLOCKED_ACTIVE_GENERATOR: 7
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100952 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100953
Steve Block6ded16b2010-05-10 14:33:55 +0100954 FunctionPatchabilityStatus.SymbolName = function(code) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100955 var enumeration = FunctionPatchabilityStatus;
956 for (name in enumeration) {
957 if (enumeration[name] == code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100958 return name;
959 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100960 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100961 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100962
963
Steve Block6ded16b2010-05-10 14:33:55 +0100964 // A logical failure in liveedit process. This means that change_log
965 // is valid and consistent description of what happened.
966 function Failure(message) {
967 this.message = message;
968 }
969 // Function (constructor) is public.
970 this.Failure = Failure;
Ben Murdochf87a2032010-10-22 12:50:53 +0100971
Steve Block6ded16b2010-05-10 14:33:55 +0100972 Failure.prototype.toString = function() {
973 return "LiveEdit Failure: " + this.message;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100974 };
Ben Murdochf87a2032010-10-22 12:50:53 +0100975
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000976 function CopyErrorPositionToDetails(e, details) {
977 function createPositionStruct(script, position) {
978 if (position == -1) return;
979 var location = script.locationFromPosition(position, true);
980 if (location == null) return;
981 return {
982 line: location.line + 1,
983 column: location.column + 1,
984 position: position
985 };
986 }
987
988 if (!("scriptObject" in e) || !("startPosition" in e)) {
989 return;
990 }
991
992 var script = e.scriptObject;
993
994 var position_struct = {
995 start: createPositionStruct(script, e.startPosition),
996 end: createPositionStruct(script, e.endPosition)
997 };
998 details.position = position_struct;
999 }
1000
Steve Block6ded16b2010-05-10 14:33:55 +01001001 // A testing entry.
1002 function GetPcFromSourcePos(func, source_pos) {
1003 return %GetFunctionCodePositionFromSource(func, source_pos);
1004 }
1005 // Function is public.
1006 this.GetPcFromSourcePos = GetPcFromSourcePos;
1007
1008 // LiveEdit main entry point: changes a script text to a new string.
Steve Block8defd9f2010-07-08 12:39:36 +01001009 function SetScriptSource(script, new_source, preview_only, change_log) {
Steve Block6ded16b2010-05-10 14:33:55 +01001010 var old_source = script.source;
Ben Murdochb8e0da22011-05-16 14:20:40 +01001011 var diff = CompareStrings(old_source, new_source);
Steve Block8defd9f2010-07-08 12:39:36 +01001012 return ApplyPatchMultiChunk(script, diff, new_source, preview_only,
1013 change_log);
Steve Block6ded16b2010-05-10 14:33:55 +01001014 }
1015 // Function is public.
1016 this.SetScriptSource = SetScriptSource;
Ben Murdochf87a2032010-10-22 12:50:53 +01001017
Ben Murdochb8e0da22011-05-16 14:20:40 +01001018 function CompareStrings(s1, s2) {
1019 return %LiveEditCompareStrings(s1, s2);
Steve Block6ded16b2010-05-10 14:33:55 +01001020 }
1021
1022 // Applies the change to the script.
1023 // The change is always a substring (change_pos, change_pos + change_len)
1024 // being replaced with a completely different string new_str.
1025 // This API is a legacy and is obsolete.
1026 //
1027 // @param {Script} script that is being changed
1028 // @param {Array} change_log a list that collects engineer-readable
1029 // description of what happened.
1030 function ApplySingleChunkPatch(script, change_pos, change_len, new_str,
1031 change_log) {
1032 var old_source = script.source;
Ben Murdochf87a2032010-10-22 12:50:53 +01001033
Steve Block6ded16b2010-05-10 14:33:55 +01001034 // Prepare new source string.
1035 var new_source = old_source.substring(0, change_pos) +
1036 new_str + old_source.substring(change_pos + change_len);
Ben Murdochf87a2032010-10-22 12:50:53 +01001037
Steve Block6ded16b2010-05-10 14:33:55 +01001038 return ApplyPatchMultiChunk(script,
1039 [ change_pos, change_pos + change_len, change_pos + new_str.length],
Steve Block8defd9f2010-07-08 12:39:36 +01001040 new_source, false, change_log);
1041 }
Ben Murdochf87a2032010-10-22 12:50:53 +01001042
Steve Block8defd9f2010-07-08 12:39:36 +01001043 // Creates JSON description for a change tree.
1044 function DescribeChangeTree(old_code_tree) {
Ben Murdochf87a2032010-10-22 12:50:53 +01001045
Steve Block8defd9f2010-07-08 12:39:36 +01001046 function ProcessOldNode(node) {
1047 var child_infos = [];
1048 for (var i = 0; i < node.children.length; i++) {
1049 var child = node.children[i];
1050 if (child.status != FunctionStatus.UNCHANGED) {
1051 child_infos.push(ProcessOldNode(child));
1052 }
1053 }
1054 var new_child_infos = [];
1055 if (node.textually_unmatched_new_nodes) {
1056 for (var i = 0; i < node.textually_unmatched_new_nodes.length; i++) {
1057 var child = node.textually_unmatched_new_nodes[i];
1058 new_child_infos.push(ProcessNewNode(child));
1059 }
1060 }
1061 var res = {
1062 name: node.info.function_name,
1063 positions: DescribePositions(node),
1064 status: node.status,
1065 children: child_infos,
Ben Murdochf87a2032010-10-22 12:50:53 +01001066 new_children: new_child_infos
Steve Block8defd9f2010-07-08 12:39:36 +01001067 };
1068 if (node.status_explanation) {
1069 res.status_explanation = node.status_explanation;
1070 }
1071 if (node.textual_corresponding_node) {
1072 res.new_positions = DescribePositions(node.textual_corresponding_node);
1073 }
1074 return res;
1075 }
Ben Murdochf87a2032010-10-22 12:50:53 +01001076
Steve Block8defd9f2010-07-08 12:39:36 +01001077 function ProcessNewNode(node) {
1078 var child_infos = [];
1079 // Do not list ancestors.
1080 if (false) {
1081 for (var i = 0; i < node.children.length; i++) {
1082 child_infos.push(ProcessNewNode(node.children[i]));
1083 }
1084 }
1085 var res = {
1086 name: node.info.function_name,
1087 positions: DescribePositions(node),
1088 children: child_infos,
1089 };
1090 return res;
1091 }
Ben Murdochf87a2032010-10-22 12:50:53 +01001092
Steve Block8defd9f2010-07-08 12:39:36 +01001093 function DescribePositions(node) {
1094 return {
1095 start_position: node.info.start_position,
1096 end_position: node.info.end_position
1097 };
1098 }
Ben Murdochf87a2032010-10-22 12:50:53 +01001099
Steve Block8defd9f2010-07-08 12:39:36 +01001100 return ProcessOldNode(old_code_tree);
Steve Block6ded16b2010-05-10 14:33:55 +01001101 }
1102
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001103 // Restarts call frame and returns value similar to what LiveEdit returns.
1104 function RestartFrame(frame_mirror) {
1105 var result = frame_mirror.restart();
1106 if (IS_STRING(result)) {
1107 throw new Failure("Failed to restart frame: " + result);
1108 }
1109 var result = {};
1110 result[NEEDS_STEP_IN_PROPERTY_NAME] = true;
1111 return result;
1112 }
1113 // Function is public.
1114 this.RestartFrame = RestartFrame;
Ben Murdochf87a2032010-10-22 12:50:53 +01001115
Steve Block6ded16b2010-05-10 14:33:55 +01001116 // Functions are public for tests.
1117 this.TestApi = {
1118 PosTranslator: PosTranslator,
Ben Murdochb8e0da22011-05-16 14:20:40 +01001119 CompareStrings: CompareStrings,
Steve Block6ded16b2010-05-10 14:33:55 +01001120 ApplySingleChunkPatch: ApplySingleChunkPatch
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001121 };
1122};