Chromium Code Reviews| 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" |
| 17 | 18 |
| 19 #ifdef V8_OS_POSIX | |
| 20 #include <dirent.h> | |
| 21 #endif | |
| 22 | |
| 18 using v8::internal::interpreter::BytecodeExpectationsPrinter; | 23 using v8::internal::interpreter::BytecodeExpectationsPrinter; |
| 19 | 24 |
| 25 #define REPORT_ERROR(MESSAGE) (((std::cerr << "ERROR: ") << MESSAGE) << '\n') | |
| 26 | |
| 20 namespace { | 27 namespace { |
| 21 | 28 |
| 29 #ifdef V8_OS_POSIX | |
| 30 const char* kGoldenFilesPath = "test/cctest/interpreter/bytecode_expectations/"; | |
| 31 #endif | |
| 32 | |
| 22 class ProgramOptions final { | 33 class ProgramOptions final { |
| 23 public: | 34 public: |
| 24 static ProgramOptions FromCommandLine(int argc, char** argv); | 35 static ProgramOptions FromCommandLine(int argc, char** argv); |
| 25 | 36 |
| 26 ProgramOptions() | 37 ProgramOptions() |
| 27 : parsing_failed_(false), | 38 : parsing_failed_(false), |
| 28 print_help_(false), | 39 print_help_(false), |
| 29 read_raw_js_snippet_(false), | 40 read_raw_js_snippet_(false), |
| 30 read_from_stdin_(false), | 41 read_from_stdin_(false), |
| 31 rebaseline_(false), | 42 rebaseline_(false), |
| 32 wrap_(true), | 43 wrap_(true), |
| 33 execute_(true), | 44 execute_(true), |
| 34 top_level_(false), | 45 top_level_(false), |
| 35 legacy_const_(false), | 46 legacy_const_(false), |
| 36 do_expressions_(false), | 47 do_expressions_(false), |
| 48 verbose_(false), | |
| 37 const_pool_type_( | 49 const_pool_type_( |
| 38 BytecodeExpectationsPrinter::ConstantPoolType::kMixed) {} | 50 BytecodeExpectationsPrinter::ConstantPoolType::kMixed) {} |
| 39 | 51 |
| 40 bool Validate() const; | 52 bool Validate() const; |
| 41 void UpdateFromHeader(std::istream& stream); // NOLINT | 53 void UpdateFromHeader(std::istream& stream); // NOLINT |
| 42 void PrintHeader(std::ostream& stream) const; // NOLINT | 54 void PrintHeader(std::ostream& stream) const; // NOLINT |
| 43 | 55 |
| 44 bool parsing_failed() const { return parsing_failed_; } | 56 bool parsing_failed() const { return parsing_failed_; } |
| 45 bool print_help() const { return print_help_; } | 57 bool print_help() const { return print_help_; } |
| 46 bool read_raw_js_snippet() const { return read_raw_js_snippet_; } | 58 bool read_raw_js_snippet() const { return read_raw_js_snippet_; } |
| 47 bool read_from_stdin() const { return read_from_stdin_; } | 59 bool read_from_stdin() const { return read_from_stdin_; } |
| 48 bool write_to_stdout() const { | 60 bool write_to_stdout() const { |
| 49 return output_filename_.empty() && !rebaseline_; | 61 return output_filename_.empty() && !rebaseline_; |
| 50 } | 62 } |
| 51 bool rebaseline() const { return rebaseline_; } | 63 bool rebaseline() const { return rebaseline_; } |
| 52 bool wrap() const { return wrap_; } | 64 bool wrap() const { return wrap_; } |
| 53 bool execute() const { return execute_; } | 65 bool execute() const { return execute_; } |
| 54 bool top_level() const { return top_level_; } | 66 bool top_level() const { return top_level_; } |
| 55 bool legacy_const() const { return legacy_const_; } | 67 bool legacy_const() const { return legacy_const_; } |
| 56 bool do_expressions() const { return do_expressions_; } | 68 bool do_expressions() const { return do_expressions_; } |
| 69 bool verbose() const { return verbose_; } | |
| 57 BytecodeExpectationsPrinter::ConstantPoolType const_pool_type() const { | 70 BytecodeExpectationsPrinter::ConstantPoolType const_pool_type() const { |
| 58 return const_pool_type_; | 71 return const_pool_type_; |
| 59 } | 72 } |
| 60 std::string input_filename() const { return input_filename_; } | 73 std::vector<std::string> input_filenames() const { return input_filenames_; } |
| 61 std::string output_filename() const { return output_filename_; } | 74 std::string output_filename() const { return output_filename_; } |
| 62 std::string test_function_name() const { return test_function_name_; } | 75 std::string test_function_name() const { return test_function_name_; } |
| 63 | 76 |
| 64 private: | 77 private: |
| 65 bool parsing_failed_; | 78 bool parsing_failed_; |
| 66 bool print_help_; | 79 bool print_help_; |
| 67 bool read_raw_js_snippet_; | 80 bool read_raw_js_snippet_; |
| 68 bool read_from_stdin_; | 81 bool read_from_stdin_; |
| 69 bool rebaseline_; | 82 bool rebaseline_; |
| 70 bool wrap_; | 83 bool wrap_; |
| 71 bool execute_; | 84 bool execute_; |
| 72 bool top_level_; | 85 bool top_level_; |
| 73 bool legacy_const_; | 86 bool legacy_const_; |
| 74 bool do_expressions_; | 87 bool do_expressions_; |
| 88 bool verbose_; | |
| 75 BytecodeExpectationsPrinter::ConstantPoolType const_pool_type_; | 89 BytecodeExpectationsPrinter::ConstantPoolType const_pool_type_; |
| 76 std::string input_filename_; | 90 std::vector<std::string> input_filenames_; |
| 77 std::string output_filename_; | 91 std::string output_filename_; |
| 78 std::string test_function_name_; | 92 std::string test_function_name_; |
| 79 }; | 93 }; |
| 80 | 94 |
| 81 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator { | 95 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator { |
| 82 public: | 96 public: |
| 83 void* Allocate(size_t length) override { | 97 void* Allocate(size_t length) override { |
| 84 void* data = AllocateUninitialized(length); | 98 void* data = AllocateUninitialized(length); |
| 85 if (data != nullptr) memset(data, 0, length); | 99 if (data != nullptr) memset(data, 0, length); |
| 86 return data; | 100 return data; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 } else if (strcmp(string, "no") == 0) { | 151 } else if (strcmp(string, "no") == 0) { |
| 138 return false; | 152 return false; |
| 139 } else { | 153 } else { |
| 140 UNREACHABLE(); | 154 UNREACHABLE(); |
| 141 return false; | 155 return false; |
| 142 } | 156 } |
| 143 } | 157 } |
| 144 | 158 |
| 145 const char* BooleanToString(bool value) { return value ? "yes" : "no"; } | 159 const char* BooleanToString(bool value) { return value ? "yes" : "no"; } |
| 146 | 160 |
| 161 #ifdef V8_OS_POSIX | |
| 162 | |
| 163 bool EndsWith(const char* string, const char* suffix) { | |
| 164 int string_size = i::StrLength(string); | |
| 165 int suffix_size = i::StrLength(suffix); | |
| 166 if (string_size < suffix_size) return false; | |
| 167 | |
| 168 for (int i = string_size - 1, j = suffix_size - 1; j >= 0; --i, --j) { | |
| 169 if (string[i] != suffix[j]) return false; | |
|
rmcilroy
2016/02/26 08:56:16
you could use memcmp here.
Stefano Sanfilippo
2016/02/26 10:22:43
Done.
| |
| 170 } | |
| 171 | |
| 172 return true; | |
| 173 } | |
| 174 | |
| 175 bool CollectGoldenFiles(std::vector<std::string>* golden_file_list, | |
| 176 const char* directory_path) { | |
| 177 DIR* directory = opendir(directory_path); | |
| 178 if (!directory) return false; | |
| 179 | |
| 180 dirent entry_buffer; | |
| 181 dirent* entry; | |
| 182 | |
| 183 while (readdir_r(directory, &entry_buffer, &entry) == 0 && entry) { | |
| 184 if (EndsWith(entry->d_name, ".golden")) { | |
| 185 std::string golden_filename(kGoldenFilesPath); | |
| 186 golden_filename += entry->d_name; | |
| 187 golden_file_list->push_back(golden_filename); | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 closedir(directory); | |
| 192 | |
| 193 return true; | |
| 194 } | |
| 195 | |
| 196 #endif // V8_OS_POSIX | |
| 197 | |
| 147 // static | 198 // static |
| 148 ProgramOptions ProgramOptions::FromCommandLine(int argc, char** argv) { | 199 ProgramOptions ProgramOptions::FromCommandLine(int argc, char** argv) { |
| 149 ProgramOptions options; | 200 ProgramOptions options; |
| 150 | 201 |
| 151 for (int i = 1; i < argc; ++i) { | 202 for (int i = 1; i < argc; ++i) { |
| 152 if (strcmp(argv[i], "--help") == 0) { | 203 if (strcmp(argv[i], "--help") == 0) { |
| 153 options.print_help_ = true; | 204 options.print_help_ = true; |
| 154 } else if (strcmp(argv[i], "--raw-js") == 0) { | 205 } else if (strcmp(argv[i], "--raw-js") == 0) { |
| 155 options.read_raw_js_snippet_ = true; | 206 options.read_raw_js_snippet_ = true; |
| 156 } else if (strncmp(argv[i], "--pool-type=", 12) == 0) { | 207 } else if (strncmp(argv[i], "--pool-type=", 12) == 0) { |
| 157 options.const_pool_type_ = ParseConstantPoolType(argv[i] + 12); | 208 options.const_pool_type_ = ParseConstantPoolType(argv[i] + 12); |
| 158 } else if (strcmp(argv[i], "--stdin") == 0) { | 209 } else if (strcmp(argv[i], "--stdin") == 0) { |
| 159 options.read_from_stdin_ = true; | 210 options.read_from_stdin_ = true; |
| 160 } else if (strcmp(argv[i], "--rebaseline") == 0) { | 211 } else if (strcmp(argv[i], "--rebaseline") == 0) { |
| 161 options.rebaseline_ = true; | 212 options.rebaseline_ = true; |
| 162 } else if (strcmp(argv[i], "--no-wrap") == 0) { | 213 } else if (strcmp(argv[i], "--no-wrap") == 0) { |
| 163 options.wrap_ = false; | 214 options.wrap_ = false; |
| 164 } else if (strcmp(argv[i], "--no-execute") == 0) { | 215 } else if (strcmp(argv[i], "--no-execute") == 0) { |
| 165 options.execute_ = false; | 216 options.execute_ = false; |
| 166 } else if (strcmp(argv[i], "--top-level") == 0) { | 217 } else if (strcmp(argv[i], "--top-level") == 0) { |
| 167 options.top_level_ = true; | 218 options.top_level_ = true; |
| 168 } else if (strcmp(argv[i], "--legacy-const") == 0) { | 219 } else if (strcmp(argv[i], "--legacy-const") == 0) { |
| 169 options.legacy_const_ = true; | 220 options.legacy_const_ = true; |
| 170 } else if (strcmp(argv[i], "--do-expressions") == 0) { | 221 } else if (strcmp(argv[i], "--do-expressions") == 0) { |
| 171 options.do_expressions_ = true; | 222 options.do_expressions_ = true; |
| 223 } else if (strcmp(argv[i], "--verbose") == 0) { | |
| 224 options.verbose_ = true; | |
| 172 } else if (strncmp(argv[i], "--output=", 9) == 0) { | 225 } else if (strncmp(argv[i], "--output=", 9) == 0) { |
| 173 options.output_filename_ = argv[i] + 9; | 226 options.output_filename_ = argv[i] + 9; |
| 174 } else if (strncmp(argv[i], "--test-function-name=", 21) == 0) { | 227 } else if (strncmp(argv[i], "--test-function-name=", 21) == 0) { |
| 175 options.test_function_name_ = argv[i] + 21; | 228 options.test_function_name_ = argv[i] + 21; |
| 176 } else if (strncmp(argv[i], "--", 2) != 0) { // It doesn't start with -- | 229 } else if (strncmp(argv[i], "--", 2) != 0) { // It doesn't start with -- |
| 177 if (!options.input_filename_.empty()) { | 230 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 { | 231 } else { |
| 184 std::cerr << "ERROR: Unknonwn option " << argv[i] << "\n"; | 232 REPORT_ERROR("Unknonwn option " << argv[i]); |
| 185 options.parsing_failed_ = true; | 233 options.parsing_failed_ = true; |
| 186 break; | 234 break; |
| 187 } | 235 } |
| 188 } | 236 } |
| 189 | 237 |
| 238 if (options.rebaseline_ && options.input_filenames_.empty()) { | |
| 239 #ifdef V8_OS_POSIX | |
| 240 if (options.verbose_) { | |
| 241 std::cout << "Looking for golden files in " << kGoldenFilesPath << '\n'; | |
| 242 } | |
| 243 if (!CollectGoldenFiles(&options.input_filenames_, kGoldenFilesPath)) { | |
| 244 REPORT_ERROR("Golden files autodiscovery failed."); | |
| 245 options.parsing_failed_ = true; | |
| 246 } | |
| 247 #else | |
| 248 REPORT_ERROR("Golden files auto discovery only works on POSIX, sorry."); | |
| 249 options.parsing_failed_ = true; | |
| 250 #endif | |
| 251 } | |
| 252 | |
| 190 return options; | 253 return options; |
| 191 } | 254 } |
| 192 | 255 |
| 193 bool ProgramOptions::Validate() const { | 256 bool ProgramOptions::Validate() const { |
| 194 if (parsing_failed_) return false; | 257 if (parsing_failed_) return false; |
| 195 if (print_help_) return true; | 258 if (print_help_) return true; |
| 196 | 259 |
| 197 if (const_pool_type_ == | 260 if (const_pool_type_ == |
| 198 BytecodeExpectationsPrinter::ConstantPoolType::kUnknown) { | 261 BytecodeExpectationsPrinter::ConstantPoolType::kUnknown) { |
| 199 std::cerr << "ERROR: Unknown constant pool type.\n"; | 262 REPORT_ERROR("Unknown constant pool type."); |
| 200 return false; | 263 return false; |
| 201 } | 264 } |
| 202 | 265 |
| 203 if (!read_from_stdin_ && input_filename_.empty()) { | 266 #ifdef V8_OS_POSIX |
| 204 std::cerr << "ERROR: No input file specified.\n"; | 267 if (!read_from_stdin_ && !rebaseline_ && input_filenames_.empty()) { |
| 268 REPORT_ERROR("No input file specified."); | |
| 205 return false; | 269 return false; |
| 206 } | 270 } |
| 271 #else | |
| 272 if (!read_from_stdin_ && input_filenames_.empty()) { | |
| 273 REPORT_ERROR("No input file specified."); | |
| 274 return false; | |
| 275 } | |
| 276 #endif | |
| 207 | 277 |
| 208 if (read_from_stdin_ && !input_filename_.empty()) { | 278 if (read_from_stdin_ && !input_filenames_.empty()) { |
| 209 std::cerr << "ERROR: Reading from stdin, but input files supplied.\n"; | 279 REPORT_ERROR("Reading from stdin, but input files supplied."); |
| 210 return false; | 280 return false; |
| 211 } | 281 } |
| 212 | 282 |
| 213 if (rebaseline_ && read_raw_js_snippet_) { | 283 if (rebaseline_ && read_raw_js_snippet_) { |
| 214 std::cerr << "ERROR: Cannot use --rebaseline on a raw JS snippet.\n"; | 284 REPORT_ERROR("Cannot use --rebaseline on a raw JS snippet."); |
| 285 return false; | |
| 286 } | |
| 287 | |
| 288 if (rebaseline_ && !output_filename_.empty()) { | |
| 289 REPORT_ERROR("Output file cannot be specified together with --rebaseline."); | |
| 290 return false; | |
| 291 } | |
| 292 | |
| 293 if (rebaseline_ && read_from_stdin_) { | |
| 294 REPORT_ERROR("Cannot --rebaseline when input is --stdin."); | |
| 295 return false; | |
| 296 } | |
| 297 | |
| 298 if (input_filenames_.size() > 1 && !rebaseline_ && !read_raw_js_snippet()) { | |
| 299 REPORT_ERROR( | |
| 300 "Multiple input files, but no --rebaseline or --raw-js specified."); | |
| 215 return false; | 301 return false; |
| 216 } | 302 } |
| 217 | 303 |
| 218 if (top_level_ && !test_function_name_.empty()) { | 304 if (top_level_ && !test_function_name_.empty()) { |
| 219 std::cerr << "ERROR: test function name specified while processing " | 305 REPORT_ERROR( |
| 220 "top level code.\n"; | 306 "Test function name specified while processing top level code."); |
| 221 return false; | 307 return false; |
| 222 } | 308 } |
| 223 | 309 |
| 224 return true; | 310 return true; |
| 225 } | 311 } |
| 226 | 312 |
| 227 void ProgramOptions::UpdateFromHeader(std::istream& stream) { | 313 void ProgramOptions::UpdateFromHeader(std::istream& stream) { |
| 228 std::string line; | 314 std::string line; |
| 229 | 315 |
| 230 // Skip to the beginning of the options header | 316 // Skip to the beginning of the options header |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 } else { | 439 } else { |
| 354 std::string snippet; | 440 std::string snippet; |
| 355 while (ReadNextSnippet(body_stream, &snippet)) { | 441 while (ReadNextSnippet(body_stream, &snippet)) { |
| 356 snippet_list->push_back(UnescapeString(snippet)); | 442 snippet_list->push_back(UnescapeString(snippet)); |
| 357 } | 443 } |
| 358 } | 444 } |
| 359 } | 445 } |
| 360 | 446 |
| 361 void GenerateExpectationsFile(std::ostream& stream, // NOLINT | 447 void GenerateExpectationsFile(std::ostream& stream, // NOLINT |
| 362 const std::vector<std::string>& snippet_list, | 448 const std::vector<std::string>& snippet_list, |
| 363 const ProgramOptions& options, | 449 const V8InitializationScope& platform, |
| 364 const char* exec_path) { | 450 const ProgramOptions& options) { |
| 365 V8InitializationScope platform(exec_path); | 451 v8::Isolate::Scope isolate_scope(platform.isolate()); |
| 366 { | 452 v8::HandleScope handle_scope(platform.isolate()); |
| 367 v8::Isolate::Scope isolate_scope(platform.isolate()); | 453 v8::Local<v8::Context> context = v8::Context::New(platform.isolate()); |
| 368 v8::HandleScope handle_scope(platform.isolate()); | 454 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 | 455 |
| 372 BytecodeExpectationsPrinter printer(platform.isolate(), | 456 BytecodeExpectationsPrinter printer(platform.isolate(), |
| 373 options.const_pool_type()); | 457 options.const_pool_type()); |
| 374 printer.set_wrap(options.wrap()); | 458 printer.set_wrap(options.wrap()); |
| 375 printer.set_execute(options.execute()); | 459 printer.set_execute(options.execute()); |
| 376 printer.set_top_level(options.top_level()); | 460 printer.set_top_level(options.top_level()); |
| 377 if (!options.test_function_name().empty()) { | 461 if (!options.test_function_name().empty()) { |
| 378 printer.set_test_function_name(options.test_function_name()); | 462 printer.set_test_function_name(options.test_function_name()); |
| 379 } | 463 } |
| 380 | 464 |
| 381 if (options.legacy_const()) i::FLAG_legacy_const = true; | 465 if (options.legacy_const()) i::FLAG_legacy_const = true; |
| 382 if (options.do_expressions()) i::FLAG_harmony_do_expressions = true; | 466 if (options.do_expressions()) i::FLAG_harmony_do_expressions = true; |
| 383 | 467 |
| 384 stream << "#\n# Autogenerated by generate-bytecode-expectations.\n#\n\n"; | 468 stream << "#\n# Autogenerated by generate-bytecode-expectations.\n#\n\n"; |
| 385 options.PrintHeader(stream); | 469 options.PrintHeader(stream); |
| 386 for (const std::string& snippet : snippet_list) { | 470 for (const std::string& snippet : snippet_list) { |
| 387 printer.PrintExpectation(stream, snippet); | 471 printer.PrintExpectation(stream, snippet); |
| 472 } | |
| 473 | |
| 474 i::FLAG_legacy_const = false; | |
| 475 i::FLAG_harmony_do_expressions = false; | |
| 476 } | |
| 477 | |
| 478 bool WriteExpectationsFile(const std::vector<std::string>& snippet_list, | |
| 479 const V8InitializationScope& platform, | |
| 480 const ProgramOptions& options, | |
| 481 const std::string& output_filename) { | |
| 482 std::ofstream output_file_handle; | |
| 483 if (!options.write_to_stdout()) { | |
| 484 output_file_handle.open(output_filename.c_str()); | |
| 485 if (!output_file_handle.is_open()) { | |
| 486 REPORT_ERROR("Could not open " << output_filename << " for writing."); | |
| 487 return false; | |
| 388 } | 488 } |
| 389 } | 489 } |
| 490 std::ostream& output_stream = | |
| 491 options.write_to_stdout() ? std::cout : output_file_handle; | |
| 492 | |
| 493 GenerateExpectationsFile(output_stream, snippet_list, platform, options); | |
| 494 | |
| 495 return true; | |
| 390 } | 496 } |
| 391 | 497 |
| 392 void PrintUsage(const char* exec_path) { | 498 void PrintUsage(const char* exec_path) { |
| 393 std::cerr | 499 std::cerr |
| 394 << "\nUsage: " << exec_path | 500 << "\nUsage: " << exec_path |
| 395 << " [OPTIONS]... [INPUT FILE]\n\n" | 501 << " [OPTIONS]... [INPUT FILES]...\n\n" |
| 396 "Options:\n" | 502 "Options:\n" |
| 397 " --help Print this help message.\n" | 503 " --help Print this help message.\n" |
| 504 " --verbose Emit messages about the progress of the tool.\n" | |
| 398 " --raw-js Read raw JavaScript, instead of the output format.\n" | 505 " --raw-js Read raw JavaScript, instead of the output format.\n" |
| 399 " --stdin Read from standard input instead of file.\n" | 506 " --stdin Read from standard input instead of file.\n" |
| 400 " --rebaseline Rebaseline input snippet file.\n" | 507 " --rebaseline Rebaseline input snippet file.\n" |
| 401 " --no-wrap Do not wrap the snippet in a function.\n" | 508 " --no-wrap Do not wrap the snippet in a function.\n" |
| 402 " --no-execute Do not execute after compilation.\n" | 509 " --no-execute Do not execute after compilation.\n" |
| 403 " --test-function-name=foo " | 510 " --test-function-name=foo " |
| 404 "Specify the name of the test function.\n" | 511 "Specify the name of the test function.\n" |
| 405 " --top-level Process top level code, not the top-level function." | 512 " --top-level Process top level code, not the top-level function.\n" |
| 406 " --legacy-const Enable legacy_const flag.\n" | 513 " --legacy-const Enable legacy_const flag.\n" |
| 407 " --do-expressions Enable harmony_do_expressions flag.\n" | 514 " --do-expressions Enable harmony_do_expressions flag.\n" |
| 408 " --output=file.name\n" | 515 " --output=file.name\n" |
| 409 " Specify the output file. If not specified, output goes to " | 516 " Specify the output file. If not specified, output goes to " |
| 410 "stdout.\n" | 517 "stdout.\n" |
| 411 " --pool-type=(number|string|mixed)\n" | 518 " --pool-type=(number|string|mixed)\n" |
| 412 " Specify the type of the entries in the constant pool " | 519 " Specify the type of the entries in the constant pool " |
| 413 "(default: mixed).\n" | 520 "(default: mixed).\n" |
| 414 "\n" | 521 "\n" |
| 415 "When using --rebaseline, flags --no-wrap, --no-execute, " | 522 "When using --rebaseline, flags --no-wrap, --no-execute, " |
| 416 "--test-function-name\nand --pool-type will be overridden by the " | 523 "--test-function-name\nand --pool-type will be overridden by the " |
| 417 "options specified in the input file\nheader.\n\n" | 524 "options specified in the input file\nheader.\n\n" |
| 418 "Each raw JavaScript file is interpreted as a single snippet.\n\n" | 525 "Each raw JavaScript file is interpreted as a single snippet.\n\n" |
| 419 "This tool is intended as a help in writing tests.\n" | 526 "This tool is intended as a help in writing tests.\n" |
| 420 "Please, DO NOT blindly copy and paste the output " | 527 "Please, DO NOT blindly copy and paste the output " |
| 421 "into the test suite.\n"; | 528 "into the test suite.\n"; |
| 422 } | 529 } |
| 423 | 530 |
| 424 } // namespace | 531 } // namespace |
| 425 | 532 |
| 426 int main(int argc, char** argv) { | 533 int main(int argc, char** argv) { |
| 427 ProgramOptions options = ProgramOptions::FromCommandLine(argc, argv); | 534 ProgramOptions options = ProgramOptions::FromCommandLine(argc, argv); |
| 428 | 535 |
| 429 if (!options.Validate() || options.print_help()) { | 536 if (!options.Validate() || options.print_help()) { |
| 430 PrintUsage(argv[0]); | 537 PrintUsage(argv[0]); |
| 431 return options.print_help() ? 0 : 1; | 538 return options.print_help() ? 0 : 1; |
| 432 } | 539 } |
| 433 | 540 |
| 434 std::ifstream input_file_handle; | 541 V8InitializationScope platform(argv[0]); |
| 435 if (!options.read_from_stdin()) { | 542 |
| 436 input_file_handle.open(options.input_filename().c_str()); | 543 std::vector<std::string> snippet_list; |
| 437 if (!input_file_handle.is_open()) { | 544 |
| 438 std::cerr << "ERROR: Could not open '" << options.input_filename() | 545 if (options.read_from_stdin()) { |
| 439 << "' for reading.\n"; | 546 // Rebaseline will never get here, so we will always take the |
| 440 return 2; | 547 // GenerateExpectationsFile at the end of this function. |
| 548 DCHECK(!options.rebaseline()); | |
| 549 ExtractSnippets(&snippet_list, std::cin, options.read_raw_js_snippet()); | |
| 550 } else { | |
| 551 for (const std::string& input_filename : options.input_filenames()) { | |
| 552 if (options.verbose()) { | |
| 553 std::cerr << "Processing " << input_filename << '\n'; | |
| 554 } | |
| 555 | |
| 556 std::ifstream input_stream(input_filename.c_str()); | |
| 557 if (!input_stream.is_open()) { | |
| 558 REPORT_ERROR("Could not open " << input_filename << " for reading."); | |
| 559 return 2; | |
| 560 } | |
| 561 | |
| 562 ProgramOptions updated_options = options; | |
| 563 if (options.rebaseline()) { | |
| 564 updated_options.UpdateFromHeader(input_stream); | |
| 565 CHECK(updated_options.Validate()); | |
| 566 } | |
| 567 | |
| 568 ExtractSnippets(&snippet_list, input_stream, | |
| 569 options.read_raw_js_snippet()); | |
| 570 | |
| 571 if (options.rebaseline()) { | |
| 572 if (!WriteExpectationsFile(snippet_list, platform, updated_options, | |
| 573 input_filename)) { | |
| 574 return 3; | |
| 575 } | |
| 576 snippet_list.clear(); | |
| 577 } | |
| 441 } | 578 } |
| 442 } | 579 } |
| 443 std::istream& input_stream = | |
| 444 options.read_from_stdin() ? std::cin : input_file_handle; | |
| 445 | 580 |
| 446 if (options.rebaseline()) { | 581 if (!options.rebaseline()) { |
| 447 options.UpdateFromHeader(input_stream); | 582 if (!WriteExpectationsFile(snippet_list, platform, options, |
| 448 CHECK(options.Validate()); | 583 options.output_filename())) { |
| 449 } | |
| 450 | |
| 451 std::vector<std::string> snippet_list; | |
| 452 ExtractSnippets(&snippet_list, input_stream, options.read_raw_js_snippet()); | |
| 453 | |
| 454 std::ofstream output_file_handle; | |
| 455 if (!options.write_to_stdout()) { | |
| 456 output_file_handle.open(options.rebaseline() | |
| 457 ? options.input_filename().c_str() | |
| 458 : options.output_filename().c_str()); | |
| 459 if (!output_file_handle.is_open()) { | |
| 460 std::cerr << "ERROR: Could not open '" << options.output_filename() | |
| 461 << "' for writing.\n"; | |
| 462 return 3; | 584 return 3; |
| 463 } | 585 } |
| 464 } | 586 } |
| 465 std::ostream& output_stream = | |
| 466 options.write_to_stdout() ? std::cout : output_file_handle; | |
| 467 | |
| 468 GenerateExpectationsFile(output_stream, snippet_list, options, argv[0]); | |
| 469 } | 587 } |
| OLD | NEW |