| 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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 *ok = false; | 120 *ok = false; |
| 121 return 0; | 121 return 0; |
| 122 } | 122 } |
| 123 n = (n << 8) + static_cast<uint32_t>(c); | 123 n = (n << 8) + static_cast<uint32_t>(c); |
| 124 } | 124 } |
| 125 return n; | 125 return n; |
| 126 } | 126 } |
| 127 | 127 |
| 128 | 128 |
| 129 bool ReadBuffer(FILE* source, void* buffer, size_t length) { | 129 bool ReadBuffer(FILE* source, void* buffer, size_t length) { |
| 130 size_t actually_read = fread(buffer, 1, length, stdin); | 130 size_t actually_read = fread(buffer, 1, length, source); |
| 131 return (actually_read == length); | 131 return (actually_read == length); |
| 132 } | 132 } |
| 133 | 133 |
| 134 | 134 |
| 135 bool WriteBuffer(FILE* dest, const void* buffer, size_t length) { | 135 bool WriteBuffer(FILE* dest, const void* buffer, size_t length) { |
| 136 size_t actually_written = fwrite(buffer, 1, length, dest); | 136 size_t actually_written = fwrite(buffer, 1, length, dest); |
| 137 return (actually_written == length); | 137 return (actually_written == length); |
| 138 } | 138 } |
| 139 | 139 |
| 140 | 140 |
| 141 template <typename T> | 141 template <typename T> |
| 142 class ScopedPointer { | 142 class ScopedPointer { |
| 143 public: | 143 public: |
| 144 explicit ScopedPointer(T* pointer) : pointer_(pointer) {} | 144 explicit ScopedPointer(T* pointer) : pointer_(pointer) {} |
| 145 ~ScopedPointer() { delete[] pointer_; } | 145 ~ScopedPointer() { delete[] pointer_; } |
| 146 T& operator[](int index) { return pointer_[index]; } | 146 T& operator[](int index) { return pointer_[index]; } |
| 147 T* operator*() { return pointer_ ;} | 147 T* operator*() { return pointer_ ;} |
| 148 private: | 148 private: |
| 149 T* pointer_; | 149 T* pointer_; |
| 150 }; | 150 }; |
| 151 | 151 |
| 152 | 152 |
| 153 // Preparse stdin and output result on stdout. | 153 // Preparse input and output result on stdout. |
| 154 int PreParseIO() { | 154 int PreParseIO(FILE* input) { |
| 155 fprintf(stderr, "LOG: Enter parsing loop\n"); | 155 fprintf(stderr, "LOG: Enter parsing loop\n"); |
| 156 bool ok = true; | 156 bool ok = true; |
| 157 uint32_t length = ReadUInt32(stdin, &ok); | 157 uint32_t length = ReadUInt32(input, &ok); |
| 158 fprintf(stderr, "LOG: Input length: %d\n", length); |
| 158 if (!ok) return kErrorReading; | 159 if (!ok) return kErrorReading; |
| 159 ScopedPointer<uint8_t> buffer(new uint8_t[length]); | 160 ScopedPointer<uint8_t> buffer(new uint8_t[length]); |
| 160 | 161 |
| 161 if (!ReadBuffer(stdin, *buffer, length)) { | 162 if (!ReadBuffer(input, *buffer, length)) { |
| 162 return kErrorReading; | 163 return kErrorReading; |
| 163 } | 164 } |
| 164 UTF8InputStream input_buffer(*buffer, static_cast<size_t>(length)); | 165 UTF8InputStream input_buffer(*buffer, static_cast<size_t>(length)); |
| 165 | 166 |
| 166 v8::PreParserData data = | 167 v8::PreParserData data = |
| 167 v8::Preparse(&input_buffer, 64 * sizeof(void*)); // NOLINT | 168 v8::Preparse(&input_buffer, 64 * 1024 * sizeof(void*)); // NOLINT |
| 168 if (data.stack_overflow()) { | 169 if (data.stack_overflow()) { |
| 170 fprintf(stderr, "LOG: Stack overflow\n"); |
| 171 fflush(stderr); |
| 169 // Report stack overflow error/no-preparser-data. | 172 // Report stack overflow error/no-preparser-data. |
| 170 WriteUInt32(stdout, 0, &ok); | 173 WriteUInt32(stdout, 0, &ok); |
| 171 if (!ok) return kErrorWriting; | 174 if (!ok) return kErrorWriting; |
| 172 return 0; | 175 return 0; |
| 173 } | 176 } |
| 174 | 177 |
| 175 uint32_t size = data.size(); | 178 uint32_t size = data.size(); |
| 179 fprintf(stderr, "LOG: Success, data size: %u\n", size); |
| 180 fflush(stderr); |
| 176 WriteUInt32(stdout, size, &ok); | 181 WriteUInt32(stdout, size, &ok); |
| 177 if (!ok) return kErrorWriting; | 182 if (!ok) return kErrorWriting; |
| 178 if (!WriteBuffer(stdout, data.data(), size)) { | 183 if (!WriteBuffer(stdout, data.data(), size)) { |
| 179 return kErrorWriting; | 184 return kErrorWriting; |
| 180 } | 185 } |
| 181 return 0; | 186 return 0; |
| 182 } | 187 } |
| 183 | 188 |
| 184 } } // namespace v8::internal | 189 } } // namespace v8::internal |
| 185 | 190 |
| 186 | 191 |
| 187 int main(int argc, char* argv[]) { | 192 int main(int argc, char* argv[]) { |
| 193 FILE* input = stdin; |
| 194 if (argc > 1) { |
| 195 char* arg = argv[1]; |
| 196 FILE* file = fopen(arg, "rb"); |
| 197 if (!file) return EXIT_FAILURE; |
| 198 } |
| 188 int status = 0; | 199 int status = 0; |
| 189 do { | 200 do { |
| 190 status = v8::internal::PreParseIO(); | 201 status = v8::internal::PreParseIO(input); |
| 191 } while (status == 0); | 202 } while (status == 0); |
| 192 fprintf(stderr, "EXIT: Failure %d\n", status); | 203 fprintf(stderr, "EXIT: Failure %d\n", status); |
| 204 fflush(stderr); |
| 193 return EXIT_FAILURE; | 205 return EXIT_FAILURE; |
| 194 } | 206 } |
| OLD | NEW |