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 |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
| 28 #include <stdlib.h> |
28 #include <stdarg.h> | 29 #include <stdarg.h> |
29 #include "../include/v8stdint.h" | 30 #include "../include/v8stdint.h" |
30 #include "globals.h" | 31 #include "../include/v8-preparser.h" |
31 #include "checks.h" | 32 #include "unicode-inl.h" |
32 #include "allocation.h" | |
33 #include "utils.h" | |
34 #include "list.h" | |
35 #include "smart-pointer.h" | |
36 #include "scanner-base.h" | |
37 #include "preparse-data.h" | |
38 #include "preparser.h" | |
39 | 33 |
40 enum ResultCode { kSuccess = 0, kErrorReading = 1, kErrorWriting = 2 }; | 34 enum ResultCode { kSuccess = 0, kErrorReading = 1, kErrorWriting = 2 }; |
41 | 35 |
42 namespace v8 { | 36 namespace v8 { |
43 namespace internal { | 37 namespace internal { |
44 | 38 |
45 // THIS FILE IS PROOF-OF-CONCEPT ONLY. | 39 // THIS FILE IS PROOF-OF-CONCEPT ONLY. |
46 // The final goal is a stand-alone preparser library. | 40 // The final goal is a stand-alone preparser library. |
47 | 41 |
48 // UTF16Buffer based on an UTF-8 string in memory. | 42 |
49 class UTF8UTF16Buffer : public UTF16Buffer { | 43 class UTF8InputStream : public v8::UnicodeInputStream { |
50 public: | 44 public: |
51 UTF8UTF16Buffer(uint8_t* buffer, size_t length) | 45 UTF8InputStream(uint8_t* buffer, size_t length) |
52 : UTF16Buffer(), | 46 : buffer_(buffer), |
53 buffer_(buffer), | |
54 offset_(0), | 47 offset_(0), |
| 48 pos_(0), |
55 end_offset_(static_cast<int>(length)) { } | 49 end_offset_(static_cast<int>(length)) { } |
56 | 50 |
57 virtual void PushBack(uc32 ch) { | 51 virtual ~UTF8InputStream() { } |
| 52 |
| 53 virtual void PushBack(int32_t ch) { |
58 // Pushback assumes that the character pushed back is the | 54 // Pushback assumes that the character pushed back is the |
59 // one that was most recently read, and jumps back in the | 55 // one that was most recently read, and jumps back in the |
60 // UTF-8 stream by the length of that character's encoding. | 56 // UTF-8 stream by the length of that character's encoding. |
61 offset_ -= unibrow::Utf8::Length(ch); | 57 offset_ -= unibrow::Utf8::Length(ch); |
62 pos_--; | 58 pos_--; |
63 #ifdef DEBUG | 59 #ifdef DEBUG |
64 int tmp = 0; | 60 if (static_cast<unsigned>(ch) <= unibrow::Utf8::kMaxOneByteChar) { |
65 ASSERT_EQ(ch, unibrow::Utf8::ValueOf(buffer_ + offset_, | 61 if (ch != buffer_[offset_]) { |
66 end_offset_ - offset_, | 62 fprintf(stderr, "Invalid pushback: '%c'.", ch); |
67 &tmp); | 63 exit(1); |
| 64 } |
| 65 } else { |
| 66 unsigned tmp = 0; |
| 67 if (static_cast<unibrow::uchar>(ch) != |
| 68 unibrow::Utf8::CalculateValue(buffer_ + offset_, |
| 69 end_offset_ - offset_, |
| 70 &tmp)) { |
| 71 fprintf(stderr, "Invalid pushback: 0x%x.", ch); |
| 72 exit(1); |
| 73 } |
| 74 } |
68 #endif | 75 #endif |
69 } | 76 } |
70 | 77 |
71 virtual uc32 Advance() { | 78 virtual int32_t Next() { |
72 if (offset_ == end_offset_) return -1; | 79 if (offset_ == end_offset_) return -1; |
73 uint8_t first_char = buffer_[offset_]; | 80 uint8_t first_char = buffer_[offset_]; |
74 if (first_char <= unibrow::Utf8::kMaxOneByteChar) { | 81 if (first_char <= unibrow::Utf8::kMaxOneByteChar) { |
75 pos_++; | 82 pos_++; |
76 offset_++; | 83 offset_++; |
77 return static_cast<uc32>(first_char); | 84 return static_cast<int32_t>(first_char); |
78 } | 85 } |
79 unibrow::uchar codepoint = | 86 unibrow::uchar codepoint = |
80 unibrow::Utf8::CalculateValue(buffer_ + offset_, | 87 unibrow::Utf8::CalculateValue(buffer_ + offset_, |
81 end_offset_ - offset_, | 88 end_offset_ - offset_, |
82 &offset_); | 89 &offset_); |
83 pos_++; | 90 pos_++; |
84 return static_cast<uc32>(codepoint); | 91 return static_cast<int32_t>(codepoint); |
85 } | |
86 | |
87 virtual void SeekForward(int pos) { | |
88 while (pos_ < pos) { | |
89 uint8_t first_byte = buffer_[offset_++]; | |
90 while (first_byte & 0x80u && offset_ < end_offset_) { | |
91 offset_++; | |
92 first_byte <<= 1; | |
93 } | |
94 pos_++; | |
95 } | |
96 } | 92 } |
97 | 93 |
98 private: | 94 private: |
99 const uint8_t* buffer_; | 95 const uint8_t* buffer_; |
100 unsigned offset_; | 96 unsigned offset_; |
| 97 unsigned pos_; |
101 unsigned end_offset_; | 98 unsigned end_offset_; |
102 }; | 99 }; |
103 | 100 |
104 | 101 |
105 class StandAloneJavaScriptScanner : public JavaScriptScanner { | |
106 public: | |
107 void Initialize(UTF16Buffer* source) { | |
108 source_ = source; | |
109 literal_flags_ = kLiteralString | kLiteralIdentifier; | |
110 Init(); | |
111 // Skip initial whitespace allowing HTML comment ends just like | |
112 // after a newline and scan first token. | |
113 has_line_terminator_before_next_ = true; | |
114 SkipWhiteSpace(); | |
115 Scan(); | |
116 } | |
117 }; | |
118 | |
119 | |
120 // Write a number to dest in network byte order. | 102 // Write a number to dest in network byte order. |
121 void WriteUInt32(FILE* dest, uint32_t value, bool* ok) { | 103 void WriteUInt32(FILE* dest, uint32_t value, bool* ok) { |
122 for (int i = 3; i >= 0; i--) { | 104 for (int i = 3; i >= 0; i--) { |
123 uint8_t byte = static_cast<uint8_t>(value >> (i << 3)); | 105 uint8_t byte = static_cast<uint8_t>(value >> (i << 3)); |
124 int result = fputc(byte, dest); | 106 int result = fputc(byte, dest); |
125 if (result == EOF) { | 107 if (result == EOF) { |
126 *ok = false; | 108 *ok = false; |
127 return; | 109 return; |
128 } | 110 } |
129 } | 111 } |
(...skipping 13 matching lines...) Expand all Loading... |
143 return n; | 125 return n; |
144 } | 126 } |
145 | 127 |
146 | 128 |
147 bool ReadBuffer(FILE* source, void* buffer, size_t length) { | 129 bool ReadBuffer(FILE* source, void* buffer, size_t length) { |
148 size_t actually_read = fread(buffer, 1, length, stdin); | 130 size_t actually_read = fread(buffer, 1, length, stdin); |
149 return (actually_read == length); | 131 return (actually_read == length); |
150 } | 132 } |
151 | 133 |
152 | 134 |
153 bool WriteBuffer(FILE* dest, void* buffer, size_t length) { | 135 bool WriteBuffer(FILE* dest, const void* buffer, size_t length) { |
154 size_t actually_written = fwrite(buffer, 1, length, dest); | 136 size_t actually_written = fwrite(buffer, 1, length, dest); |
155 return (actually_written == length); | 137 return (actually_written == length); |
156 } | 138 } |
157 | 139 |
| 140 |
| 141 template <typename T> |
| 142 class ScopedPointer { |
| 143 public: |
| 144 explicit ScopedPointer(T* pointer) : pointer_(pointer) {} |
| 145 ~ScopedPointer() { delete[] pointer_; } |
| 146 T& operator[](int index) { return pointer_[index]; } |
| 147 T* operator*() { return pointer_ ;} |
| 148 private: |
| 149 T* pointer_; |
| 150 }; |
| 151 |
| 152 |
158 // Preparse stdin and output result on stdout. | 153 // Preparse stdin and output result on stdout. |
159 int PreParseIO() { | 154 int PreParseIO() { |
160 fprintf(stderr, "LOG: Enter parsing loop\n"); | 155 fprintf(stderr, "LOG: Enter parsing loop\n"); |
161 bool ok = true; | 156 bool ok = true; |
162 uint32_t length = ReadUInt32(stdin, &ok); | 157 uint32_t length = ReadUInt32(stdin, &ok); |
163 if (!ok) return kErrorReading; | 158 if (!ok) return kErrorReading; |
164 SmartPointer<byte> buffer(NewArray<byte>(length)); | 159 ScopedPointer<uint8_t> buffer(new uint8_t[length]); |
| 160 |
165 if (!ReadBuffer(stdin, *buffer, length)) { | 161 if (!ReadBuffer(stdin, *buffer, length)) { |
166 return kErrorReading; | 162 return kErrorReading; |
167 } | 163 } |
168 UTF8UTF16Buffer input_buffer(*buffer, static_cast<size_t>(length)); | 164 UTF8InputStream input_buffer(*buffer, static_cast<size_t>(length)); |
169 StandAloneJavaScriptScanner scanner; | |
170 scanner.Initialize(&input_buffer); | |
171 CompleteParserRecorder recorder; | |
172 preparser::PreParser preparser; | |
173 | 165 |
174 if (!preparser.PreParseProgram(&scanner, &recorder, true)) { | 166 v8::PreParserData data = |
175 if (scanner.stack_overflow()) { | 167 v8::Preparse(&input_buffer, 64 * sizeof(void*)); // NOLINT |
176 // Report stack overflow error/no-preparser-data. | 168 if (data.stack_overflow()) { |
177 WriteUInt32(stdout, 0, &ok); | 169 // Report stack overflow error/no-preparser-data. |
178 if (!ok) return kErrorWriting; | 170 WriteUInt32(stdout, 0, &ok); |
179 return 0; | 171 if (!ok) return kErrorWriting; |
180 } | 172 return 0; |
181 } | 173 } |
182 Vector<unsigned> pre_data = recorder.ExtractData(); | |
183 | 174 |
184 uint32_t size = static_cast<uint32_t>(pre_data.length() * sizeof(uint32_t)); | 175 uint32_t size = data.size(); |
185 WriteUInt32(stdout, size, &ok); | 176 WriteUInt32(stdout, size, &ok); |
186 if (!ok) return kErrorWriting; | 177 if (!ok) return kErrorWriting; |
187 if (!WriteBuffer(stdout, | 178 if (!WriteBuffer(stdout, data.data(), size)) { |
188 reinterpret_cast<byte*>(pre_data.start()), | |
189 size)) { | |
190 return kErrorWriting; | 179 return kErrorWriting; |
191 } | 180 } |
192 return 0; | 181 return 0; |
193 } | 182 } |
194 | 183 |
195 // Functions declared by allocation.h | |
196 | |
197 void FatalProcessOutOfMemory(const char* location) { | |
198 V8_Fatal("", 0, location); | |
199 } | |
200 | |
201 bool EnableSlowAsserts() { return true; } | |
202 | |
203 } } // namespace v8::internal | 184 } } // namespace v8::internal |
204 | 185 |
205 | 186 |
206 int main(int argc, char* argv[]) { | 187 int main(int argc, char* argv[]) { |
207 int status = 0; | 188 int status = 0; |
208 do { | 189 do { |
209 status = v8::internal::PreParseIO(); | 190 status = v8::internal::PreParseIO(); |
210 } while (status == 0); | 191 } while (status == 0); |
211 fprintf(stderr, "EXIT: Failure %d\n", status); | 192 fprintf(stderr, "EXIT: Failure %d\n", status); |
212 return EXIT_FAILURE; | 193 return EXIT_FAILURE; |
213 } | 194 } |
214 | |
215 | |
216 // Fatal error handling declared by checks.h. | |
217 | |
218 extern "C" void V8_Fatal(const char* file, int line, const char* format, ...) { | |
219 fflush(stdout); | |
220 fflush(stderr); | |
221 va_list arguments; | |
222 va_start(arguments, format); | |
223 vfprintf(stderr, format, arguments); | |
224 va_end(arguments); | |
225 fputs("\n#\n\n", stderr); | |
226 exit(EXIT_FAILURE); | |
227 } | |
OLD | NEW |