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
3 // found in the LICENSE file.
5 #ifndef V8_SCANNER_CHARACTER_STREAMS_H_
6 #define V8_SCANNER_CHARACTER_STREAMS_H_
8 #include "src/scanner.h"
13 // A buffered character stream based on a random access character
14 // source (ReadBlock can be called with pos_ pointing to any position,
15 // even positions before the current).
16 class BufferedUtf16CharacterStream: public Utf16CharacterStream {
18 BufferedUtf16CharacterStream();
19 virtual ~BufferedUtf16CharacterStream();
21 virtual void PushBack(uc32 character);
24 static const size_t kBufferSize = 512;
25 static const size_t kPushBackStepSize = 16;
27 virtual size_t SlowSeekForward(size_t delta);
28 virtual bool ReadBlock();
29 virtual void SlowPushBack(uc16 character);
31 virtual size_t BufferSeekForward(size_t delta) = 0;
32 virtual size_t FillBuffer(size_t position) = 0;
34 const uc16* pushback_limit_;
35 uc16 buffer_[kBufferSize];
39 // Generic string stream.
40 class GenericStringUtf16CharacterStream: public BufferedUtf16CharacterStream {
42 GenericStringUtf16CharacterStream(Handle<String> data, size_t start_position,
44 virtual ~GenericStringUtf16CharacterStream();
47 virtual size_t BufferSeekForward(size_t delta);
48 virtual size_t FillBuffer(size_t position);
50 Handle<String> string_;
55 // Utf16 stream based on a literal UTF-8 string.
56 class Utf8ToUtf16CharacterStream: public BufferedUtf16CharacterStream {
58 Utf8ToUtf16CharacterStream(const byte* data, size_t length);
59 virtual ~Utf8ToUtf16CharacterStream();
61 static size_t CopyChars(uint16_t* dest, size_t length, const byte* src,
62 size_t* src_pos, size_t src_length);
65 virtual size_t BufferSeekForward(size_t delta);
66 virtual size_t FillBuffer(size_t char_position);
67 void SetRawPosition(size_t char_position);
69 const byte* raw_data_;
70 size_t raw_data_length_; // Measured in bytes, not characters.
72 // The character position of the character at raw_data[raw_data_pos_].
73 // Not necessarily the same as pos_.
74 size_t raw_character_position_;
78 // ExternalStreamingStream is a wrapper around an ExternalSourceStream (see
79 // include/v8.h) subclass implemented by the embedder.
80 class ExternalStreamingStream : public BufferedUtf16CharacterStream {
82 ExternalStreamingStream(ScriptCompiler::ExternalSourceStream* source_stream,
83 v8::ScriptCompiler::StreamedSource::Encoding encoding)
84 : source_stream_(source_stream),
87 current_data_offset_(0),
88 current_data_length_(0),
89 utf8_split_char_buffer_length_(0) {}
91 virtual ~ExternalStreamingStream() { delete[] current_data_; }
93 size_t BufferSeekForward(size_t delta) override {
94 // We never need to seek forward when streaming scripts. We only seek
95 // forward when we want to parse a function whose location we already know,
96 // and when streaming, we don't know the locations of anything we haven't
102 size_t FillBuffer(size_t position) override;
105 void HandleUtf8SplitCharacters(size_t* data_in_buffer);
107 ScriptCompiler::ExternalSourceStream* source_stream_;
108 v8::ScriptCompiler::StreamedSource::Encoding encoding_;
109 const uint8_t* current_data_;
110 size_t current_data_offset_;
111 size_t current_data_length_;
112 // For converting UTF-8 characters which are split across two data chunks.
113 uint8_t utf8_split_char_buffer_[4];
114 size_t utf8_split_char_buffer_length_;
118 // UTF16 buffer to read characters from an external string.
119 class ExternalTwoByteStringUtf16CharacterStream: public Utf16CharacterStream {
121 ExternalTwoByteStringUtf16CharacterStream(Handle<ExternalTwoByteString> data,
124 virtual ~ExternalTwoByteStringUtf16CharacterStream();
126 virtual void PushBack(uc32 character) {
127 DCHECK(buffer_cursor_ > raw_data_);
133 virtual size_t SlowSeekForward(size_t delta) {
134 // Fast case always handles seeking.
137 virtual bool ReadBlock() {
138 // Entire string is read at start.
141 Handle<ExternalTwoByteString> source_;
142 const uc16* raw_data_; // Pointer to the actual array of characters.
145 } } // namespace v8::internal
147 #endif // V8_SCANNER_CHARACTER_STREAMS_H_