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" |
12 | 13 |
13 namespace v8 { | 14 namespace v8 { |
14 namespace internal { | 15 namespace internal { |
15 | 16 |
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 // ---------------------------------------------------------------------------- | 17 // ---------------------------------------------------------------------------- |
74 // BufferedUtf16CharacterStreams | 18 // BufferedUtf16CharacterStreams |
19 // | |
20 // A buffered character stream based on a random access character | |
21 // source (ReadBlock can be called with pos() pointing to any position, | |
22 // even positions before the current). | |
23 class BufferedUtf16CharacterStream : public Utf16CharacterStream { | |
24 public: | |
25 BufferedUtf16CharacterStream(); | |
26 | |
27 protected: | |
28 static const size_t kBufferSize = 512; | |
29 | |
30 bool ReadBlock() override; | |
31 | |
32 // FillBuffer should read up to kBufferSize characters at position and store | |
33 // them into buffer_[0..]. It returns the number of characters stored. | |
34 virtual size_t FillBuffer(size_t position) = 0; | |
35 | |
36 // Fixed sized buffer that this class reads from. | |
37 // The base class' buffer_start_ should always point to buffer_. | |
38 uc16 buffer_[kBufferSize]; | |
39 }; | |
75 | 40 |
76 BufferedUtf16CharacterStream::BufferedUtf16CharacterStream() | 41 BufferedUtf16CharacterStream::BufferedUtf16CharacterStream() |
77 : Utf16CharacterStream(), | 42 : Utf16CharacterStream(buffer_, buffer_, buffer_, 0) {} |
78 pushback_limit_(NULL) { | 43 |
79 // Initialize buffer as being empty. First read will fill the buffer. | 44 bool BufferedUtf16CharacterStream::ReadBlock() { |
45 DCHECK_EQ(buffer_start_, buffer_); | |
46 | |
47 size_t position = pos(); | |
48 buffer_pos_ = position; | |
80 buffer_cursor_ = buffer_; | 49 buffer_cursor_ = buffer_; |
81 buffer_end_ = buffer_; | 50 buffer_end_ = buffer_ + FillBuffer(position); |
82 } | 51 DCHECK_EQ(pos(), position); |
83 | 52 DCHECK_LE(buffer_end_, buffer_start_ + kBufferSize); |
84 | 53 return buffer_cursor_ < buffer_end_; |
85 BufferedUtf16CharacterStream::~BufferedUtf16CharacterStream() { } | 54 } |
86 | 55 |
87 void BufferedUtf16CharacterStream::PushBack(uc32 character) { | 56 // ---------------------------------------------------------------------------- |
88 if (character == kEndOfInput) { | 57 // GenericStringUtf16CharacterStream. |
89 pos_--; | 58 // |
90 return; | 59 // A stream w/ a data source being a (flattened) Handle<String>. |
91 } | 60 |
92 if (pushback_limit_ == NULL && buffer_cursor_ > buffer_) { | 61 class GenericStringUtf16CharacterStream : public BufferedUtf16CharacterStream { |
93 // buffer_ is writable, buffer_cursor_ is const pointer. | 62 public: |
94 buffer_[--buffer_cursor_ - buffer_] = static_cast<uc16>(character); | 63 GenericStringUtf16CharacterStream(Handle<String> data, size_t start_position, |
95 pos_--; | 64 size_t end_position); |
96 return; | 65 |
97 } | 66 protected: |
98 SlowPushBack(static_cast<uc16>(character)); | 67 size_t FillBuffer(size_t position) override; |
99 } | 68 |
100 | 69 Handle<String> string_; |
101 | 70 size_t length_; |
102 void BufferedUtf16CharacterStream::SlowPushBack(uc16 character) { | 71 }; |
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_GE(end_position, start_position); |
161 pos_ = start_position; | 77 DCHECK_GE(static_cast<size_t>(string_->length()), |
162 } | 78 end_position - start_position); |
163 | 79 buffer_pos_ = start_position; |
164 | 80 } |
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 | 81 |
190 size_t GenericStringUtf16CharacterStream::FillBuffer(size_t from_pos) { | 82 size_t GenericStringUtf16CharacterStream::FillBuffer(size_t from_pos) { |
191 if (from_pos >= length_) return 0; | 83 if (from_pos >= length_) return 0; |
192 size_t length = kBufferSize; | 84 |
193 if (from_pos + length > length_) { | 85 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), | 86 String::WriteToFlat<uc16>(*string_, buffer_, static_cast<int>(from_pos), |
197 static_cast<int>(from_pos + length)); | 87 static_cast<int>(from_pos + length)); |
198 return length; | 88 return length; |
199 } | 89 } |
200 | 90 |
201 | 91 // ---------------------------------------------------------------------------- |
202 // ---------------------------------------------------------------------------- | 92 // ExternalTwoByteStringUtf16CharacterStream. |
203 // ExternalStreamingStream | 93 // |
204 | 94 // A stream whose data source is a Handle<ExternalTwoByteString>. It avoids |
205 size_t ExternalStreamingStream::FillBuffer(size_t position) { | 95 // all data copying. |
206 // Ignore "position" which is the position in the decoded data. Instead, | 96 |
207 // ExternalStreamingStream keeps track of the position in the raw data. | 97 class ExternalTwoByteStringUtf16CharacterStream : public Utf16CharacterStream { |
208 size_t data_in_buffer = 0; | 98 public: |
209 // Note that the UTF-8 decoder might not be able to fill the buffer | 99 ExternalTwoByteStringUtf16CharacterStream(Handle<ExternalTwoByteString> data, |
210 // completely; it will typically leave the last character empty (see | 100 size_t start_position, |
211 // Utf8ToUtf16CharacterStream::CopyChars). | 101 size_t end_position); |
212 while (data_in_buffer < kBufferSize - 1) { | 102 |
213 if (current_data_ == NULL) { | 103 private: |
214 // GetSomeData will wait until the embedder has enough data. Here's an | 104 bool ReadBlock() override; |
215 // interface between the API which uses size_t (which is the correct type | 105 |
216 // here) and the internal parts which use size_t. | 106 const uc16* raw_data_; // Pointer to the actual array of characters. |
217 current_data_length_ = source_stream_->GetMoreData(¤t_data_); | 107 size_t start_pos_; |
218 current_data_offset_ = 0; | 108 size_t end_pos_; |
219 bool data_ends = current_data_length_ == 0; | 109 }; |
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 | |
428 ExternalTwoByteStringUtf16CharacterStream:: | |
429 ~ExternalTwoByteStringUtf16CharacterStream() { } | |
430 | 110 |
431 ExternalTwoByteStringUtf16CharacterStream:: | 111 ExternalTwoByteStringUtf16CharacterStream:: |
432 ExternalTwoByteStringUtf16CharacterStream( | 112 ExternalTwoByteStringUtf16CharacterStream( |
433 Handle<ExternalTwoByteString> data, int start_position, | 113 Handle<ExternalTwoByteString> data, size_t start_position, |
434 int end_position) | 114 size_t end_position) |
435 : raw_data_(data->GetTwoByteData(start_position)), bookmark_(kNoBookmark) { | 115 : raw_data_(data->GetTwoByteData(static_cast<int>(start_position))), |
436 buffer_cursor_ = raw_data_, | 116 start_pos_(start_position), |
437 buffer_end_ = raw_data_ + (end_position - start_position); | 117 end_pos_(end_position) { |
438 pos_ = start_position; | 118 buffer_start_ = raw_data_; |
439 } | 119 buffer_cursor_ = raw_data_; |
440 | 120 buffer_end_ = raw_data_ + (end_pos_ - start_pos_); |
441 | 121 buffer_pos_ = start_pos_; |
442 bool ExternalTwoByteStringUtf16CharacterStream::SetBookmark() { | 122 } |
443 bookmark_ = pos_; | 123 |
444 return true; | 124 bool ExternalTwoByteStringUtf16CharacterStream::ReadBlock() { |
445 } | 125 size_t position = pos(); |
446 | 126 bool have_data = start_pos_ <= position && position < end_pos_; |
447 | 127 if (have_data) { |
448 void ExternalTwoByteStringUtf16CharacterStream::ResetToBookmark() { | 128 buffer_pos_ = start_pos_; |
449 DCHECK(bookmark_ != kNoBookmark); | 129 buffer_cursor_ = raw_data_ + (position - start_pos_), |
450 pos_ = bookmark_; | 130 buffer_end_ = raw_data_ + (end_pos_ - start_pos_); |
451 buffer_cursor_ = raw_data_ + bookmark_; | 131 } else { |
132 buffer_pos_ = position; | |
133 buffer_cursor_ = raw_data_; | |
134 buffer_end_ = raw_data_; | |
135 } | |
136 return have_data; | |
452 } | 137 } |
453 | 138 |
454 // ---------------------------------------------------------------------------- | 139 // ---------------------------------------------------------------------------- |
455 // ExternalOneByteStringUtf16CharacterStream | 140 // ExternalOneByteStringUtf16CharacterStream |
456 | 141 // |
457 ExternalOneByteStringUtf16CharacterStream:: | 142 // A stream whose data source is a Handle<ExternalOneByteString>. |
458 ~ExternalOneByteStringUtf16CharacterStream() {} | 143 |
144 class ExternalOneByteStringUtf16CharacterStream | |
145 : public BufferedUtf16CharacterStream { | |
146 public: | |
147 ExternalOneByteStringUtf16CharacterStream(Handle<ExternalOneByteString> data, | |
148 size_t start_position, | |
149 size_t end_position); | |
150 | |
151 // For testing: | |
152 ExternalOneByteStringUtf16CharacterStream(const char* data, size_t length); | |
153 | |
154 protected: | |
155 size_t FillBuffer(size_t position) override; | |
156 | |
157 const uint8_t* raw_data_; // Pointer to the actual array of characters. | |
158 size_t length_; | |
159 }; | |
459 | 160 |
460 ExternalOneByteStringUtf16CharacterStream:: | 161 ExternalOneByteStringUtf16CharacterStream:: |
461 ExternalOneByteStringUtf16CharacterStream( | 162 ExternalOneByteStringUtf16CharacterStream( |
462 Handle<ExternalOneByteString> data, int start_position, | 163 Handle<ExternalOneByteString> data, size_t start_position, |
463 int end_position) | 164 size_t end_position) |
464 : raw_data_(data->GetChars()), | 165 : raw_data_(data->GetChars()), length_(end_position) { |
465 length_(end_position), | |
466 bookmark_(kNoBookmark) { | |
467 DCHECK(end_position >= start_position); | 166 DCHECK(end_position >= start_position); |
468 pos_ = start_position; | 167 buffer_pos_ = start_position; |
469 } | 168 } |
470 | 169 |
471 ExternalOneByteStringUtf16CharacterStream:: | 170 ExternalOneByteStringUtf16CharacterStream:: |
472 ExternalOneByteStringUtf16CharacterStream(const char* data, size_t length) | 171 ExternalOneByteStringUtf16CharacterStream(const char* data, size_t length) |
473 : raw_data_(reinterpret_cast<const uint8_t*>(data)), | 172 : 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 | 173 |
501 size_t ExternalOneByteStringUtf16CharacterStream::FillBuffer(size_t from_pos) { | 174 size_t ExternalOneByteStringUtf16CharacterStream::FillBuffer(size_t from_pos) { |
502 if (from_pos >= length_) return 0; | 175 if (from_pos >= length_) return 0; |
176 | |
503 size_t length = Min(kBufferSize, length_ - from_pos); | 177 size_t length = Min(kBufferSize, length_ - from_pos); |
504 for (size_t i = 0; i < length; ++i) { | 178 i::CopyCharsUnsigned(buffer_, raw_data_ + from_pos, length); |
505 buffer_[i] = static_cast<uc16>(raw_data_[from_pos + i]); | |
506 } | |
507 return length; | 179 return length; |
508 } | 180 } |
509 | 181 |
182 // ---------------------------------------------------------------------------- | |
183 // Utf8ExternalStreamingStream - chunked streaming of Utf-8 data. | |
184 // | |
185 // This implementation is fairly complex, since data arrives in chunks which | |
186 // may 'cut' arbitrarily into utf-8 characters. Also, seeking to a given | |
187 // character position is tricky because the byte position cannot be dericed | |
188 // from the character position. | |
189 | |
190 class Utf8ExternalStreamingStream : public BufferedUtf16CharacterStream { | |
191 public: | |
192 Utf8ExternalStreamingStream( | |
193 ScriptCompiler::ExternalSourceStream* source_stream) | |
194 : current_({0, {0, 0, unibrow::Utf8::Utf8IncrementalBuffer(0)}}), | |
195 source_stream_(source_stream) {} | |
196 ~Utf8ExternalStreamingStream() override { | |
197 for (size_t i = 0; i < chunks_.size(); i++) delete[] chunks_[i].chunk; | |
198 } | |
199 | |
200 protected: | |
201 size_t FillBuffer(size_t position) override; | |
202 | |
203 private: | |
204 // A position within the data stream. It stores: | |
205 // - The 'physical' position (# of bytes in the stream), | |
206 // - the 'logical' position (# of ucs-2 characters, also within the stream), | |
207 // - a possibly incomplete utf-8 char at the current 'physical' position. | |
208 struct ChunkPosition { | |
marja
2016/09/15 08:25:16
Now this doesn't contain the chunk number any more
vogelheim
2016/09/15 11:29:25
Hmm.. I called it StreamPosition.
| |
209 size_t bytes; | |
210 size_t chars; | |
211 unibrow::Utf8::Utf8IncrementalBuffer incomplete_char; | |
212 }; | |
213 | |
214 // Position contains a ChunkPosition and the index of the chunk the position | |
215 // points into. (The chunk_no could be derived from pos, but that'd be | |
216 // an expensive search through all chunks.) | |
217 struct Position { | |
marja
2016/09/15 08:25:16
And this could be called PositionAndChunk or sth.
vogelheim
2016/09/15 11:29:25
I'd prefer to keep this one as Position, since
- 1
| |
218 size_t chunk_no; | |
219 ChunkPosition pos; | |
220 }; | |
221 | |
222 // A chunk in the list of chunks, containing: | |
223 // - The chunk data (data pointer and length), and | |
224 // - the position at the first byte of the chunk. | |
225 struct Chunk { | |
226 const uint8_t* chunk; | |
227 size_t length; | |
228 ChunkPosition start; | |
229 }; | |
230 | |
231 // Within the current chunk, skip forward from current_ towards position. | |
232 void SkipToPosition(size_t position); | |
233 // Within the current chunk, fill the buffer_ (while it has capacity). | |
234 void FillBufferFromCurrentChunk(); | |
235 // Fetch a new chunk (assuming current_ is at the end of the current data). | |
236 bool FetchChunk(); | |
237 // Search through the chunks and set current_ to point to the given position. | |
238 // (This call is potentially expensive.) | |
239 void SearchPosition(size_t position); | |
240 | |
241 std::vector<Chunk> chunks_; | |
242 Position current_; | |
243 ScriptCompiler::ExternalSourceStream* source_stream_; | |
marja
2016/09/15 08:25:16
I like this new version, now the structs and their
vogelheim
2016/09/15 11:29:25
Acknowledged.
| |
244 }; | |
245 | |
246 void Utf8ExternalStreamingStream::SkipToPosition(size_t position) { | |
247 DCHECK_LE(current_.pos.chars, position); // We can only skip forward. | |
248 | |
249 // Already there? Then return immediately. | |
250 if (current_.pos.chars == position) return; | |
251 | |
252 const Chunk& chunk = chunks_[current_.chunk_no]; | |
253 DCHECK(current_.pos.bytes >= chunk.start.bytes); | |
254 | |
255 unibrow::Utf8::Utf8IncrementalBuffer incomplete_char = | |
256 chunk.start.incomplete_char; | |
257 size_t it = current_.pos.bytes - chunk.start.bytes; | |
258 size_t chars = chunk.start.chars; | |
259 while (it < chunk.length && chars < position) { | |
260 unibrow::uchar t = | |
261 unibrow::Utf8::ValueOfIncremental(chunk.chunk[it], &incomplete_char); | |
262 if (t != unibrow::Utf8::kIncomplete) { | |
263 chars++; | |
264 if (t > unibrow::Utf16::kMaxNonSurrogateCharCode) chars++; | |
265 } | |
266 it++; | |
267 } | |
268 | |
269 current_.pos.bytes += it; | |
270 current_.pos.chars = chars; | |
271 current_.pos.incomplete_char = incomplete_char; | |
272 current_.chunk_no += (it == chunk.length); | |
273 } | |
274 | |
275 void Utf8ExternalStreamingStream::FillBufferFromCurrentChunk() { | |
276 DCHECK_LT(current_.chunk_no, chunks_.size()); | |
277 DCHECK_EQ(buffer_start_, buffer_cursor_); | |
278 DCHECK_LT(buffer_end_ + 1, buffer_start_ + kBufferSize); | |
279 | |
280 const Chunk& chunk = chunks_[current_.chunk_no]; | |
281 | |
282 // The buffer_ is writeable, but buffer_*_ members are const. So we get a | |
marja
2016/09/15 08:25:16
Typo: writable
vogelheim
2016/09/15 11:29:26
Done.
| |
283 // non-const pointer into buffer that points to the same char as buffer_end_. | |
marja
2016/09/15 08:25:16
Hmm, what, why? Do we need to write into it? No?
vogelheim
2016/09/15 11:29:25
Yes, we need to write into the buffer. See lines #
| |
284 uint16_t* cursor = buffer_ + (buffer_end_ - buffer_start_); | |
285 DCHECK_EQ(cursor, buffer_end_); | |
286 | |
287 unibrow::Utf8::Utf8IncrementalBuffer incomplete_char = | |
288 current_.pos.incomplete_char; | |
289 size_t it; | |
290 for (it = current_.pos.bytes - chunk.start.bytes; | |
291 it < chunk.length && cursor + 1 < buffer_start_ + kBufferSize; it++) { | |
292 unibrow::uchar t = | |
293 unibrow::Utf8::ValueOfIncremental(chunk.chunk[it], &incomplete_char); | |
294 if (t == unibrow::Utf8::kIncomplete) continue; | |
295 if (t <= unibrow::Utf16::kMaxNonSurrogateCharCode) { | |
296 *(cursor++) = static_cast<uc16>(t); | |
297 } else { | |
298 *(cursor++) = unibrow::Utf16::LeadSurrogate(t); | |
299 *(cursor++) = unibrow::Utf16::TrailSurrogate(t); | |
300 } | |
301 } | |
302 | |
303 current_.pos.bytes = chunk.start.bytes + it; | |
304 current_.pos.chars += (cursor - buffer_end_); | |
305 current_.pos.incomplete_char = incomplete_char; | |
306 current_.chunk_no += (it == chunk.length); | |
307 | |
308 buffer_end_ = cursor; | |
309 } | |
310 | |
311 bool Utf8ExternalStreamingStream::FetchChunk() { | |
312 DCHECK_EQ(current_.chunk_no, chunks_.size()); | |
313 DCHECK(chunks_.empty() || chunks_.back().length != 0); | |
314 | |
315 const uint8_t* chunk = nullptr; | |
316 size_t length = source_stream_->GetMoreData(&chunk); | |
317 chunks_.push_back({chunk, length, current_.pos}); | |
318 return length > 0; | |
319 } | |
320 | |
321 void Utf8ExternalStreamingStream::SearchPosition(size_t position) { | |
322 // If current_ already points to the right position, we're done. | |
323 // | |
324 // This is expected to be the common case, since we typically call | |
325 // FillBuffer right after the current buffer. | |
326 if (current_.pos.chars == position) return; | |
327 | |
328 // No chunks. Then current_ must be at the start, which is the correct | |
329 // position for an empty chunks array anyhow. | |
330 if (chunks_.empty()) { | |
331 DCHECK_EQ(current_.pos.bytes, 0); | |
332 DCHECK_EQ(current_.pos.chars, 0); | |
333 return; | |
marja
2016/09/15 08:25:16
Hmm? Why don't we need to get more data to find "p
vogelheim
2016/09/15 11:29:26
Done.
Yeah... that's a left over from an older ve
| |
334 } | |
335 | |
336 // Search for the last chunk whose start position is less or equal to | |
337 // position. | |
338 size_t chunk_no = 0; | |
339 for (chunk_no = chunks_.size() - 1; | |
340 chunk_no > 0 && chunks_[chunk_no].start.chars > position; chunk_no--) { | |
341 } | |
marja
2016/09/15 08:25:16
Style nit: I'd write this like:
size_t chunk_no =
vogelheim
2016/09/15 11:29:25
Done.
| |
342 | |
343 // Did we find the terminating (zero-length) chunk? Then we're seeking | |
344 // behind the end of the data, and position does not exist. | |
345 // Set current_ to point to the terminating chunk. | |
346 if (chunks_[chunk_no].length == 0) { | |
347 current_ = {chunk_no, chunks_[chunk_no].start}; | |
348 return; | |
349 } | |
350 | |
351 // Did we find the non-last chunk? Then our position must be within chunk_no. | |
352 if (chunk_no + 1 < chunks_.size()) { | |
353 // Fancy-pants optimization for ASCII chunks within a utf-8 stream. | |
354 // (Many web sites declare utf-8 encoding, but use only (or almost only) the | |
355 // ASCII subset for their JavaScript sources. We can exploit this, by | |
356 // checking whether the # bytes in a chunk are equal to the # chars, and if | |
357 // so avoid the expensive SkipToPosition.) | |
358 bool ascii_only_chunk = | |
359 (chunks_[chunk_no + 1].start.bytes - chunks_[chunk_no].start.bytes) == | |
360 (chunks_[chunk_no + 1].start.chars - chunks_[chunk_no].start.chars); | |
361 if (ascii_only_chunk) { | |
362 size_t skip = position - chunks_[chunk_no].start.chars; | |
363 current_ = {chunk_no, | |
364 {chunks_[chunk_no].start.bytes + skip, | |
365 chunks_[chunk_no].start.chars + skip, | |
366 unibrow::Utf8::Utf8IncrementalBuffer(0)}}; | |
marja
2016/09/15 08:25:17
This is great!
vogelheim
2016/09/15 11:29:25
Acknowledged.
| |
367 } else { | |
368 current_ = {chunk_no, chunks_[chunk_no].start}; | |
369 SkipToPosition(position); | |
370 } | |
371 | |
372 // Since position was within the chunk, SkipToPosition should have found | |
373 // something. | |
374 DCHECK_EQ(position, current_.pos.chars); | |
375 return; | |
376 } | |
377 | |
378 // What's left: We're in the last, non-terminating chunk. Our position | |
379 // may be in the chunk, but it may also be in 'future' chunks, which we'll | |
380 // have to obtain. | |
381 DCHECK_EQ(chunk_no, chunks_.size() - 1); | |
382 current_ = {chunk_no, chunks_[chunk_no].start}; | |
383 SkipToPosition(position); | |
384 while (current_.pos.chars < position && chunks_.back().length > 0) { | |
385 DCHECK_EQ(current_.chunk_no, chunks_.size()); | |
386 if (FetchChunk()) { | |
387 SkipToPosition(position); | |
marja
2016/09/15 08:25:16
One thing I still find surprising in this CL is th
vogelheim
2016/09/15 11:29:25
Done, sort of.
| |
388 } | |
389 } | |
390 // We'll return with a postion != the desired position only if we're out | |
391 // of data. In that case, we'll point to the terminating chunk. | |
392 DCHECK_IMPLIES(current_.pos.chars != position, chunks_.back().length == 0); | |
393 DCHECK_IMPLIES(current_.pos.chars != position, chunks_[chunk_no].length == 0); | |
394 } | |
395 | |
396 size_t Utf8ExternalStreamingStream::FillBuffer(size_t position) { | |
397 buffer_cursor_ = buffer_; | |
398 buffer_end_ = buffer_; | |
399 | |
400 SearchPosition(position); | |
401 bool out_of_data = current_.chunk_no != chunks_.size() && | |
402 chunks_[current_.chunk_no].length == 0; | |
403 if (out_of_data) return 0; | |
404 | |
405 // Fill the buffer, until we have at least one char (or are out of data). | |
406 // (The embedder might give us 1-byte blocks within a utf-8 char, so we | |
407 // can't guarantee progress with one chunk. Thus we iterate.) | |
408 while (!out_of_data && buffer_cursor_ == buffer_end_) { | |
409 // At end of current data, but there might be more? Then fetch it. | |
410 if (current_.chunk_no == chunks_.size()) { | |
411 out_of_data = !FetchChunk(); | |
412 } | |
413 if (!out_of_data) FillBufferFromCurrentChunk(); | |
414 } | |
415 | |
416 DCHECK_EQ(current_.pos.chars - position, buffer_end_ - buffer_cursor_); | |
417 return buffer_end_ - buffer_cursor_; | |
418 } | |
419 | |
420 // ---------------------------------------------------------------------------- | |
421 // Chunks - helper for One- + TwoByteExternalStreamingStream | |
422 namespace { | |
423 | |
424 struct Chunk { | |
425 const uint8_t* chunk; | |
marja
2016/09/15 08:25:16
I'd call it "data" instead of "chunk" (because the
vogelheim
2016/09/15 11:29:25
Done.
| |
426 size_t byte_length; | |
427 size_t byte_pos; | |
428 }; | |
429 | |
430 typedef std::vector<struct Chunk> Chunks; | |
431 | |
432 void DeleteChunks(Chunks& chunks) { | |
433 for (size_t i = 0; i < chunks.size(); i++) delete[] chunks[i].chunk; | |
434 } | |
435 | |
436 // Return the chunk index for the chunk containing position. | |
437 // If position is behind the end of the stream, the index of the last, | |
438 // zero-length chunk is returned. | |
439 size_t FindChunk(Chunks& chunks, ScriptCompiler::ExternalSourceStream* source_, | |
440 size_t position) { | |
441 size_t end_pos = | |
442 chunks.empty() ? 0 : (chunks.back().byte_pos + chunks.back().byte_length); | |
443 | |
444 // Get more data if needed. We usually won't enter the loop body. | |
445 bool out_of_data = !chunks.empty() && chunks.back().byte_length == 0; | |
446 while (!out_of_data && end_pos <= position + 1) { | |
447 const uint8_t* chunk = nullptr; | |
448 size_t len = source_->GetMoreData(&chunk); | |
449 | |
450 chunks.push_back({chunk, len, end_pos}); | |
451 end_pos += len; | |
452 out_of_data = (len == 0); | |
453 } | |
454 | |
455 // Here, we should always have at least one chunk, and we either have the | |
456 // chunk we were looking for, or we're out of data. Also, out_of_data and | |
457 // end_pos are current (and designate whether we have exhausted the stream, | |
458 // and the length of data received so far, respectively). | |
459 DCHECK(!chunks.empty()); | |
460 DCHECK_EQ(end_pos, chunks.back().byte_pos + chunks.back().byte_length); | |
461 DCHECK_EQ(out_of_data, chunks.back().byte_length == 0); | |
462 DCHECK(position < end_pos || out_of_data); | |
463 | |
464 // Edge case: position is behind the end of stream: Return the last (length 0) | |
465 // chunk to indicate the end of the stream. | |
466 if (position >= end_pos) { | |
467 DCHECK(out_of_data); | |
468 return chunks.size() - 1; | |
469 } | |
470 | |
471 // We almost always 'stream', meaning we want data from the last chunk, so | |
472 // let's look at chunks back-to-front. | |
473 size_t chunk_no = chunks.size() - 1; | |
474 while (chunks[chunk_no].byte_pos > position) { | |
475 DCHECK_NE(chunk_no, 0); | |
476 chunk_no--; | |
477 } | |
478 DCHECK_LE(chunks[chunk_no].byte_pos, position); | |
479 DCHECK_LT(position, chunks[chunk_no].byte_pos + chunks[chunk_no].byte_length); | |
480 return chunk_no; | |
481 } | |
482 | |
483 } // anonymous namespace | |
484 | |
485 // ---------------------------------------------------------------------------- | |
486 // OneByteExternalStreamingStream | |
487 // | |
488 // A stream of latin-1 encoded, chunked data. | |
489 | |
490 class OneByteExternalStreamingStream : public BufferedUtf16CharacterStream { | |
491 public: | |
492 explicit OneByteExternalStreamingStream( | |
493 ScriptCompiler::ExternalSourceStream* source) | |
494 : source_(source) {} | |
495 ~OneByteExternalStreamingStream() override { DeleteChunks(chunks_); } | |
496 | |
497 protected: | |
498 size_t FillBuffer(size_t position) override; | |
499 | |
500 private: | |
501 Chunks chunks_; | |
502 ScriptCompiler::ExternalSourceStream* source_; | |
503 }; | |
504 | |
505 size_t OneByteExternalStreamingStream::FillBuffer(size_t position) { | |
506 Chunk& chunk = chunks_[FindChunk(chunks_, source_, position)]; | |
marja
2016/09/15 08:25:16
Why not const Chunk& ?
vogelheim
2016/09/15 11:29:25
Done.
| |
507 if (chunk.byte_length == 0) return 0; | |
508 | |
509 size_t start_pos = position - chunk.byte_pos; | |
510 size_t len = i::Min(kBufferSize, chunk.byte_length - start_pos); | |
511 i::CopyCharsUnsigned(buffer_, chunk.chunk + start_pos, len); | |
512 return len; | |
513 } | |
514 | |
515 // ---------------------------------------------------------------------------- | |
516 // TwoByteExternalStreamingStream | |
517 // | |
518 // A stream of ucs-2 data, delivered in chunks. Chunks may be 'cut' into the | |
519 // middle of characters (or even contain only one byte), which adds a bit | |
520 // of complexity. This stream avoid all data copying, except for characters | |
521 // that cross chunk boundaries. | |
522 | |
523 class TwoByteExternalStreamingStream : public Utf16CharacterStream { | |
524 public: | |
525 explicit TwoByteExternalStreamingStream( | |
526 ScriptCompiler::ExternalSourceStream* source); | |
527 ~TwoByteExternalStreamingStream() override; | |
528 | |
529 protected: | |
530 bool ReadBlock() override; | |
531 | |
532 Chunks chunks_; | |
533 ScriptCompiler::ExternalSourceStream* source_; | |
534 uc16 one_char_buffer_; | |
535 }; | |
536 | |
537 TwoByteExternalStreamingStream::TwoByteExternalStreamingStream( | |
538 ScriptCompiler::ExternalSourceStream* source) | |
539 : Utf16CharacterStream(&one_char_buffer_, &one_char_buffer_, | |
540 &one_char_buffer_, 0), | |
541 source_(source), | |
542 one_char_buffer_(0) {} | |
543 | |
544 TwoByteExternalStreamingStream::~TwoByteExternalStreamingStream() { | |
545 DeleteChunks(chunks_); | |
546 } | |
547 | |
548 bool TwoByteExternalStreamingStream::ReadBlock() { | |
549 size_t position = pos(); | |
550 | |
551 // We'll search for the 2nd byte of our character, to make sure we | |
552 // have enough data for at least one character. | |
553 size_t chunk_no = FindChunk(chunks_, source_, 2 * position + 1); | |
554 | |
555 // Out of data? Return 0. | |
556 if (chunks_[chunk_no].byte_length == 0) { | |
557 buffer_cursor_ = buffer_start_; | |
558 buffer_end_ = buffer_start_; | |
559 return false; | |
560 } | |
561 | |
562 Chunk& current = chunks_[chunk_no]; | |
563 | |
564 // Annoying edge case: Chunks may not be 2-byte aligned, meaning that a | |
565 // character may be split between the previous and the current chunk. | |
566 // If we find such a lonely byte at the beginning of the chunk, we'll use | |
567 // one_char_buffer_ to hold the full character. | |
568 bool lonely_byte = (chunks_[chunk_no].byte_pos == (2 * position + 1)); | |
569 if (lonely_byte) { | |
570 DCHECK_NE(chunk_no, 0); | |
571 Chunk& previous_chunk = chunks_[chunk_no - 1]; | |
572 uc16 character = previous_chunk.chunk[previous_chunk.byte_length - 1] | | |
573 current.chunk[0] << 8; | |
574 | |
575 one_char_buffer_ = character; | |
576 buffer_pos_ = position; | |
577 buffer_start_ = &one_char_buffer_; | |
578 buffer_cursor_ = &one_char_buffer_; | |
579 buffer_end_ = &one_char_buffer_ + 1; | |
580 return true; | |
581 } | |
582 | |
583 // Common case: character is in current chunk. | |
584 DCHECK_LE(current.byte_pos, 2 * position); | |
585 DCHECK_LT(2 * position + 1, current.byte_pos + current.byte_length); | |
586 | |
587 // Determine # of full ucs-2 chars in stream, and whether we started on an odd | |
588 // byte boundary. | |
589 bool odd_start = (current.byte_pos % 2) == 1; | |
590 size_t number_chars = (current.byte_length - odd_start) / 2; | |
591 | |
592 // Point the buffer_*_ members into the current chunk and set buffer_cursor_ | |
593 // to point to position. Be careful when converting the byte positions (in | |
594 // Chunk) to the ucs-2 character positions (in buffer_*_ members). | |
595 buffer_start_ = reinterpret_cast<const uint16_t*>(current.chunk + odd_start); | |
596 buffer_end_ = buffer_start_ + number_chars; | |
597 buffer_pos_ = (current.byte_pos + odd_start) / 2; | |
598 buffer_cursor_ = buffer_start_ + (position - buffer_pos_); | |
599 DCHECK_EQ(position, pos()); | |
600 return true; | |
601 } | |
602 | |
603 // ---------------------------------------------------------------------------- | |
604 // ScannerStream: Create stream instances. | |
605 | |
606 Utf16CharacterStream* ScannerStream::For(Handle<String> data) { | |
607 return ScannerStream::For(data, 0, data->length()); | |
608 } | |
609 | |
610 Utf16CharacterStream* ScannerStream::For(Handle<String> data, int start_pos, | |
611 int end_pos) { | |
612 DCHECK(start_pos >= 0); | |
613 DCHECK(end_pos <= data->length()); | |
614 if (data->IsExternalOneByteString()) { | |
615 return new ExternalOneByteStringUtf16CharacterStream( | |
616 Handle<ExternalOneByteString>::cast(data), start_pos, end_pos); | |
617 } else if (data->IsExternalTwoByteString()) { | |
618 return new ExternalTwoByteStringUtf16CharacterStream( | |
619 Handle<ExternalTwoByteString>::cast(data), start_pos, end_pos); | |
620 } else { | |
621 // TODO(vogelheim): Maybe call data.Flatten() first? | |
622 return new GenericStringUtf16CharacterStream(data, start_pos, end_pos); | |
623 } | |
624 } | |
625 | |
626 std::unique_ptr<Utf16CharacterStream> ScannerStream::ForTesting( | |
627 const char* data) { | |
628 return ScannerStream::ForTesting(data, strlen(data)); | |
629 } | |
630 | |
631 std::unique_ptr<Utf16CharacterStream> ScannerStream::ForTesting( | |
632 const char* data, size_t length) { | |
633 return std::unique_ptr<Utf16CharacterStream>( | |
634 new ExternalOneByteStringUtf16CharacterStream(data, length)); | |
635 } | |
636 | |
637 Utf16CharacterStream* ScannerStream::For( | |
638 ScriptCompiler::ExternalSourceStream* source_stream, | |
639 v8::ScriptCompiler::StreamedSource::Encoding encoding) { | |
640 switch (encoding) { | |
641 case v8::ScriptCompiler::StreamedSource::TWO_BYTE: | |
642 return new TwoByteExternalStreamingStream(source_stream); | |
643 case v8::ScriptCompiler::StreamedSource::ONE_BYTE: | |
644 return new OneByteExternalStreamingStream(source_stream); | |
645 case v8::ScriptCompiler::StreamedSource::UTF8: | |
646 return new Utf8ExternalStreamingStream(source_stream); | |
647 } | |
648 UNREACHABLE(); | |
649 return nullptr; | |
650 } | |
651 | |
510 } // namespace internal | 652 } // namespace internal |
511 } // namespace v8 | 653 } // namespace v8 |
OLD | NEW |