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 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 } | 422 } |
423 | 423 |
424 Debug.LiveEditChangeScript.Failure.prototype.toString = function() { | 424 Debug.LiveEditChangeScript.Failure.prototype.toString = function() { |
425 return "LiveEdit Failure: " + this.message; | 425 return "LiveEdit Failure: " + this.message; |
426 } | 426 } |
427 | 427 |
428 // A testing entry. | 428 // A testing entry. |
429 Debug.LiveEditChangeScript.GetPcFromSourcePos = function(func, source_pos) { | 429 Debug.LiveEditChangeScript.GetPcFromSourcePos = function(func, source_pos) { |
430 return %GetFunctionCodePositionFromSource(func, source_pos); | 430 return %GetFunctionCodePositionFromSource(func, source_pos); |
431 } | 431 } |
| 432 |
| 433 // A LiveEdit namespace is declared inside a single function constructor. |
| 434 Debug.LiveEdit = new function() { |
| 435 var LiveEdit = this; |
| 436 |
| 437 |
| 438 // LiveEdit main entry point: changes a script text to a new string. |
| 439 LiveEdit.SetScriptSource = function(script, new_source, change_log) { |
| 440 var old_source = script.source; |
| 441 var diff = FindSimpleDiff(old_source, new_source); |
| 442 if (!diff) { |
| 443 return; |
| 444 } |
| 445 Debug.LiveEditChangeScript(script, diff.change_pos, diff.old_len, |
| 446 new_source.substring(diff.change_pos, diff.change_pos + diff.new_len), |
| 447 change_log); |
| 448 } |
| 449 |
| 450 |
| 451 // Finds a difference between 2 strings in form of a single chunk. |
| 452 // This is a temporary solution. We should calculate a read diff instead. |
| 453 function FindSimpleDiff(old_source, new_source) { |
| 454 var change_pos; |
| 455 var old_len; |
| 456 var new_len; |
| 457 |
| 458 // A find range block. Whenever control leaves it, it should set 3 local |
| 459 // variables declared above. |
| 460 find_range: |
| 461 { |
| 462 // First look from the beginning of strings. |
| 463 var pos1; |
| 464 { |
| 465 var next_pos; |
| 466 for (pos1 = 0; true; pos1 = next_pos) { |
| 467 if (pos1 >= old_source.length) { |
| 468 change_pos = pos1; |
| 469 old_len = 0; |
| 470 new_len = new_source.length - pos1; |
| 471 break find_range; |
| 472 } |
| 473 if (pos1 >= new_source.length) { |
| 474 change_pos = pos1; |
| 475 old_len = old_source.length - pos1; |
| 476 new_len = 0; |
| 477 break find_range; |
| 478 } |
| 479 if (old_source[pos1] != new_source[pos1]) { |
| 480 break; |
| 481 } |
| 482 next_pos = pos1 + 1; |
| 483 } |
| 484 } |
| 485 // Now compare strings from the ends. |
| 486 change_pos = pos1; |
| 487 var pos_old; |
| 488 var pos_new; |
| 489 { |
| 490 for (pos_old = old_source.length - 1, pos_new = new_source.length - 1; |
| 491 true; |
| 492 pos_old--, pos_new--) { |
| 493 if (pos_old - change_pos + 1 < 0 || pos_new - change_pos + 1 < 0) { |
| 494 old_len = pos_old - change_pos + 2; |
| 495 new_len = pos_new - change_pos + 2; |
| 496 break find_range; |
| 497 } |
| 498 if (old_source[pos_old] != new_source[pos_new]) { |
| 499 old_len = pos_old - change_pos + 1; |
| 500 new_len = pos_new - change_pos + 1; |
| 501 break find_range; |
| 502 } |
| 503 } |
| 504 } |
| 505 } |
| 506 |
| 507 if (old_len == 0 && new_len == 0) { |
| 508 // no change |
| 509 return; |
| 510 } |
| 511 |
| 512 return { "change_pos": change_pos, "old_len": old_len, "new_len": new_len }; |
| 513 } |
| 514 } |
OLD | NEW |