| 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_PARSING_SCANNER_CHARACTER_STREAMS_H_ | 5 #ifndef V8_PARSING_SCANNER_CHARACTER_STREAMS_H_ |
| 6 #define V8_PARSING_SCANNER_CHARACTER_STREAMS_H_ | 6 #define V8_PARSING_SCANNER_CHARACTER_STREAMS_H_ |
| 7 | 7 |
| 8 #include "include/v8.h" // for v8::ScriptCompiler |
| 8 #include "src/handles.h" | 9 #include "src/handles.h" |
| 9 #include "src/parsing/scanner.h" | |
| 10 #include "src/vector.h" | |
| 11 | 10 |
| 12 namespace v8 { | 11 namespace v8 { |
| 13 namespace internal { | 12 namespace internal { |
| 14 | 13 |
| 15 // Forward declarations. | 14 class Utf16CharacterStream; |
| 16 class ExternalTwoByteString; | |
| 17 class ExternalOneByteString; | |
| 18 | 15 |
| 19 // A buffered character stream based on a random access character | 16 class ScannerStream { |
| 20 // source (ReadBlock can be called with pos_ pointing to any position, | |
| 21 // even positions before the current). | |
| 22 class BufferedUtf16CharacterStream: public Utf16CharacterStream { | |
| 23 public: | 17 public: |
| 24 BufferedUtf16CharacterStream(); | 18 static Utf16CharacterStream* For(Handle<String> data); |
| 25 ~BufferedUtf16CharacterStream() override; | 19 static Utf16CharacterStream* For(Handle<String> data, int start_pos, |
| 26 | 20 int end_pos); |
| 27 void PushBack(uc32 character) override; | 21 static Utf16CharacterStream* For( |
| 28 | 22 ScriptCompiler::ExternalSourceStream* source_stream, |
| 29 protected: | 23 ScriptCompiler::StreamedSource::Encoding encoding); |
| 30 static const size_t kBufferSize = 512; | |
| 31 static const size_t kPushBackStepSize = 16; | |
| 32 | |
| 33 size_t SlowSeekForward(size_t delta) override; | |
| 34 bool ReadBlock() override; | |
| 35 virtual void SlowPushBack(uc16 character); | |
| 36 | |
| 37 virtual size_t BufferSeekForward(size_t delta) = 0; | |
| 38 virtual size_t FillBuffer(size_t position) = 0; | |
| 39 | |
| 40 const uc16* pushback_limit_; | |
| 41 uc16 buffer_[kBufferSize]; | |
| 42 }; | |
| 43 | |
| 44 | |
| 45 // Generic string stream. | |
| 46 class GenericStringUtf16CharacterStream: public BufferedUtf16CharacterStream { | |
| 47 public: | |
| 48 GenericStringUtf16CharacterStream(Handle<String> data, size_t start_position, | |
| 49 size_t end_position); | |
| 50 ~GenericStringUtf16CharacterStream() override; | |
| 51 | |
| 52 bool SetBookmark() override; | |
| 53 void ResetToBookmark() override; | |
| 54 | |
| 55 protected: | |
| 56 static const size_t kNoBookmark = -1; | |
| 57 | |
| 58 size_t BufferSeekForward(size_t delta) override; | |
| 59 size_t FillBuffer(size_t position) override; | |
| 60 | |
| 61 Handle<String> string_; | |
| 62 size_t length_; | |
| 63 size_t bookmark_; | |
| 64 }; | |
| 65 | |
| 66 | |
| 67 // ExternalStreamingStream is a wrapper around an ExternalSourceStream (see | |
| 68 // include/v8.h) subclass implemented by the embedder. | |
| 69 class ExternalStreamingStream : public BufferedUtf16CharacterStream { | |
| 70 public: | |
| 71 ExternalStreamingStream(ScriptCompiler::ExternalSourceStream* source_stream, | |
| 72 v8::ScriptCompiler::StreamedSource::Encoding encoding) | |
| 73 : source_stream_(source_stream), | |
| 74 encoding_(encoding), | |
| 75 current_data_(NULL), | |
| 76 current_data_offset_(0), | |
| 77 current_data_length_(0), | |
| 78 utf8_split_char_buffer_length_(0), | |
| 79 bookmark_(0), | |
| 80 bookmark_data_is_from_current_data_(false), | |
| 81 bookmark_data_offset_(0), | |
| 82 bookmark_utf8_split_char_buffer_length_(0) {} | |
| 83 | |
| 84 ~ExternalStreamingStream() override { | |
| 85 delete[] current_data_; | |
| 86 bookmark_buffer_.Dispose(); | |
| 87 bookmark_data_.Dispose(); | |
| 88 } | |
| 89 | |
| 90 size_t BufferSeekForward(size_t delta) override { | |
| 91 // We never need to seek forward when streaming scripts. We only seek | |
| 92 // forward when we want to parse a function whose location we already know, | |
| 93 // and when streaming, we don't know the locations of anything we haven't | |
| 94 // seen yet. | |
| 95 UNREACHABLE(); | |
| 96 return 0; | |
| 97 } | |
| 98 | |
| 99 size_t FillBuffer(size_t position) override; | |
| 100 | |
| 101 bool SetBookmark() override; | |
| 102 void ResetToBookmark() override; | |
| 103 | |
| 104 private: | |
| 105 void HandleUtf8SplitCharacters(size_t* data_in_buffer); | |
| 106 void FlushCurrent(); | |
| 107 | |
| 108 ScriptCompiler::ExternalSourceStream* source_stream_; | |
| 109 v8::ScriptCompiler::StreamedSource::Encoding encoding_; | |
| 110 const uint8_t* current_data_; | |
| 111 size_t current_data_offset_; | |
| 112 size_t current_data_length_; | |
| 113 // For converting UTF-8 characters which are split across two data chunks. | |
| 114 uint8_t utf8_split_char_buffer_[4]; | |
| 115 size_t utf8_split_char_buffer_length_; | |
| 116 | |
| 117 // Bookmark support. See comments in ExternalStreamingStream::SetBookmark | |
| 118 // for additional details. | |
| 119 size_t bookmark_; | |
| 120 Vector<uint16_t> bookmark_buffer_; | |
| 121 Vector<uint8_t> bookmark_data_; | |
| 122 bool bookmark_data_is_from_current_data_; | |
| 123 size_t bookmark_data_offset_; | |
| 124 uint8_t bookmark_utf8_split_char_buffer_[4]; | |
| 125 size_t bookmark_utf8_split_char_buffer_length_; | |
| 126 }; | |
| 127 | |
| 128 | |
| 129 // UTF16 buffer to read characters from an external string. | |
| 130 class ExternalTwoByteStringUtf16CharacterStream: public Utf16CharacterStream { | |
| 131 public: | |
| 132 ExternalTwoByteStringUtf16CharacterStream(Handle<ExternalTwoByteString> data, | |
| 133 int start_position, | |
| 134 int end_position); | |
| 135 ~ExternalTwoByteStringUtf16CharacterStream() override; | |
| 136 | |
| 137 void PushBack(uc32 character) override { | |
| 138 DCHECK(buffer_cursor_ > raw_data_); | |
| 139 pos_--; | |
| 140 if (character != kEndOfInput) { | |
| 141 buffer_cursor_--; | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 bool SetBookmark() override; | |
| 146 void ResetToBookmark() override; | |
| 147 | |
| 148 private: | |
| 149 size_t SlowSeekForward(size_t delta) override { | |
| 150 // Fast case always handles seeking. | |
| 151 return 0; | |
| 152 } | |
| 153 bool ReadBlock() override { | |
| 154 // Entire string is read at start. | |
| 155 return false; | |
| 156 } | |
| 157 const uc16* raw_data_; // Pointer to the actual array of characters. | |
| 158 | |
| 159 static const size_t kNoBookmark = -1; | |
| 160 | |
| 161 size_t bookmark_; | |
| 162 }; | |
| 163 | |
| 164 // UTF16 buffer to read characters from an external latin1 string. | |
| 165 class ExternalOneByteStringUtf16CharacterStream | |
| 166 : public BufferedUtf16CharacterStream { | |
| 167 public: | |
| 168 ExternalOneByteStringUtf16CharacterStream(Handle<ExternalOneByteString> data, | |
| 169 int start_position, | |
| 170 int end_position); | |
| 171 ~ExternalOneByteStringUtf16CharacterStream() override; | |
| 172 | 24 |
| 173 // For testing: | 25 // For testing: |
| 174 explicit ExternalOneByteStringUtf16CharacterStream(const char* data); | 26 static std::unique_ptr<Utf16CharacterStream> ForTesting(const char* data); |
| 175 ExternalOneByteStringUtf16CharacterStream(const char* data, size_t length); | 27 static std::unique_ptr<Utf16CharacterStream> ForTesting(const char* data, |
| 176 | 28 size_t length); |
| 177 bool SetBookmark() override; | |
| 178 void ResetToBookmark() override; | |
| 179 | |
| 180 private: | |
| 181 static const size_t kNoBookmark = -1; | |
| 182 | |
| 183 size_t BufferSeekForward(size_t delta) override; | |
| 184 size_t FillBuffer(size_t position) override; | |
| 185 | |
| 186 const uint8_t* raw_data_; // Pointer to the actual array of characters. | |
| 187 size_t length_; | |
| 188 size_t bookmark_; | |
| 189 }; | 29 }; |
| 190 | 30 |
| 191 } // namespace internal | 31 } // namespace internal |
| 192 } // namespace v8 | 32 } // namespace v8 |
| 193 | 33 |
| 194 #endif // V8_PARSING_SCANNER_CHARACTER_STREAMS_H_ | 34 #endif // V8_PARSING_SCANNER_CHARACTER_STREAMS_H_ |
| OLD | NEW |