OLD | NEW |
---|---|
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 23 matching lines...) Expand all Loading... | |
34 #include "scanner-base.h" | 34 #include "scanner-base.h" |
35 #include "preparse-data.h" | 35 #include "preparse-data.h" |
36 #include "preparser.h" | 36 #include "preparser.h" |
37 | 37 |
38 namespace v8 { | 38 namespace v8 { |
39 namespace internal { | 39 namespace internal { |
40 | 40 |
41 // UTF16Buffer based on a v8::UnicodeInputStream. | 41 // UTF16Buffer based on a v8::UnicodeInputStream. |
42 class InputStreamUTF16Buffer : public UC16CharacterStream { | 42 class InputStreamUTF16Buffer : public UC16CharacterStream { |
43 public: | 43 public: |
44 /* The InputStreamUTF16Buffer maintains an internal buffer | |
45 * that is filled in chunks from the UC16CharacterStream. | |
46 * It also maintains unlimited pushback capability, but optimized | |
47 * for small pushbacks. | |
48 * The pushback_buffer_ pointer points to the limit of pushbacks | |
49 * in the current buffer. There is room for a few pushback'ed chars before | |
50 * the buffer containing the most recently read chunk. If this is overflowed, | |
51 * an external buffer is allocated/reused to hold further pushbacks, and | |
Rico
2010/12/08 09:46:25
and but -> and
Lasse Reichstein
2010/12/08 10:05:36
Done.
| |
52 * but pushback_buffer_ and buffer_cursor_/buffer_end_ now points to the | |
53 * new buffer. When this buffer is read to the end again, the cursor is | |
54 * switched back to the internal buffer | |
55 */ | |
44 explicit InputStreamUTF16Buffer(v8::UnicodeInputStream* stream) | 56 explicit InputStreamUTF16Buffer(v8::UnicodeInputStream* stream) |
45 : UC16CharacterStream(), | 57 : UC16CharacterStream(), |
46 stream_(stream), | 58 stream_(stream), |
47 pushback_active_(false) { | 59 pushback_buffer_(buffer_), |
60 pushback_buffer_end_cache_(NULL), | |
61 pushback_buffer_backing_(NULL), | |
62 pushback_buffer_backing_size_(0) { | |
48 buffer_cursor_ = buffer_end_ = buffer_ + kPushBackSize; | 63 buffer_cursor_ = buffer_end_ = buffer_ + kPushBackSize; |
49 } | 64 } |
50 | 65 |
51 virtual ~InputStreamUTF16Buffer() { } | 66 virtual ~InputStreamUTF16Buffer() { |
67 if (pushback_buffer_backing_ != NULL) { | |
68 DeleteArray(pushback_buffer_backing_); | |
69 } | |
70 } | |
52 | 71 |
53 virtual void PushBack(uc16 ch) { | 72 virtual void PushBack(uc16 ch) { |
54 ASSERT(pos_ > 0); | 73 ASSERT(pos_ > 0); |
55 if (buffer_cursor_ > buffer_) { | 74 if (buffer_cursor_ <= pushback_buffer_) { |
56 // While we can stay within the buffer, just do so. | 75 // No more room in the current buffer to do pushbacks. |
57 *--buffer_cursor_ = ch; | 76 if (pushback_buffer_end_cache_ == NULL) { |
58 pos_--; | 77 // We have overflowed the pushback space at the beginning of buffer_. |
59 return; | 78 // Switch to using a separate allocated pushback buffer. |
79 if (pushback_buffer_backing_ == NULL) { | |
80 // Allocate a buffer the first time we need it. | |
81 pushback_buffer_backing_ = NewArray<uc16>(kPushBackSize); | |
82 pushback_buffer_backing_size_ = kPushBackSize; | |
83 } | |
84 pushback_buffer_ = pushback_buffer_backing_; | |
85 pushback_buffer_end_cache_ = buffer_end_; | |
86 buffer_end_ = pushback_buffer_backing_ + pushback_buffer_backing_size_; | |
87 buffer_cursor_ = buffer_end_ - 1; | |
88 } else { | |
89 // Hit the bottom of the allocated pushback buffer. | |
90 // Double the buffer and continue. | |
91 uc16* new_buffer = NewArray<uc16>(pushback_buffer_backing_size_ * 2); | |
92 memcpy(new_buffer + pushback_buffer_backing_size_, | |
93 pushback_buffer_backing_, | |
94 pushback_buffer_backing_size_); | |
95 DeleteArray(pushback_buffer_backing_); | |
96 buffer_cursor_ = new_buffer + pushback_buffer_backing_size_; | |
97 pushback_buffer_backing_ = pushback_buffer_ = new_buffer; | |
98 buffer_end_ = pushback_buffer_backing_ + pushback_buffer_backing_size_; | |
99 } | |
60 } | 100 } |
61 if (!pushback_active_) { | 101 pushback_buffer_[buffer_cursor_ - pushback_buffer_- 1] = ch; |
62 // Push back the entire buffer to the stream and let the | |
63 // stream handle pushbacks from now. | |
64 // We leave buffer_cursor_ == buffer_end_, so the next read | |
65 // will fill the buffer from the current position. | |
66 // This should happen exceedingly rarely. | |
67 while (buffer_end_ > buffer_) { | |
68 stream_->PushBack(*--buffer_end_); | |
69 } | |
70 buffer_cursor_ = buffer_end_; | |
71 pushback_active_ = true; | |
72 } | |
73 stream_->PushBack(ch); | |
74 pos_--; | 102 pos_--; |
75 } | 103 } |
76 | 104 |
77 protected: | 105 protected: |
78 virtual bool ReadBlock() { | 106 virtual bool ReadBlock() { |
107 if (pushback_buffer_end_cache_ != NULL) { | |
108 buffer_cursor_ = buffer_; | |
109 buffer_end_ = pushback_buffer_end_cache_; | |
110 pushback_buffer_end_cache_ = NULL; | |
111 return buffer_end_ > buffer_cursor_; | |
112 } | |
79 // Copy the top of the buffer into the pushback area. | 113 // Copy the top of the buffer into the pushback area. |
80 pushback_active_ = false; | |
81 int32_t value; | 114 int32_t value; |
82 uc16* buffer_start = buffer_ + kPushBackSize; | 115 uc16* buffer_start = buffer_ + kPushBackSize; |
83 buffer_cursor_ = buffer_end_ = buffer_start; | 116 buffer_cursor_ = buffer_end_ = buffer_start; |
84 while ((value = stream_->Next()) >= 0) { | 117 while ((value = stream_->Next()) >= 0) { |
85 if (value > static_cast<int32_t>(unibrow::Utf8::kMaxThreeByteChar)) { | 118 if (value > static_cast<int32_t>(unibrow::Utf8::kMaxThreeByteChar)) { |
86 value = unibrow::Utf8::kBadChar; | 119 value = unibrow::Utf8::kBadChar; |
87 } | 120 } |
88 // buffer_end_ is a const pointer, but buffer_ is writable. | 121 // buffer_end_ is a const pointer, but buffer_ is writable. |
89 buffer_start[buffer_end_++ - buffer_start] = static_cast<uc16>(value); | 122 buffer_start[buffer_end_++ - buffer_start] = static_cast<uc16>(value); |
90 if (buffer_end_ == buffer_ + kPushBackSize + kBufferSize) break; | 123 if (buffer_end_ == buffer_ + kPushBackSize + kBufferSize) break; |
91 } | 124 } |
92 return buffer_end_ > buffer_start; | 125 return buffer_end_ > buffer_start; |
93 } | 126 } |
94 | 127 |
95 virtual unsigned SlowSeekForward(unsigned pos) { | 128 virtual unsigned SlowSeekForward(unsigned pos) { |
96 // Seeking in the input is not used by preparsing. | 129 // Seeking in the input is not used by preparsing. |
97 // It's only used by the real parser based on preparser data. | 130 // It's only used by the real parser based on preparser data. |
98 UNIMPLEMENTED(); | 131 UNIMPLEMENTED(); |
99 return 0; | 132 return 0; |
100 } | 133 } |
101 | 134 |
102 private: | 135 private: |
103 static const unsigned kBufferSize = 512; | 136 static const unsigned kBufferSize = 512; |
104 static const unsigned kPushBackSize = 16; | 137 static const unsigned kPushBackSize = 16; |
105 v8::UnicodeInputStream* const stream_; | 138 v8::UnicodeInputStream* const stream_; |
106 // Buffer holding first kPushBackSize characters of pushback buffer, | 139 // Buffer holding first kPushBackSize characters of pushback buffer, |
107 // then kBufferSize chars of read-ahead. | 140 // then kBufferSize chars of read-ahead. |
108 // The pushback buffer is only used if pushing back characters past | 141 // The pushback buffer is only used if pushing back characters past |
109 // the start of a block. | 142 // the start of a block. |
110 uc16 buffer_[kBufferSize + kPushBackSize]; | 143 uc16 buffer_[kPushBackSize + kBufferSize]; |
111 bool pushback_active_; | 144 // Limit of pushbacks before new allocation is necessary. |
145 uc16* pushback_buffer_; | |
146 // Only if that pushback buffer at the start of buffer_ isn't sufficient | |
147 // is the following used. | |
148 const uc16* pushback_buffer_end_cache_; | |
149 uc16* pushback_buffer_backing_; | |
150 unsigned pushback_buffer_backing_size_; | |
112 }; | 151 }; |
113 | 152 |
114 | 153 |
115 class StandAloneJavaScriptScanner : public JavaScriptScanner { | 154 class StandAloneJavaScriptScanner : public JavaScriptScanner { |
116 public: | 155 public: |
117 void Initialize(UC16CharacterStream* source) { | 156 void Initialize(UC16CharacterStream* source) { |
118 source_ = source; | 157 source_ = source; |
119 literal_flags_ = kLiteralString | kLiteralIdentifier; | 158 literal_flags_ = kLiteralString | kLiteralIdentifier; |
120 Init(); | 159 Init(); |
121 // Skip initial whitespace allowing HTML comment ends just like | 160 // Skip initial whitespace allowing HTML comment ends just like |
122 // after a newline and scan first token. | 161 // after a newline and scan first token. |
123 has_line_terminator_before_next_ = true; | 162 has_line_terminator_before_next_ = true; |
124 SkipWhiteSpace(); | 163 SkipWhiteSpace(); |
125 Scan(); | 164 Scan(); |
126 } | 165 } |
127 }; | 166 }; |
128 | 167 |
129 | 168 |
130 // Functions declared by allocation.h | 169 // Functions declared by allocation.h |
131 | 170 |
132 void FatalProcessOutOfMemory(const char* reason) { | 171 void FatalProcessOutOfMemory(const char* reason) { |
133 V8_Fatal(__FILE__, __LINE__, reason); | 172 V8_Fatal(__FILE__, __LINE__, reason); |
134 } | 173 } |
135 | 174 |
136 bool EnableSlowAsserts() { return true; } | 175 bool EnableSlowAsserts() { return true; } |
137 | 176 |
138 | |
139 } // namespace internal. | 177 } // namespace internal. |
140 | 178 |
141 | 179 |
142 UnicodeInputStream::~UnicodeInputStream() { } | 180 UnicodeInputStream::~UnicodeInputStream() { } |
143 | 181 |
144 | 182 |
145 PreParserData Preparse(UnicodeInputStream* input, size_t max_stack) { | 183 PreParserData Preparse(UnicodeInputStream* input, size_t max_stack) { |
146 internal::InputStreamUTF16Buffer buffer(input); | 184 internal::InputStreamUTF16Buffer buffer(input); |
147 uintptr_t stack_limit = reinterpret_cast<uintptr_t>(&buffer) - max_stack; | 185 uintptr_t stack_limit = reinterpret_cast<uintptr_t>(&buffer) - max_stack; |
148 internal::StandAloneJavaScriptScanner scanner; | 186 internal::StandAloneJavaScriptScanner scanner; |
(...skipping 13 matching lines...) Expand all Loading... | |
162 return PreParserData(size, data); | 200 return PreParserData(size, data); |
163 } | 201 } |
164 | 202 |
165 } // namespace v8. | 203 } // namespace v8. |
166 | 204 |
167 | 205 |
168 // Used by ASSERT macros and other immediate exits. | 206 // Used by ASSERT macros and other immediate exits. |
169 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) { | 207 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) { |
170 exit(EXIT_FAILURE); | 208 exit(EXIT_FAILURE); |
171 } | 209 } |
OLD | NEW |