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