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 #ifndef V8_SCANNER_CHARACTER_STREAMS_H_ | 5 #ifndef V8_SCANNER_CHARACTER_STREAMS_H_ |
6 #define V8_SCANNER_CHARACTER_STREAMS_H_ | 6 #define V8_SCANNER_CHARACTER_STREAMS_H_ |
7 | 7 |
8 #include "src/scanner.h" | 8 #include "src/scanner.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
11 namespace internal { | 11 namespace internal { |
12 | 12 |
13 // A buffered character stream based on a random access character | 13 // A buffered character stream based on a random access character |
14 // source (ReadBlock can be called with pos_ pointing to any position, | 14 // source (ReadBlock can be called with pos_ pointing to any position, |
15 // even positions before the current). | 15 // even positions before the current). |
16 class BufferedUtf16CharacterStream: public Utf16CharacterStream { | 16 class BufferedUtf16CharacterStream: public Utf16CharacterStream { |
17 public: | 17 public: |
18 BufferedUtf16CharacterStream(); | 18 BufferedUtf16CharacterStream(); |
19 virtual ~BufferedUtf16CharacterStream(); | 19 virtual ~BufferedUtf16CharacterStream(); |
20 | 20 |
21 virtual void PushBack(uc32 character); | 21 virtual void PushBack(uc32 character); |
22 | 22 |
23 protected: | 23 protected: |
24 static const unsigned kBufferSize = 512; | 24 static const size_t kBufferSize = 512; |
25 static const unsigned kPushBackStepSize = 16; | 25 static const size_t kPushBackStepSize = 16; |
26 | 26 |
27 virtual unsigned SlowSeekForward(unsigned delta); | 27 virtual size_t SlowSeekForward(size_t delta); |
28 virtual bool ReadBlock(); | 28 virtual bool ReadBlock(); |
29 virtual void SlowPushBack(uc16 character); | 29 virtual void SlowPushBack(uc16 character); |
30 | 30 |
31 virtual unsigned BufferSeekForward(unsigned delta) = 0; | 31 virtual size_t BufferSeekForward(size_t delta) = 0; |
32 virtual unsigned FillBuffer(unsigned position) = 0; | 32 virtual size_t FillBuffer(size_t position) = 0; |
33 | 33 |
34 const uc16* pushback_limit_; | 34 const uc16* pushback_limit_; |
35 uc16 buffer_[kBufferSize]; | 35 uc16 buffer_[kBufferSize]; |
36 }; | 36 }; |
37 | 37 |
38 | 38 |
39 // Generic string stream. | 39 // Generic string stream. |
40 class GenericStringUtf16CharacterStream: public BufferedUtf16CharacterStream { | 40 class GenericStringUtf16CharacterStream: public BufferedUtf16CharacterStream { |
41 public: | 41 public: |
42 GenericStringUtf16CharacterStream(Handle<String> data, | 42 GenericStringUtf16CharacterStream(Handle<String> data, size_t start_position, |
43 unsigned start_position, | 43 size_t end_position); |
44 unsigned end_position); | |
45 virtual ~GenericStringUtf16CharacterStream(); | 44 virtual ~GenericStringUtf16CharacterStream(); |
46 | 45 |
47 protected: | 46 protected: |
48 virtual unsigned BufferSeekForward(unsigned delta); | 47 virtual size_t BufferSeekForward(size_t delta); |
49 virtual unsigned FillBuffer(unsigned position); | 48 virtual size_t FillBuffer(size_t position); |
50 | 49 |
51 Handle<String> string_; | 50 Handle<String> string_; |
52 unsigned length_; | 51 size_t length_; |
53 }; | 52 }; |
54 | 53 |
55 | 54 |
56 // Utf16 stream based on a literal UTF-8 string. | 55 // Utf16 stream based on a literal UTF-8 string. |
57 class Utf8ToUtf16CharacterStream: public BufferedUtf16CharacterStream { | 56 class Utf8ToUtf16CharacterStream: public BufferedUtf16CharacterStream { |
58 public: | 57 public: |
59 Utf8ToUtf16CharacterStream(const byte* data, unsigned length); | 58 Utf8ToUtf16CharacterStream(const byte* data, size_t length); |
60 virtual ~Utf8ToUtf16CharacterStream(); | 59 virtual ~Utf8ToUtf16CharacterStream(); |
61 | 60 |
62 static unsigned CopyChars(uint16_t* dest, unsigned length, const byte* src, | 61 static size_t CopyChars(uint16_t* dest, size_t length, const byte* src, |
63 unsigned* src_pos, unsigned src_length); | 62 size_t* src_pos, size_t src_length); |
64 | 63 |
65 protected: | 64 protected: |
66 virtual unsigned BufferSeekForward(unsigned delta); | 65 virtual size_t BufferSeekForward(size_t delta); |
67 virtual unsigned FillBuffer(unsigned char_position); | 66 virtual size_t FillBuffer(size_t char_position); |
68 void SetRawPosition(unsigned char_position); | 67 void SetRawPosition(size_t char_position); |
69 | 68 |
70 const byte* raw_data_; | 69 const byte* raw_data_; |
71 unsigned raw_data_length_; // Measured in bytes, not characters. | 70 size_t raw_data_length_; // Measured in bytes, not characters. |
72 unsigned raw_data_pos_; | 71 size_t raw_data_pos_; |
73 // The character position of the character at raw_data[raw_data_pos_]. | 72 // The character position of the character at raw_data[raw_data_pos_]. |
74 // Not necessarily the same as pos_. | 73 // Not necessarily the same as pos_. |
75 unsigned raw_character_position_; | 74 size_t raw_character_position_; |
76 }; | 75 }; |
77 | 76 |
78 | 77 |
79 // ExternalStreamingStream is a wrapper around an ExternalSourceStream (see | 78 // ExternalStreamingStream is a wrapper around an ExternalSourceStream (see |
80 // include/v8.h) subclass implemented by the embedder. | 79 // include/v8.h) subclass implemented by the embedder. |
81 class ExternalStreamingStream : public BufferedUtf16CharacterStream { | 80 class ExternalStreamingStream : public BufferedUtf16CharacterStream { |
82 public: | 81 public: |
83 ExternalStreamingStream(ScriptCompiler::ExternalSourceStream* source_stream, | 82 ExternalStreamingStream(ScriptCompiler::ExternalSourceStream* source_stream, |
84 v8::ScriptCompiler::StreamedSource::Encoding encoding) | 83 v8::ScriptCompiler::StreamedSource::Encoding encoding) |
85 : source_stream_(source_stream), | 84 : source_stream_(source_stream), |
86 encoding_(encoding), | 85 encoding_(encoding), |
87 current_data_(NULL), | 86 current_data_(NULL), |
88 current_data_offset_(0), | 87 current_data_offset_(0), |
89 current_data_length_(0), | 88 current_data_length_(0), |
90 utf8_split_char_buffer_length_(0) {} | 89 utf8_split_char_buffer_length_(0) {} |
91 | 90 |
92 virtual ~ExternalStreamingStream() { delete[] current_data_; } | 91 virtual ~ExternalStreamingStream() { delete[] current_data_; } |
93 | 92 |
94 unsigned BufferSeekForward(unsigned delta) OVERRIDE { | 93 size_t BufferSeekForward(size_t delta) OVERRIDE { |
95 // We never need to seek forward when streaming scripts. We only seek | 94 // We never need to seek forward when streaming scripts. We only seek |
96 // forward when we want to parse a function whose location we already know, | 95 // forward when we want to parse a function whose location we already know, |
97 // and when streaming, we don't know the locations of anything we haven't | 96 // and when streaming, we don't know the locations of anything we haven't |
98 // seen yet. | 97 // seen yet. |
99 UNREACHABLE(); | 98 UNREACHABLE(); |
100 return 0; | 99 return 0; |
101 } | 100 } |
102 | 101 |
103 unsigned FillBuffer(unsigned position) OVERRIDE; | 102 size_t FillBuffer(size_t position) OVERRIDE; |
104 | 103 |
105 private: | 104 private: |
106 void HandleUtf8SplitCharacters(unsigned* data_in_buffer); | 105 void HandleUtf8SplitCharacters(size_t* data_in_buffer); |
107 | 106 |
108 ScriptCompiler::ExternalSourceStream* source_stream_; | 107 ScriptCompiler::ExternalSourceStream* source_stream_; |
109 v8::ScriptCompiler::StreamedSource::Encoding encoding_; | 108 v8::ScriptCompiler::StreamedSource::Encoding encoding_; |
110 const uint8_t* current_data_; | 109 const uint8_t* current_data_; |
111 unsigned current_data_offset_; | 110 size_t current_data_offset_; |
112 unsigned current_data_length_; | 111 size_t current_data_length_; |
113 // For converting UTF-8 characters which are split across two data chunks. | 112 // For converting UTF-8 characters which are split across two data chunks. |
114 uint8_t utf8_split_char_buffer_[4]; | 113 uint8_t utf8_split_char_buffer_[4]; |
115 unsigned utf8_split_char_buffer_length_; | 114 size_t utf8_split_char_buffer_length_; |
116 }; | 115 }; |
117 | 116 |
118 | 117 |
119 // UTF16 buffer to read characters from an external string. | 118 // UTF16 buffer to read characters from an external string. |
120 class ExternalTwoByteStringUtf16CharacterStream: public Utf16CharacterStream { | 119 class ExternalTwoByteStringUtf16CharacterStream: public Utf16CharacterStream { |
121 public: | 120 public: |
122 ExternalTwoByteStringUtf16CharacterStream(Handle<ExternalTwoByteString> data, | 121 ExternalTwoByteStringUtf16CharacterStream(Handle<ExternalTwoByteString> data, |
123 int start_position, | 122 int start_position, |
124 int end_position); | 123 int end_position); |
125 virtual ~ExternalTwoByteStringUtf16CharacterStream(); | 124 virtual ~ExternalTwoByteStringUtf16CharacterStream(); |
126 | 125 |
127 virtual void PushBack(uc32 character) { | 126 virtual void PushBack(uc32 character) { |
128 DCHECK(buffer_cursor_ > raw_data_); | 127 DCHECK(buffer_cursor_ > raw_data_); |
129 buffer_cursor_--; | 128 buffer_cursor_--; |
130 pos_--; | 129 pos_--; |
131 } | 130 } |
132 | 131 |
133 protected: | 132 protected: |
134 virtual unsigned SlowSeekForward(unsigned delta) { | 133 virtual size_t SlowSeekForward(size_t delta) { |
135 // Fast case always handles seeking. | 134 // Fast case always handles seeking. |
136 return 0; | 135 return 0; |
137 } | 136 } |
138 virtual bool ReadBlock() { | 137 virtual bool ReadBlock() { |
139 // Entire string is read at start. | 138 // Entire string is read at start. |
140 return false; | 139 return false; |
141 } | 140 } |
142 Handle<ExternalTwoByteString> source_; | 141 Handle<ExternalTwoByteString> source_; |
143 const uc16* raw_data_; // Pointer to the actual array of characters. | 142 const uc16* raw_data_; // Pointer to the actual array of characters. |
144 }; | 143 }; |
145 | 144 |
146 } } // namespace v8::internal | 145 } } // namespace v8::internal |
147 | 146 |
148 #endif // V8_SCANNER_CHARACTER_STREAMS_H_ | 147 #endif // V8_SCANNER_CHARACTER_STREAMS_H_ |
OLD | NEW |