| OLD | NEW |
| 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 10 matching lines...) Expand all Loading... |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // LiveEdit feature implementation. The script should be executed after | 28 // LiveEdit feature implementation. The script should be executed after |
| 29 // debug-debugger.js. | 29 // debug-debugger.js. |
| 30 | 30 |
| 31 // A LiveEdit namespace is declared inside a single function constructor. |
| 32 Debug.LiveEdit = new function() { |
| 33 |
| 34 // TODO(LiveEdit): restore indentation below |
| 31 | 35 |
| 32 // Changes script text and recompiles all relevant functions if possible. | 36 // Changes script text and recompiles all relevant functions if possible. |
| 33 // The change is always a substring (change_pos, change_pos + change_len) | 37 // The change is always a substring (change_pos, change_pos + change_len) |
| 34 // being replaced with a completely different string new_str. | 38 // being replaced with a completely different string new_str. |
| 35 // | 39 // |
| 36 // Only one function will have its Code changed in result of this function. | 40 // Only one function will have its Code changed in result of this function. |
| 37 // All nested functions (should they have any instances at the moment) are left | 41 // All nested functions (should they have any instances at the moment) are left |
| 38 // unchanged and re-linked to a newly created script instance representing old | 42 // unchanged and re-linked to a newly created script instance representing old |
| 39 // version of the source. (Generally speaking, | 43 // version of the source. (Generally speaking, |
| 40 // during the change all nested functions are erased and completely different | 44 // during the change all nested functions are erased and completely different |
| 41 // set of nested functions are introduced.) All other functions just have | 45 // set of nested functions are introduced.) All other functions just have |
| 42 // their positions updated. | 46 // their positions updated. |
| 43 // | 47 // |
| 44 // @param {Script} script that is being changed | 48 // @param {Script} script that is being changed |
| 45 // @param {Array} change_log a list that collects engineer-readable description | 49 // @param {Array} change_log a list that collects engineer-readable description |
| 46 // of what happened. | 50 // of what happened. |
| 47 Debug.LiveEditChangeScript = function(script, change_pos, change_len, new_str, | 51 function ApplyPatch(script, change_pos, change_len, new_str, |
| 48 change_log) { | 52 change_log) { |
| 49 | 53 |
| 50 // So far the function works as namespace. | |
| 51 var liveedit = Debug.LiveEditChangeScript; | |
| 52 var Assert = liveedit.Assert; | |
| 53 | |
| 54 // Fully compiles source string as a script. Returns Array of | 54 // Fully compiles source string as a script. Returns Array of |
| 55 // FunctionCompileInfo -- a descriptions of all functions of the script. | 55 // FunctionCompileInfo -- a descriptions of all functions of the script. |
| 56 // Elements of array are ordered by start positions of functions (from top | 56 // Elements of array are ordered by start positions of functions (from top |
| 57 // to bottom) in the source. Fields outer_index and next_sibling_index help | 57 // to bottom) in the source. Fields outer_index and next_sibling_index help |
| 58 // to navigate the nesting structure of functions. | 58 // to navigate the nesting structure of functions. |
| 59 // | 59 // |
| 60 // The script is used for compilation, because it produces code that | 60 // The script is used for compilation, because it produces code that |
| 61 // needs to be linked with some particular script (for nested functions). | 61 // needs to be linked with some particular script (for nested functions). |
| 62 function DebugGatherCompileInfo(source) { | 62 function DebugGatherCompileInfo(source) { |
| 63 // Get function info, elements are partially sorted (it is a tree | 63 // Get function info, elements are partially sorted (it is a tree |
| 64 // of nested functions serialized as parent followed by serialized children. | 64 // of nested functions serialized as parent followed by serialized children. |
| 65 var raw_compile_info = %LiveEditGatherCompileInfo(script, source); | 65 var raw_compile_info = %LiveEditGatherCompileInfo(script, source); |
| 66 | 66 |
| 67 // Sort function infos by start position field. | 67 // Sort function infos by start position field. |
| 68 var compile_info = new Array(); | 68 var compile_info = new Array(); |
| 69 var old_index_map = new Array(); | 69 var old_index_map = new Array(); |
| 70 for (var i = 0; i < raw_compile_info.length; i++) { | 70 for (var i = 0; i < raw_compile_info.length; i++) { |
| 71 compile_info.push(new liveedit.FunctionCompileInfo(raw_compile_info[i]))
; | 71 compile_info.push(new FunctionCompileInfo(raw_compile_info[i])); |
| 72 old_index_map.push(i); | 72 old_index_map.push(i); |
| 73 } | 73 } |
| 74 | 74 |
| 75 for (var i = 0; i < compile_info.length; i++) { | 75 for (var i = 0; i < compile_info.length; i++) { |
| 76 var k = i; | 76 var k = i; |
| 77 for (var j = i + 1; j < compile_info.length; j++) { | 77 for (var j = i + 1; j < compile_info.length; j++) { |
| 78 if (compile_info[k].start_position > compile_info[j].start_position) { | 78 if (compile_info[k].start_position > compile_info[j].start_position) { |
| 79 k = j; | 79 k = j; |
| 80 } | 80 } |
| 81 } | 81 } |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 // Prepare new source string. | 215 // Prepare new source string. |
| 216 var new_source = old_source.substring(0, change_pos) + | 216 var new_source = old_source.substring(0, change_pos) + |
| 217 new_str + old_source.substring(change_pos + change_len); | 217 new_str + old_source.substring(change_pos + change_len); |
| 218 | 218 |
| 219 // Find all SharedFunctionInfo's that are compiled from this script. | 219 // Find all SharedFunctionInfo's that are compiled from this script. |
| 220 var shared_raw_list = %LiveEditFindSharedFunctionInfosForScript(script); | 220 var shared_raw_list = %LiveEditFindSharedFunctionInfosForScript(script); |
| 221 | 221 |
| 222 var shared_infos = new Array(); | 222 var shared_infos = new Array(); |
| 223 | 223 |
| 224 for (var i = 0; i < shared_raw_list.length; i++) { | 224 for (var i = 0; i < shared_raw_list.length; i++) { |
| 225 shared_infos.push(new liveedit.SharedInfoWrapper(shared_raw_list[i])); | 225 shared_infos.push(new SharedInfoWrapper(shared_raw_list[i])); |
| 226 } | 226 } |
| 227 | 227 |
| 228 // Gather compile information about old version of script. | 228 // Gather compile information about old version of script. |
| 229 var old_compile_info = DebugGatherCompileInfo(old_source); | 229 var old_compile_info = DebugGatherCompileInfo(old_source); |
| 230 | 230 |
| 231 // Gather compile information about new version of script. | 231 // Gather compile information about new version of script. |
| 232 var new_compile_info; | 232 var new_compile_info; |
| 233 try { | 233 try { |
| 234 new_compile_info = DebugGatherCompileInfo(new_source); | 234 new_compile_info = DebugGatherCompileInfo(new_source); |
| 235 } catch (e) { | 235 } catch (e) { |
| 236 throw new liveedit.Failure("Failed to compile new version of script: " + e); | 236 throw new Failure("Failed to compile new version of script: " + e); |
| 237 } | 237 } |
| 238 | 238 |
| 239 // An index of a single function, that is going to have its code replaced. | 239 // An index of a single function, that is going to have its code replaced. |
| 240 var function_being_patched = | 240 var function_being_patched = |
| 241 FindChangedFunction(old_compile_info, change_pos, change_len_old); | 241 FindChangedFunction(old_compile_info, change_pos, change_len_old); |
| 242 | 242 |
| 243 // In old and new script versions function with a change should have the | 243 // In old and new script versions function with a change should have the |
| 244 // same indexes. | 244 // same indexes. |
| 245 var function_being_patched2 = | 245 var function_being_patched2 = |
| 246 FindChangedFunction(new_compile_info, change_pos, change_len_new); | 246 FindChangedFunction(new_compile_info, change_pos, change_len_new); |
| 247 Assert(function_being_patched == function_being_patched2, | 247 Assert(function_being_patched == function_being_patched2, |
| 248 "inconsistent old/new compile info"); | 248 "inconsistent old/new compile info"); |
| 249 | 249 |
| 250 // Check that function being patched has the same expectations in a new | 250 // Check that function being patched has the same expectations in a new |
| 251 // version. Otherwise we cannot safely patch its behavior and should | 251 // version. Otherwise we cannot safely patch its behavior and should |
| 252 // choose the outer function instead. | 252 // choose the outer function instead. |
| 253 while (!liveedit.CompareFunctionExpectations( | 253 while (!CompareFunctionExpectations( |
| 254 old_compile_info[function_being_patched], | 254 old_compile_info[function_being_patched], |
| 255 new_compile_info[function_being_patched])) { | 255 new_compile_info[function_being_patched])) { |
| 256 | 256 |
| 257 Assert(old_compile_info[function_being_patched].outer_index == | 257 Assert(old_compile_info[function_being_patched].outer_index == |
| 258 new_compile_info[function_being_patched].outer_index); | 258 new_compile_info[function_being_patched].outer_index); |
| 259 function_being_patched = | 259 function_being_patched = |
| 260 old_compile_info[function_being_patched].outer_index; | 260 old_compile_info[function_being_patched].outer_index; |
| 261 Assert(function_being_patched != -1); | 261 Assert(function_being_patched != -1); |
| 262 } | 262 } |
| 263 | 263 |
| 264 // Check that function being patched is not currently on stack. | 264 // Check that function being patched is not currently on stack. |
| 265 liveedit.CheckStackActivations( | 265 CheckStackActivations( |
| 266 [ FindFunctionInfo(function_being_patched) ], change_log ); | 266 [ FindFunctionInfo(function_being_patched) ], change_log ); |
| 267 | 267 |
| 268 | 268 |
| 269 // Committing all changes. | 269 // Committing all changes. |
| 270 var old_script_name = liveedit.CreateNameForOldScript(script); | 270 var old_script_name = CreateNameForOldScript(script); |
| 271 | 271 |
| 272 // Update the script text and create a new script representing an old | 272 // Update the script text and create a new script representing an old |
| 273 // version of the script. | 273 // version of the script. |
| 274 var old_script = %LiveEditReplaceScript(script, new_source, old_script_name); | 274 var old_script = %LiveEditReplaceScript(script, new_source, old_script_name); |
| 275 | 275 |
| 276 PatchCode(new_compile_info[function_being_patched], | 276 PatchCode(new_compile_info[function_being_patched], |
| 277 FindFunctionInfo(function_being_patched)); | 277 FindFunctionInfo(function_being_patched)); |
| 278 | 278 |
| 279 var position_patch_report = new Array(); | 279 var position_patch_report = new Array(); |
| 280 change_log.push( {position_patched: position_patch_report} ); | 280 change_log.push( {position_patched: position_patch_report} ); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 } | 313 } |
| 314 | 314 |
| 315 var link_to_old_script_report = new Array(); | 315 var link_to_old_script_report = new Array(); |
| 316 change_log.push( { linked_to_old_script: link_to_old_script_report } ); | 316 change_log.push( { linked_to_old_script: link_to_old_script_report } ); |
| 317 | 317 |
| 318 // We need to link to old script all former nested functions. | 318 // We need to link to old script all former nested functions. |
| 319 for (var i = function_being_patched + 1; i < old_next_sibling; i++) { | 319 for (var i = function_being_patched + 1; i < old_next_sibling; i++) { |
| 320 LinkToOldScript(FindFunctionInfo(i), old_script); | 320 LinkToOldScript(FindFunctionInfo(i), old_script); |
| 321 } | 321 } |
| 322 } | 322 } |
| 323 // Function is public. |
| 324 this.ApplyPatch = ApplyPatch; |
| 323 | 325 |
| 324 Debug.LiveEditChangeScript.Assert = function(condition, message) { | 326 function Assert(condition, message) { |
| 325 if (!condition) { | 327 if (!condition) { |
| 326 if (message) { | 328 if (message) { |
| 327 throw "Assert " + message; | 329 throw "Assert " + message; |
| 328 } else { | 330 } else { |
| 329 throw "Assert"; | 331 throw "Assert"; |
| 330 } | 332 } |
| 331 } | 333 } |
| 332 } | 334 } |
| 333 | 335 |
| 334 // An object describing function compilation details. Its index fields | 336 // An object describing function compilation details. Its index fields |
| 335 // apply to indexes inside array that stores these objects. | 337 // apply to indexes inside array that stores these objects. |
| 336 Debug.LiveEditChangeScript.FunctionCompileInfo = function(raw_array) { | 338 function FunctionCompileInfo(raw_array) { |
| 337 this.function_name = raw_array[0]; | 339 this.function_name = raw_array[0]; |
| 338 this.start_position = raw_array[1]; | 340 this.start_position = raw_array[1]; |
| 339 this.end_position = raw_array[2]; | 341 this.end_position = raw_array[2]; |
| 340 this.param_num = raw_array[3]; | 342 this.param_num = raw_array[3]; |
| 341 this.code = raw_array[4]; | 343 this.code = raw_array[4]; |
| 342 this.scope_info = raw_array[5]; | 344 this.scope_info = raw_array[5]; |
| 343 this.outer_index = raw_array[6]; | 345 this.outer_index = raw_array[6]; |
| 344 this.next_sibling_index = null; | 346 this.next_sibling_index = null; |
| 345 this.raw_array = raw_array; | 347 this.raw_array = raw_array; |
| 346 } | 348 } |
| 347 | 349 |
| 348 // A structure describing SharedFunctionInfo. | 350 function SharedInfoWrapper(raw_array) { |
| 349 Debug.LiveEditChangeScript.SharedInfoWrapper = function(raw_array) { | |
| 350 this.function_name = raw_array[0]; | 351 this.function_name = raw_array[0]; |
| 351 this.start_position = raw_array[1]; | 352 this.start_position = raw_array[1]; |
| 352 this.end_position = raw_array[2]; | 353 this.end_position = raw_array[2]; |
| 353 this.info = raw_array[3]; | 354 this.info = raw_array[3]; |
| 354 this.raw_array = raw_array; | 355 this.raw_array = raw_array; |
| 355 } | 356 } |
| 356 | 357 |
| 357 // Adds a suffix to script name to mark that it is old version. | 358 // Adds a suffix to script name to mark that it is old version. |
| 358 Debug.LiveEditChangeScript.CreateNameForOldScript = function(script) { | 359 function CreateNameForOldScript(script) { |
| 359 // TODO(635): try better than this; support several changes. | 360 // TODO(635): try better than this; support several changes. |
| 360 return script.name + " (old)"; | 361 return script.name + " (old)"; |
| 361 } | 362 } |
| 362 | 363 |
| 363 // Compares a function interface old and new version, whether it | 364 // Compares a function interface old and new version, whether it |
| 364 // changed or not. | 365 // changed or not. |
| 365 Debug.LiveEditChangeScript.CompareFunctionExpectations = | 366 function CompareFunctionExpectations(function_info1, function_info2) { |
| 366 function(function_info1, function_info2) { | |
| 367 // Check that function has the same number of parameters (there may exist | 367 // Check that function has the same number of parameters (there may exist |
| 368 // an adapter, that won't survive function parameter number change). | 368 // an adapter, that won't survive function parameter number change). |
| 369 if (function_info1.param_num != function_info2.param_num) { | 369 if (function_info1.param_num != function_info2.param_num) { |
| 370 return false; | 370 return false; |
| 371 } | 371 } |
| 372 var scope_info1 = function_info1.scope_info; | 372 var scope_info1 = function_info1.scope_info; |
| 373 var scope_info2 = function_info2.scope_info; | 373 var scope_info2 = function_info2.scope_info; |
| 374 | 374 |
| 375 if (!scope_info1) { | 375 if (!scope_info1) { |
| 376 return !scope_info2; | 376 return !scope_info2; |
| 377 } | 377 } |
| 378 | 378 |
| 379 if (scope_info1.length != scope_info2.length) { | 379 if (scope_info1.length != scope_info2.length) { |
| 380 return false; | 380 return false; |
| 381 } | 381 } |
| 382 | 382 |
| 383 // Check that outer scope structure is not changed. Otherwise the function | 383 // Check that outer scope structure is not changed. Otherwise the function |
| 384 // will not properly work with existing scopes. | 384 // will not properly work with existing scopes. |
| 385 return scope_info1.toString() == scope_info2.toString(); | 385 return scope_info1.toString() == scope_info2.toString(); |
| 386 } | 386 } |
| 387 | 387 |
| 388 // Minifier forward declaration. |
| 389 var FunctionPatchabilityStatus; |
| 390 |
| 388 // For array of wrapped shared function infos checks that none of them | 391 // For array of wrapped shared function infos checks that none of them |
| 389 // have activations on stack (of any thread). Throws a Failure exception | 392 // have activations on stack (of any thread). Throws a Failure exception |
| 390 // if this proves to be false. | 393 // if this proves to be false. |
| 391 Debug.LiveEditChangeScript.CheckStackActivations = function(shared_wrapper_list, | 394 function CheckStackActivations(shared_wrapper_list, change_log) { |
| 392 change_log) { | |
| 393 var liveedit = Debug.LiveEditChangeScript; | |
| 394 | |
| 395 var shared_list = new Array(); | 395 var shared_list = new Array(); |
| 396 for (var i = 0; i < shared_wrapper_list.length; i++) { | 396 for (var i = 0; i < shared_wrapper_list.length; i++) { |
| 397 shared_list[i] = shared_wrapper_list[i].info; | 397 shared_list[i] = shared_wrapper_list[i].info; |
| 398 } | 398 } |
| 399 var result = %LiveEditCheckAndDropActivations(shared_list, true); | 399 var result = %LiveEditCheckAndDropActivations(shared_list, true); |
| 400 if (result[shared_list.length]) { | 400 if (result[shared_list.length]) { |
| 401 // Extra array element may contain error message. | 401 // Extra array element may contain error message. |
| 402 throw new liveedit.Failure(result[shared_list.length]); | 402 throw new Failure(result[shared_list.length]); |
| 403 } | 403 } |
| 404 | 404 |
| 405 var problems = new Array(); | 405 var problems = new Array(); |
| 406 var dropped = new Array(); | 406 var dropped = new Array(); |
| 407 for (var i = 0; i < shared_list.length; i++) { | 407 for (var i = 0; i < shared_list.length; i++) { |
| 408 var shared = shared_wrapper_list[i]; | 408 var shared = shared_wrapper_list[i]; |
| 409 if (result[i] == liveedit.FunctionPatchabilityStatus.REPLACED_ON_ACTIVE_STAC
K) { | 409 if (result[i] == FunctionPatchabilityStatus.REPLACED_ON_ACTIVE_STACK) { |
| 410 dropped.push({ name: shared.function_name } ); | 410 dropped.push({ name: shared.function_name } ); |
| 411 } else if (result[i] != liveedit.FunctionPatchabilityStatus.AVAILABLE_FOR_PA
TCH) { | 411 } else if (result[i] != FunctionPatchabilityStatus.AVAILABLE_FOR_PATCH) { |
| 412 var description = { | 412 var description = { |
| 413 name: shared.function_name, | 413 name: shared.function_name, |
| 414 start_pos: shared.start_position, | 414 start_pos: shared.start_position, |
| 415 end_pos: shared.end_position, | 415 end_pos: shared.end_position, |
| 416 replace_problem: | 416 replace_problem: |
| 417 liveedit.FunctionPatchabilityStatus.SymbolName(result[i]) | 417 FunctionPatchabilityStatus.SymbolName(result[i]) |
| 418 }; | 418 }; |
| 419 problems.push(description); | 419 problems.push(description); |
| 420 } | 420 } |
| 421 } | 421 } |
| 422 if (dropped.length > 0) { | 422 if (dropped.length > 0) { |
| 423 change_log.push({ dropped_from_stack: dropped }); | 423 change_log.push({ dropped_from_stack: dropped }); |
| 424 } | 424 } |
| 425 if (problems.length > 0) { | 425 if (problems.length > 0) { |
| 426 change_log.push( { functions_on_stack: problems } ); | 426 change_log.push( { functions_on_stack: problems } ); |
| 427 throw new liveedit.Failure("Blocked by functions on stack"); | 427 throw new Failure("Blocked by functions on stack"); |
| 428 } | 428 } |
| 429 } | 429 } |
| 430 | 430 |
| 431 // A copy of the FunctionPatchabilityStatus enum from liveedit.h | 431 // A copy of the FunctionPatchabilityStatus enum from liveedit.h |
| 432 Debug.LiveEditChangeScript.FunctionPatchabilityStatus = { | 432 var FunctionPatchabilityStatus = { |
| 433 AVAILABLE_FOR_PATCH: 1, | 433 AVAILABLE_FOR_PATCH: 1, |
| 434 BLOCKED_ON_ACTIVE_STACK: 2, | 434 BLOCKED_ON_ACTIVE_STACK: 2, |
| 435 BLOCKED_ON_OTHER_STACK: 3, | 435 BLOCKED_ON_OTHER_STACK: 3, |
| 436 BLOCKED_UNDER_NATIVE_CODE: 4, | 436 BLOCKED_UNDER_NATIVE_CODE: 4, |
| 437 REPLACED_ON_ACTIVE_STACK: 5 | 437 REPLACED_ON_ACTIVE_STACK: 5 |
| 438 } | 438 } |
| 439 | 439 |
| 440 Debug.LiveEditChangeScript.FunctionPatchabilityStatus.SymbolName = | 440 FunctionPatchabilityStatus.SymbolName = function(code) { |
| 441 function(code) { | 441 var enum = FunctionPatchabilityStatus; |
| 442 var enum = Debug.LiveEditChangeScript.FunctionPatchabilityStatus; | |
| 443 for (name in enum) { | 442 for (name in enum) { |
| 444 if (enum[name] == code) { | 443 if (enum[name] == code) { |
| 445 return name; | 444 return name; |
| 446 } | 445 } |
| 447 } | 446 } |
| 448 } | 447 } |
| 449 | 448 |
| 450 | 449 |
| 451 // A logical failure in liveedit process. This means that change_log | 450 // A logical failure in liveedit process. This means that change_log |
| 452 // is valid and consistent description of what happened. | 451 // is valid and consistent description of what happened. |
| 453 Debug.LiveEditChangeScript.Failure = function(message) { | 452 function Failure(message) { |
| 454 this.message = message; | 453 this.message = message; |
| 455 } | 454 } |
| 455 // Function (constructor) is public. |
| 456 this.Failure = Failure; |
| 456 | 457 |
| 457 Debug.LiveEditChangeScript.Failure.prototype.toString = function() { | 458 Failure.prototype.toString = function() { |
| 458 return "LiveEdit Failure: " + this.message; | 459 return "LiveEdit Failure: " + this.message; |
| 459 } | 460 } |
| 460 | 461 |
| 461 // A testing entry. | 462 // A testing entry. |
| 462 Debug.LiveEditChangeScript.GetPcFromSourcePos = function(func, source_pos) { | 463 function GetPcFromSourcePos(func, source_pos) { |
| 463 return %GetFunctionCodePositionFromSource(func, source_pos); | 464 return %GetFunctionCodePositionFromSource(func, source_pos); |
| 464 } | 465 } |
| 466 // Function is public. |
| 467 this.GetPcFromSourcePos = GetPcFromSourcePos; |
| 465 | 468 |
| 466 // A LiveEdit namespace is declared inside a single function constructor. | 469 // TODO(LiveEdit): restore indentation above |
| 467 Debug.LiveEdit = new function() { | |
| 468 var LiveEdit = this; | |
| 469 | |
| 470 | 470 |
| 471 // LiveEdit main entry point: changes a script text to a new string. | 471 // LiveEdit main entry point: changes a script text to a new string. |
| 472 LiveEdit.SetScriptSource = function(script, new_source, change_log) { | 472 function SetScriptSource(script, new_source, change_log) { |
| 473 var old_source = script.source; | 473 var old_source = script.source; |
| 474 var diff = FindSimpleDiff(old_source, new_source); | 474 var diff = FindSimpleDiff(old_source, new_source); |
| 475 if (!diff) { | 475 if (!diff) { |
| 476 return; | 476 return; |
| 477 } | 477 } |
| 478 Debug.LiveEditChangeScript(script, diff.change_pos, diff.old_len, | 478 ApplyPatch(script, diff.change_pos, diff.old_len, |
| 479 new_source.substring(diff.change_pos, diff.change_pos + diff.new_len), | 479 new_source.substring(diff.change_pos, diff.change_pos + diff.new_len), |
| 480 change_log); | 480 change_log); |
| 481 } | 481 } |
| 482 // Function is public. |
| 483 this.SetScriptSource = SetScriptSource; |
| 482 | 484 |
| 483 | 485 |
| 484 // Finds a difference between 2 strings in form of a single chunk. | 486 // Finds a difference between 2 strings in form of a single chunk. |
| 485 // This is a temporary solution. We should calculate a read diff instead. | 487 // This is a temporary solution. We should calculate a read diff instead. |
| 486 function FindSimpleDiff(old_source, new_source) { | 488 function FindSimpleDiff(old_source, new_source) { |
| 487 var change_pos; | 489 var change_pos; |
| 488 var old_len; | 490 var old_len; |
| 489 var new_len; | 491 var new_len; |
| 490 | 492 |
| 491 // A find range block. Whenever control leaves it, it should set 3 local | 493 // A find range block. Whenever control leaves it, it should set 3 local |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 } | 540 } |
| 539 | 541 |
| 540 if (old_len == 0 && new_len == 0) { | 542 if (old_len == 0 && new_len == 0) { |
| 541 // no change | 543 // no change |
| 542 return; | 544 return; |
| 543 } | 545 } |
| 544 | 546 |
| 545 return { "change_pos": change_pos, "old_len": old_len, "new_len": new_len }; | 547 return { "change_pos": change_pos, "old_len": old_len, "new_len": new_len }; |
| 546 } | 548 } |
| 547 } | 549 } |
| OLD | NEW |