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 21 matching lines...) Expand all Loading... |
32 #include "utils.h" | 32 #include "utils.h" |
33 #include "list.h" | 33 #include "list.h" |
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 UTF16Buffer { |
43 public: | 43 public: |
44 /* The InputStreamUTF16Buffer maintains an internal buffer | 44 explicit InputStreamUTF16Buffer(UnicodeInputStream* stream) |
45 * that is filled in chunks from the UC16CharacterStream. | 45 : UTF16Buffer(), |
46 * It also maintains unlimited pushback capability, but optimized | 46 stream_(stream) { } |
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 | |
52 * 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 */ | |
56 explicit InputStreamUTF16Buffer(v8::UnicodeInputStream* stream) | |
57 : UC16CharacterStream(), | |
58 stream_(stream), | |
59 pushback_buffer_(buffer_), | |
60 pushback_buffer_end_cache_(NULL), | |
61 pushback_buffer_backing_(NULL), | |
62 pushback_buffer_backing_size_(0) { | |
63 buffer_cursor_ = buffer_end_ = buffer_ + kPushBackSize; | |
64 } | |
65 | 47 |
66 virtual ~InputStreamUTF16Buffer() { | 48 virtual ~InputStreamUTF16Buffer() { } |
67 if (pushback_buffer_backing_ != NULL) { | |
68 DeleteArray(pushback_buffer_backing_); | |
69 } | |
70 } | |
71 | 49 |
72 virtual void PushBack(uc16 ch) { | 50 virtual void PushBack(uc32 ch) { |
73 ASSERT(pos_ > 0); | 51 stream_->PushBack(ch); |
74 if (buffer_cursor_ <= pushback_buffer_) { | |
75 // No more room in the current buffer to do pushbacks. | |
76 if (pushback_buffer_end_cache_ == NULL) { | |
77 // We have overflowed the pushback space at the beginning of buffer_. | |
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 } | |
100 } | |
101 pushback_buffer_[buffer_cursor_ - pushback_buffer_- 1] = ch; | |
102 pos_--; | 52 pos_--; |
103 } | 53 } |
104 | 54 |
105 protected: | 55 virtual uc32 Advance() { |
106 virtual bool ReadBlock() { | 56 uc32 result = stream_->Next(); |
107 if (pushback_buffer_end_cache_ != NULL) { | 57 if (result >= 0) pos_++; |
108 buffer_cursor_ = buffer_; | 58 return result; |
109 buffer_end_ = pushback_buffer_end_cache_; | |
110 pushback_buffer_end_cache_ = NULL; | |
111 return buffer_end_ > buffer_cursor_; | |
112 } | |
113 // Copy the top of the buffer into the pushback area. | |
114 int32_t value; | |
115 uc16* buffer_start = buffer_ + kPushBackSize; | |
116 buffer_cursor_ = buffer_end_ = buffer_start; | |
117 while ((value = stream_->Next()) >= 0) { | |
118 if (value > static_cast<int32_t>(unibrow::Utf8::kMaxThreeByteChar)) { | |
119 value = unibrow::Utf8::kBadChar; | |
120 } | |
121 // buffer_end_ is a const pointer, but buffer_ is writable. | |
122 buffer_start[buffer_end_++ - buffer_start] = static_cast<uc16>(value); | |
123 if (buffer_end_ == buffer_ + kPushBackSize + kBufferSize) break; | |
124 } | |
125 return buffer_end_ > buffer_start; | |
126 } | 59 } |
127 | 60 |
128 virtual unsigned SlowSeekForward(unsigned pos) { | 61 virtual void SeekForward(int pos) { |
129 // Seeking in the input is not used by preparsing. | 62 // Seeking in the input is not used by preparsing. |
130 // It's only used by the real parser based on preparser data. | 63 // It's only used by the real parser based on preparser data. |
131 UNIMPLEMENTED(); | 64 UNIMPLEMENTED(); |
132 return 0; | |
133 } | 65 } |
134 | 66 |
135 private: | 67 private: |
136 static const unsigned kBufferSize = 512; | |
137 static const unsigned kPushBackSize = 16; | |
138 v8::UnicodeInputStream* const stream_; | 68 v8::UnicodeInputStream* const stream_; |
139 // Buffer holding first kPushBackSize characters of pushback buffer, | |
140 // then kBufferSize chars of read-ahead. | |
141 // The pushback buffer is only used if pushing back characters past | |
142 // the start of a block. | |
143 uc16 buffer_[kPushBackSize + kBufferSize]; | |
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_; | |
151 }; | 69 }; |
152 | 70 |
153 | 71 |
154 class StandAloneJavaScriptScanner : public JavaScriptScanner { | 72 class StandAloneJavaScriptScanner : public JavaScriptScanner { |
155 public: | 73 public: |
156 void Initialize(UC16CharacterStream* source) { | 74 void Initialize(UTF16Buffer* source) { |
157 source_ = source; | 75 source_ = source; |
158 literal_flags_ = kLiteralString | kLiteralIdentifier; | 76 literal_flags_ = kLiteralString | kLiteralIdentifier; |
159 Init(); | 77 Init(); |
160 // Skip initial whitespace allowing HTML comment ends just like | 78 // Skip initial whitespace allowing HTML comment ends just like |
161 // after a newline and scan first token. | 79 // after a newline and scan first token. |
162 has_line_terminator_before_next_ = true; | 80 has_line_terminator_before_next_ = true; |
163 SkipWhiteSpace(); | 81 SkipWhiteSpace(); |
164 Scan(); | 82 Scan(); |
165 } | 83 } |
166 }; | 84 }; |
167 | 85 |
168 | 86 |
169 // Functions declared by allocation.h | 87 // Functions declared by allocation.h |
170 | 88 |
171 void FatalProcessOutOfMemory(const char* reason) { | 89 void FatalProcessOutOfMemory(const char* reason) { |
172 V8_Fatal(__FILE__, __LINE__, reason); | 90 V8_Fatal(__FILE__, __LINE__, reason); |
173 } | 91 } |
174 | 92 |
175 bool EnableSlowAsserts() { return true; } | 93 bool EnableSlowAsserts() { return true; } |
176 | 94 |
| 95 |
177 } // namespace internal. | 96 } // namespace internal. |
178 | 97 |
179 | 98 |
180 UnicodeInputStream::~UnicodeInputStream() { } | 99 UnicodeInputStream::~UnicodeInputStream() { } |
181 | 100 |
182 | 101 |
183 PreParserData Preparse(UnicodeInputStream* input, size_t max_stack) { | 102 PreParserData Preparse(UnicodeInputStream* input, size_t max_stack) { |
184 internal::InputStreamUTF16Buffer buffer(input); | 103 internal::InputStreamUTF16Buffer buffer(input); |
185 uintptr_t stack_limit = reinterpret_cast<uintptr_t>(&buffer) - max_stack; | 104 uintptr_t stack_limit = reinterpret_cast<uintptr_t>(&buffer) - max_stack; |
186 internal::StandAloneJavaScriptScanner scanner; | 105 internal::StandAloneJavaScriptScanner scanner; |
(...skipping 13 matching lines...) Expand all Loading... |
200 return PreParserData(size, data); | 119 return PreParserData(size, data); |
201 } | 120 } |
202 | 121 |
203 } // namespace v8. | 122 } // namespace v8. |
204 | 123 |
205 | 124 |
206 // Used by ASSERT macros and other immediate exits. | 125 // Used by ASSERT macros and other immediate exits. |
207 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) { | 126 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) { |
208 exit(EXIT_FAILURE); | 127 exit(EXIT_FAILURE); |
209 } | 128 } |
OLD | NEW |