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].* | |
|
nickie
2016/09/09 11:21:48
I can't see any Mem(Copy|Move) in the code that ch
vogelheim
2016/09/14 11:28:18
Done.
| |
| 12 | 14 |
| 13 namespace v8 { | 15 namespace v8 { |
| 14 namespace internal { | 16 namespace internal { |
| 15 | 17 |
| 16 namespace { | |
| 17 | |
| 18 size_t CopyUtf8CharsToUtf16Chars(uint16_t* dest, size_t length, const byte* src, | |
| 19 size_t* src_pos, size_t src_length) { | |
| 20 static const unibrow::uchar kMaxUtf16Character = | |
| 21 unibrow::Utf16::kMaxNonSurrogateCharCode; | |
| 22 size_t i = 0; | |
| 23 // Because of the UTF-16 lead and trail surrogates, we stop filling the buffer | |
| 24 // one character early (in the normal case), because we need to have at least | |
| 25 // two free spaces in the buffer to be sure that the next character will fit. | |
| 26 while (i < length - 1) { | |
| 27 if (*src_pos == src_length) break; | |
| 28 unibrow::uchar c = src[*src_pos]; | |
| 29 if (c <= unibrow::Utf8::kMaxOneByteChar) { | |
| 30 *src_pos = *src_pos + 1; | |
| 31 } else { | |
| 32 c = unibrow::Utf8::CalculateValue(src + *src_pos, src_length - *src_pos, | |
| 33 src_pos); | |
| 34 } | |
| 35 if (c > kMaxUtf16Character) { | |
| 36 dest[i++] = unibrow::Utf16::LeadSurrogate(c); | |
| 37 dest[i++] = unibrow::Utf16::TrailSurrogate(c); | |
| 38 } else { | |
| 39 dest[i++] = static_cast<uc16>(c); | |
| 40 } | |
| 41 } | |
| 42 return i; | |
| 43 } | |
| 44 | |
| 45 size_t CopyCharsHelper(uint16_t* dest, size_t length, const uint8_t* src, | |
| 46 size_t* src_pos, size_t src_length, | |
| 47 ScriptCompiler::StreamedSource::Encoding encoding) { | |
| 48 // It's possible that this will be called with length 0, but don't assume that | |
| 49 // the functions this calls handle it gracefully. | |
| 50 if (length == 0) return 0; | |
| 51 | |
| 52 if (encoding == ScriptCompiler::StreamedSource::UTF8) { | |
| 53 return CopyUtf8CharsToUtf16Chars(dest, length, src, src_pos, src_length); | |
| 54 } | |
| 55 | |
| 56 size_t to_fill = length; | |
| 57 if (to_fill > src_length - *src_pos) to_fill = src_length - *src_pos; | |
| 58 | |
| 59 if (encoding == ScriptCompiler::StreamedSource::ONE_BYTE) { | |
| 60 v8::internal::CopyChars<uint8_t, uint16_t>(dest, src + *src_pos, to_fill); | |
| 61 } else { | |
| 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 // ---------------------------------------------------------------------------- | 18 // ---------------------------------------------------------------------------- |
| 74 // BufferedUtf16CharacterStreams | 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; | |
|
nickie
2016/09/09 11:21:48
Is the destructor necessary in this class? The de
vogelheim
2016/09/14 11:28:19
I think so. This thing will be deleted via its sup
nickie
2016/09/14 11:48:17
There has to be a virtual destructor but it does n
| |
| 28 | |
| 29 protected: | |
| 30 static const size_t kBufferSize = 512; | |
| 31 | |
| 32 bool ReadBlock() override; | |
| 33 | |
| 34 // FillBuffer should read up to kBufferSize characters at position and store | |
| 35 // them into buffer_[0..]. It returns the number of characters stored. | |
| 36 virtual size_t FillBuffer(size_t position) = 0; | |
| 37 | |
| 38 // Fixed sized buffer that this class reads from. | |
| 39 // buffer_start_ should always point to buffer_ | |
|
nickie
2016/09/09 11:21:48
Maybe introduce "The base class's " in the beginni
vogelheim
2016/09/14 11:28:19
Done.
| |
| 40 uc16 buffer_[kBufferSize]; | |
| 41 }; | |
| 75 | 42 |
| 76 BufferedUtf16CharacterStream::BufferedUtf16CharacterStream() | 43 BufferedUtf16CharacterStream::BufferedUtf16CharacterStream() |
| 77 : Utf16CharacterStream(), | 44 : Utf16CharacterStream(buffer_, buffer_, buffer_, 0) {} |
| 78 pushback_limit_(NULL) { | 45 |
| 79 // Initialize buffer as being empty. First read will fill the buffer. | 46 BufferedUtf16CharacterStream::~BufferedUtf16CharacterStream() {} |
| 47 | |
| 48 bool BufferedUtf16CharacterStream::ReadBlock() { | |
| 49 DCHECK_EQ(buffer_start_, buffer_); | |
| 50 | |
| 51 size_t position = pos(); | |
| 52 buffer_pos_ = position; | |
| 80 buffer_cursor_ = buffer_; | 53 buffer_cursor_ = buffer_; |
| 81 buffer_end_ = buffer_; | 54 buffer_end_ = buffer_ + FillBuffer(position); |
| 82 } | 55 DCHECK_EQ(pos(), position); |
| 83 | 56 DCHECK_LE(buffer_end_, buffer_start_ + kBufferSize); |
| 84 | 57 return buffer_cursor_ < buffer_end_; |
| 85 BufferedUtf16CharacterStream::~BufferedUtf16CharacterStream() { } | 58 } |
| 86 | 59 |
| 87 void BufferedUtf16CharacterStream::PushBack(uc32 character) { | 60 // ---------------------------------------------------------------------------- |
| 88 if (character == kEndOfInput) { | 61 // GenericStringUtf16CharacterStream. |
| 89 pos_--; | 62 // |
| 90 return; | 63 // A stream w/ a data source being a (flattened) Handle<String>. |
| 91 } | 64 |
| 92 if (pushback_limit_ == NULL && buffer_cursor_ > buffer_) { | 65 class GenericStringUtf16CharacterStream : public BufferedUtf16CharacterStream { |
| 93 // buffer_ is writable, buffer_cursor_ is const pointer. | 66 public: |
| 94 buffer_[--buffer_cursor_ - buffer_] = static_cast<uc16>(character); | 67 GenericStringUtf16CharacterStream(Handle<String> data, size_t start_position, |
| 95 pos_--; | 68 size_t end_position); |
| 96 return; | 69 ~GenericStringUtf16CharacterStream() override; |
|
nickie
2016/09/09 11:21:48
Same. Is the empty destructor necessary?
| |
| 97 } | 70 |
| 98 SlowPushBack(static_cast<uc16>(character)); | 71 protected: |
| 99 } | 72 size_t FillBuffer(size_t position) override; |
| 100 | 73 |
| 101 | 74 Handle<String> string_; |
| 102 void BufferedUtf16CharacterStream::SlowPushBack(uc16 character) { | 75 size_t length_; |
| 103 // In pushback mode, the end of the buffer contains pushback, | 76 }; |
| 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 | 77 |
| 157 GenericStringUtf16CharacterStream::GenericStringUtf16CharacterStream( | 78 GenericStringUtf16CharacterStream::GenericStringUtf16CharacterStream( |
| 158 Handle<String> data, size_t start_position, size_t end_position) | 79 Handle<String> data, size_t start_position, size_t end_position) |
| 159 : string_(data), length_(end_position), bookmark_(kNoBookmark) { | 80 : string_(data), length_(end_position) { |
| 160 DCHECK(end_position >= start_position); | 81 DCHECK_GE(end_position, start_position); |
| 161 pos_ = start_position; | 82 DCHECK_GE(static_cast<size_t>(string_->length()), |
| 162 } | 83 (end_position - start_position)); |
|
nickie
2016/09/09 11:21:48
nit: why use parentheses here?
vogelheim
2016/09/14 11:28:18
Done.
| |
| 163 | 84 buffer_pos_ = start_position; |
| 164 | 85 } |
| 165 GenericStringUtf16CharacterStream::~GenericStringUtf16CharacterStream() { } | 86 |
| 166 | 87 GenericStringUtf16CharacterStream::~GenericStringUtf16CharacterStream() {} |
| 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 | 88 |
| 190 size_t GenericStringUtf16CharacterStream::FillBuffer(size_t from_pos) { | 89 size_t GenericStringUtf16CharacterStream::FillBuffer(size_t from_pos) { |
| 191 if (from_pos >= length_) return 0; | 90 if (from_pos >= length_) return 0; |
| 192 size_t length = kBufferSize; | 91 |
| 193 if (from_pos + length > length_) { | 92 size_t length = i::Min(kBufferSize, length_ - from_pos); |
| 194 length = length_ - from_pos; | |
| 195 } | |
| 196 String::WriteToFlat<uc16>(*string_, buffer_, static_cast<int>(from_pos), | 93 String::WriteToFlat<uc16>(*string_, buffer_, static_cast<int>(from_pos), |
| 197 static_cast<int>(from_pos + length)); | 94 static_cast<int>(from_pos + length)); |
| 198 return length; | 95 return length; |
| 199 } | 96 } |
| 200 | 97 |
| 201 | 98 // ---------------------------------------------------------------------------- |
| 202 // ---------------------------------------------------------------------------- | 99 // ExternalTwoByteStringUtf16CharacterStream. |
| 203 // ExternalStreamingStream | 100 // |
| 204 | 101 // A stream whose data source is a Handle<ExternalTwoByteString>. It avoids |
| 205 size_t ExternalStreamingStream::FillBuffer(size_t position) { | 102 // all data copying. |
| 206 // Ignore "position" which is the position in the decoded data. Instead, | 103 |
| 207 // ExternalStreamingStream keeps track of the position in the raw data. | 104 class ExternalTwoByteStringUtf16CharacterStream : public Utf16CharacterStream { |
| 208 size_t data_in_buffer = 0; | 105 public: |
| 209 // Note that the UTF-8 decoder might not be able to fill the buffer | 106 ExternalTwoByteStringUtf16CharacterStream(Handle<ExternalTwoByteString> data, |
| 210 // completely; it will typically leave the last character empty (see | 107 size_t start_position, |
| 211 // Utf8ToUtf16CharacterStream::CopyChars). | 108 size_t end_position); |
| 212 while (data_in_buffer < kBufferSize - 1) { | 109 ~ExternalTwoByteStringUtf16CharacterStream() override; |
|
nickie
2016/09/09 11:21:48
Same.
| |
| 213 if (current_data_ == NULL) { | 110 |
| 214 // GetSomeData will wait until the embedder has enough data. Here's an | 111 private: |
| 215 // interface between the API which uses size_t (which is the correct type | 112 bool ReadBlock() override; |
| 216 // here) and the internal parts which use size_t. | 113 |
| 217 current_data_length_ = source_stream_->GetMoreData(¤t_data_); | 114 const uc16* raw_data_; // Pointer to the actual array of characters. |
| 218 current_data_offset_ = 0; | 115 }; |
| 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 | 116 |
| 428 ExternalTwoByteStringUtf16CharacterStream:: | 117 ExternalTwoByteStringUtf16CharacterStream:: |
| 429 ~ExternalTwoByteStringUtf16CharacterStream() { } | 118 ~ExternalTwoByteStringUtf16CharacterStream() {} |
| 430 | 119 |
| 431 ExternalTwoByteStringUtf16CharacterStream:: | 120 ExternalTwoByteStringUtf16CharacterStream:: |
| 432 ExternalTwoByteStringUtf16CharacterStream( | 121 ExternalTwoByteStringUtf16CharacterStream( |
| 433 Handle<ExternalTwoByteString> data, int start_position, | 122 Handle<ExternalTwoByteString> data, size_t start_position, |
| 434 int end_position) | 123 size_t end_position) |
| 435 : raw_data_(data->GetTwoByteData(start_position)), bookmark_(kNoBookmark) { | 124 : raw_data_(data->GetTwoByteData(start_position)) { |
| 125 buffer_start_ = raw_data_; | |
| 436 buffer_cursor_ = raw_data_, | 126 buffer_cursor_ = raw_data_, |
| 437 buffer_end_ = raw_data_ + (end_position - start_position); | 127 buffer_end_ = raw_data_ + (end_position - start_position); |
| 438 pos_ = start_position; | 128 buffer_pos_ = start_position; |
| 439 } | 129 } |
| 440 | 130 |
| 441 | 131 bool ExternalTwoByteStringUtf16CharacterStream::ReadBlock() { |
| 442 bool ExternalTwoByteStringUtf16CharacterStream::SetBookmark() { | 132 // Entire string is read at start. |
| 443 bookmark_ = pos_; | 133 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 } | 134 } |
| 453 | 135 |
| 454 // ---------------------------------------------------------------------------- | 136 // ---------------------------------------------------------------------------- |
| 455 // ExternalOneByteStringUtf16CharacterStream | 137 // ExternalOneByteStringUtf16CharacterStream |
| 138 // | |
| 139 // A stream whose data source is a Handle<ExternalOneByteString>. | |
| 140 | |
| 141 class ExternalOneByteStringUtf16CharacterStream | |
| 142 : public BufferedUtf16CharacterStream { | |
| 143 public: | |
| 144 ExternalOneByteStringUtf16CharacterStream(Handle<ExternalOneByteString> data, | |
| 145 size_t start_position, | |
| 146 size_t end_position); | |
| 147 ~ExternalOneByteStringUtf16CharacterStream() override; | |
|
nickie
2016/09/09 11:21:48
Same.
| |
| 148 | |
| 149 // For testing: | |
| 150 ExternalOneByteStringUtf16CharacterStream(const char* data, size_t length); | |
| 151 | |
| 152 protected: | |
| 153 size_t FillBuffer(size_t position) override; | |
| 154 | |
| 155 const uint8_t* raw_data_; // Pointer to the actual array of characters. | |
| 156 size_t length_; | |
| 157 }; | |
| 456 | 158 |
| 457 ExternalOneByteStringUtf16CharacterStream:: | 159 ExternalOneByteStringUtf16CharacterStream:: |
| 458 ~ExternalOneByteStringUtf16CharacterStream() {} | 160 ~ExternalOneByteStringUtf16CharacterStream() {} |
| 459 | 161 |
| 460 ExternalOneByteStringUtf16CharacterStream:: | 162 ExternalOneByteStringUtf16CharacterStream:: |
| 461 ExternalOneByteStringUtf16CharacterStream( | 163 ExternalOneByteStringUtf16CharacterStream( |
| 462 Handle<ExternalOneByteString> data, int start_position, | 164 Handle<ExternalOneByteString> data, size_t start_position, |
| 463 int end_position) | 165 size_t end_position) |
| 464 : raw_data_(data->GetChars()), | 166 : raw_data_(data->GetChars()), length_(end_position) { |
| 465 length_(end_position), | |
| 466 bookmark_(kNoBookmark) { | |
| 467 DCHECK(end_position >= start_position); | 167 DCHECK(end_position >= start_position); |
| 468 pos_ = start_position; | 168 buffer_pos_ = start_position; |
| 469 } | 169 } |
| 470 | 170 |
| 471 ExternalOneByteStringUtf16CharacterStream:: | 171 ExternalOneByteStringUtf16CharacterStream:: |
| 472 ExternalOneByteStringUtf16CharacterStream(const char* data, size_t length) | 172 ExternalOneByteStringUtf16CharacterStream(const char* data, size_t length) |
| 473 : raw_data_(reinterpret_cast<const uint8_t*>(data)), | 173 : 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 | 174 |
| 501 size_t ExternalOneByteStringUtf16CharacterStream::FillBuffer(size_t from_pos) { | 175 size_t ExternalOneByteStringUtf16CharacterStream::FillBuffer(size_t from_pos) { |
| 502 if (from_pos >= length_) return 0; | 176 if (from_pos >= length_) return 0; |
| 177 | |
| 503 size_t length = Min(kBufferSize, length_ - from_pos); | 178 size_t length = Min(kBufferSize, length_ - from_pos); |
| 504 for (size_t i = 0; i < length; ++i) { | 179 i::CopyCharsUnsigned(buffer_, raw_data_ + from_pos, length); |
| 505 buffer_[i] = static_cast<uc16>(raw_data_[from_pos + i]); | |
| 506 } | |
| 507 return length; | 180 return length; |
| 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 { | |
| 198 for (size_t i = 0; i < chunks_.size(); i++) delete[] chunks_[i].chunk; | |
| 199 } | |
| 200 | |
| 201 protected: | |
| 202 size_t FillBuffer(size_t position) override; | |
| 203 | |
| 204 private: | |
| 205 // ChunkPos points to a position into the chunked stream, containing: | |
| 206 // - # of the chunk in the chunks_ vector, | |
| 207 // - the 'physical' byte_pos, i.e., the number of bytes from the stream's | |
| 208 // beginning, | |
| 209 // - the 'logical' char_pos, i.e. the number of Unicode characters counting | |
| 210 // from the beginning of the stream, | |
| 211 // - the incomplete_char, which stores a possibly incomplete utf-8 decoder | |
| 212 // state, | |
| 213 // iff the 'physical' byte_pos points into the middle of a utf-8 character. | |
| 214 struct ChunkPos { | |
| 215 size_t chunk_no; | |
| 216 size_t byte_pos; | |
| 217 size_t char_pos; | |
| 218 unibrow::Utf8::Utf8IncrementalBuffer incomplete_char; | |
| 219 }; | |
| 220 | |
| 221 // A chunk in the list of chunks, containing the data for the chunk (chunk + | |
| 222 // byte_length), and the position of the chunk within the stream (that is, | |
| 223 // the chunk contains the data starting at | |
| 224 // byte_pos/char_pos/incomplete_first_char). | |
| 225 // | |
| 226 // It contains: | |
| 227 // - a data pointer + length, chunk (owned by the stream) and byte_length, | |
| 228 // - the 'physical' byte_pos of the chunk (i.e. the number of bytes, counted | |
| 229 // from the beginning of the stream, at which this chunk begins), | |
| 230 // - the 'logical' char_pos of the chunk (i.e. the number of Unicode | |
| 231 // characters, counted from the beginning of the stream), | |
| 232 // - the incomplete_first_char, which stores a possibly incomplete utf-8 | |
| 233 // decoder state, iff the 'physical' byte_pos points into the middle of a | |
| 234 // utf-8 character. | |
| 235 // | |
| 236 // So each chunk contains the bytes [byte_pos .. byte_pos + byte_length - 1] | |
| 237 // from the entire stream, and decoding from the beginning of the chunk | |
| 238 // (using incomplete_first_char) will yield the stream's char_pos-th unicode | |
| 239 // character. | |
| 240 struct Chunk { | |
| 241 const uint8_t* chunk; | |
| 242 size_t byte_length; | |
| 243 size_t byte_pos; | |
| 244 size_t char_pos; | |
| 245 unibrow::Utf8::Utf8IncrementalBuffer incomplete_first_char; | |
| 246 }; | |
| 247 std::vector<Chunk> chunks_; | |
| 248 ChunkPos current_; | |
| 249 ScriptCompiler::ExternalSourceStream* source_stream_; | |
| 250 }; | |
| 251 | |
| 252 size_t Utf8ExternalStreamingStream::FillBuffer(size_t position) { | |
| 253 // If the desired position is *not* at the current_ position, then we need | |
| 254 // to search through the chunks_ vector. | |
| 255 if (position != current_.char_pos && !chunks_.empty()) { | |
| 256 // Find the appropriate chunk. | |
| 257 size_t chunk = chunks_.size() - 1; | |
| 258 while (position < chunks_[chunk].char_pos) chunk--; | |
| 259 | |
| 260 // Chunk found (or we're in the last chunk and are not sure yet whether the | |
| 261 // position is in this chunk, or one of the following ones). Go | |
| 262 // incrementally through the buffer until we have found position. | |
| 263 Chunk& current_chunk = chunks_[chunk]; | |
| 264 unibrow::Utf8::Utf8IncrementalBuffer b = | |
| 265 current_chunk.incomplete_first_char; | |
| 266 size_t it = 0; | |
| 267 size_t cpos = current_chunk.char_pos; | |
| 268 while (cpos < position && it < current_chunk.byte_length) { | |
| 269 unibrow::uchar t = | |
| 270 unibrow::Utf8::ValueOfIncremental(current_chunk.chunk[it], &b); | |
| 271 if (t != unibrow::Utf8::kIncomplete) { | |
| 272 cpos++; | |
| 273 } | |
| 274 it++; | |
| 275 } | |
| 276 // At this point, we have either found our position; or we're at the end | |
| 277 // of the last chunk. That should only happen if we seek forward (which | |
| 278 // the Scanner doesn't do); but if it does we can fix it by recursing. | |
| 279 if (cpos == position) { | |
| 280 current_ = {chunk, current_chunk.byte_pos + it, cpos, b}; | |
| 281 } else { | |
| 282 DCHECK_EQ(chunk + 1, chunks_.size()); | |
| 283 current_ = {chunk + 1, current_chunk.byte_pos + it, cpos, b}; | |
| 284 return FillBuffer(position); | |
| 285 } | |
| 286 } | |
| 287 | |
| 288 // Fetch more data if we've exhausted the last chunk. | |
| 289 if (current_.chunk_no == chunks_.size()) { | |
| 290 const uint8_t* chunk = nullptr; | |
| 291 size_t length = source_stream_->GetMoreData(&chunk); | |
| 292 chunks_.push_back({chunk, length, current_.byte_pos, current_.char_pos, | |
| 293 current_.incomplete_char}); | |
| 294 } | |
| 295 | |
| 296 // Return 0 if we're out of data. That is: If we have a zero-length chunk. | |
| 297 Chunk& current_chunk = chunks_[current_.chunk_no]; | |
| 298 if (current_chunk.byte_length == 0) return 0; | |
| 299 | |
| 300 // At this point, we know we have data to return in current_chunk. | |
| 301 uc16* buffer_cursor = buffer_; | |
| 302 uc16* buffer_end = buffer_ + kBufferSize; | |
| 303 unibrow::Utf8::Utf8IncrementalBuffer incomplete_char = | |
| 304 (current_.byte_pos == current_chunk.byte_pos) | |
| 305 ? current_chunk.incomplete_first_char | |
| 306 : unibrow::Utf8::Utf8IncrementalBuffer(0); | |
| 307 | |
| 308 size_t it; | |
| 309 for (it = current_.byte_pos - current_chunk.byte_pos; | |
| 310 it < current_chunk.byte_length && buffer_cursor < buffer_end - 1; it++) { | |
| 311 unibrow::uchar t = unibrow::Utf8::ValueOfIncremental( | |
| 312 current_chunk.chunk[it], &incomplete_char); | |
| 313 if (t == unibrow::Utf8::kIncomplete) continue; | |
| 314 if (t <= unibrow::Utf16::kMaxNonSurrogateCharCode) { | |
| 315 *(buffer_cursor++) = static_cast<uc16>(t); | |
| 316 } else { | |
| 317 *(buffer_cursor++) = unibrow::Utf16::LeadSurrogate(t); | |
| 318 *(buffer_cursor++) = unibrow::Utf16::TrailSurrogate(t); | |
| 319 } | |
| 320 } | |
| 321 buffer_cursor_ = buffer_; | |
| 322 buffer_end_ = buffer_cursor; | |
| 323 | |
| 324 current_.byte_pos = current_chunk.byte_pos + it; | |
| 325 current_.char_pos += (buffer_end_ - buffer_cursor_); | |
| 326 if (it == current_chunk.byte_length) { | |
| 327 current_.chunk_no++; | |
| 328 current_.incomplete_char = incomplete_char; | |
| 329 | |
| 330 if (current_.char_pos == position) { | |
| 331 // This tests for the pathological condition that we haven't produced | |
| 332 // a single character, but we're not out of data. (E.g. when the | |
| 333 // embedder gives us a single-byte chunk, and that one byte is at start | |
| 334 // or middle of a multi-byte utf-8 sequence.) Since we can't return 0 - | |
| 335 // this would indicate the end of data - we will simply recurse. | |
| 336 return FillBuffer(position); | |
| 337 } | |
| 338 } | |
| 339 | |
| 340 return current_.char_pos - position; | |
| 341 } | |
| 342 | |
| 343 // ---------------------------------------------------------------------------- | |
| 344 // Chunks - helper for One- + TwoByteExternalStreamingStream | |
| 345 namespace { | |
| 346 | |
| 347 struct Chunk { | |
| 348 const uint8_t* chunk; | |
| 349 size_t byte_length; | |
| 350 size_t byte_pos; | |
| 351 }; | |
| 352 | |
| 353 typedef std::vector<struct Chunk> Chunks; | |
| 354 | |
| 355 void DeleteChunks(Chunks& chunks) { | |
| 356 for (size_t i = 0; i < chunks.size(); i++) delete[] chunks[i].chunk; | |
| 357 } | |
| 358 | |
| 359 // Return the chunk index for the chunk containing position. | |
| 360 // If position is behind the end of the stream, the index of the last, | |
| 361 // zero-length chunk is returned. | |
| 362 size_t FindChunk(Chunks& chunks, ScriptCompiler::ExternalSourceStream* source_, | |
| 363 size_t position) { | |
| 364 size_t end_pos = | |
| 365 chunks.empty() ? 0 : (chunks.back().byte_pos + chunks.back().byte_length); | |
| 366 | |
| 367 // Get more data if needed. We usually won't enter the loop body. | |
| 368 bool out_of_data = !chunks.empty() && chunks.back().byte_length == 0; | |
| 369 while (!out_of_data && end_pos <= position + 1) { | |
| 370 const uint8_t* chunk = nullptr; | |
| 371 size_t len = source_->GetMoreData(&chunk); | |
| 372 | |
| 373 chunks.push_back({chunk, len, end_pos}); | |
| 374 end_pos += len; | |
| 375 out_of_data = (len == 0); | |
| 376 } | |
| 377 | |
| 378 // Here, we should always have at least one chunk, and we either have the | |
| 379 // chunk we were looking for, or we're out of data. Also, out_of_data and | |
| 380 // end_pos are current (and designate whether we have exhausted the stream, | |
| 381 // and the length of data received so far, respectively). | |
| 382 DCHECK(!chunks.empty()); | |
| 383 DCHECK_EQ(end_pos, chunks.back().byte_pos + chunks.back().byte_length); | |
| 384 DCHECK_EQ(out_of_data, chunks.back().byte_length == 0); | |
| 385 DCHECK(position < end_pos || out_of_data); | |
| 386 | |
| 387 // Edge case: position is behind the end of stream: Return the last (length 0) | |
| 388 // chunk to indicate the end of the stream. | |
| 389 if (position >= end_pos) { | |
| 390 DCHECK(out_of_data); | |
| 391 return chunks.size() - 1; | |
| 392 } | |
| 393 | |
| 394 // We almost always 'stream', meaning we want data from the last chunk, so | |
| 395 // let's look at chunks back-to-front. | |
| 396 size_t chunk_no = chunks.size() - 1; | |
| 397 while (chunks[chunk_no].byte_pos > position) { | |
| 398 DCHECK_NE(chunk_no, 0); | |
| 399 chunk_no--; | |
| 400 } | |
| 401 DCHECK_LE(chunks[chunk_no].byte_pos, position); | |
| 402 DCHECK_LT(position, chunks[chunk_no].byte_pos + chunks[chunk_no].byte_length); | |
| 403 return chunk_no; | |
| 404 } | |
| 405 | |
| 406 } // anonymous namespace | |
| 407 | |
| 408 // ---------------------------------------------------------------------------- | |
| 409 // OneByteExternalStreamingStream | |
| 410 // | |
| 411 // A stream of latin-1 encoded, chunked data. | |
| 412 | |
| 413 class OneByteExternalStreamingStream : public BufferedUtf16CharacterStream { | |
| 414 public: | |
| 415 explicit OneByteExternalStreamingStream( | |
| 416 ScriptCompiler::ExternalSourceStream* source) | |
| 417 : source_(source) {} | |
| 418 ~OneByteExternalStreamingStream() override { DeleteChunks(chunks_); } | |
| 419 | |
| 420 protected: | |
| 421 size_t FillBuffer(size_t position) override; | |
| 422 | |
| 423 private: | |
| 424 Chunks chunks_; | |
| 425 ScriptCompiler::ExternalSourceStream* source_; | |
| 426 }; | |
| 427 | |
| 428 size_t OneByteExternalStreamingStream::FillBuffer(size_t position) { | |
| 429 Chunk& chunk = chunks_[FindChunk(chunks_, source_, position)]; | |
| 430 if (chunk.byte_length == 0) return 0; | |
| 431 | |
| 432 size_t start_pos = position - chunk.byte_pos; | |
| 433 size_t len = i::Min(kBufferSize, chunk.byte_length - start_pos); | |
| 434 i::CopyCharsUnsigned(buffer_, chunk.chunk + start_pos, len); | |
| 435 return len; | |
| 436 } | |
| 437 | |
| 438 // ---------------------------------------------------------------------------- | |
| 439 // TwoByteExternalStreamingStream | |
| 440 // | |
| 441 // A stream of ucs-2 data, delivered in chunks. Chunks may be 'cut' into the | |
| 442 // middle of characters (or even contain only one byte), which adds a bit | |
| 443 // of complexity. This stream avoid all data copying, except for characters | |
| 444 // that cross chunk boundaries. | |
| 445 | |
| 446 class TwoByteExternalStreamingStream : public Utf16CharacterStream { | |
| 447 public: | |
| 448 explicit TwoByteExternalStreamingStream( | |
| 449 ScriptCompiler::ExternalSourceStream* source); | |
| 450 ~TwoByteExternalStreamingStream() override; | |
| 451 | |
| 452 protected: | |
| 453 bool ReadBlock() override; | |
| 454 | |
| 455 Chunks chunks_; | |
| 456 ScriptCompiler::ExternalSourceStream* source_; | |
| 457 uc16 one_char_buffer_; | |
| 458 }; | |
| 459 | |
| 460 TwoByteExternalStreamingStream::TwoByteExternalStreamingStream( | |
| 461 ScriptCompiler::ExternalSourceStream* source) | |
| 462 : Utf16CharacterStream(&one_char_buffer_, &one_char_buffer_, | |
| 463 &one_char_buffer_, 0), | |
| 464 source_(source), | |
| 465 one_char_buffer_(0) {} | |
| 466 | |
| 467 TwoByteExternalStreamingStream::~TwoByteExternalStreamingStream() { | |
| 468 DeleteChunks(chunks_); | |
| 469 } | |
| 470 | |
| 471 bool TwoByteExternalStreamingStream::ReadBlock() { | |
| 472 size_t position = pos(); | |
| 473 | |
| 474 // We'll search for the 2nd byte of our character, to make sure we | |
| 475 // have enough data for at least one character. | |
| 476 size_t chunk_no = FindChunk(chunks_, source_, 2 * position + 1); | |
| 477 | |
| 478 // Out of data? Return 0. | |
| 479 if (chunks_[chunk_no].byte_length == 0) return false; | |
| 480 | |
| 481 Chunk& current = chunks_[chunk_no]; | |
| 482 | |
| 483 // Annoying edge case: Chunks may not be 2-byte aligned, meaning that a | |
| 484 // character may be split between the previous and the current chunk. | |
| 485 // If we find such a lonely byte at the beginning of the chunk, we'll use | |
| 486 // one_char_buffer_ to hold the full character. | |
| 487 bool loneley_byte = (chunks_[chunk_no].byte_pos == (2 * position + 1)); | |
|
nickie
2016/09/09 11:21:48
nit: lonely
vogelheim
2016/09/14 11:28:18
Done.
| |
| 488 if (loneley_byte) { | |
| 489 DCHECK_NE(chunk_no, 0); | |
| 490 Chunk& previous_chunk = chunks_[chunk_no - 1]; | |
| 491 uc16 character = previous_chunk.chunk[previous_chunk.byte_length - 1] | | |
| 492 current.chunk[0] << 8; | |
| 493 | |
| 494 one_char_buffer_ = character; | |
| 495 buffer_pos_ = position; | |
| 496 buffer_start_ = &one_char_buffer_; | |
| 497 buffer_cursor_ = &one_char_buffer_; | |
| 498 buffer_end_ = &one_char_buffer_ + 1; | |
| 499 return true; | |
| 500 } | |
| 501 | |
| 502 // Common case: character is in current chunk. | |
| 503 DCHECK_LE(current.byte_pos, 2 * position); | |
| 504 DCHECK_LT(2 * position + 1, current.byte_pos + current.byte_length); | |
| 505 | |
| 506 // Determine # of full ucs-2 chars in stream, and whether we started on an odd | |
| 507 // byte boundary. | |
| 508 bool odd_start = (current.byte_pos % 2) == 1; | |
| 509 size_t number_chars = (current.byte_length - odd_start) / 2; | |
| 510 | |
| 511 // Point the buffer_*_ members into the current chunk and set buffer_cursor_ | |
| 512 // to point to position. Be careful when converting the byte positions (in | |
| 513 // Chunk) to the ucs-2 character positions (in buffer_*_ members). | |
| 514 buffer_start_ = reinterpret_cast<const uint16_t*>(current.chunk + odd_start); | |
| 515 buffer_end_ = buffer_start_ + number_chars; | |
| 516 buffer_pos_ = (current.byte_pos + odd_start) / 2; | |
| 517 buffer_cursor_ = buffer_start_ + (position - buffer_pos_); | |
| 518 DCHECK_EQ(position, pos()); | |
| 519 return true; | |
| 520 } | |
| 521 | |
| 522 // ---------------------------------------------------------------------------- | |
| 523 // ScannerStream: Create stream instances. | |
| 524 | |
| 525 Utf16CharacterStream* ScannerStream::For(Handle<String> data) { | |
| 526 return ScannerStream::For(data, 0, data->length()); | |
| 527 } | |
| 528 | |
| 529 Utf16CharacterStream* ScannerStream::For(Handle<String> data, int start_pos, | |
| 530 int end_pos) { | |
| 531 DCHECK(start_pos >= 0); | |
| 532 DCHECK(end_pos <= data->length()); | |
| 533 if (data->IsExternalOneByteString()) { | |
| 534 return new ExternalOneByteStringUtf16CharacterStream( | |
| 535 Handle<ExternalOneByteString>::cast(data), start_pos, end_pos); | |
| 536 } else if (data->IsExternalTwoByteString()) { | |
| 537 return new ExternalTwoByteStringUtf16CharacterStream( | |
| 538 Handle<ExternalTwoByteString>::cast(data), start_pos, end_pos); | |
| 539 } else { | |
| 540 // TODO(vogelheim): Maybe call data.Flatten() first? | |
| 541 return new GenericStringUtf16CharacterStream(data, start_pos, end_pos); | |
| 542 } | |
| 543 } | |
| 544 | |
| 545 std::unique_ptr<Utf16CharacterStream> ScannerStream::ForTesting( | |
| 546 const char* data) { | |
| 547 return ScannerStream::ForTesting(data, strlen(data)); | |
| 548 } | |
| 549 | |
| 550 std::unique_ptr<Utf16CharacterStream> ScannerStream::ForTesting( | |
| 551 const char* data, size_t length) { | |
| 552 return std::unique_ptr<Utf16CharacterStream>( | |
| 553 new ExternalOneByteStringUtf16CharacterStream(data, length)); | |
| 554 } | |
| 555 | |
| 556 Utf16CharacterStream* ScannerStream::For( | |
| 557 ScriptCompiler::ExternalSourceStream* source_stream, | |
| 558 v8::ScriptCompiler::StreamedSource::Encoding encoding) { | |
| 559 switch (encoding) { | |
| 560 case v8::ScriptCompiler::StreamedSource::TWO_BYTE: | |
| 561 return new TwoByteExternalStreamingStream(source_stream); | |
| 562 case v8::ScriptCompiler::StreamedSource::ONE_BYTE: | |
| 563 return new OneByteExternalStreamingStream(source_stream); | |
| 564 case v8::ScriptCompiler::StreamedSource::UTF8: | |
| 565 return new Utf8ExternalStreamingStream(source_stream); | |
| 566 } | |
| 567 UNREACHABLE(); | |
| 568 return nullptr; | |
| 569 } | |
| 570 | |
| 510 } // namespace internal | 571 } // namespace internal |
| 511 } // namespace v8 | 572 } // namespace v8 |
| OLD | NEW |