OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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. |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 uint32_t size = data->size(); | 196 uint32_t size = data->size(); |
197 fprintf(stderr, "LOG: data size: %u\n", size); | 197 fprintf(stderr, "LOG: data size: %u\n", size); |
198 if (!WriteBuffer(stdout, data->data(), size)) { | 198 if (!WriteBuffer(stdout, data->data(), size)) { |
199 perror("ERROR: Writing data"); | 199 perror("ERROR: Writing data"); |
200 fflush(stderr); | 200 fflush(stderr); |
201 } | 201 } |
202 exit(EXIT_FAILURE); | 202 exit(EXIT_FAILURE); |
203 }; | 203 }; |
204 | 204 |
205 | 205 |
| 206 bool IsFlag(const char* arg) { |
| 207 // Anything starting with '-' is considered a flag. |
| 208 // It's summarily ignored for now. |
| 209 return arg[0] == '-'; |
| 210 } |
| 211 |
| 212 |
| 213 struct ExceptionExpectation { |
| 214 ExceptionExpectation() |
| 215 : throws(false), type(NULL), beg_pos(-1), end_pos(-1) { } |
| 216 bool throws; |
| 217 const char* type; |
| 218 int beg_pos; |
| 219 int end_pos; |
| 220 }; |
| 221 |
| 222 |
206 void CheckException(v8::PreParserData* data, | 223 void CheckException(v8::PreParserData* data, |
207 bool throws, | 224 ExceptionExpectation* expects) { |
208 const char* message, | |
209 int beg_pos, | |
210 int end_pos) { | |
211 PreparseDataInterpreter reader(data->data(), data->size()); | 225 PreparseDataInterpreter reader(data->data(), data->size()); |
212 if (throws) { | 226 if (expects->throws) { |
213 if (!reader.throws()) { | 227 if (!reader.throws()) { |
214 if (message == NULL) { | 228 if (expects->type == NULL) { |
215 fail(data, "Didn't throw as expected\n"); | 229 fail(data, "Didn't throw as expected\n"); |
216 } else { | 230 } else { |
217 fail(data, "Didn't throw \"%s\" as expected\n", message); | 231 fail(data, "Didn't throw \"%s\" as expected\n", expects->type); |
218 } | 232 } |
219 } | 233 } |
220 if (message != NULL) { | 234 if (expects->type != NULL) { |
221 const char* actual_message = reader.message(); | 235 const char* actual_message = reader.message(); |
222 if (strcmp(message, actual_message)) { | 236 if (strcmp(expects->type, actual_message)) { |
223 fail(data, "Wrong error message. Expected <%s>, found <%s>\n", | 237 fail(data, "Wrong error message. Expected <%s>, found <%s>\n", |
224 message, actual_message); | 238 expects->type, actual_message); |
225 } | 239 } |
226 } | 240 } |
227 if (beg_pos >= 0) { | 241 if (expects->beg_pos >= 0) { |
228 if (beg_pos != reader.beg_pos()) { | 242 if (expects->beg_pos != reader.beg_pos()) { |
229 fail(data, "Wrong error start position: Expected %i, found %i\n", | 243 fail(data, "Wrong error start position: Expected %i, found %i\n", |
230 beg_pos, reader.beg_pos()); | 244 expects->beg_pos, reader.beg_pos()); |
231 } | 245 } |
232 } | 246 } |
233 if (end_pos >= 0) { | 247 if (expects->end_pos >= 0) { |
234 if (end_pos != reader.end_pos()) { | 248 if (expects->end_pos != reader.end_pos()) { |
235 fail(data, "Wrong error end position: Expected %i, found %i\n", | 249 fail(data, "Wrong error end position: Expected %i, found %i\n", |
236 end_pos, reader.end_pos()); | 250 expects->end_pos, reader.end_pos()); |
237 } | 251 } |
238 } | 252 } |
239 } else if (reader.throws()) { | 253 } else if (reader.throws()) { |
240 const char* message = reader.message(); | 254 const char* message = reader.message(); |
241 fail(data, "Throws unexpectedly with message: %s\n", | 255 fail(data, "Throws unexpectedly with message: %s\n", message); |
242 message); | |
243 } | 256 } |
244 } | 257 } |
245 | 258 |
246 int main(int argc, char* argv[]) { | 259 |
247 // Check for filename argument. | 260 ExceptionExpectation ParseExpectation(int argc, const char* argv[]) { |
248 if (argc < 2) { | 261 ExceptionExpectation expects; |
| 262 |
| 263 // Parse exception expectations from (the remainder of) the command line. |
| 264 int arg_index = 0; |
| 265 // Skip any flags. |
| 266 while (argc > arg_index && IsFlag(argv[arg_index])) arg_index++; |
| 267 if (argc > arg_index) { |
| 268 if (strncmp("throws", argv[arg_index], 7)) { |
| 269 // First argument after filename, if present, must be the verbatim |
| 270 // "throws", marking that the preparsing should fail with an exception. |
| 271 fail(NULL, "ERROR: Extra arguments not prefixed by \"throws\".\n"); |
| 272 } |
| 273 expects.throws = true; |
| 274 do { |
| 275 arg_index++; |
| 276 } while (argc > arg_index && IsFlag(argv[arg_index])); |
| 277 if (argc > arg_index) { |
| 278 // Next argument is the exception type identifier. |
| 279 expects.type = argv[arg_index]; |
| 280 do { |
| 281 arg_index++; |
| 282 } while (argc > arg_index && IsFlag(argv[arg_index])); |
| 283 if (argc > arg_index) { |
| 284 expects.beg_pos = atoi(argv[arg_index]); |
| 285 do { |
| 286 arg_index++; |
| 287 } while (argc > arg_index && IsFlag(argv[arg_index])); |
| 288 if (argc > arg_index) { |
| 289 expects.end_pos = atoi(argv[arg_index]); |
| 290 } |
| 291 } |
| 292 } |
| 293 } |
| 294 return expects; |
| 295 } |
| 296 |
| 297 |
| 298 int main(int argc, const char* argv[]) { |
| 299 // Parse command line. |
| 300 // Format: preparser <scriptfile> ["throws" [<exn-type> [<start> [<end>]]]] |
| 301 // Any flags on the line are ignored. |
| 302 |
| 303 // Check for mandatory filename argument. |
| 304 int arg_index = 1; |
| 305 while (argc > arg_index && IsFlag(argv[arg_index])) arg_index++; |
| 306 if (argc <= arg_index) { |
249 fail(NULL, "ERROR: No filename on command line.\n"); | 307 fail(NULL, "ERROR: No filename on command line.\n"); |
250 } | 308 } |
251 const char* filename = argv[1]; | 309 const char* filename = argv[arg_index]; |
252 | 310 // Check remainder of command line for exception expectations. |
253 // Parse expectations. | 311 arg_index++; |
254 bool throws = false; | 312 ExceptionExpectation expects = |
255 const char* throws_message = NULL; | 313 ParseExpectation(argc - arg_index, argv + arg_index); |
256 int throws_beg_pos = -1; | |
257 int throws_end_pos = -1; | |
258 // Check for throws argument. | |
259 if (argc > 2) { | |
260 if (strncmp("throws", argv[2], 6)) { | |
261 fail(NULL, "ERROR: Extra arguments not prefixed by \"throws\".\n"); | |
262 } | |
263 throws = true; | |
264 if (argc > 3) { | |
265 throws_message = argv[3]; | |
266 } | |
267 if (argc > 4) { | |
268 throws_beg_pos = atoi(argv[4]); | |
269 } | |
270 if (argc > 5) { | |
271 throws_end_pos = atoi(argv[5]); | |
272 } | |
273 } | |
274 | 314 |
275 // Open JS file. | 315 // Open JS file. |
276 FILE* input = fopen(filename, "rb"); | 316 FILE* input = fopen(filename, "rb"); |
277 if (input == NULL) { | 317 if (input == NULL) { |
278 perror("ERROR: Error opening file"); | 318 perror("ERROR: Error opening file"); |
279 fflush(stderr); | 319 fflush(stderr); |
280 return EXIT_FAILURE; | 320 return EXIT_FAILURE; |
281 } | 321 } |
282 | 322 |
283 // Find length of JS file. | 323 // Find length of JS file. |
(...skipping 19 matching lines...) Expand all Loading... |
303 size_t kMaxStackSize = 64 * 1024 * sizeof(void*); // NOLINT | 343 size_t kMaxStackSize = 64 * 1024 * sizeof(void*); // NOLINT |
304 v8::PreParserData data = v8::Preparse(&input_buffer, kMaxStackSize); | 344 v8::PreParserData data = v8::Preparse(&input_buffer, kMaxStackSize); |
305 | 345 |
306 // Fail if stack overflow. | 346 // Fail if stack overflow. |
307 if (data.stack_overflow()) { | 347 if (data.stack_overflow()) { |
308 fail(&data, "ERROR: Stack overflow\n"); | 348 fail(&data, "ERROR: Stack overflow\n"); |
309 } | 349 } |
310 | 350 |
311 // Check that the expected exception is thrown, if an exception is | 351 // Check that the expected exception is thrown, if an exception is |
312 // expected. | 352 // expected. |
313 CheckException(&data, throws, throws_message, | 353 CheckException(&data, &expects); |
314 throws_beg_pos, throws_end_pos); | |
315 | 354 |
316 return EXIT_SUCCESS; | 355 return EXIT_SUCCESS; |
317 } | 356 } |
OLD | NEW |