Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(624)

Side by Side Diff: src/liveedit-debugger.js

Issue 1697016: LiveEdit: clean JS sources a bit (Closed)
Patch Set: tab Created 10 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/debug-debugger.js ('k') | test/mjsunit/debug-liveedit-1.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 26 matching lines...) Expand all
37 // recompiled instead. 37 // recompiled instead.
38 // If the function may not be recompiled (e.g. it was completely erased in new 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 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 40 // create a new instance of this function goes away. An old version of script
41 // is created to back up this obsolete function. 41 // is created to back up this obsolete function.
42 // All unchanged functions have their positions updated accordingly. 42 // All unchanged functions have their positions updated accordingly.
43 // 43 //
44 // LiveEdit namespace is declared inside a single function constructor. 44 // LiveEdit namespace is declared inside a single function constructor.
45 Debug.LiveEdit = new function() { 45 Debug.LiveEdit = new function() {
46 46
47 // Applies the change to the script.
48 // The change is always a substring (change_pos, change_pos + change_len)
49 // being replaced with a completely different string new_str.
50 // This API is a legacy and is obsolete.
51 //
52 // @param {Script} script that is being changed
53 // @param {Array} change_log a list that collects engineer-readable
54 // description of what happened.
55 function ApplyPatch(script, change_pos, change_len, new_str,
56 change_log) {
57 var old_source = script.source;
58
59 // Prepare new source string.
60 var new_source = old_source.substring(0, change_pos) +
61 new_str + old_source.substring(change_pos + change_len);
62
63 return ApplyPatchMultiChunk(script,
64 [ change_pos, change_pos + change_len, change_pos + new_str.length],
65 new_source, change_log);
66 }
67 // Function is public.
68 this.ApplyPatch = ApplyPatch;
69
70 // Forward declaration for minifier. 47 // Forward declaration for minifier.
71 var FunctionStatus; 48 var FunctionStatus;
72 49
50
73 // Applies the change to the script. 51 // Applies the change to the script.
74 // The change is in form of list of chunks encoded in a single array as 52 // The change is in form of list of chunks encoded in a single array as
75 // a series of triplets (pos1_start, pos1_end, pos2_end) 53 // a series of triplets (pos1_start, pos1_end, pos2_end)
76 function ApplyPatchMultiChunk(script, diff_array, new_source, change_log) { 54 function ApplyPatchMultiChunk(script, diff_array, new_source, change_log) {
77 55
78 // Fully compiles source string as a script. Returns Array of 56 var old_source = script.source;
79 // FunctionCompileInfo -- a descriptions of all functions of the script. 57
80 // Elements of array are ordered by start positions of functions (from top 58 // Gather compile information about old version of script.
81 // to bottom) in the source. Fields outer_index and next_sibling_index help 59 var old_compile_info = GatherCompileInfo(old_source, script);
82 // to navigate the nesting structure of functions.
83 //
84 // The script is used for compilation, because it produces code that
85 // needs to be linked with some particular script (for nested functions).
86 function GatherCompileInfo(source) {
87 // Get function info, elements are partially sorted (it is a tree of
88 // nested functions serialized as parent followed by serialized children.
89 var raw_compile_info = %LiveEditGatherCompileInfo(script, source);
90 60
91 // Sort function infos by start position field. 61 // Build tree structures for old and new versions of the script.
92 var compile_info = new Array(); 62 var root_old_node = BuildCodeInfoTree(old_compile_info);
93 var old_index_map = new Array();
94 for (var i = 0; i < raw_compile_info.length; i++) {
95 var info = new FunctionCompileInfo(raw_compile_info[i]);
96 // Remove all links to the actual script. Breakpoints system and
97 // LiveEdit itself believe that any function in heap that points to a
98 // particular script is a regular function.
99 // For some functions we will restore this link later.
100 %LiveEditFunctionSetScript(info.shared_function_info, void 0);
101 compile_info.push(info);
102 old_index_map.push(i);
103 }
104
105 for (var i = 0; i < compile_info.length; i++) {
106 var k = i;
107 for (var j = i + 1; j < compile_info.length; j++) {
108 if (compile_info[k].start_position > compile_info[j].start_position) {
109 k = j;
110 }
111 }
112 if (k != i) {
113 var temp_info = compile_info[k];
114 var temp_index = old_index_map[k];
115 compile_info[k] = compile_info[i];
116 old_index_map[k] = old_index_map[i];
117 compile_info[i] = temp_info;
118 old_index_map[i] = temp_index;
119 }
120 }
121
122 // After sorting update outer_inder field using old_index_map. Also
123 // set next_sibling_index field.
124 var current_index = 0;
125
126 // The recursive function, that goes over all children of a particular
127 // node (i.e. function info).
128 function ResetIndexes(new_parent_index, old_parent_index) {
129 var previous_sibling = -1;
130 while (current_index < compile_info.length &&
131 compile_info[current_index].outer_index == old_parent_index) {
132 var saved_index = current_index;
133 compile_info[saved_index].outer_index = new_parent_index;
134 if (previous_sibling != -1) {
135 compile_info[previous_sibling].next_sibling_index = saved_index;
136 }
137 previous_sibling = saved_index;
138 current_index++;
139 ResetIndexes(saved_index, old_index_map[saved_index]);
140 }
141 if (previous_sibling != -1) {
142 compile_info[previous_sibling].next_sibling_index = -1;
143 }
144 }
145
146 ResetIndexes(-1, -1);
147 Assert(current_index == compile_info.length);
148
149 return compile_info;
150 }
151
152 // Variable forward declarations. Preprocessor "Minifier" needs them.
153 var old_compile_info;
154 var shared_infos;
155 // Finds SharedFunctionInfo that corresponds compile info with index
156 // in old version of the script.
157 // TODO(LiveEdit): Redesign this part. Find live SharedFunctionInfo
158 // about the time that FindCorrespondingFunctions is being run.
159 function FindFunctionInfo(index) {
160 var old_info = old_compile_info[index];
161 for (var i = 0; i < shared_infos.length; i++) {
162 var info = shared_infos[i];
163 if (info.start_position == old_info.start_position &&
164 info.end_position == old_info.end_position) {
165 return info;
166 }
167 }
168 }
169
170 // Replaces function's Code.
171 function PatchCode(old_node) {
172 var new_info = old_node.corresponding_node.info;
173 var shared_info = FindFunctionInfo(old_node.array_index);
174 if (shared_info) {
175 %LiveEditReplaceFunctionCode(new_info.raw_array, shared_info.raw_array);
176 63
177 // The function got a new code. However, this new code brings all new 64 var pos_translator = new PosTranslator(diff_array);
178 // instances of SharedFunctionInfo for nested functions. However, 65
179 // we want the original instances to be used wherever possible. 66 // Analyze changes.
180 // (This is because old instances and new instances will be both 67 MarkChangedFunctions(root_old_node, pos_translator.GetChunks());
181 // linked to a script and breakpoints subsystem does not really 68
182 // expects this; neither does LiveEdit subsystem on next call). 69 // Find all SharedFunctionInfo's that were compiled from this script.
183 for (var i = 0; i < old_node.children.length; i++) { 70 FindLiveSharedInfos(root_old_node, script);
184 if (old_node.children[i].corresponding_node) {
185 var corresponding_child = old_node.children[i].corresponding_node;
186 var child_shared_info =
187 FindFunctionInfo(old_node.children[i].array_index);
188 if (child_shared_info) {
189 %LiveEditReplaceRefToNestedFunction(shared_info.info,
190 corresponding_child.info.shared_function_info,
191 child_shared_info.info);
192 }
193 }
194 }
195
196 change_log.push( {function_patched: new_info.function_name} );
197 } else {
198 change_log.push( {function_patched: new_info.function_name,
199 function_info_not_found: true} );
200 }
201 }
202
203 71
204 var position_patch_report;
205 function PatchPositions(old_info, shared_info) {
206 if (!shared_info) {
207 // TODO(LiveEdit): function is not compiled yet or is already collected.
208 position_patch_report.push(
209 { name: old_info.function_name, info_not_found: true } );
210 return;
211 }
212 %LiveEditPatchFunctionPositions(
213 shared_info.raw_array, diff_array);
214 position_patch_report.push( { name: old_info.function_name } );
215 }
216
217 var link_to_old_script_report;
218 var old_script;
219 // Makes a function associated with another instance of a script (the
220 // one representing its old version). This way the function still
221 // may access its own text.
222 function LinkToOldScript(shared_info, old_info_node) {
223 if (shared_info) {
224 %LiveEditFunctionSetScript(shared_info.info, old_script);
225 link_to_old_script_report.push( { name: shared_info.function_name } );
226 } else {
227 link_to_old_script_report.push(
228 { name: old_info_node.info.function_name, not_found: true } );
229 }
230 }
231
232
233 var old_source = script.source;
234
235 // Find all SharedFunctionInfo's that are compiled from this script.
236 var shared_raw_list = %LiveEditFindSharedFunctionInfosForScript(script);
237
238 var shared_infos = new Array();
239
240 for (var i = 0; i < shared_raw_list.length; i++) {
241 shared_infos.push(new SharedInfoWrapper(shared_raw_list[i]));
242 }
243
244 // Gather compile information about old version of script.
245 var old_compile_info = GatherCompileInfo(old_source);
246
247 // Gather compile information about new version of script. 72 // Gather compile information about new version of script.
248 var new_compile_info; 73 var new_compile_info;
249 try { 74 try {
250 new_compile_info = GatherCompileInfo(new_source); 75 new_compile_info = GatherCompileInfo(new_source, script);
251 } catch (e) { 76 } catch (e) {
252 throw new Failure("Failed to compile new version of script: " + e); 77 throw new Failure("Failed to compile new version of script: " + e);
253 } 78 }
254
255 var pos_translator = new PosTranslator(diff_array);
256
257 // Build tree structures for old and new versions of the script.
258 var root_old_node = BuildCodeInfoTree(old_compile_info);
259 var root_new_node = BuildCodeInfoTree(new_compile_info); 79 var root_new_node = BuildCodeInfoTree(new_compile_info);
260 80
261 // Analyze changes. 81 // Link recompiled script data with other data.
262 MarkChangedFunctions(root_old_node, pos_translator.GetChunks());
263 FindCorrespondingFunctions(root_old_node, root_new_node); 82 FindCorrespondingFunctions(root_old_node, root_new_node);
264 83
265 // Prepare to-do lists. 84 // Prepare to-do lists.
266 var replace_code_list = new Array(); 85 var replace_code_list = new Array();
267 var link_to_old_script_list = new Array(); 86 var link_to_old_script_list = new Array();
268 var link_to_original_script_list = new Array(); 87 var link_to_original_script_list = new Array();
269 var update_positions_list = new Array(); 88 var update_positions_list = new Array();
270 89
271 function HarvestTodo(old_node) { 90 function HarvestTodo(old_node) {
272 function CollectDamaged(node) { 91 function CollectDamaged(node) {
(...skipping 27 matching lines...) Expand all
300 for (var i = 0; i < old_node.children.length; i++) { 119 for (var i = 0; i < old_node.children.length; i++) {
301 HarvestTodo(old_node.children[i]); 120 HarvestTodo(old_node.children[i]);
302 } 121 }
303 } 122 }
304 123
305 HarvestTodo(root_old_node); 124 HarvestTodo(root_old_node);
306 125
307 // Collect shared infos for functions whose code need to be patched. 126 // Collect shared infos for functions whose code need to be patched.
308 var replaced_function_infos = new Array(); 127 var replaced_function_infos = new Array();
309 for (var i = 0; i < replace_code_list.length; i++) { 128 for (var i = 0; i < replace_code_list.length; i++) {
310 var info = FindFunctionInfo(replace_code_list[i].array_index); 129 var info_wrapper = replace_code_list[i].live_shared_info_wrapper;
311 if (info) { 130 if (info_wrapper) {
312 replaced_function_infos.push(info); 131 replaced_function_infos.push(info_wrapper);
313 } 132 }
314 } 133 }
315 134
316 // Check that function being patched is not currently on stack. 135 // Check that function being patched is not currently on stack.
317 CheckStackActivations(replaced_function_infos, change_log); 136 CheckStackActivations(replaced_function_infos, change_log);
318 137
319 138
320 // We haven't changed anything before this line yet. 139 // We haven't changed anything before this line yet.
321 // Committing all changes. 140 // Committing all changes.
322 141
(...skipping 14 matching lines...) Expand all
337 // Update the script text and create a new script representing an old 156 // Update the script text and create a new script representing an old
338 // version of the script. 157 // version of the script.
339 old_script = %LiveEditReplaceScript(script, new_source, 158 old_script = %LiveEditReplaceScript(script, new_source,
340 old_script_name); 159 old_script_name);
341 160
342 var link_to_old_script_report = new Array(); 161 var link_to_old_script_report = new Array();
343 change_log.push( { linked_to_old_script: link_to_old_script_report } ); 162 change_log.push( { linked_to_old_script: link_to_old_script_report } );
344 163
345 // We need to link to old script all former nested functions. 164 // We need to link to old script all former nested functions.
346 for (var i = 0; i < link_to_old_script_list.length; i++) { 165 for (var i = 0; i < link_to_old_script_list.length; i++) {
347 LinkToOldScript( 166 LinkToOldScript(link_to_old_script_list[i], old_script,
348 FindFunctionInfo(link_to_old_script_list[i].array_index), 167 link_to_old_script_report);
349 link_to_old_script_list[i]);
350 } 168 }
351 } 169 }
352 170
353 // Link to an actual script all the functions that we are going to use. 171 // Link to an actual script all the functions that we are going to use.
354 for (var i = 0; i < link_to_original_script_list.length; i++) { 172 for (var i = 0; i < link_to_original_script_list.length; i++) {
355 %LiveEditFunctionSetScript( 173 %LiveEditFunctionSetScript(
356 link_to_original_script_list[i].info.shared_function_info, script); 174 link_to_original_script_list[i].info.shared_function_info, script);
357 } 175 }
358 176
359 for (var i = 0; i < replace_code_list.length; i++) { 177 for (var i = 0; i < replace_code_list.length; i++) {
360 PatchCode(replace_code_list[i]); 178 PatchFunctionCode(replace_code_list[i], change_log);
361 } 179 }
362 180
363 var position_patch_report = new Array(); 181 var position_patch_report = new Array();
364 change_log.push( {position_patched: position_patch_report} ); 182 change_log.push( {position_patched: position_patch_report} );
365 183
366 for (var i = 0; i < update_positions_list.length; i++) { 184 for (var i = 0; i < update_positions_list.length; i++) {
367 // TODO(LiveEdit): take into account wether it's source_changed or 185 // TODO(LiveEdit): take into account wether it's source_changed or
368 // unchanged and whether positions changed at all. 186 // unchanged and whether positions changed at all.
369 PatchPositions(update_positions_list[i].info, 187 PatchPositions(update_positions_list[i], diff_array,
370 FindFunctionInfo(update_positions_list[i].array_index)); 188 position_patch_report);
371 } 189 }
372 190
373 break_points_restorer(pos_translator, old_script); 191 break_points_restorer(pos_translator, old_script);
374 } 192 }
375 // Function is public. 193 // Function is public.
376 this.ApplyPatchMultiChunk = ApplyPatchMultiChunk; 194 this.ApplyPatchMultiChunk = ApplyPatchMultiChunk;
377 195
196
197 // Fully compiles source string as a script. Returns Array of
198 // FunctionCompileInfo -- a descriptions of all functions of the script.
199 // Elements of array are ordered by start positions of functions (from top
200 // to bottom) in the source. Fields outer_index and next_sibling_index help
201 // to navigate the nesting structure of functions.
202 //
203 // All functions get compiled linked to script provided as parameter script.
204 // TODO(LiveEdit): consider not using actual scripts as script, because
205 // we have to manually erase all links right after compile.
206 function GatherCompileInfo(source, script) {
207 // Get function info, elements are partially sorted (it is a tree of
208 // nested functions serialized as parent followed by serialized children.
209 var raw_compile_info = %LiveEditGatherCompileInfo(script, source);
210
211 // Sort function infos by start position field.
212 var compile_info = new Array();
213 var old_index_map = new Array();
214 for (var i = 0; i < raw_compile_info.length; i++) {
215 var info = new FunctionCompileInfo(raw_compile_info[i]);
216 // Remove all links to the actual script. Breakpoints system and
217 // LiveEdit itself believe that any function in heap that points to a
218 // particular script is a regular function.
219 // For some functions we will restore this link later.
220 %LiveEditFunctionSetScript(info.shared_function_info, void 0);
221 compile_info.push(info);
222 old_index_map.push(i);
223 }
224
225 for (var i = 0; i < compile_info.length; i++) {
226 var k = i;
227 for (var j = i + 1; j < compile_info.length; j++) {
228 if (compile_info[k].start_position > compile_info[j].start_position) {
229 k = j;
230 }
231 }
232 if (k != i) {
233 var temp_info = compile_info[k];
234 var temp_index = old_index_map[k];
235 compile_info[k] = compile_info[i];
236 old_index_map[k] = old_index_map[i];
237 compile_info[i] = temp_info;
238 old_index_map[i] = temp_index;
239 }
240 }
241
242 // After sorting update outer_inder field using old_index_map. Also
243 // set next_sibling_index field.
244 var current_index = 0;
245
246 // The recursive function, that goes over all children of a particular
247 // node (i.e. function info).
248 function ResetIndexes(new_parent_index, old_parent_index) {
249 var previous_sibling = -1;
250 while (current_index < compile_info.length &&
251 compile_info[current_index].outer_index == old_parent_index) {
252 var saved_index = current_index;
253 compile_info[saved_index].outer_index = new_parent_index;
254 if (previous_sibling != -1) {
255 compile_info[previous_sibling].next_sibling_index = saved_index;
256 }
257 previous_sibling = saved_index;
258 current_index++;
259 ResetIndexes(saved_index, old_index_map[saved_index]);
260 }
261 if (previous_sibling != -1) {
262 compile_info[previous_sibling].next_sibling_index = -1;
263 }
264 }
265
266 ResetIndexes(-1, -1);
267 Assert(current_index == compile_info.length);
268
269 return compile_info;
270 }
271
272
273 // Replaces function's Code.
274 function PatchFunctionCode(old_node, change_log) {
275 var new_info = old_node.corresponding_node.info;
276 var shared_info_wrapper = old_node.live_shared_info_wrapper;
277 if (shared_info_wrapper) {
278 %LiveEditReplaceFunctionCode(new_info.raw_array,
279 shared_info_wrapper.raw_array);
280
281 // The function got a new code. However, this new code brings all new
282 // instances of SharedFunctionInfo for nested functions. However,
283 // we want the original instances to be used wherever possible.
284 // (This is because old instances and new instances will be both
285 // linked to a script and breakpoints subsystem does not really
286 // expects this; neither does LiveEdit subsystem on next call).
287 for (var i = 0; i < old_node.children.length; i++) {
288 if (old_node.children[i].corresponding_node) {
289 var corresponding_child = old_node.children[i].corresponding_node;
290 var child_shared_info_wrapper =
291 old_node.children[i].live_shared_info_wrapper;
292 if (child_shared_info_wrapper) {
293 %LiveEditReplaceRefToNestedFunction(shared_info_wrapper.info,
294 corresponding_child.info.shared_function_info,
295 child_shared_info_wrapper.info);
296 }
297 }
298 }
299
300 change_log.push( {function_patched: new_info.function_name} );
301 } else {
302 change_log.push( {function_patched: new_info.function_name,
303 function_info_not_found: true} );
304 }
305 }
306
307
308 // Makes a function associated with another instance of a script (the
309 // one representing its old version). This way the function still
310 // may access its own text.
311 function LinkToOldScript(old_info_node, old_script, report_array) {
312 var shared_info = old_info_node.live_shared_info_wrapper;
313 if (shared_info) {
314 %LiveEditFunctionSetScript(shared_info.info, old_script);
315 report_array.push( { name: shared_info.function_name } );
316 } else {
317 report_array.push(
318 { name: old_info_node.info.function_name, not_found: true } );
319 }
320 }
321
378 322
379 // Returns function that restores breakpoints. 323 // Returns function that restores breakpoints.
380 function TemporaryRemoveBreakPoints(original_script, change_log) { 324 function TemporaryRemoveBreakPoints(original_script, change_log) {
381 var script_break_points = GetScriptBreakPoints(original_script); 325 var script_break_points = GetScriptBreakPoints(original_script);
382 326
383 var break_points_update_report = []; 327 var break_points_update_report = [];
384 change_log.push( { break_points_update: break_points_update_report } ); 328 change_log.push( { break_points_update: break_points_update_report } );
385 329
386 var break_point_old_positions = []; 330 var break_point_old_positions = [];
387 for (var i = 0; i < script_break_points.length; i++) { 331 for (var i = 0; i < script_break_points.length; i++) {
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 this.parent = void 0; 487 this.parent = void 0;
544 488
545 this.status = FunctionStatus.UNCHANGED; 489 this.status = FunctionStatus.UNCHANGED;
546 // Status explanation is used for debugging purposes and will be shown 490 // Status explanation is used for debugging purposes and will be shown
547 // in user UI if some explanations are needed. 491 // in user UI if some explanations are needed.
548 this.status_explanation = void 0; 492 this.status_explanation = void 0;
549 this.new_start_pos = void 0; 493 this.new_start_pos = void 0;
550 this.new_end_pos = void 0; 494 this.new_end_pos = void 0;
551 this.corresponding_node = void 0; 495 this.corresponding_node = void 0;
552 this.unmatched_new_nodes = void 0; 496 this.unmatched_new_nodes = void 0;
497 this.live_shared_info_wrapper = void 0;
553 } 498 }
554 499
555 // From array of function infos that is implicitly a tree creates 500 // From array of function infos that is implicitly a tree creates
556 // an actual tree of functions in script. 501 // an actual tree of functions in script.
557 function BuildCodeInfoTree(code_info_array) { 502 function BuildCodeInfoTree(code_info_array) {
558 // Throughtout all function we iterate over input array. 503 // Throughtout all function we iterate over input array.
559 var index = 0; 504 var index = 0;
560 505
561 // Recursive function that builds a branch of tree. 506 // Recursive function that builds a branch of tree.
562 function BuildNode() { 507 function BuildNode() {
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
760 } 705 }
761 old_node.unmatched_new_nodes = unmatched_new_nodes_list; 706 old_node.unmatched_new_nodes = unmatched_new_nodes_list;
762 } 707 }
763 708
764 ProcessChildren(old_code_tree, new_code_tree); 709 ProcessChildren(old_code_tree, new_code_tree);
765 710
766 old_code_tree.corresponding_node = new_code_tree; 711 old_code_tree.corresponding_node = new_code_tree;
767 Assert(old_code_tree.status != FunctionStatus.DAMAGED, 712 Assert(old_code_tree.status != FunctionStatus.DAMAGED,
768 "Script became damaged"); 713 "Script became damaged");
769 } 714 }
715
716 function FindLiveSharedInfos(old_code_tree, script) {
717 var shared_raw_list = %LiveEditFindSharedFunctionInfosForScript(script);
718
719 var shared_infos = new Array();
720
721 for (var i = 0; i < shared_raw_list.length; i++) {
722 shared_infos.push(new SharedInfoWrapper(shared_raw_list[i]));
723 }
724
725 // Finds SharedFunctionInfo that corresponds compile info with index
726 // in old version of the script.
727 function FindFunctionInfo(compile_info) {
728 for (var i = 0; i < shared_infos.length; i++) {
729 var wrapper = shared_infos[i];
730 if (wrapper.start_position == compile_info.start_position &&
731 wrapper.end_position == compile_info.end_position) {
732 return wrapper;
733 }
734 }
735 }
736
737 function TraverseTree(node) {
738 var info_wrapper = FindFunctionInfo(node.info);
739 if (info_wrapper) {
740 node.live_shared_info_wrapper = info_wrapper;
741 }
742 for (var i = 0; i < node.children.length; i++) {
743 TraverseTree(node.children[i]);
744 }
745 }
746
747 TraverseTree(old_code_tree);
748 }
770 749
771 750
772 // An object describing function compilation details. Its index fields 751 // An object describing function compilation details. Its index fields
773 // apply to indexes inside array that stores these objects. 752 // apply to indexes inside array that stores these objects.
774 function FunctionCompileInfo(raw_array) { 753 function FunctionCompileInfo(raw_array) {
775 this.function_name = raw_array[0]; 754 this.function_name = raw_array[0];
776 this.start_position = raw_array[1]; 755 this.start_position = raw_array[1];
777 this.end_position = raw_array[2]; 756 this.end_position = raw_array[2];
778 this.param_num = raw_array[3]; 757 this.param_num = raw_array[3];
779 this.code = raw_array[4]; 758 this.code = raw_array[4];
780 this.scope_info = raw_array[5]; 759 this.scope_info = raw_array[5];
781 this.outer_index = raw_array[6]; 760 this.outer_index = raw_array[6];
782 this.shared_function_info = raw_array[7]; 761 this.shared_function_info = raw_array[7];
783 this.next_sibling_index = null; 762 this.next_sibling_index = null;
784 this.raw_array = raw_array; 763 this.raw_array = raw_array;
785 } 764 }
786 765
787 function SharedInfoWrapper(raw_array) { 766 function SharedInfoWrapper(raw_array) {
788 this.function_name = raw_array[0]; 767 this.function_name = raw_array[0];
789 this.start_position = raw_array[1]; 768 this.start_position = raw_array[1];
790 this.end_position = raw_array[2]; 769 this.end_position = raw_array[2];
791 this.info = raw_array[3]; 770 this.info = raw_array[3];
792 this.raw_array = raw_array; 771 this.raw_array = raw_array;
793 } 772 }
794 773
774 // Changes positions (including all statments) in function.
775 function PatchPositions(old_info_node, diff_array, report_array) {
776 var shared_info_wrapper = old_info_node.live_shared_info_wrapper;
777 if (!shared_info_wrapper) {
778 // TODO(LiveEdit): function is not compiled yet or is already collected.
779 report_array.push(
780 { name: old_info_node.info.function_name, info_not_found: true } );
781 return;
782 }
783 %LiveEditPatchFunctionPositions(shared_info_wrapper.raw_array,
784 diff_array);
785 report_array.push( { name: old_info_node.info.function_name } );
786 }
787
795 // Adds a suffix to script name to mark that it is old version. 788 // Adds a suffix to script name to mark that it is old version.
796 function CreateNameForOldScript(script) { 789 function CreateNameForOldScript(script) {
797 // TODO(635): try better than this; support several changes. 790 // TODO(635): try better than this; support several changes.
798 return script.name + " (old)"; 791 return script.name + " (old)";
799 } 792 }
800 793
801 // Compares a function interface old and new version, whether it 794 // Compares a function interface old and new version, whether it
802 // changed or not. 795 // changed or not.
803 function CompareFunctionExpectations(function_info1, function_info2) { 796 function CompareFunctionExpectations(function_info1, function_info2) {
804 // Check that function has the same number of parameters (there may exist 797 // Check that function has the same number of parameters (there may exist
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
912 return; 905 return;
913 } 906 }
914 ApplyPatchMultiChunk(script, diff, new_source, change_log); 907 ApplyPatchMultiChunk(script, diff, new_source, change_log);
915 } 908 }
916 // Function is public. 909 // Function is public.
917 this.SetScriptSource = SetScriptSource; 910 this.SetScriptSource = SetScriptSource;
918 911
919 function CompareStringsLinewise(s1, s2) { 912 function CompareStringsLinewise(s1, s2) {
920 return %LiveEditCompareStringsLinewise(s1, s2); 913 return %LiveEditCompareStringsLinewise(s1, s2);
921 } 914 }
915
916 // Applies the change to the script.
917 // The change is always a substring (change_pos, change_pos + change_len)
918 // being replaced with a completely different string new_str.
919 // This API is a legacy and is obsolete.
920 //
921 // @param {Script} script that is being changed
922 // @param {Array} change_log a list that collects engineer-readable
923 // description of what happened.
924 function ApplySingleChunkPatch(script, change_pos, change_len, new_str,
925 change_log) {
926 var old_source = script.source;
927
928 // Prepare new source string.
929 var new_source = old_source.substring(0, change_pos) +
930 new_str + old_source.substring(change_pos + change_len);
931
932 return ApplyPatchMultiChunk(script,
933 [ change_pos, change_pos + change_len, change_pos + new_str.length],
934 new_source, change_log);
935 }
936
922 937
923 // Functions are public for tests. 938 // Functions are public for tests.
924 this.TestApi = { 939 this.TestApi = {
925 PosTranslator: PosTranslator, 940 PosTranslator: PosTranslator,
926 CompareStringsLinewise: CompareStringsLinewise 941 CompareStringsLinewise: CompareStringsLinewise,
942 ApplySingleChunkPatch: ApplySingleChunkPatch
927 } 943 }
928 } 944 }
OLDNEW
« no previous file with comments | « src/debug-debugger.js ('k') | test/mjsunit/debug-liveedit-1.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698