OLD | NEW |
---|---|
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <cstring> | 5 #include <cstring> |
6 #include <fstream> | 6 #include <fstream> |
7 #include <vector> | |
7 | 8 |
8 #include "test/cctest/interpreter/bytecode-expectations-printer.h" | 9 #include "test/cctest/interpreter/bytecode-expectations-printer.h" |
9 | 10 |
10 #include "include/libplatform/libplatform.h" | 11 #include "include/libplatform/libplatform.h" |
11 #include "include/v8.h" | 12 #include "include/v8.h" |
12 | 13 |
13 #include "src/base/logging.h" | 14 #include "src/base/logging.h" |
14 #include "src/base/smart-pointers.h" | 15 #include "src/base/smart-pointers.h" |
15 #include "src/compiler.h" | 16 #include "src/compiler.h" |
16 #include "src/interpreter/interpreter.h" | 17 #include "src/interpreter/interpreter.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
50 } | 51 } |
51 bool rebaseline() const { return rebaseline_; } | 52 bool rebaseline() const { return rebaseline_; } |
52 bool wrap() const { return wrap_; } | 53 bool wrap() const { return wrap_; } |
53 bool execute() const { return execute_; } | 54 bool execute() const { return execute_; } |
54 bool top_level() const { return top_level_; } | 55 bool top_level() const { return top_level_; } |
55 bool legacy_const() const { return legacy_const_; } | 56 bool legacy_const() const { return legacy_const_; } |
56 bool do_expressions() const { return do_expressions_; } | 57 bool do_expressions() const { return do_expressions_; } |
57 BytecodeExpectationsPrinter::ConstantPoolType const_pool_type() const { | 58 BytecodeExpectationsPrinter::ConstantPoolType const_pool_type() const { |
58 return const_pool_type_; | 59 return const_pool_type_; |
59 } | 60 } |
60 std::string input_filename() const { return input_filename_; } | 61 std::vector<std::string> input_filenames() const { return input_filenames_; } |
61 std::string output_filename() const { return output_filename_; } | 62 std::string output_filename() const { return output_filename_; } |
62 std::string test_function_name() const { return test_function_name_; } | 63 std::string test_function_name() const { return test_function_name_; } |
63 | 64 |
64 private: | 65 private: |
65 bool parsing_failed_; | 66 bool parsing_failed_; |
66 bool print_help_; | 67 bool print_help_; |
67 bool read_raw_js_snippet_; | 68 bool read_raw_js_snippet_; |
68 bool read_from_stdin_; | 69 bool read_from_stdin_; |
69 bool rebaseline_; | 70 bool rebaseline_; |
70 bool wrap_; | 71 bool wrap_; |
71 bool execute_; | 72 bool execute_; |
72 bool top_level_; | 73 bool top_level_; |
73 bool legacy_const_; | 74 bool legacy_const_; |
74 bool do_expressions_; | 75 bool do_expressions_; |
75 BytecodeExpectationsPrinter::ConstantPoolType const_pool_type_; | 76 BytecodeExpectationsPrinter::ConstantPoolType const_pool_type_; |
76 std::string input_filename_; | 77 std::vector<std::string> input_filenames_; |
77 std::string output_filename_; | 78 std::string output_filename_; |
78 std::string test_function_name_; | 79 std::string test_function_name_; |
79 }; | 80 }; |
80 | 81 |
81 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator { | 82 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator { |
82 public: | 83 public: |
83 void* Allocate(size_t length) override { | 84 void* Allocate(size_t length) override { |
84 void* data = AllocateUninitialized(length); | 85 void* data = AllocateUninitialized(length); |
85 if (data != nullptr) memset(data, 0, length); | 86 if (data != nullptr) memset(data, 0, length); |
86 return data; | 87 return data; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 options.top_level_ = true; | 168 options.top_level_ = true; |
168 } else if (strcmp(argv[i], "--legacy-const") == 0) { | 169 } else if (strcmp(argv[i], "--legacy-const") == 0) { |
169 options.legacy_const_ = true; | 170 options.legacy_const_ = true; |
170 } else if (strcmp(argv[i], "--do-expressions") == 0) { | 171 } else if (strcmp(argv[i], "--do-expressions") == 0) { |
171 options.do_expressions_ = true; | 172 options.do_expressions_ = true; |
172 } else if (strncmp(argv[i], "--output=", 9) == 0) { | 173 } else if (strncmp(argv[i], "--output=", 9) == 0) { |
173 options.output_filename_ = argv[i] + 9; | 174 options.output_filename_ = argv[i] + 9; |
174 } else if (strncmp(argv[i], "--test-function-name=", 21) == 0) { | 175 } else if (strncmp(argv[i], "--test-function-name=", 21) == 0) { |
175 options.test_function_name_ = argv[i] + 21; | 176 options.test_function_name_ = argv[i] + 21; |
176 } else if (strncmp(argv[i], "--", 2) != 0) { // It doesn't start with -- | 177 } else if (strncmp(argv[i], "--", 2) != 0) { // It doesn't start with -- |
177 if (!options.input_filename_.empty()) { | 178 options.input_filenames_.push_back(argv[i]); |
178 std::cerr << "ERROR: More than one input file specified\n"; | |
179 options.parsing_failed_ = true; | |
180 break; | |
181 } | |
182 options.input_filename_ = argv[i]; | |
183 } else { | 179 } else { |
184 std::cerr << "ERROR: Unknonwn option " << argv[i] << "\n"; | 180 std::cerr << "ERROR: Unknonwn option " << argv[i] << "\n"; |
185 options.parsing_failed_ = true; | 181 options.parsing_failed_ = true; |
186 break; | 182 break; |
187 } | 183 } |
188 } | 184 } |
189 | 185 |
190 return options; | 186 return options; |
191 } | 187 } |
192 | 188 |
193 bool ProgramOptions::Validate() const { | 189 bool ProgramOptions::Validate() const { |
194 if (parsing_failed_) return false; | 190 if (parsing_failed_) return false; |
195 if (print_help_) return true; | 191 if (print_help_) return true; |
196 | 192 |
197 if (const_pool_type_ == | 193 if (const_pool_type_ == |
198 BytecodeExpectationsPrinter::ConstantPoolType::kUnknown) { | 194 BytecodeExpectationsPrinter::ConstantPoolType::kUnknown) { |
199 std::cerr << "ERROR: Unknown constant pool type.\n"; | 195 std::cerr << "ERROR: Unknown constant pool type.\n"; |
200 return false; | 196 return false; |
201 } | 197 } |
202 | 198 |
203 if (!read_from_stdin_ && input_filename_.empty()) { | 199 if (!read_from_stdin_ && input_filenames_.empty()) { |
204 std::cerr << "ERROR: No input file specified.\n"; | 200 std::cerr << "ERROR: No input file specified.\n"; |
205 return false; | 201 return false; |
206 } | 202 } |
207 | 203 |
208 if (read_from_stdin_ && !input_filename_.empty()) { | 204 if (read_from_stdin_ && !input_filenames_.empty()) { |
209 std::cerr << "ERROR: Reading from stdin, but input files supplied.\n"; | 205 std::cerr << "ERROR: Reading from stdin, but input files supplied.\n"; |
210 return false; | 206 return false; |
211 } | 207 } |
212 | 208 |
213 if (rebaseline_ && read_raw_js_snippet_) { | 209 if (rebaseline_ && read_raw_js_snippet_) { |
214 std::cerr << "ERROR: Cannot use --rebaseline on a raw JS snippet.\n"; | 210 std::cerr << "ERROR: Cannot use --rebaseline on a raw JS snippet.\n"; |
215 return false; | 211 return false; |
216 } | 212 } |
217 | 213 |
214 if (rebaseline_ && !output_filename_.empty()) { | |
215 std::cerr << "ERROR: Output file cannot be specified together with " | |
216 "--rebaseline.\n"; | |
217 return false; | |
218 } | |
219 | |
220 if (rebaseline_ && read_from_stdin_) { | |
221 std::cerr << "ERROR: Cannot --rebaseline when input is --stdin.\n"; | |
222 return false; | |
223 } | |
224 | |
225 if (input_filenames_.size() > 1 && !rebaseline_ && !read_raw_js_snippet()) { | |
226 std::cerr << "ERROR: Multiple input files, but no --rebaseline or --raw-js " | |
227 "specified.\n"; | |
228 return false; | |
229 } | |
230 | |
218 if (top_level_ && !test_function_name_.empty()) { | 231 if (top_level_ && !test_function_name_.empty()) { |
219 std::cerr << "ERROR: test function name specified while processing " | 232 std::cerr << "ERROR: test function name specified while processing " |
220 "top level code.\n"; | 233 "top level code.\n"; |
221 return false; | 234 return false; |
222 } | 235 } |
223 | 236 |
224 return true; | 237 return true; |
225 } | 238 } |
226 | 239 |
227 void ProgramOptions::UpdateFromHeader(std::istream& stream) { | 240 void ProgramOptions::UpdateFromHeader(std::istream& stream) { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
353 } else { | 366 } else { |
354 std::string snippet; | 367 std::string snippet; |
355 while (ReadNextSnippet(body_stream, &snippet)) { | 368 while (ReadNextSnippet(body_stream, &snippet)) { |
356 snippet_list->push_back(UnescapeString(snippet)); | 369 snippet_list->push_back(UnescapeString(snippet)); |
357 } | 370 } |
358 } | 371 } |
359 } | 372 } |
360 | 373 |
361 void GenerateExpectationsFile(std::ostream& stream, // NOLINT | 374 void GenerateExpectationsFile(std::ostream& stream, // NOLINT |
362 const std::vector<std::string>& snippet_list, | 375 const std::vector<std::string>& snippet_list, |
363 const ProgramOptions& options, | 376 const V8InitializationScope& platform, |
364 const char* exec_path) { | 377 const ProgramOptions& options) { |
365 V8InitializationScope platform(exec_path); | 378 v8::Isolate::Scope isolate_scope(platform.isolate()); |
366 { | 379 v8::HandleScope handle_scope(platform.isolate()); |
367 v8::Isolate::Scope isolate_scope(platform.isolate()); | 380 v8::Local<v8::Context> context = v8::Context::New(platform.isolate()); |
368 v8::HandleScope handle_scope(platform.isolate()); | 381 v8::Context::Scope context_scope(context); |
369 v8::Local<v8::Context> context = v8::Context::New(platform.isolate()); | |
370 v8::Context::Scope context_scope(context); | |
371 | 382 |
372 BytecodeExpectationsPrinter printer(platform.isolate(), | 383 BytecodeExpectationsPrinter printer(platform.isolate(), |
373 options.const_pool_type()); | 384 options.const_pool_type()); |
374 printer.set_wrap(options.wrap()); | 385 printer.set_wrap(options.wrap()); |
375 printer.set_execute(options.execute()); | 386 printer.set_execute(options.execute()); |
376 printer.set_top_level(options.top_level()); | 387 printer.set_top_level(options.top_level()); |
377 if (!options.test_function_name().empty()) { | 388 if (!options.test_function_name().empty()) { |
378 printer.set_test_function_name(options.test_function_name()); | 389 printer.set_test_function_name(options.test_function_name()); |
379 } | 390 } |
380 | 391 |
381 if (options.legacy_const()) i::FLAG_legacy_const = true; | 392 if (options.legacy_const()) i::FLAG_legacy_const = true; |
382 if (options.do_expressions()) i::FLAG_harmony_do_expressions = true; | 393 if (options.do_expressions()) i::FLAG_harmony_do_expressions = true; |
383 | 394 |
384 stream << "#\n# Autogenerated by generate-bytecode-expectations.\n#\n\n"; | 395 stream << "#\n# Autogenerated by generate-bytecode-expectations.\n#\n\n"; |
385 options.PrintHeader(stream); | 396 options.PrintHeader(stream); |
386 for (const std::string& snippet : snippet_list) { | 397 for (const std::string& snippet : snippet_list) { |
387 printer.PrintExpectation(stream, snippet); | 398 printer.PrintExpectation(stream, snippet); |
388 } | |
389 } | 399 } |
390 } | 400 } |
391 | 401 |
392 void PrintUsage(const char* exec_path) { | 402 void PrintUsage(const char* exec_path) { |
393 std::cerr | 403 std::cerr |
394 << "\nUsage: " << exec_path | 404 << "\nUsage: " << exec_path |
395 << " [OPTIONS]... [INPUT FILE]\n\n" | 405 << " [OPTIONS]... [INPUT FILES]...\n\n" |
396 "Options:\n" | 406 "Options:\n" |
397 " --help Print this help message.\n" | 407 " --help Print this help message.\n" |
398 " --raw-js Read raw JavaScript, instead of the output format.\n" | 408 " --raw-js Read raw JavaScript, instead of the output format.\n" |
399 " --stdin Read from standard input instead of file.\n" | 409 " --stdin Read from standard input instead of file.\n" |
400 " --rebaseline Rebaseline input snippet file.\n" | 410 " --rebaseline Rebaseline input snippet file.\n" |
401 " --no-wrap Do not wrap the snippet in a function.\n" | 411 " --no-wrap Do not wrap the snippet in a function.\n" |
402 " --no-execute Do not execute after compilation.\n" | 412 " --no-execute Do not execute after compilation.\n" |
403 " --test-function-name=foo " | 413 " --test-function-name=foo " |
404 "Specify the name of the test function.\n" | 414 "Specify the name of the test function.\n" |
405 " --top-level Process top level code, not the top-level function." | 415 " --top-level Process top level code, not the top-level function.\n" |
406 " --legacy-const Enable legacy_const flag.\n" | 416 " --legacy-const Enable legacy_const flag.\n" |
407 " --do-expressions Enable harmony_do_expressions flag.\n" | 417 " --do-expressions Enable harmony_do_expressions flag.\n" |
408 " --output=file.name\n" | 418 " --output=file.name\n" |
409 " Specify the output file. If not specified, output goes to " | 419 " Specify the output file. If not specified, output goes to " |
410 "stdout.\n" | 420 "stdout.\n" |
411 " --pool-type=(number|string|mixed)\n" | 421 " --pool-type=(number|string|mixed)\n" |
412 " Specify the type of the entries in the constant pool " | 422 " Specify the type of the entries in the constant pool " |
413 "(default: mixed).\n" | 423 "(default: mixed).\n" |
414 "\n" | 424 "\n" |
415 "When using --rebaseline, flags --no-wrap, --no-execute, " | 425 "When using --rebaseline, flags --no-wrap, --no-execute, " |
416 "--test-function-name\nand --pool-type will be overridden by the " | 426 "--test-function-name\nand --pool-type will be overridden by the " |
417 "options specified in the input file\nheader.\n\n" | 427 "options specified in the input file\nheader.\n\n" |
418 "Each raw JavaScript file is interpreted as a single snippet.\n\n" | 428 "Each raw JavaScript file is interpreted as a single snippet.\n\n" |
419 "This tool is intended as a help in writing tests.\n" | 429 "This tool is intended as a help in writing tests.\n" |
420 "Please, DO NOT blindly copy and paste the output " | 430 "Please, DO NOT blindly copy and paste the output " |
421 "into the test suite.\n"; | 431 "into the test suite.\n"; |
422 } | 432 } |
423 | 433 |
424 } // namespace | 434 int DoRebaseline(const V8InitializationScope& platform, |
rmcilroy
2016/02/25 14:12:28
As discussed offline, let's try to merge these thr
Stefano Sanfilippo
2016/02/25 14:23:50
I'm working on it right now.
| |
435 const ProgramOptions& base_options) { | |
436 DCHECK(base_options.rebaseline()); | |
437 for (const std::string& input_filename : base_options.input_filenames()) { | |
438 std::cerr << "Updating " << input_filename << '\n'; | |
425 | 439 |
426 int main(int argc, char** argv) { | 440 std::ifstream input_stream(input_filename); |
427 ProgramOptions options = ProgramOptions::FromCommandLine(argc, argv); | 441 if (!input_stream.is_open()) { |
428 | 442 std::cerr << "ERROR: Could not open '" << input_filename |
429 if (!options.Validate() || options.print_help()) { | |
430 PrintUsage(argv[0]); | |
431 return options.print_help() ? 0 : 1; | |
432 } | |
433 | |
434 std::ifstream input_file_handle; | |
435 if (!options.read_from_stdin()) { | |
436 input_file_handle.open(options.input_filename().c_str()); | |
437 if (!input_file_handle.is_open()) { | |
438 std::cerr << "ERROR: Could not open '" << options.input_filename() | |
439 << "' for reading.\n"; | 443 << "' for reading.\n"; |
440 return 2; | 444 return 2; |
441 } | 445 } |
446 | |
447 ProgramOptions options = base_options; | |
448 options.UpdateFromHeader(input_stream); | |
449 CHECK(options.Validate()); | |
450 | |
451 std::vector<std::string> snippet_list; | |
452 ExtractSnippets(&snippet_list, input_stream, false); | |
453 | |
454 std::ofstream output_stream(input_filename); | |
455 if (!output_stream.is_open()) { | |
456 std::cerr << "ERROR: Could not open '" << input_filename | |
oth
2016/02/25 13:12:38
These ERROR messages could all be reported by a co
Stefano Sanfilippo
2016/02/25 13:23:56
Done.
| |
457 << "' for writing.\n"; | |
458 return 3; | |
459 } | |
460 | |
461 GenerateExpectationsFile(output_stream, snippet_list, platform, options); | |
462 } | |
463 return 0; | |
464 } | |
465 | |
466 int DoRawJS(const V8InitializationScope& platform, | |
467 const ProgramOptions& options) { | |
468 DCHECK(options.read_raw_js_snippet()); | |
469 | |
470 std::vector<std::string> snippet_list; | |
471 | |
472 if (options.read_from_stdin()) { | |
473 snippet_list.push_back(ReadRawJSSnippet(std::cin)); | |
474 } else { | |
475 for (const std::string& input_filename : options.input_filenames()) { | |
476 std::cerr << "Reading " << input_filename << '\n'; | |
oth
2016/02/25 13:12:38
This looks like left over debugging.
Stefano Sanfilippo
2016/02/25 13:23:56
Actually it's not, I thought it could give some fe
rmcilroy
2016/02/25 14:12:28
I think just remove this, the tool should be as si
Stefano Sanfilippo
2016/02/25 14:23:50
Added a verbose flag. Done.
| |
477 | |
478 std::ifstream input_stream(input_filename.c_str()); | |
479 if (!input_stream.is_open()) { | |
480 std::cerr << "ERROR: Could not open '" << input_filename | |
481 << "' for reading.\n"; | |
482 return 2; | |
483 } | |
484 | |
485 snippet_list.push_back(ReadRawJSSnippet(input_stream)); | |
486 } | |
487 } | |
488 | |
489 std::ofstream output_file_handle; | |
490 if (!options.write_to_stdout()) { | |
491 output_file_handle.open(options.output_filename().c_str()); | |
492 if (!output_file_handle.is_open()) { | |
493 std::cerr << "ERROR: Could not open '" << options.output_filename() | |
494 << "' for writing.\n"; | |
495 return 3; | |
496 } | |
497 } | |
498 std::ostream& output_stream = | |
499 options.write_to_stdout() ? std::cout : output_file_handle; | |
500 | |
501 GenerateExpectationsFile(output_stream, snippet_list, platform, options); | |
502 | |
503 return 0; | |
504 } | |
505 | |
506 int DoDefault(const V8InitializationScope& platform, | |
507 const ProgramOptions& options) { | |
508 std::ifstream input_file_handle; | |
509 if (!options.read_from_stdin()) { | |
510 const char* input_filename = options.input_filenames().front().c_str(); | |
511 input_file_handle.open(input_filename); | |
512 if (!input_file_handle.is_open()) { | |
513 std::cerr << "ERROR: Could not open '" << input_filename | |
514 << "' for reading.\n"; | |
515 return 2; | |
516 } | |
442 } | 517 } |
443 std::istream& input_stream = | 518 std::istream& input_stream = |
444 options.read_from_stdin() ? std::cin : input_file_handle; | 519 options.read_from_stdin() ? std::cin : input_file_handle; |
445 | 520 |
446 if (options.rebaseline()) { | |
447 options.UpdateFromHeader(input_stream); | |
448 CHECK(options.Validate()); | |
449 } | |
450 | |
451 std::vector<std::string> snippet_list; | 521 std::vector<std::string> snippet_list; |
452 ExtractSnippets(&snippet_list, input_stream, options.read_raw_js_snippet()); | 522 ExtractSnippets(&snippet_list, input_stream, false); |
453 | 523 |
454 std::ofstream output_file_handle; | 524 std::ofstream output_file_handle; |
455 if (!options.write_to_stdout()) { | 525 if (!options.write_to_stdout()) { |
456 output_file_handle.open(options.rebaseline() | 526 output_file_handle.open(options.output_filename().c_str()); |
457 ? options.input_filename().c_str() | |
458 : options.output_filename().c_str()); | |
459 if (!output_file_handle.is_open()) { | 527 if (!output_file_handle.is_open()) { |
460 std::cerr << "ERROR: Could not open '" << options.output_filename() | 528 std::cerr << "ERROR: Could not open '" << options.output_filename() |
461 << "' for writing.\n"; | 529 << "' for writing.\n"; |
462 return 3; | 530 return 3; |
463 } | 531 } |
464 } | 532 } |
465 std::ostream& output_stream = | 533 std::ostream& output_stream = |
466 options.write_to_stdout() ? std::cout : output_file_handle; | 534 options.write_to_stdout() ? std::cout : output_file_handle; |
467 | 535 |
468 GenerateExpectationsFile(output_stream, snippet_list, options, argv[0]); | 536 GenerateExpectationsFile(output_stream, snippet_list, platform, options); |
537 | |
538 return 0; | |
469 } | 539 } |
540 | |
541 } // namespace | |
542 | |
543 int main(int argc, char** argv) { | |
544 ProgramOptions options = ProgramOptions::FromCommandLine(argc, argv); | |
545 | |
546 if (!options.Validate() || options.print_help()) { | |
547 PrintUsage(argv[0]); | |
548 return options.print_help() ? 0 : 1; | |
549 } | |
550 | |
551 V8InitializationScope platform(argv[0]); | |
552 | |
553 if (options.read_raw_js_snippet()) { | |
554 return DoRawJS(platform, options); | |
555 } else if (options.rebaseline()) { | |
556 return DoRebaseline(platform, options); | |
557 } else { | |
558 return DoDefault(platform, options); | |
559 } | |
560 } | |
OLD | NEW |