Chromium Code Reviews| 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/parsing/scanner-character-streams.h" | 5 #include "src/parsing/scanner-character-streams.h" |
| 6 | 6 |
| 7 #include "include/v8.h" | 7 #include "include/v8.h" |
| 8 #include "src/globals.h" | 8 #include "src/globals.h" |
| 9 #include "src/handles.h" | 9 #include "src/handles.h" |
| 10 #include "src/objects-inl.h" | 10 #include "src/objects-inl.h" |
| 11 #include "src/parsing/scanner.h" | |
| 11 #include "src/unicode-inl.h" | 12 #include "src/unicode-inl.h" |
| 13 #include "src/utils.h" // for Mem[Copy|Move].* | |
| 12 | 14 |
| 13 namespace v8 { | 15 namespace v8 { |
| 14 namespace internal { | 16 namespace internal { |
| 15 | 17 |
| 16 namespace { | 18 // ---------------------------------------------------------------------------- |
| 19 // BufferedUtf16CharacterStreams | |
| 20 // | |
| 21 // A buffered character stream based on a random access character | |
| 22 // source (ReadBlock can be called with pos_ pointing to any position, | |
| 23 // even positions before the current). | |
| 24 class BufferedUtf16CharacterStream : public Utf16CharacterStream { | |
| 25 public: | |
| 26 BufferedUtf16CharacterStream(); | |
| 27 ~BufferedUtf16CharacterStream() override; | |
| 17 | 28 |
| 18 size_t CopyUtf8CharsToUtf16Chars(uint16_t* dest, size_t length, const byte* src, | 29 protected: |
| 19 size_t* src_pos, size_t src_length) { | 30 static const size_t kBufferSize = 512; |
| 20 static const unibrow::uchar kMaxUtf16Character = | 31 |
| 21 unibrow::Utf16::kMaxNonSurrogateCharCode; | 32 bool ReadBlock() override; |
| 22 size_t i = 0; | 33 virtual size_t FillBuffer(size_t position) = 0; |
| 23 // Because of the UTF-16 lead and trail surrogates, we stop filling the buffer | 34 |
| 24 // one character early (in the normal case), because we need to have at least | 35 uc16 buffer_[kBufferSize]; |
| 25 // two free spaces in the buffer to be sure that the next character will fit. | 36 }; |
| 26 while (i < length - 1) { | 37 |
| 27 if (*src_pos == src_length) break; | 38 BufferedUtf16CharacterStream::BufferedUtf16CharacterStream() |
| 28 unibrow::uchar c = src[*src_pos]; | 39 : Utf16CharacterStream(buffer_, buffer_, buffer_, 0) {} |
| 29 if (c <= unibrow::Utf8::kMaxOneByteChar) { | 40 |
| 30 *src_pos = *src_pos + 1; | 41 BufferedUtf16CharacterStream::~BufferedUtf16CharacterStream() {} |
| 31 } else { | 42 |
| 32 c = unibrow::Utf8::CalculateValue(src + *src_pos, src_length - *src_pos, | 43 bool BufferedUtf16CharacterStream::ReadBlock() { |
| 33 src_pos); | 44 DCHECK_EQ(buffer_start_, buffer_); // Please nobody mess w/ buffer_start_. |
|
marja
2016/09/08 09:46:23
Hmm, nikolaos@ commented on this offline I think..
vogelheim
2016/09/08 15:02:18
None of BufferedUtf16CharacterStream's sub-classes
| |
| 34 } | 45 |
| 35 if (c > kMaxUtf16Character) { | 46 size_t position = pos(); |
| 36 dest[i++] = unibrow::Utf16::LeadSurrogate(c); | 47 buffer_pos_ = position; |
| 37 dest[i++] = unibrow::Utf16::TrailSurrogate(c); | 48 buffer_cursor_ = buffer_; |
| 38 } else { | 49 buffer_end_ = buffer_ + FillBuffer(position); |
| 39 dest[i++] = static_cast<uc16>(c); | 50 DCHECK_EQ(pos(), position); |
| 40 } | 51 DCHECK_LE(buffer_end_, buffer_start_ + kBufferSize); |
| 41 } | 52 return buffer_cursor_ < buffer_end_; |
| 42 return i; | |
| 43 } | 53 } |
| 44 | 54 |
| 45 size_t CopyCharsHelper(uint16_t* dest, size_t length, const uint8_t* src, | 55 // ---------------------------------------------------------------------------- |
| 46 size_t* src_pos, size_t src_length, | 56 // GenericStringUtf16CharacterStream. |
| 47 ScriptCompiler::StreamedSource::Encoding encoding) { | 57 // |
| 48 // It's possible that this will be called with length 0, but don't assume that | 58 // A stream w/ a data source being a (flattened) Handle<String>. |
| 49 // the functions this calls handle it gracefully. | |
| 50 if (length == 0) return 0; | |
| 51 | 59 |
| 52 if (encoding == ScriptCompiler::StreamedSource::UTF8) { | 60 class GenericStringUtf16CharacterStream : public BufferedUtf16CharacterStream { |
| 53 return CopyUtf8CharsToUtf16Chars(dest, length, src, src_pos, src_length); | 61 public: |
| 54 } | 62 GenericStringUtf16CharacterStream(Handle<String> data, size_t start_position, |
| 63 size_t end_position); | |
| 64 ~GenericStringUtf16CharacterStream() override; | |
| 55 | 65 |
| 56 size_t to_fill = length; | 66 protected: |
| 57 if (to_fill > src_length - *src_pos) to_fill = src_length - *src_pos; | 67 size_t FillBuffer(size_t position) override; |
| 58 | 68 |
| 59 if (encoding == ScriptCompiler::StreamedSource::ONE_BYTE) { | 69 Handle<String> string_; |
| 60 v8::internal::CopyChars<uint8_t, uint16_t>(dest, src + *src_pos, to_fill); | 70 size_t length_; |
| 61 } else { | 71 }; |
| 62 DCHECK(encoding == ScriptCompiler::StreamedSource::TWO_BYTE); | |
| 63 v8::internal::CopyChars<uint16_t, uint16_t>( | |
| 64 dest, reinterpret_cast<const uint16_t*>(src + *src_pos), to_fill); | |
| 65 } | |
| 66 *src_pos += to_fill; | |
| 67 return to_fill; | |
| 68 } | |
| 69 | |
| 70 } // namespace | |
| 71 | |
| 72 | |
| 73 // ---------------------------------------------------------------------------- | |
| 74 // BufferedUtf16CharacterStreams | |
| 75 | |
| 76 BufferedUtf16CharacterStream::BufferedUtf16CharacterStream() | |
| 77 : Utf16CharacterStream(), | |
| 78 pushback_limit_(NULL) { | |
| 79 // Initialize buffer as being empty. First read will fill the buffer. | |
| 80 buffer_cursor_ = buffer_; | |
| 81 buffer_end_ = buffer_; | |
| 82 } | |
| 83 | |
| 84 | |
| 85 BufferedUtf16CharacterStream::~BufferedUtf16CharacterStream() { } | |
| 86 | |
| 87 void BufferedUtf16CharacterStream::PushBack(uc32 character) { | |
| 88 if (character == kEndOfInput) { | |
| 89 pos_--; | |
| 90 return; | |
| 91 } | |
| 92 if (pushback_limit_ == NULL && buffer_cursor_ > buffer_) { | |
| 93 // buffer_ is writable, buffer_cursor_ is const pointer. | |
| 94 buffer_[--buffer_cursor_ - buffer_] = static_cast<uc16>(character); | |
| 95 pos_--; | |
| 96 return; | |
| 97 } | |
| 98 SlowPushBack(static_cast<uc16>(character)); | |
| 99 } | |
| 100 | |
| 101 | |
| 102 void BufferedUtf16CharacterStream::SlowPushBack(uc16 character) { | |
| 103 // In pushback mode, the end of the buffer contains pushback, | |
| 104 // and the start of the buffer (from buffer start to pushback_limit_) | |
| 105 // contains valid data that comes just after the pushback. | |
| 106 // We NULL the pushback_limit_ if pushing all the way back to the | |
| 107 // start of the buffer. | |
| 108 | |
| 109 if (pushback_limit_ == NULL) { | |
| 110 // Enter pushback mode. | |
| 111 pushback_limit_ = buffer_end_; | |
| 112 buffer_end_ = buffer_ + kBufferSize; | |
| 113 buffer_cursor_ = buffer_end_; | |
| 114 } | |
| 115 // Ensure that there is room for at least one pushback. | |
| 116 DCHECK(buffer_cursor_ > buffer_); | |
| 117 DCHECK(pos_ > 0); | |
| 118 buffer_[--buffer_cursor_ - buffer_] = character; | |
| 119 if (buffer_cursor_ == buffer_) { | |
| 120 pushback_limit_ = NULL; | |
| 121 } else if (buffer_cursor_ < pushback_limit_) { | |
| 122 pushback_limit_ = buffer_cursor_; | |
| 123 } | |
| 124 pos_--; | |
| 125 } | |
| 126 | |
| 127 | |
| 128 bool BufferedUtf16CharacterStream::ReadBlock() { | |
| 129 buffer_cursor_ = buffer_; | |
| 130 if (pushback_limit_ != NULL) { | |
| 131 // Leave pushback mode. | |
| 132 buffer_end_ = pushback_limit_; | |
| 133 pushback_limit_ = NULL; | |
| 134 // If there were any valid characters left at the | |
| 135 // start of the buffer, use those. | |
| 136 if (buffer_cursor_ < buffer_end_) return true; | |
| 137 // Otherwise read a new block. | |
| 138 } | |
| 139 size_t length = FillBuffer(pos_); | |
| 140 buffer_end_ = buffer_ + length; | |
| 141 return length > 0; | |
| 142 } | |
| 143 | |
| 144 | |
| 145 size_t BufferedUtf16CharacterStream::SlowSeekForward(size_t delta) { | |
| 146 // Leave pushback mode (i.e., ignore that there might be valid data | |
| 147 // in the buffer before the pushback_limit_ point). | |
| 148 pushback_limit_ = NULL; | |
| 149 return BufferSeekForward(delta); | |
| 150 } | |
| 151 | |
| 152 | |
| 153 // ---------------------------------------------------------------------------- | |
| 154 // GenericStringUtf16CharacterStream | |
| 155 | |
| 156 | 72 |
| 157 GenericStringUtf16CharacterStream::GenericStringUtf16CharacterStream( | 73 GenericStringUtf16CharacterStream::GenericStringUtf16CharacterStream( |
| 158 Handle<String> data, size_t start_position, size_t end_position) | 74 Handle<String> data, size_t start_position, size_t end_position) |
| 159 : string_(data), length_(end_position), bookmark_(kNoBookmark) { | 75 : string_(data), length_(end_position) { |
| 160 DCHECK(end_position >= start_position); | 76 DCHECK(end_position >= start_position); |
| 161 pos_ = start_position; | 77 DCHECK_GE(static_cast<size_t>(string_->length()), |
| 78 (end_position - start_position)); | |
| 79 buffer_pos_ = start_position; | |
| 162 } | 80 } |
| 163 | 81 |
| 164 | 82 GenericStringUtf16CharacterStream::~GenericStringUtf16CharacterStream() {} |
| 165 GenericStringUtf16CharacterStream::~GenericStringUtf16CharacterStream() { } | |
| 166 | |
| 167 | |
| 168 bool GenericStringUtf16CharacterStream::SetBookmark() { | |
| 169 bookmark_ = pos_; | |
| 170 return true; | |
| 171 } | |
| 172 | |
| 173 | |
| 174 void GenericStringUtf16CharacterStream::ResetToBookmark() { | |
| 175 DCHECK(bookmark_ != kNoBookmark); | |
| 176 pos_ = bookmark_; | |
| 177 buffer_cursor_ = buffer_; | |
| 178 buffer_end_ = buffer_ + FillBuffer(pos_); | |
| 179 } | |
| 180 | |
| 181 | |
| 182 size_t GenericStringUtf16CharacterStream::BufferSeekForward(size_t delta) { | |
| 183 size_t old_pos = pos_; | |
| 184 pos_ = Min(pos_ + delta, length_); | |
| 185 ReadBlock(); | |
| 186 return pos_ - old_pos; | |
| 187 } | |
| 188 | |
| 189 | 83 |
| 190 size_t GenericStringUtf16CharacterStream::FillBuffer(size_t from_pos) { | 84 size_t GenericStringUtf16CharacterStream::FillBuffer(size_t from_pos) { |
| 191 if (from_pos >= length_) return 0; | 85 if (from_pos >= length_) return 0; |
| 86 | |
| 192 size_t length = kBufferSize; | 87 size_t length = kBufferSize; |
| 193 if (from_pos + length > length_) { | 88 if (from_pos + length > length_) { |
| 194 length = length_ - from_pos; | 89 length = length_ - from_pos; |
| 195 } | 90 } |
| 196 String::WriteToFlat<uc16>(*string_, buffer_, static_cast<int>(from_pos), | 91 String::WriteToFlat<uc16>(*string_, buffer_, static_cast<int>(from_pos), |
| 197 static_cast<int>(from_pos + length)); | 92 static_cast<int>(from_pos + length)); |
| 198 return length; | 93 return length; |
| 199 } | 94 } |
| 200 | 95 |
| 201 | 96 // ---------------------------------------------------------------------------- |
| 202 // ---------------------------------------------------------------------------- | 97 // ExternalTwoByteStringUtf16CharacterStream. |
| 203 // ExternalStreamingStream | 98 // |
| 204 | 99 // A stream whose data source is a Handle<ExternalTwoByteString>. It avoids |
| 205 size_t ExternalStreamingStream::FillBuffer(size_t position) { | 100 // all data copying. |
| 206 // Ignore "position" which is the position in the decoded data. Instead, | 101 |
| 207 // ExternalStreamingStream keeps track of the position in the raw data. | 102 class ExternalTwoByteStringUtf16CharacterStream : public Utf16CharacterStream { |
| 208 size_t data_in_buffer = 0; | 103 public: |
| 209 // Note that the UTF-8 decoder might not be able to fill the buffer | 104 ExternalTwoByteStringUtf16CharacterStream(Handle<ExternalTwoByteString> data, |
| 210 // completely; it will typically leave the last character empty (see | 105 int start_position, |
| 211 // Utf8ToUtf16CharacterStream::CopyChars). | 106 int end_position); |
| 212 while (data_in_buffer < kBufferSize - 1) { | 107 ~ExternalTwoByteStringUtf16CharacterStream() override; |
| 213 if (current_data_ == NULL) { | 108 |
| 214 // GetSomeData will wait until the embedder has enough data. Here's an | 109 private: |
| 215 // interface between the API which uses size_t (which is the correct type | 110 bool ReadBlock() override; |
| 216 // here) and the internal parts which use size_t. | 111 |
| 217 current_data_length_ = source_stream_->GetMoreData(¤t_data_); | 112 const uc16* raw_data_; // Pointer to the actual array of characters. |
| 218 current_data_offset_ = 0; | 113 }; |
| 219 bool data_ends = current_data_length_ == 0; | |
| 220 bookmark_data_is_from_current_data_ = false; | |
| 221 | |
| 222 // A caveat: a data chunk might end with bytes from an incomplete UTF-8 | |
| 223 // character (the rest of the bytes will be in the next chunk). | |
| 224 if (encoding_ == ScriptCompiler::StreamedSource::UTF8) { | |
| 225 HandleUtf8SplitCharacters(&data_in_buffer); | |
| 226 if (!data_ends && current_data_offset_ == current_data_length_) { | |
| 227 // The data stream didn't end, but we used all the data in the | |
| 228 // chunk. This will only happen when the chunk was really small. We | |
| 229 // don't handle the case where a UTF-8 character is split over several | |
| 230 // chunks; in that case V8 won't crash, but it will be a parse error. | |
| 231 FlushCurrent(); | |
| 232 continue; // Request a new chunk. | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 // Did the data stream end? | |
| 237 if (data_ends) { | |
| 238 DCHECK(utf8_split_char_buffer_length_ == 0); | |
| 239 return data_in_buffer; | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 // Fill the buffer from current_data_. | |
| 244 size_t new_offset = 0; | |
| 245 size_t new_chars_in_buffer = | |
| 246 CopyCharsHelper(buffer_ + data_in_buffer, kBufferSize - data_in_buffer, | |
| 247 current_data_ + current_data_offset_, &new_offset, | |
| 248 current_data_length_ - current_data_offset_, encoding_); | |
| 249 data_in_buffer += new_chars_in_buffer; | |
| 250 current_data_offset_ += new_offset; | |
| 251 DCHECK(data_in_buffer <= kBufferSize); | |
| 252 | |
| 253 // Did we use all the data in the data chunk? | |
| 254 if (current_data_offset_ == current_data_length_) { | |
| 255 FlushCurrent(); | |
| 256 } | |
| 257 } | |
| 258 return data_in_buffer; | |
| 259 } | |
| 260 | |
| 261 | |
| 262 bool ExternalStreamingStream::SetBookmark() { | |
| 263 // Bookmarking for this stream is a bit more complex than expected, since | |
| 264 // the stream state is distributed over several places: | |
| 265 // - pos_ (inherited from Utf16CharacterStream) | |
| 266 // - buffer_cursor_ and buffer_end_ (also from Utf16CharacterStream) | |
| 267 // - buffer_ (from BufferedUtf16CharacterStream) | |
| 268 // - current_data_ (+ .._offset_ and .._length) (this class) | |
| 269 // - utf8_split_char_buffer_* (a partial utf8 symbol at the block boundary) | |
| 270 // | |
| 271 // The underlying source_stream_ instance likely could re-construct this | |
| 272 // local data for us, but with the given interfaces we have no way of | |
| 273 // accomplishing this. Thus, we'll have to save all data locally. | |
| 274 // | |
| 275 // What gets saved where: | |
| 276 // - pos_ => bookmark_ | |
| 277 // - buffer_[buffer_cursor_ .. buffer_end_] => bookmark_buffer_ | |
| 278 // - current_data_[.._offset_ .. .._length_] => bookmark_data_ | |
| 279 // - utf8_split_char_buffer_* => bookmark_utf8_split... | |
| 280 // | |
| 281 // To make sure we don't unnecessarily copy data, we also maintain | |
| 282 // whether bookmark_data_ contains a copy of the current current_data_ | |
| 283 // block. This is done with: | |
| 284 // - bookmark_data_is_from_current_data_ | |
| 285 // - bookmark_data_offset_: offset into bookmark_data_ | |
| 286 // | |
| 287 // Note that bookmark_data_is_from_current_data_ must be maintained | |
| 288 // whenever current_data_ is updated. | |
| 289 | |
| 290 bookmark_ = pos_; | |
| 291 | |
| 292 size_t buffer_length = buffer_end_ - buffer_cursor_; | |
| 293 bookmark_buffer_.Dispose(); | |
| 294 bookmark_buffer_ = Vector<uint16_t>::New(static_cast<int>(buffer_length)); | |
| 295 CopyCharsUnsigned(bookmark_buffer_.start(), buffer_cursor_, buffer_length); | |
| 296 | |
| 297 size_t data_length = current_data_length_ - current_data_offset_; | |
| 298 size_t bookmark_data_length = static_cast<size_t>(bookmark_data_.length()); | |
| 299 if (bookmark_data_is_from_current_data_ && | |
| 300 data_length < bookmark_data_length) { | |
| 301 // Fast case: bookmark_data_ was previously copied from the current | |
| 302 // data block, and we have enough data for this bookmark. | |
| 303 bookmark_data_offset_ = bookmark_data_length - data_length; | |
| 304 } else { | |
| 305 // Slow case: We need to copy current_data_. | |
| 306 bookmark_data_.Dispose(); | |
| 307 bookmark_data_ = Vector<uint8_t>::New(static_cast<int>(data_length)); | |
| 308 CopyBytes(bookmark_data_.start(), current_data_ + current_data_offset_, | |
| 309 data_length); | |
| 310 bookmark_data_is_from_current_data_ = true; | |
| 311 bookmark_data_offset_ = 0; | |
| 312 } | |
| 313 | |
| 314 bookmark_utf8_split_char_buffer_length_ = utf8_split_char_buffer_length_; | |
| 315 for (size_t i = 0; i < utf8_split_char_buffer_length_; i++) { | |
| 316 bookmark_utf8_split_char_buffer_[i] = utf8_split_char_buffer_[i]; | |
| 317 } | |
| 318 | |
| 319 return source_stream_->SetBookmark(); | |
| 320 } | |
| 321 | |
| 322 | |
| 323 void ExternalStreamingStream::ResetToBookmark() { | |
| 324 source_stream_->ResetToBookmark(); | |
| 325 FlushCurrent(); | |
| 326 | |
| 327 pos_ = bookmark_; | |
| 328 | |
| 329 // bookmark_data_* => current_data_* | |
| 330 // (current_data_ assumes ownership of its memory.) | |
| 331 current_data_offset_ = 0; | |
| 332 current_data_length_ = bookmark_data_.length() - bookmark_data_offset_; | |
| 333 uint8_t* data = new uint8_t[current_data_length_]; | |
| 334 CopyCharsUnsigned(data, bookmark_data_.begin() + bookmark_data_offset_, | |
| 335 current_data_length_); | |
| 336 delete[] current_data_; | |
| 337 current_data_ = data; | |
| 338 bookmark_data_is_from_current_data_ = true; | |
| 339 | |
| 340 // bookmark_buffer_ needs to be copied to buffer_. | |
| 341 CopyCharsUnsigned(buffer_, bookmark_buffer_.begin(), | |
| 342 bookmark_buffer_.length()); | |
| 343 buffer_cursor_ = buffer_; | |
| 344 buffer_end_ = buffer_ + bookmark_buffer_.length(); | |
| 345 | |
| 346 // utf8 split char buffer | |
| 347 utf8_split_char_buffer_length_ = bookmark_utf8_split_char_buffer_length_; | |
| 348 for (size_t i = 0; i < bookmark_utf8_split_char_buffer_length_; i++) { | |
| 349 utf8_split_char_buffer_[i] = bookmark_utf8_split_char_buffer_[i]; | |
| 350 } | |
| 351 } | |
| 352 | |
| 353 | |
| 354 void ExternalStreamingStream::FlushCurrent() { | |
| 355 delete[] current_data_; | |
| 356 current_data_ = NULL; | |
| 357 current_data_length_ = 0; | |
| 358 current_data_offset_ = 0; | |
| 359 bookmark_data_is_from_current_data_ = false; | |
| 360 } | |
| 361 | |
| 362 | |
| 363 void ExternalStreamingStream::HandleUtf8SplitCharacters( | |
| 364 size_t* data_in_buffer) { | |
| 365 // Note the following property of UTF-8 which makes this function possible: | |
| 366 // Given any byte, we can always read its local environment (in both | |
| 367 // directions) to find out the (possibly multi-byte) character it belongs | |
| 368 // to. Single byte characters are of the form 0b0XXXXXXX. The first byte of a | |
| 369 // multi-byte character is of the form 0b110XXXXX, 0b1110XXXX or | |
| 370 // 0b11110XXX. The continuation bytes are of the form 0b10XXXXXX. | |
| 371 | |
| 372 // First check if we have leftover data from the last chunk. | |
| 373 unibrow::uchar c; | |
| 374 if (utf8_split_char_buffer_length_ > 0) { | |
| 375 // Move the bytes which are part of the split character (which started in | |
| 376 // the previous chunk) into utf8_split_char_buffer_. Note that the | |
| 377 // continuation bytes are of the form 0b10XXXXXX, thus c >> 6 == 2. | |
| 378 while (current_data_offset_ < current_data_length_ && | |
| 379 utf8_split_char_buffer_length_ < 4 && | |
| 380 (c = current_data_[current_data_offset_]) >> 6 == 2) { | |
| 381 utf8_split_char_buffer_[utf8_split_char_buffer_length_] = c; | |
| 382 ++utf8_split_char_buffer_length_; | |
| 383 ++current_data_offset_; | |
| 384 } | |
| 385 | |
| 386 // Convert the data in utf8_split_char_buffer_. | |
| 387 size_t new_offset = 0; | |
| 388 size_t new_chars_in_buffer = | |
| 389 CopyCharsHelper(buffer_ + *data_in_buffer, | |
| 390 kBufferSize - *data_in_buffer, utf8_split_char_buffer_, | |
| 391 &new_offset, utf8_split_char_buffer_length_, encoding_); | |
| 392 *data_in_buffer += new_chars_in_buffer; | |
| 393 // Make sure we used all the data. | |
| 394 DCHECK(new_offset == utf8_split_char_buffer_length_); | |
| 395 DCHECK(*data_in_buffer <= kBufferSize); | |
| 396 | |
| 397 utf8_split_char_buffer_length_ = 0; | |
| 398 } | |
| 399 | |
| 400 // Move bytes which are part of an incomplete character from the end of the | |
| 401 // current chunk to utf8_split_char_buffer_. They will be converted when the | |
| 402 // next data chunk arrives. Note that all valid UTF-8 characters are at most 4 | |
| 403 // bytes long, but if the data is invalid, we can have character values bigger | |
| 404 // than unibrow::Utf8::kMaxOneByteChar for more than 4 consecutive bytes. | |
| 405 while (current_data_length_ > current_data_offset_ && | |
| 406 (c = current_data_[current_data_length_ - 1]) > | |
| 407 unibrow::Utf8::kMaxOneByteChar && | |
| 408 utf8_split_char_buffer_length_ < 4) { | |
| 409 --current_data_length_; | |
| 410 ++utf8_split_char_buffer_length_; | |
| 411 if (c >= (3 << 6)) { | |
| 412 // 3 << 6 = 0b11000000; this is the first byte of the multi-byte | |
| 413 // character. No need to copy the previous characters into the conversion | |
| 414 // buffer (even if they're multi-byte). | |
| 415 break; | |
| 416 } | |
| 417 } | |
| 418 CHECK(utf8_split_char_buffer_length_ <= 4); | |
| 419 for (size_t i = 0; i < utf8_split_char_buffer_length_; ++i) { | |
| 420 utf8_split_char_buffer_[i] = current_data_[current_data_length_ + i]; | |
| 421 } | |
| 422 } | |
| 423 | |
| 424 | |
| 425 // ---------------------------------------------------------------------------- | |
| 426 // ExternalTwoByteStringUtf16CharacterStream | |
| 427 | 114 |
| 428 ExternalTwoByteStringUtf16CharacterStream:: | 115 ExternalTwoByteStringUtf16CharacterStream:: |
| 429 ~ExternalTwoByteStringUtf16CharacterStream() { } | 116 ~ExternalTwoByteStringUtf16CharacterStream() {} |
| 430 | 117 |
| 431 ExternalTwoByteStringUtf16CharacterStream:: | 118 ExternalTwoByteStringUtf16CharacterStream:: |
| 432 ExternalTwoByteStringUtf16CharacterStream( | 119 ExternalTwoByteStringUtf16CharacterStream( |
| 433 Handle<ExternalTwoByteString> data, int start_position, | 120 Handle<ExternalTwoByteString> data, int start_position, |
| 434 int end_position) | 121 int end_position) |
| 435 : raw_data_(data->GetTwoByteData(start_position)), bookmark_(kNoBookmark) { | 122 : raw_data_(data->GetTwoByteData(start_position)) { |
| 123 buffer_start_ = raw_data_; | |
| 436 buffer_cursor_ = raw_data_, | 124 buffer_cursor_ = raw_data_, |
| 437 buffer_end_ = raw_data_ + (end_position - start_position); | 125 buffer_end_ = raw_data_ + (end_position - start_position); |
| 438 pos_ = start_position; | 126 buffer_pos_ = start_position; |
| 439 } | 127 } |
| 440 | 128 |
| 441 | 129 bool ExternalTwoByteStringUtf16CharacterStream::ReadBlock() { |
| 442 bool ExternalTwoByteStringUtf16CharacterStream::SetBookmark() { | 130 // Entire string is read at start. |
| 443 bookmark_ = pos_; | 131 return false; |
| 444 return true; | |
| 445 } | |
| 446 | |
| 447 | |
| 448 void ExternalTwoByteStringUtf16CharacterStream::ResetToBookmark() { | |
| 449 DCHECK(bookmark_ != kNoBookmark); | |
| 450 pos_ = bookmark_; | |
| 451 buffer_cursor_ = raw_data_ + bookmark_; | |
| 452 } | 132 } |
| 453 | 133 |
| 454 // ---------------------------------------------------------------------------- | 134 // ---------------------------------------------------------------------------- |
| 455 // ExternalOneByteStringUtf16CharacterStream | 135 // ExternalOneByteStringUtf16CharacterStream |
| 136 // | |
| 137 // A stream whose data source is a Handle<ExternalOneByteString>. | |
| 138 | |
| 139 class ExternalOneByteStringUtf16CharacterStream | |
| 140 : public BufferedUtf16CharacterStream { | |
| 141 public: | |
| 142 ExternalOneByteStringUtf16CharacterStream(Handle<ExternalOneByteString> data, | |
| 143 int start_position, | |
| 144 int end_position); | |
| 145 ~ExternalOneByteStringUtf16CharacterStream() override; | |
| 146 | |
| 147 // For testing: | |
| 148 ExternalOneByteStringUtf16CharacterStream(const char* data, size_t length); | |
| 149 | |
| 150 protected: | |
| 151 size_t FillBuffer(size_t position) override; | |
| 152 | |
| 153 const uint8_t* raw_data_; // Pointer to the actual array of characters. | |
| 154 size_t length_; | |
| 155 }; | |
| 456 | 156 |
| 457 ExternalOneByteStringUtf16CharacterStream:: | 157 ExternalOneByteStringUtf16CharacterStream:: |
| 458 ~ExternalOneByteStringUtf16CharacterStream() {} | 158 ~ExternalOneByteStringUtf16CharacterStream() {} |
| 459 | 159 |
| 460 ExternalOneByteStringUtf16CharacterStream:: | 160 ExternalOneByteStringUtf16CharacterStream:: |
| 461 ExternalOneByteStringUtf16CharacterStream( | 161 ExternalOneByteStringUtf16CharacterStream( |
| 462 Handle<ExternalOneByteString> data, int start_position, | 162 Handle<ExternalOneByteString> data, int start_position, |
| 463 int end_position) | 163 int end_position) |
| 464 : raw_data_(data->GetChars()), | 164 : raw_data_(data->GetChars()), length_(end_position) { |
| 465 length_(end_position), | |
| 466 bookmark_(kNoBookmark) { | |
| 467 DCHECK(end_position >= start_position); | 165 DCHECK(end_position >= start_position); |
| 468 pos_ = start_position; | 166 buffer_pos_ = start_position; |
| 469 } | 167 } |
| 470 | 168 |
| 471 ExternalOneByteStringUtf16CharacterStream:: | 169 ExternalOneByteStringUtf16CharacterStream:: |
| 472 ExternalOneByteStringUtf16CharacterStream(const char* data, size_t length) | 170 ExternalOneByteStringUtf16CharacterStream(const char* data, size_t length) |
| 473 : raw_data_(reinterpret_cast<const uint8_t*>(data)), | 171 : raw_data_(reinterpret_cast<const uint8_t*>(data)), length_(length) {} |
| 474 length_(length), | |
| 475 bookmark_(kNoBookmark) {} | |
| 476 | |
| 477 ExternalOneByteStringUtf16CharacterStream:: | |
| 478 ExternalOneByteStringUtf16CharacterStream(const char* data) | |
| 479 : ExternalOneByteStringUtf16CharacterStream(data, strlen(data)) {} | |
| 480 | |
| 481 bool ExternalOneByteStringUtf16CharacterStream::SetBookmark() { | |
| 482 bookmark_ = pos_; | |
| 483 return true; | |
| 484 } | |
| 485 | |
| 486 void ExternalOneByteStringUtf16CharacterStream::ResetToBookmark() { | |
| 487 DCHECK(bookmark_ != kNoBookmark); | |
| 488 pos_ = bookmark_; | |
| 489 buffer_cursor_ = buffer_; | |
| 490 buffer_end_ = buffer_ + FillBuffer(pos_); | |
| 491 } | |
| 492 | |
| 493 size_t ExternalOneByteStringUtf16CharacterStream::BufferSeekForward( | |
| 494 size_t delta) { | |
| 495 size_t old_pos = pos_; | |
| 496 pos_ = Min(pos_ + delta, length_); | |
| 497 ReadBlock(); | |
| 498 return pos_ - old_pos; | |
| 499 } | |
| 500 | 172 |
| 501 size_t ExternalOneByteStringUtf16CharacterStream::FillBuffer(size_t from_pos) { | 173 size_t ExternalOneByteStringUtf16CharacterStream::FillBuffer(size_t from_pos) { |
| 502 if (from_pos >= length_) return 0; | 174 if (from_pos >= length_) return 0; |
| 175 | |
| 503 size_t length = Min(kBufferSize, length_ - from_pos); | 176 size_t length = Min(kBufferSize, length_ - from_pos); |
| 504 for (size_t i = 0; i < length; ++i) { | 177 for (size_t i = 0; i < length; ++i) { |
| 505 buffer_[i] = static_cast<uc16>(raw_data_[from_pos + i]); | 178 buffer_[i] = static_cast<uc16>(raw_data_[from_pos + i]); |
| 506 } | 179 } |
| 507 return length; | 180 return length; |
|
nickie
2016/09/07 13:48:44
This for loop should be equivalent to an instance
vogelheim
2016/09/08 15:02:18
Yes, nice catch. Done.
| |
| 508 } | 181 } |
| 509 | 182 |
| 183 // ---------------------------------------------------------------------------- | |
| 184 // Utf8ExternalStreamingStream - chunked streaming of Utf-8 data. | |
| 185 // | |
| 186 // This implementation is fairly complex, since data arrives in chunks which | |
| 187 // may 'cut' arbitrarily into utf-8 characters. Also, seeking to a given | |
| 188 // character position is tricky because the byte position cannot be dericed | |
| 189 // from the character position. | |
| 190 | |
| 191 class Utf8ExternalStreamingStream : public BufferedUtf16CharacterStream { | |
| 192 public: | |
| 193 Utf8ExternalStreamingStream( | |
| 194 ScriptCompiler::ExternalSourceStream* source_stream) | |
| 195 : current_({0, 0, 0, unibrow::Utf8::Utf8IncrementalBuffer(0)}), | |
| 196 source_stream_(source_stream) {} | |
| 197 ~Utf8ExternalStreamingStream() override { DeleteChunks(); } | |
| 198 | |
| 199 protected: | |
| 200 size_t FillBuffer(size_t position) override; | |
| 201 | |
| 202 private: | |
| 203 void DeleteChunks() { | |
| 204 for (size_t i = 0; i < chunks_.size(); i++) delete[] chunks_[i].chunk; | |
| 205 chunks_.clear(); | |
| 206 } | |
| 207 | |
| 208 // ChunkPos points to a position into the chunked stream, containing: | |
| 209 // - # of the chunk in the chunks_ vector, | |
| 210 // - the 'physical' byte_pos, i.e., the number of bytes from the stream's | |
| 211 // beginning, | |
| 212 // - the 'logical' char_pos, i.e. the number of Unicode characters counting | |
| 213 // from the beginning of the stream, | |
| 214 // - the incomplete_char, which stores a possibly incomplete utf-8 decoder | |
| 215 // state, | |
| 216 // iff the 'physical' byte_pos points into the middle of a utf-8 character. | |
| 217 struct ChunkPos { | |
| 218 size_t chunk_no; | |
| 219 size_t byte_pos; | |
| 220 size_t char_pos; | |
| 221 unibrow::Utf8::Utf8IncrementalBuffer incomplete_char; | |
| 222 }; | |
| 223 | |
| 224 // A chunk in the list of chunks, containing the data for the chunk (chunk + | |
| 225 // byte_length), and the position of the chunk within the stream (that is, | |
| 226 // the chunk contains the data starting at | |
| 227 // byte_pos/char_pos/incomplete_first_char). | |
| 228 // | |
| 229 // It contains: | |
| 230 // - a data pointer + length, chunk (owned by the stream) and byte_length, | |
| 231 // - the 'physical' byte_pos of the chunk (i.e. the number of bytes, counted | |
| 232 // from the beginning of the stream, at which this chunk begins), | |
| 233 // - the 'logical' char_pos of the chunk (i.e. the number of Unicode | |
| 234 // characters, counted from the beginning of the stream), | |
| 235 // - the incomplete_first_char, which stores a possibly incomplete utf-8 | |
| 236 // decoder state, iff the 'physical' byte_pos points into the middle of a | |
| 237 // utf-8 character. | |
| 238 // | |
| 239 // So each chunk contains the bytes [byte_pos .. byte_pos + byte_length - 1] | |
| 240 // from the entire stream, and decoding from the beginning of the chunk | |
| 241 // (using incomplete_first_char) will yield the stream's char_pos-th unicode | |
| 242 // character. | |
| 243 struct Chunk { | |
| 244 const uint8_t* chunk; | |
| 245 size_t byte_length; | |
| 246 size_t byte_pos; | |
|
marja
2016/09/08 09:46:23
It's confusing that you above use byte_pos and cha
vogelheim
2016/09/08 15:02:17
I don't get the confusing part.
Essentially, a po
| |
| 247 size_t char_pos; | |
| 248 unibrow::Utf8::Utf8IncrementalBuffer incomplete_first_char; | |
| 249 }; | |
| 250 std::vector<Chunk> chunks_; | |
| 251 ChunkPos current_; | |
| 252 ScriptCompiler::ExternalSourceStream* source_stream_; | |
| 253 }; | |
| 254 | |
| 255 size_t Utf8ExternalStreamingStream::FillBuffer(size_t position) { | |
| 256 // If the desired position is *not* at the current_ position, then we need | |
|
marja
2016/09/08 09:46:23
Could this function be structured differently so t
vogelheim
2016/09/08 15:02:18
I'm skeptical.
The fast path is what happens afte
| |
| 257 // to search through the chunks_ vector. | |
| 258 if (position != current_.char_pos && !chunks_.empty()) { | |
| 259 // Find the appropriate chunk. | |
| 260 size_t chunk = chunks_.size() - 1; | |
| 261 while (position < chunks_[chunk].char_pos) chunk--; | |
| 262 | |
| 263 Chunk& current = chunks_[chunk]; | |
|
marja
2016/09/08 09:46:23
Could you rename current to current_chunk, just to
vogelheim
2016/09/08 15:02:18
Done.
| |
| 264 unibrow::Utf8::Utf8IncrementalBuffer b = current.incomplete_first_char; | |
|
marja
2016/09/08 09:46:23
I had to think for a while why we need to do this
vogelheim
2016/09/08 15:02:17
Done.
| |
| 265 size_t it = 0; | |
| 266 size_t cpos = current.char_pos; | |
| 267 while (cpos < position && it < current.byte_length) { | |
| 268 unibrow::uchar t = | |
| 269 unibrow::Utf8::ValueOfIncremental(current.chunk[it], b); | |
| 270 if (t != unibrow::Utf8::kIncomplete) { | |
| 271 cpos++; | |
| 272 } | |
| 273 it++; | |
| 274 } | |
| 275 // At this point, we have either found our position; or we're at the end | |
| 276 // of the last block. That should only happen if we seek forward (which | |
|
marja
2016/09/08 09:46:23
... of the last chunk?
vogelheim
2016/09/08 15:02:18
Done.
| |
| 277 // the Scanner doesn't do); but if it does we can fix it by recursing. | |
| 278 if (cpos == position) { | |
| 279 current_ = {chunk, current.byte_pos + it, cpos, b}; | |
| 280 } else { | |
| 281 DCHECK_EQ(chunk + 1, chunks_.size()); | |
| 282 current_ = {chunk + 1, current.byte_pos + it, cpos, b}; | |
| 283 return FillBuffer(position); | |
| 284 } | |
| 285 } | |
|
marja
2016/09/08 09:46:23
Can finding the position be moved to a helper func
vogelheim
2016/09/08 15:02:17
See above.
| |
| 286 | |
| 287 // Fetch more data if we've exhausted the last chunk. | |
| 288 if (current_.chunk_no == chunks_.size()) { | |
| 289 const uint8_t* chunk = nullptr; | |
| 290 size_t length = source_stream_->GetMoreData(&chunk); | |
| 291 chunks_.push_back({chunk, length, current_.byte_pos, current_.char_pos, | |
| 292 current_.incomplete_char}); | |
| 293 } | |
| 294 | |
| 295 // Return 0 if we're out of data. That is: If we have a zero-length chunk. | |
| 296 Chunk& current_chunk = chunks_[current_.chunk_no]; | |
|
marja
2016/09/08 09:46:23
Oops, now we have current_chunk..
It's confusing
vogelheim
2016/09/08 15:02:17
Done... I guess? It's now current_chunk and curren
| |
| 297 if (current_chunk.byte_length == 0) return 0; | |
| 298 | |
| 299 // At this point, we know we have data to return in current_chunk. | |
| 300 uc16* buffer_cursor = buffer_; | |
| 301 uc16* buffer_end = buffer_ + kBufferSize; | |
| 302 unibrow::Utf8::Utf8IncrementalBuffer incomplete_char = | |
| 303 (current_.byte_pos == current_chunk.byte_pos) | |
| 304 ? current_chunk.incomplete_first_char | |
| 305 : unibrow::Utf8::Utf8IncrementalBuffer(0); | |
|
marja
2016/09/08 09:46:23
Umm, what? Why?
vogelheim
2016/09/08 15:02:17
As the method comment says: "and decoding from the
| |
| 306 | |
| 307 size_t it; | |
| 308 for (it = current_.byte_pos - current_chunk.byte_pos; | |
| 309 it < current_chunk.byte_length && buffer_cursor < buffer_end - 1; it++) { | |
| 310 unibrow::uchar t = unibrow::Utf8::ValueOfIncremental( | |
| 311 current_chunk.chunk[it], incomplete_char); | |
| 312 if (t == unibrow::Utf8::kIncomplete) continue; | |
| 313 if (t <= unibrow::Utf16::kMaxNonSurrogateCharCode) { | |
| 314 *(buffer_cursor++) = static_cast<uc16>(t); | |
| 315 } else { | |
| 316 *(buffer_cursor++) = unibrow::Utf16::LeadSurrogate(t); | |
| 317 *(buffer_cursor++) = unibrow::Utf16::TrailSurrogate(t); | |
| 318 } | |
| 319 } | |
| 320 buffer_cursor_ = buffer_; | |
| 321 buffer_end_ = buffer_cursor; | |
| 322 | |
| 323 current_.byte_pos = current_chunk.byte_pos + it; | |
| 324 current_.char_pos += (buffer_end_ - buffer_cursor_); | |
| 325 if (it == current_chunk.byte_length) { | |
| 326 current_.chunk_no++; | |
| 327 current_.incomplete_char = incomplete_char; | |
| 328 | |
| 329 if (current_.char_pos == position) { | |
| 330 // This tests for the pathological condition that we haven't produced | |
| 331 // a single character, but we're not out of data. (E.g. when the | |
| 332 // embedder gives us a single-byte chunk, and that one byte is at start | |
| 333 // or middle of a multi-byte utf-8 sequence.) Since we can't return 0 - | |
| 334 // this would indicate the end of data - we will simply recurse. | |
| 335 return FillBuffer(position); | |
| 336 } | |
| 337 } | |
| 338 | |
| 339 return current_.char_pos - position; | |
| 340 } | |
| 341 | |
| 342 // ---------------------------------------------------------------------------- | |
| 343 // Chunks - helper for One- + TwoByteExternalStreamingStream | |
| 344 namespace { | |
| 345 | |
| 346 struct Chunk { | |
| 347 const uint8_t* chunk; | |
| 348 size_t byte_length; | |
| 349 size_t byte_pos; | |
| 350 }; | |
| 351 | |
| 352 typedef std::vector<struct Chunk> Chunks; | |
| 353 | |
| 354 void DeleteChunks(Chunks& chunks) { | |
| 355 for (size_t i = 0; i < chunks.size(); i++) delete[] chunks[i].chunk; | |
| 356 } | |
| 357 | |
| 358 // Return the chunk index for the chunk containing position. | |
| 359 // If position is behind the end of the stream, the index of the last, | |
| 360 // zero-length chunk is returned. | |
| 361 size_t FindChunk(Chunks& chunks, ScriptCompiler::ExternalSourceStream* source_, | |
| 362 size_t position) { | |
| 363 size_t end_pos = | |
| 364 chunks.empty() ? 0 : (chunks.back().byte_pos + chunks.back().byte_length); | |
| 365 | |
| 366 // Get more data if needed. We usually won't enter the loop body. | |
| 367 bool out_of_data = !chunks.empty() && chunks.back().byte_length == 0; | |
| 368 while (!out_of_data && end_pos <= position + 1) { | |
| 369 const uint8_t* chunk = nullptr; | |
| 370 size_t len = source_->GetMoreData(&chunk); | |
| 371 | |
| 372 chunks.push_back({chunk, len, end_pos}); | |
| 373 end_pos += len; | |
| 374 out_of_data = (len == 0); | |
| 375 } | |
| 376 | |
| 377 // Here, we should always have at least one chunk, and we either have the | |
| 378 // chunk we were looking for, or we're out of data. Also, out_of_data and | |
| 379 // end_pos are current (and designate whether we have exhausted the stream, | |
| 380 // and the length of data received so far, respectively). | |
| 381 DCHECK(!chunks.empty()); | |
| 382 DCHECK_EQ(end_pos, chunks.back().byte_pos + chunks.back().byte_length); | |
| 383 DCHECK_EQ(out_of_data, chunks.back().byte_length == 0); | |
| 384 DCHECK(position < end_pos || out_of_data); | |
| 385 | |
| 386 // Edge case: position is behind the end of stream: Return the last (length 0) | |
| 387 // chunk to indicate the end of the stream. | |
| 388 if (position >= end_pos) { | |
| 389 DCHECK(out_of_data); | |
| 390 return chunks.size() - 1; | |
| 391 } | |
| 392 | |
| 393 // We almost always 'stream', meaning we want data from the last chunk, so | |
| 394 // let's look at chunks back-to-front. | |
| 395 size_t chunk_no = chunks.size() - 1; | |
| 396 while (chunks[chunk_no].byte_pos > position) { | |
| 397 DCHECK_NE(chunk_no, 0); | |
| 398 chunk_no--; | |
| 399 } | |
| 400 DCHECK_LE(chunks[chunk_no].byte_pos, position); | |
| 401 DCHECK_LT(position, chunks[chunk_no].byte_pos + chunks[chunk_no].byte_length); | |
| 402 return chunk_no; | |
| 403 } | |
| 404 | |
| 405 } // anonymous namespace | |
| 406 | |
| 407 // ---------------------------------------------------------------------------- | |
| 408 // OneByteExternalStreamingStream | |
| 409 // | |
| 410 // A stream of latin-1 encoded, chunked data. | |
| 411 | |
| 412 class OneByteExternalStreamingStream : public BufferedUtf16CharacterStream { | |
| 413 public: | |
| 414 explicit OneByteExternalStreamingStream( | |
| 415 ScriptCompiler::ExternalSourceStream* source) | |
| 416 : source_(source) {} | |
| 417 ~OneByteExternalStreamingStream() override { DeleteChunks(chunks_); } | |
| 418 | |
| 419 protected: | |
| 420 size_t FillBuffer(size_t position) override; | |
| 421 | |
| 422 private: | |
| 423 Chunks chunks_; | |
| 424 ScriptCompiler::ExternalSourceStream* source_; | |
| 425 }; | |
| 426 | |
| 427 size_t OneByteExternalStreamingStream::FillBuffer(size_t position) { | |
| 428 Chunk& chunk = chunks_[FindChunk(chunks_, source_, position)]; | |
|
marja
2016/09/08 09:46:23
Here you already have a FindChunk helper, so I thi
vogelheim
2016/09/08 15:02:18
Nack. Here, I had two identical uses of it, so thi
| |
| 429 if (chunk.byte_length == 0) return 0; | |
| 430 | |
| 431 size_t start_pos = position - chunk.byte_pos; | |
| 432 size_t len = i::Min(kBufferSize, chunk.byte_length - start_pos); | |
| 433 i::CopyCharsUnsigned(buffer_, chunk.chunk + start_pos, len); | |
| 434 return len; | |
| 435 } | |
| 436 | |
| 437 // ---------------------------------------------------------------------------- | |
| 438 // TwoByteExternalStreamingStream | |
| 439 // | |
| 440 // A stream of ucs-2 data, delivered in chunks. Chunks may be 'cut' into the | |
| 441 // middle of characters (or even contain only one byte), which adds a bit | |
| 442 // of complexity. This stream avoid all data copying, except for characters | |
| 443 // that cross chunk boundaries. | |
| 444 | |
| 445 class TwoByteExternalStreamingStream : public Utf16CharacterStream { | |
| 446 public: | |
| 447 explicit TwoByteExternalStreamingStream( | |
| 448 ScriptCompiler::ExternalSourceStream* source); | |
| 449 ~TwoByteExternalStreamingStream() override; | |
| 450 | |
| 451 protected: | |
| 452 bool ReadBlock() override; | |
| 453 | |
| 454 Chunks chunks_; | |
| 455 ScriptCompiler::ExternalSourceStream* source_; | |
| 456 uc16 one_char_buffer_; | |
| 457 }; | |
| 458 | |
| 459 TwoByteExternalStreamingStream::TwoByteExternalStreamingStream( | |
| 460 ScriptCompiler::ExternalSourceStream* source) | |
| 461 : Utf16CharacterStream(&one_char_buffer_, &one_char_buffer_, | |
| 462 &one_char_buffer_, 0), | |
| 463 source_(source), | |
| 464 one_char_buffer_(0) {} | |
| 465 | |
| 466 TwoByteExternalStreamingStream::~TwoByteExternalStreamingStream() { | |
| 467 DeleteChunks(chunks_); | |
| 468 } | |
| 469 | |
| 470 bool TwoByteExternalStreamingStream::ReadBlock() { | |
| 471 size_t position = pos(); | |
| 472 | |
| 473 // We'll search for the 2nd byte of our character, to make sure we | |
| 474 // have enough data for at least one character. | |
| 475 size_t chunk_no = FindChunk(chunks_, source_, 2 * position + 1); | |
| 476 | |
|
marja
2016/09/08 09:46:23
What happens if we have data where the last charac
vogelheim
2016/09/08 15:02:18
For the last chunk, the rounding (below: number_of
| |
| 477 // Out of data? Return 0. | |
| 478 if (chunks_[chunk_no].byte_length == 0) return false; | |
| 479 | |
| 480 Chunk& current = chunks_[chunk_no]; | |
| 481 | |
| 482 // Annoying edge case: Chunks may not be 2-byte aligned, meaning that a | |
| 483 // character may be split between the previous and the current chunk. | |
| 484 // If we find such a lonely byte at the beginning of the chunk, we'll use | |
| 485 // one_char_buffer_ to hold the full character. | |
| 486 bool lonley_byte = (chunks_[chunk_no].byte_pos == (2 * position + 1)); | |
|
marja
2016/09/08 09:46:23
typo: lonley
vogelheim
2016/09/08 15:02:17
Done.
| |
| 487 if (lonley_byte) { | |
| 488 DCHECK_NE(chunk_no, 0); | |
| 489 Chunk& previous_chunk = chunks_[chunk_no - 1]; | |
| 490 uc16 character = previous_chunk.chunk[previous_chunk.byte_length - 1] | | |
| 491 current.chunk[0] << 8; | |
| 492 | |
| 493 one_char_buffer_ = character; | |
| 494 buffer_pos_ = position; | |
| 495 buffer_start_ = &one_char_buffer_; | |
| 496 buffer_cursor_ = &one_char_buffer_; | |
| 497 buffer_end_ = &one_char_buffer_ + 1; | |
| 498 return true; | |
| 499 } | |
| 500 | |
| 501 // Common case: character is in current chunk. | |
| 502 DCHECK_LE(current.byte_pos, 2 * position); | |
| 503 DCHECK_LT(2 * position + 1, current.byte_pos + current.byte_length); | |
| 504 | |
| 505 // Determine # of full ucs-2 chars in stream, and whether we started on an odd | |
| 506 // byte boundary. | |
| 507 bool odd_start = (current.byte_pos % 2) == 1; | |
| 508 size_t number_chars = (current.byte_length - odd_start) / 2; | |
| 509 | |
| 510 // Point the buffer_*_ members into the current chunk and set buffer_cursor_ | |
| 511 // to point to position. Be careful when converting the byte positions (in | |
| 512 // Chunk) to the ucs-2 character positions (in buffer_*_ members). | |
| 513 buffer_start_ = reinterpret_cast<const uint16_t*>(current.chunk + odd_start); | |
| 514 buffer_end_ = buffer_start_ + number_chars; | |
| 515 buffer_pos_ = (current.byte_pos + odd_start) / 2; | |
| 516 buffer_cursor_ = buffer_start_ + (position - buffer_pos_); | |
| 517 DCHECK_EQ(position, pos()); | |
| 518 return true; | |
| 519 } | |
| 520 | |
| 521 // ---------------------------------------------------------------------------- | |
| 522 // ScannerStream: Create stream instances. | |
| 523 | |
| 524 Utf16CharacterStream* ScannerStream::For(Handle<String> data) { | |
| 525 return ScannerStream::For(data, 0, data->length()); | |
| 526 } | |
| 527 | |
| 528 Utf16CharacterStream* ScannerStream::For(Handle<String> data, int start_pos, | |
| 529 int end_pos) { | |
| 530 DCHECK(start_pos >= 0); | |
| 531 DCHECK(end_pos <= data->length()); | |
| 532 if (data->IsExternalOneByteString()) { | |
| 533 return new ExternalOneByteStringUtf16CharacterStream( | |
| 534 Handle<ExternalOneByteString>::cast(data), start_pos, end_pos); | |
| 535 } else if (data->IsExternalTwoByteString()) { | |
| 536 return new ExternalTwoByteStringUtf16CharacterStream( | |
| 537 Handle<ExternalTwoByteString>::cast(data), start_pos, end_pos); | |
| 538 } else { | |
| 539 // TODO(vogelheim): Maybe call data.Flatten() first? | |
| 540 return new GenericStringUtf16CharacterStream(data, start_pos, end_pos); | |
| 541 } | |
| 542 } | |
| 543 | |
| 544 std::unique_ptr<Utf16CharacterStream> ScannerStream::ForTesting( | |
| 545 const char* data) { | |
| 546 return ScannerStream::ForTesting(data, strlen(data)); | |
| 547 } | |
| 548 | |
| 549 std::unique_ptr<Utf16CharacterStream> ScannerStream::ForTesting( | |
| 550 const char* data, size_t length) { | |
| 551 return std::unique_ptr<Utf16CharacterStream>( | |
| 552 new ExternalOneByteStringUtf16CharacterStream(data, length)); | |
| 553 } | |
| 554 | |
| 555 Utf16CharacterStream* ScannerStream::For( | |
| 556 ScriptCompiler::ExternalSourceStream* source_stream, | |
| 557 v8::ScriptCompiler::StreamedSource::Encoding encoding) { | |
| 558 switch (encoding) { | |
| 559 case v8::ScriptCompiler::StreamedSource::TWO_BYTE: | |
| 560 return new TwoByteExternalStreamingStream(source_stream); | |
| 561 case v8::ScriptCompiler::StreamedSource::ONE_BYTE: | |
| 562 return new OneByteExternalStreamingStream(source_stream); | |
| 563 case v8::ScriptCompiler::StreamedSource::UTF8: | |
| 564 return new Utf8ExternalStreamingStream(source_stream); | |
| 565 } | |
| 566 UNREACHABLE(); | |
| 567 return nullptr; | |
| 568 } | |
| 569 | |
| 510 } // namespace internal | 570 } // namespace internal |
| 511 } // namespace v8 | 571 } // namespace v8 |
| OLD | NEW |