| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/v8.h" | 5 #include "src/v8.h" |
| 6 | 6 |
| 7 #include "src/scanner-character-streams.h" | 7 #include "src/scanner-character-streams.h" |
| 8 | 8 |
| 9 #include "include/v8.h" | 9 #include "include/v8.h" |
| 10 #include "src/handles.h" | 10 #include "src/handles.h" |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 // completely; it will typically leave the last character empty (see | 339 // completely; it will typically leave the last character empty (see |
| 340 // Utf8ToUtf16CharacterStream::CopyChars). | 340 // Utf8ToUtf16CharacterStream::CopyChars). |
| 341 while (data_in_buffer < kBufferSize - 1) { | 341 while (data_in_buffer < kBufferSize - 1) { |
| 342 if (current_data_ == NULL) { | 342 if (current_data_ == NULL) { |
| 343 // GetSomeData will wait until the embedder has enough data. Here's an | 343 // GetSomeData will wait until the embedder has enough data. Here's an |
| 344 // interface between the API which uses size_t (which is the correct type | 344 // interface between the API which uses size_t (which is the correct type |
| 345 // here) and the internal parts which use size_t. | 345 // here) and the internal parts which use size_t. |
| 346 current_data_length_ = source_stream_->GetMoreData(¤t_data_); | 346 current_data_length_ = source_stream_->GetMoreData(¤t_data_); |
| 347 current_data_offset_ = 0; | 347 current_data_offset_ = 0; |
| 348 bool data_ends = current_data_length_ == 0; | 348 bool data_ends = current_data_length_ == 0; |
| 349 bookmark_data_is_from_current_data_ = false; |
| 349 | 350 |
| 350 // A caveat: a data chunk might end with bytes from an incomplete UTF-8 | 351 // A caveat: a data chunk might end with bytes from an incomplete UTF-8 |
| 351 // character (the rest of the bytes will be in the next chunk). | 352 // character (the rest of the bytes will be in the next chunk). |
| 352 if (encoding_ == ScriptCompiler::StreamedSource::UTF8) { | 353 if (encoding_ == ScriptCompiler::StreamedSource::UTF8) { |
| 353 HandleUtf8SplitCharacters(&data_in_buffer); | 354 HandleUtf8SplitCharacters(&data_in_buffer); |
| 354 if (!data_ends && current_data_offset_ == current_data_length_) { | 355 if (!data_ends && current_data_offset_ == current_data_length_) { |
| 355 // The data stream didn't end, but we used all the data in the | 356 // The data stream didn't end, but we used all the data in the |
| 356 // chunk. This will only happen when the chunk was really small. We | 357 // chunk. This will only happen when the chunk was really small. We |
| 357 // don't handle the case where a UTF-8 character is split over several | 358 // don't handle the case where a UTF-8 character is split over several |
| 358 // chunks; in that case V8 won't crash, but it will be a parse error. | 359 // chunks; in that case V8 won't crash, but it will be a parse error. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 // | 399 // |
| 399 // The underlying source_stream_ instance likely could re-construct this | 400 // The underlying source_stream_ instance likely could re-construct this |
| 400 // local data for us, but with the given interfaces we have no way of | 401 // local data for us, but with the given interfaces we have no way of |
| 401 // accomplishing this. Thus, we'll have to save all data locally. | 402 // accomplishing this. Thus, we'll have to save all data locally. |
| 402 // | 403 // |
| 403 // What gets saved where: | 404 // What gets saved where: |
| 404 // - pos_ => bookmark_ | 405 // - pos_ => bookmark_ |
| 405 // - buffer_[buffer_cursor_ .. buffer_end_] => bookmark_buffer_ | 406 // - buffer_[buffer_cursor_ .. buffer_end_] => bookmark_buffer_ |
| 406 // - current_data_[.._offset_ .. .._length_] => bookmark_data_ | 407 // - current_data_[.._offset_ .. .._length_] => bookmark_data_ |
| 407 // - utf8_split_char_buffer_* => bookmark_utf8_split... | 408 // - utf8_split_char_buffer_* => bookmark_utf8_split... |
| 409 // |
| 410 // To make sure we don't unnecessarily copy data, we also maintain |
| 411 // whether bookmark_data_ contains a copy of the current current_data_ |
| 412 // block. This is done with: |
| 413 // - bookmark_data_is_from_current_data_ |
| 414 // - bookmark_data_offset_: offset into bookmark_data_ |
| 415 // |
| 416 // Note that bookmark_data_is_from_current_data_ must be maintained |
| 417 // whenever current_data_ is updated. |
| 408 | 418 |
| 409 bookmark_ = pos_; | 419 bookmark_ = pos_; |
| 410 | 420 |
| 411 size_t buffer_length = buffer_end_ - buffer_cursor_; | 421 size_t buffer_length = buffer_end_ - buffer_cursor_; |
| 412 bookmark_buffer_.Dispose(); | 422 bookmark_buffer_.Dispose(); |
| 413 bookmark_buffer_ = Vector<uint16_t>::New(static_cast<int>(buffer_length)); | 423 bookmark_buffer_ = Vector<uint16_t>::New(static_cast<int>(buffer_length)); |
| 414 CopyCharsUnsigned(bookmark_buffer_.start(), buffer_cursor_, buffer_length); | 424 CopyCharsUnsigned(bookmark_buffer_.start(), buffer_cursor_, buffer_length); |
| 415 | 425 |
| 416 size_t data_length = current_data_length_ - current_data_offset_; | 426 size_t data_length = current_data_length_ - current_data_offset_; |
| 417 bookmark_data_.Dispose(); | 427 size_t bookmark_data_length = static_cast<size_t>(bookmark_data_.length()); |
| 418 bookmark_data_ = Vector<uint8_t>::New(static_cast<int>(data_length)); | 428 if (bookmark_data_is_from_current_data_ && |
| 419 CopyBytes(bookmark_data_.start(), current_data_ + current_data_offset_, | 429 data_length < bookmark_data_length) { |
| 420 data_length); | 430 // Fast case: bookmark_data_ was previously copied from the current |
| 431 // data block, and we have enough data for this bookmark. |
| 432 bookmark_data_offset_ = bookmark_data_length - data_length; |
| 433 } else { |
| 434 // Slow case: We need to copy current_data_. |
| 435 bookmark_data_.Dispose(); |
| 436 bookmark_data_ = Vector<uint8_t>::New(static_cast<int>(data_length)); |
| 437 CopyBytes(bookmark_data_.start(), current_data_ + current_data_offset_, |
| 438 data_length); |
| 439 bookmark_data_is_from_current_data_ = true; |
| 440 bookmark_data_offset_ = 0; |
| 441 } |
| 421 | 442 |
| 422 bookmark_utf8_split_char_buffer_length_ = utf8_split_char_buffer_length_; | 443 bookmark_utf8_split_char_buffer_length_ = utf8_split_char_buffer_length_; |
| 423 for (size_t i = 0; i < utf8_split_char_buffer_length_; i++) { | 444 for (size_t i = 0; i < utf8_split_char_buffer_length_; i++) { |
| 424 bookmark_utf8_split_char_buffer_[i] = utf8_split_char_buffer_[i]; | 445 bookmark_utf8_split_char_buffer_[i] = utf8_split_char_buffer_[i]; |
| 425 } | 446 } |
| 426 | 447 |
| 427 return source_stream_->SetBookmark(); | 448 return source_stream_->SetBookmark(); |
| 428 } | 449 } |
| 429 | 450 |
| 430 | 451 |
| 431 void ExternalStreamingStream::ResetToBookmark() { | 452 void ExternalStreamingStream::ResetToBookmark() { |
| 432 source_stream_->ResetToBookmark(); | 453 source_stream_->ResetToBookmark(); |
| 433 FlushCurrent(); | 454 FlushCurrent(); |
| 434 | 455 |
| 435 pos_ = bookmark_; | 456 pos_ = bookmark_; |
| 436 | 457 |
| 437 // bookmark_data_* => current_data_* | 458 // bookmark_data_* => current_data_* |
| 438 // (current_data_ assumes ownership of its memory.) | 459 // (current_data_ assumes ownership of its memory.) |
| 439 uint8_t* data = new uint8_t[bookmark_data_.length()]; | |
| 440 current_data_offset_ = 0; | 460 current_data_offset_ = 0; |
| 441 current_data_length_ = bookmark_data_.length(); | 461 current_data_length_ = bookmark_data_.length() - bookmark_data_offset_; |
| 442 CopyCharsUnsigned(data, bookmark_data_.begin(), bookmark_data_.length()); | 462 uint8_t* data = new uint8_t[current_data_length_]; |
| 463 CopyCharsUnsigned(data, bookmark_data_.begin() + bookmark_data_offset_, |
| 464 current_data_length_); |
| 443 delete[] current_data_; | 465 delete[] current_data_; |
| 444 current_data_ = data; | 466 current_data_ = data; |
| 467 bookmark_data_is_from_current_data_ = true; |
| 445 | 468 |
| 446 // bookmark_buffer_ needs to be copied to buffer_. | 469 // bookmark_buffer_ needs to be copied to buffer_. |
| 447 CopyCharsUnsigned(buffer_, bookmark_buffer_.begin(), | 470 CopyCharsUnsigned(buffer_, bookmark_buffer_.begin(), |
| 448 bookmark_buffer_.length()); | 471 bookmark_buffer_.length()); |
| 449 buffer_cursor_ = buffer_; | 472 buffer_cursor_ = buffer_; |
| 450 buffer_end_ = buffer_ + bookmark_buffer_.length(); | 473 buffer_end_ = buffer_ + bookmark_buffer_.length(); |
| 451 | 474 |
| 452 // utf8 split char buffer | 475 // utf8 split char buffer |
| 453 utf8_split_char_buffer_length_ = bookmark_utf8_split_char_buffer_length_; | 476 utf8_split_char_buffer_length_ = bookmark_utf8_split_char_buffer_length_; |
| 454 for (size_t i = 0; i < bookmark_utf8_split_char_buffer_length_; i++) { | 477 for (size_t i = 0; i < bookmark_utf8_split_char_buffer_length_; i++) { |
| 455 utf8_split_char_buffer_[i] = bookmark_utf8_split_char_buffer_[i]; | 478 utf8_split_char_buffer_[i] = bookmark_utf8_split_char_buffer_[i]; |
| 456 } | 479 } |
| 457 } | 480 } |
| 458 | 481 |
| 459 | 482 |
| 460 void ExternalStreamingStream::FlushCurrent() { | 483 void ExternalStreamingStream::FlushCurrent() { |
| 461 delete[] current_data_; | 484 delete[] current_data_; |
| 462 current_data_ = NULL; | 485 current_data_ = NULL; |
| 463 current_data_length_ = 0; | 486 current_data_length_ = 0; |
| 464 current_data_offset_ = 0; | 487 current_data_offset_ = 0; |
| 488 bookmark_data_is_from_current_data_ = false; |
| 465 } | 489 } |
| 466 | 490 |
| 467 | 491 |
| 468 void ExternalStreamingStream::HandleUtf8SplitCharacters( | 492 void ExternalStreamingStream::HandleUtf8SplitCharacters( |
| 469 size_t* data_in_buffer) { | 493 size_t* data_in_buffer) { |
| 470 // Note the following property of UTF-8 which makes this function possible: | 494 // Note the following property of UTF-8 which makes this function possible: |
| 471 // Given any byte, we can always read its local environment (in both | 495 // Given any byte, we can always read its local environment (in both |
| 472 // directions) to find out the (possibly multi-byte) character it belongs | 496 // directions) to find out the (possibly multi-byte) character it belongs |
| 473 // to. Single byte characters are of the form 0b0XXXXXXX. The first byte of a | 497 // to. Single byte characters are of the form 0b0XXXXXXX. The first byte of a |
| 474 // multi-byte character is of the form 0b110XXXXX, 0b1110XXXX or | 498 // multi-byte character is of the form 0b110XXXXX, 0b1110XXXX or |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 } | 578 } |
| 555 | 579 |
| 556 | 580 |
| 557 void ExternalTwoByteStringUtf16CharacterStream::ResetToBookmark() { | 581 void ExternalTwoByteStringUtf16CharacterStream::ResetToBookmark() { |
| 558 DCHECK(bookmark_ != kNoBookmark); | 582 DCHECK(bookmark_ != kNoBookmark); |
| 559 pos_ = bookmark_; | 583 pos_ = bookmark_; |
| 560 buffer_cursor_ = raw_data_ + bookmark_; | 584 buffer_cursor_ = raw_data_ + bookmark_; |
| 561 } | 585 } |
| 562 } // namespace internal | 586 } // namespace internal |
| 563 } // namespace v8 | 587 } // namespace v8 |
| OLD | NEW |