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 <stdlib.h> |
29 #include <stdarg.h> | 29 #include <stdarg.h> |
30 #include <stdio.h> | |
31 | |
32 #include "../include/v8stdint.h" | 30 #include "../include/v8stdint.h" |
33 #include "../include/v8-preparser.h" | 31 #include "../include/v8-preparser.h" |
| 32 #include "unicode-inl.h" |
34 | 33 |
35 // This file is only used for testing the stand-alone preparser | 34 enum ResultCode { kSuccess = 0, kErrorReading = 1, kErrorWriting = 2 }; |
36 // library. | 35 |
37 // The first (and only) argument must be the path of a JavaScript file. | 36 namespace v8 { |
38 // This file is preparsed and the resulting preparser data is written | 37 namespace internal { |
39 // to stdout. Diagnostic output is output on stderr. | 38 |
40 // The file must contain only ASCII characters (UTF-8 isn't supported). | 39 // THIS FILE IS PROOF-OF-CONCEPT ONLY. |
41 // The file is read into memory, so it should have a reasonable size. | 40 // The final goal is a stand-alone preparser library. |
42 | 41 |
43 | 42 |
44 // Adapts an ASCII string to the UnicodeInputStream interface. | 43 class UTF8InputStream : public v8::UnicodeInputStream { |
45 class AsciiInputStream : public v8::UnicodeInputStream { | |
46 public: | 44 public: |
47 AsciiInputStream(uint8_t* buffer, size_t length) | 45 UTF8InputStream(uint8_t* buffer, size_t length) |
48 : buffer_(buffer), | 46 : buffer_(buffer), |
49 end_offset_(static_cast<int>(length)), | 47 offset_(0), |
50 offset_(0) { } | 48 pos_(0), |
| 49 end_offset_(static_cast<int>(length)) { } |
51 | 50 |
52 virtual ~AsciiInputStream() { } | 51 virtual ~UTF8InputStream() { } |
53 | 52 |
54 virtual void PushBack(int32_t ch) { | 53 virtual void PushBack(int32_t ch) { |
55 offset_--; | 54 // Pushback assumes that the character pushed back is the |
| 55 // one that was most recently read, and jumps back in the |
| 56 // UTF-8 stream by the length of that character's encoding. |
| 57 offset_ -= unibrow::Utf8::Length(ch); |
| 58 pos_--; |
56 #ifdef DEBUG | 59 #ifdef DEBUG |
57 if (offset_ < 0 || | 60 if (static_cast<unsigned>(ch) <= unibrow::Utf8::kMaxOneByteChar) { |
58 (ch != ((offset_ >= end_offset_) ? -1 : buffer_[offset_]))) { | 61 if (ch != buffer_[offset_]) { |
59 fprintf(stderr, "Invalid pushback: '%c' at offset %d.", ch, offset_); | 62 fprintf(stderr, "Invalid pushback: '%c'.", ch); |
60 exit(1); | 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 } |
61 } | 74 } |
62 #endif | 75 #endif |
63 } | 76 } |
64 | 77 |
65 virtual int32_t Next() { | 78 virtual int32_t Next() { |
66 if (offset_ >= end_offset_) { | 79 if (offset_ == end_offset_) return -1; |
67 offset_++; // Increment anyway to allow symmetric pushbacks. | 80 uint8_t first_char = buffer_[offset_]; |
68 return -1; | 81 if (first_char <= unibrow::Utf8::kMaxOneByteChar) { |
| 82 pos_++; |
| 83 offset_++; |
| 84 return static_cast<int32_t>(first_char); |
69 } | 85 } |
70 uint8_t next_char = buffer_[offset_]; | 86 unibrow::uchar codepoint = |
71 #ifdef DEBUG | 87 unibrow::Utf8::CalculateValue(buffer_ + offset_, |
72 if (next_char > 0x7fu) { | 88 end_offset_ - offset_, |
73 fprintf(stderr, "Non-ASCII character in input: '%c'.", next_char); | 89 &offset_); |
74 exit(1); | 90 pos_++; |
75 } | 91 return static_cast<int32_t>(codepoint); |
76 #endif | |
77 offset_++; | |
78 return static_cast<int32_t>(next_char); | |
79 } | 92 } |
80 | 93 |
81 private: | 94 private: |
82 const uint8_t* buffer_; | 95 const uint8_t* buffer_; |
83 const int end_offset_; | 96 unsigned offset_; |
84 int offset_; | 97 unsigned pos_; |
| 98 unsigned end_offset_; |
85 }; | 99 }; |
86 | 100 |
87 | 101 |
| 102 // Write a number to dest in network byte order. |
| 103 void WriteUInt32(FILE* dest, uint32_t value, bool* ok) { |
| 104 for (int i = 3; i >= 0; i--) { |
| 105 uint8_t byte = static_cast<uint8_t>(value >> (i << 3)); |
| 106 int result = fputc(byte, dest); |
| 107 if (result == EOF) { |
| 108 *ok = false; |
| 109 return; |
| 110 } |
| 111 } |
| 112 } |
| 113 |
| 114 // Read number from FILE* in network byte order. |
| 115 uint32_t ReadUInt32(FILE* source, bool* ok) { |
| 116 uint32_t n = 0; |
| 117 for (int i = 0; i < 4; i++) { |
| 118 int c = fgetc(source); |
| 119 if (c == EOF) { |
| 120 *ok = false; |
| 121 return 0; |
| 122 } |
| 123 n = (n << 8) + static_cast<uint32_t>(c); |
| 124 } |
| 125 return n; |
| 126 } |
| 127 |
| 128 |
88 bool ReadBuffer(FILE* source, void* buffer, size_t length) { | 129 bool ReadBuffer(FILE* source, void* buffer, size_t length) { |
89 size_t actually_read = fread(buffer, 1, length, source); | 130 size_t actually_read = fread(buffer, 1, length, source); |
90 return (actually_read == length); | 131 return (actually_read == length); |
91 } | 132 } |
92 | 133 |
93 | 134 |
94 bool WriteBuffer(FILE* dest, const void* buffer, size_t length) { | 135 bool WriteBuffer(FILE* dest, const void* buffer, size_t length) { |
95 size_t actually_written = fwrite(buffer, 1, length, dest); | 136 size_t actually_written = fwrite(buffer, 1, length, dest); |
96 return (actually_written == length); | 137 return (actually_written == length); |
97 } | 138 } |
98 | 139 |
99 | 140 |
100 template <typename T> | 141 template <typename T> |
101 class ScopedPointer { | 142 class ScopedPointer { |
102 public: | 143 public: |
103 explicit ScopedPointer(T* pointer) : pointer_(pointer) {} | 144 explicit ScopedPointer(T* pointer) : pointer_(pointer) {} |
104 ~ScopedPointer() { delete[] pointer_; } | 145 ~ScopedPointer() { delete[] pointer_; } |
105 T& operator[](int index) { return pointer_[index]; } | 146 T& operator[](int index) { return pointer_[index]; } |
106 T* operator*() { return pointer_ ;} | 147 T* operator*() { return pointer_ ;} |
107 private: | 148 private: |
108 T* pointer_; | 149 T* pointer_; |
109 }; | 150 }; |
110 | 151 |
111 | 152 |
112 int main(int argc, char* argv[]) { | 153 // Preparse input and output result on stdout. |
113 // Check for filename argument. | 154 int PreParseIO(FILE* input) { |
114 if (argc < 2) { | 155 fprintf(stderr, "LOG: Enter parsing loop\n"); |
115 fprintf(stderr, "ERROR: No filename on command line.\n"); | 156 bool ok = true; |
| 157 uint32_t length = ReadUInt32(input, &ok); |
| 158 fprintf(stderr, "LOG: Input length: %d\n", length); |
| 159 if (!ok) return kErrorReading; |
| 160 ScopedPointer<uint8_t> buffer(new uint8_t[length]); |
| 161 |
| 162 if (!ReadBuffer(input, *buffer, length)) { |
| 163 return kErrorReading; |
| 164 } |
| 165 UTF8InputStream input_buffer(*buffer, static_cast<size_t>(length)); |
| 166 |
| 167 v8::PreParserData data = |
| 168 v8::Preparse(&input_buffer, 64 * 1024 * sizeof(void*)); // NOLINT |
| 169 if (data.stack_overflow()) { |
| 170 fprintf(stderr, "LOG: Stack overflow\n"); |
116 fflush(stderr); | 171 fflush(stderr); |
117 return EXIT_FAILURE; | 172 // Report stack overflow error/no-preparser-data. |
118 } | 173 WriteUInt32(stdout, 0, &ok); |
119 const char* filename = argv[1]; | 174 if (!ok) return kErrorWriting; |
120 | 175 return 0; |
121 // Open JS file. | |
122 FILE* input = fopen(filename, "rb"); | |
123 if (input == NULL) { | |
124 perror("ERROR: Error opening file"); | |
125 fflush(stderr); | |
126 return EXIT_FAILURE; | |
127 } | 176 } |
128 | 177 |
129 // Find length of JS file. | |
130 if (fseek(input, 0, SEEK_END) != 0) { | |
131 perror("ERROR: Error during seek"); | |
132 fflush(stderr); | |
133 return EXIT_FAILURE; | |
134 } | |
135 size_t length = static_cast<size_t>(ftell(input)); | |
136 rewind(input); | |
137 | |
138 // Read JS file into memory buffer. | |
139 ScopedPointer<uint8_t> buffer(new uint8_t[length]); | |
140 if (!ReadBuffer(input, *buffer, length)) { | |
141 perror("ERROR: Reading file"); | |
142 fflush(stderr); | |
143 return EXIT_FAILURE; | |
144 } | |
145 fclose(input); | |
146 | |
147 // Preparse input file. | |
148 AsciiInputStream input_buffer(*buffer, length); | |
149 size_t kMaxStackSize = 64 * 1024 * sizeof(void*); // NOLINT | |
150 v8::PreParserData data = v8::Preparse(&input_buffer, kMaxStackSize); | |
151 | |
152 // Fail if stack overflow. | |
153 if (data.stack_overflow()) { | |
154 fprintf(stderr, "ERROR: Stack overflow\n"); | |
155 fflush(stderr); | |
156 return EXIT_FAILURE; | |
157 } | |
158 | |
159 // Print preparser data to stdout. | |
160 uint32_t size = data.size(); | 178 uint32_t size = data.size(); |
161 fprintf(stderr, "LOG: Success, data size: %u\n", size); | 179 fprintf(stderr, "LOG: Success, data size: %u\n", size); |
162 fflush(stderr); | 180 fflush(stderr); |
| 181 WriteUInt32(stdout, size, &ok); |
| 182 if (!ok) return kErrorWriting; |
163 if (!WriteBuffer(stdout, data.data(), size)) { | 183 if (!WriteBuffer(stdout, data.data(), size)) { |
164 perror("ERROR: Writing data"); | 184 return kErrorWriting; |
165 return EXIT_FAILURE; | |
166 } | 185 } |
| 186 return 0; |
| 187 } |
167 | 188 |
168 return EXIT_SUCCESS; | 189 } } // namespace v8::internal |
| 190 |
| 191 |
| 192 int main(int argc, char* argv[]) { |
| 193 FILE* input = stdin; |
| 194 if (argc > 1) { |
| 195 char* arg = argv[1]; |
| 196 input = fopen(arg, "rb"); |
| 197 if (input == NULL) return EXIT_FAILURE; |
| 198 } |
| 199 int status = 0; |
| 200 do { |
| 201 status = v8::internal::PreParseIO(input); |
| 202 } while (status == 0); |
| 203 fprintf(stderr, "EXIT: Failure %d\n", status); |
| 204 fflush(stderr); |
| 205 return EXIT_FAILURE; |
169 } | 206 } |
OLD | NEW |