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 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 for (int i = 0; i < len; i++) { | 268 for (int i = 0; i < len; i++) { |
269 ASSERT(buf1.has_more() && buf2.has_more()); | 269 ASSERT(buf1.has_more() && buf2.has_more()); |
270 if (buf1.GetNext() != buf2.GetNext()) { | 270 if (buf1.GetNext() != buf2.GetNext()) { |
271 return false; | 271 return false; |
272 } | 272 } |
273 } | 273 } |
274 return true; | 274 return true; |
275 } | 275 } |
276 | 276 |
277 | 277 |
| 278 // A helper class that writes chunk numbers into JSArray. |
| 279 // Each chunk is stored as 3 array elements: (pos1_begin, pos1_end, pos2_end). |
| 280 class CompareOutputArrayWriter { |
| 281 public: |
| 282 CompareOutputArrayWriter() |
| 283 : array_(Factory::NewJSArray(10)), current_size_(0) {} |
| 284 |
| 285 Handle<JSArray> GetResult() { |
| 286 return array_; |
| 287 } |
| 288 |
| 289 void WriteChunk(int char_pos1, int char_pos2, int char_len1, int char_len2) { |
| 290 SetElement(array_, current_size_, Handle<Object>(Smi::FromInt(char_pos1))); |
| 291 SetElement(array_, current_size_ + 1, |
| 292 Handle<Object>(Smi::FromInt(char_pos1 + char_len1))); |
| 293 SetElement(array_, current_size_ + 2, |
| 294 Handle<Object>(Smi::FromInt(char_pos2 + char_len2))); |
| 295 current_size_ += 3; |
| 296 } |
| 297 |
| 298 private: |
| 299 Handle<JSArray> array_; |
| 300 int current_size_; |
| 301 }; |
| 302 |
| 303 |
| 304 // Represents 2 strings as 2 arrays of tokens. |
| 305 // TODO(LiveEdit): Currently it's actually an array of charactres. |
| 306 // Make array of tokens instead. |
| 307 class TokensCompareInput : public Comparator::Input { |
| 308 public: |
| 309 TokensCompareInput(Handle<String> s1, int offset1, int len1, |
| 310 Handle<String> s2, int offset2, int len2) |
| 311 : s1_(s1), offset1_(offset1), len1_(len1), |
| 312 s2_(s2), offset2_(offset2), len2_(len2) { |
| 313 } |
| 314 virtual int getLength1() { |
| 315 return len1_; |
| 316 } |
| 317 virtual int getLength2() { |
| 318 return len2_; |
| 319 } |
| 320 bool equals(int index1, int index2) { |
| 321 return s1_->Get(offset1_ + index1) == s2_->Get(offset2_ + index2); |
| 322 } |
| 323 |
| 324 private: |
| 325 Handle<String> s1_; |
| 326 int offset1_; |
| 327 int len1_; |
| 328 Handle<String> s2_; |
| 329 int offset2_; |
| 330 int len2_; |
| 331 }; |
| 332 |
| 333 |
| 334 // Stores compare result in JSArray. Converts substring positions |
| 335 // to absolute positions. |
| 336 class TokensCompareOutput : public Comparator::Output { |
| 337 public: |
| 338 TokensCompareOutput(CompareOutputArrayWriter* array_writer, |
| 339 int offset1, int offset2) |
| 340 : array_writer_(array_writer), offset1_(offset1), offset2_(offset2) { |
| 341 } |
| 342 |
| 343 void AddChunk(int pos1, int pos2, int len1, int len2) { |
| 344 array_writer_->WriteChunk(pos1 + offset1_, pos2 + offset2_, len1, len2); |
| 345 } |
| 346 |
| 347 private: |
| 348 CompareOutputArrayWriter* array_writer_; |
| 349 int offset1_; |
| 350 int offset2_; |
| 351 }; |
| 352 |
| 353 |
278 // Wraps raw n-elements line_ends array as a list of n+1 lines. The last line | 354 // Wraps raw n-elements line_ends array as a list of n+1 lines. The last line |
279 // never has terminating new line character. | 355 // never has terminating new line character. |
280 class LineEndsWrapper { | 356 class LineEndsWrapper { |
281 public: | 357 public: |
282 explicit LineEndsWrapper(Handle<String> string) | 358 explicit LineEndsWrapper(Handle<String> string) |
283 : ends_array_(CalculateLineEnds(string, false)), | 359 : ends_array_(CalculateLineEnds(string, false)), |
284 string_len_(string->length()) { | 360 string_len_(string->length()) { |
285 } | 361 } |
286 int length() { | 362 int length() { |
287 return ends_array_->length() + 1; | 363 return ends_array_->length() + 1; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 } | 419 } |
344 | 420 |
345 private: | 421 private: |
346 Handle<String> s1_; | 422 Handle<String> s1_; |
347 Handle<String> s2_; | 423 Handle<String> s2_; |
348 LineEndsWrapper line_ends1_; | 424 LineEndsWrapper line_ends1_; |
349 LineEndsWrapper line_ends2_; | 425 LineEndsWrapper line_ends2_; |
350 }; | 426 }; |
351 | 427 |
352 | 428 |
353 // Stores compare result in JSArray. Each chunk is stored as 3 array elements: | 429 // Stores compare result in JSArray. For each chunk tries to conduct |
354 // (pos1_begin, pos1_end, pos2_end). | 430 // a fine-grained nested diff token-wise. |
355 class LineArrayCompareOutput : public Comparator::Output { | 431 class TokenizingLineArrayCompareOutput : public Comparator::Output { |
356 public: | 432 public: |
357 LineArrayCompareOutput(LineEndsWrapper line_ends1, LineEndsWrapper line_ends2) | 433 TokenizingLineArrayCompareOutput(LineEndsWrapper line_ends1, |
358 : array_(Factory::NewJSArray(10)), current_size_(0), | 434 LineEndsWrapper line_ends2, |
359 line_ends1_(line_ends1), line_ends2_(line_ends2) { | 435 Handle<String> s1, Handle<String> s2) |
| 436 : line_ends1_(line_ends1), line_ends2_(line_ends2), s1_(s1), s2_(s2) { |
360 } | 437 } |
361 | 438 |
362 void AddChunk(int line_pos1, int line_pos2, int line_len1, int line_len2) { | 439 void AddChunk(int line_pos1, int line_pos2, int line_len1, int line_len2) { |
363 int char_pos1 = line_ends1_.GetLineStart(line_pos1); | 440 int char_pos1 = line_ends1_.GetLineStart(line_pos1); |
364 int char_pos2 = line_ends2_.GetLineStart(line_pos2); | 441 int char_pos2 = line_ends2_.GetLineStart(line_pos2); |
365 int char_len1 = line_ends1_.GetLineStart(line_pos1 + line_len1) - char_pos1; | 442 int char_len1 = line_ends1_.GetLineStart(line_pos1 + line_len1) - char_pos1; |
366 int char_len2 = line_ends2_.GetLineStart(line_pos2 + line_len2) - char_pos2; | 443 int char_len2 = line_ends2_.GetLineStart(line_pos2 + line_len2) - char_pos2; |
367 | 444 |
368 SetElement(array_, current_size_, Handle<Object>(Smi::FromInt(char_pos1))); | 445 if (char_len1 < CHUNK_LEN_LIMIT && char_len2 < CHUNK_LEN_LIMIT) { |
369 SetElement(array_, current_size_ + 1, | 446 // Chunk is small enough to conduct a nested token-level diff. |
370 Handle<Object>(Smi::FromInt(char_pos1 + char_len1))); | 447 HandleScope subTaskScope; |
371 SetElement(array_, current_size_ + 2, | 448 |
372 Handle<Object>(Smi::FromInt(char_pos2 + char_len2))); | 449 TokensCompareInput tokens_input(s1_, char_pos1, char_len1, |
373 current_size_ += 3; | 450 s2_, char_pos2, char_len2); |
| 451 TokensCompareOutput tokens_output(&array_writer_, char_pos1, |
| 452 char_pos2); |
| 453 |
| 454 Comparator::CalculateDifference(&tokens_input, &tokens_output); |
| 455 } else { |
| 456 array_writer_.WriteChunk(char_pos1, char_pos2, char_len1, char_len2); |
| 457 } |
374 } | 458 } |
375 | 459 |
376 Handle<JSArray> GetResult() { | 460 Handle<JSArray> GetResult() { |
377 return array_; | 461 return array_writer_.GetResult(); |
378 } | 462 } |
379 | 463 |
380 private: | 464 private: |
381 Handle<JSArray> array_; | 465 static const int CHUNK_LEN_LIMIT = 800; |
382 int current_size_; | 466 |
| 467 CompareOutputArrayWriter array_writer_; |
383 LineEndsWrapper line_ends1_; | 468 LineEndsWrapper line_ends1_; |
384 LineEndsWrapper line_ends2_; | 469 LineEndsWrapper line_ends2_; |
| 470 Handle<String> s1_; |
| 471 Handle<String> s2_; |
385 }; | 472 }; |
386 | 473 |
387 | 474 |
388 Handle<JSArray> LiveEdit::CompareStringsLinewise(Handle<String> s1, | 475 Handle<JSArray> LiveEdit::CompareStrings(Handle<String> s1, |
389 Handle<String> s2) { | 476 Handle<String> s2) { |
390 LineEndsWrapper line_ends1(s1); | 477 LineEndsWrapper line_ends1(s1); |
391 LineEndsWrapper line_ends2(s2); | 478 LineEndsWrapper line_ends2(s2); |
392 | 479 |
393 LineArrayCompareInput input(s1, s2, line_ends1, line_ends2); | 480 LineArrayCompareInput input(s1, s2, line_ends1, line_ends2); |
394 LineArrayCompareOutput output(line_ends1, line_ends2); | 481 TokenizingLineArrayCompareOutput output(line_ends1, line_ends2, s1, s2); |
395 | 482 |
396 Comparator::CalculateDifference(&input, &output); | 483 Comparator::CalculateDifference(&input, &output); |
397 | 484 |
398 return output.GetResult(); | 485 return output.GetResult(); |
399 } | 486 } |
400 | 487 |
401 | 488 |
402 static void CompileScriptForTracker(Handle<Script> script) { | 489 static void CompileScriptForTracker(Handle<Script> script) { |
403 // TODO(635): support extensions. | 490 // TODO(635): support extensions. |
404 PostponeInterruptsScope postpone; | 491 PostponeInterruptsScope postpone; |
(...skipping 1150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1555 | 1642 |
1556 bool LiveEditFunctionTracker::IsActive() { | 1643 bool LiveEditFunctionTracker::IsActive() { |
1557 return false; | 1644 return false; |
1558 } | 1645 } |
1559 | 1646 |
1560 #endif // ENABLE_DEBUGGER_SUPPORT | 1647 #endif // ENABLE_DEBUGGER_SUPPORT |
1561 | 1648 |
1562 | 1649 |
1563 | 1650 |
1564 } } // namespace v8::internal | 1651 } } // namespace v8::internal |
OLD | NEW |