| 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 <fstream> | 6 #include <fstream> |
| 6 #include <iostream> | 7 |
| 8 #include "test/cctest/interpreter/bytecode-expectations-printer.h" |
| 7 | 9 |
| 8 #include "include/libplatform/libplatform.h" | 10 #include "include/libplatform/libplatform.h" |
| 9 #include "include/v8.h" | 11 #include "include/v8.h" |
| 10 | 12 |
| 11 #include "src/base/logging.h" | 13 #include "src/base/logging.h" |
| 12 #include "src/base/smart-pointers.h" | 14 #include "src/base/smart-pointers.h" |
| 13 #include "src/compiler.h" | 15 #include "src/compiler.h" |
| 14 | |
| 15 #include "src/interpreter/bytecode-array-iterator.h" | |
| 16 #include "src/interpreter/bytecode-generator.h" | |
| 17 #include "src/interpreter/bytecodes.h" | |
| 18 #include "src/interpreter/interpreter.h" | 16 #include "src/interpreter/interpreter.h" |
| 19 | 17 |
| 20 using namespace i::interpreter; | 18 using v8::internal::interpreter::BytecodeExpectationsPrinter; |
| 21 | 19 |
| 22 namespace { | 20 namespace { |
| 23 | 21 |
| 24 const char* kIndent = " "; | 22 class ProgramOptions { |
| 23 public: |
| 24 static ProgramOptions FromCommandLine(int argc, char** argv); |
| 25 | 25 |
| 26 enum ConstantPoolType { | 26 ProgramOptions() |
| 27 kConstantPoolTypeUnknown, | 27 : parsing_failed_(false), |
| 28 kConstantPoolTypeString, | 28 print_help_(false), |
| 29 kConstantPoolTypeInteger, | 29 read_raw_js_snippet_(false), |
| 30 kConstantPoolTypeDouble, | 30 read_from_stdin_(false), |
| 31 kConstantPoolTypeMixed, | 31 const_pool_type_( |
| 32 BytecodeExpectationsPrinter::ConstantPoolType::kMixed) {} |
| 33 |
| 34 bool Validate() const; |
| 35 |
| 36 bool parsing_failed() const { return parsing_failed_; } |
| 37 bool print_help() const { return print_help_; } |
| 38 bool read_raw_js_snippet() const { return read_raw_js_snippet_; } |
| 39 bool read_from_stdin() const { return read_from_stdin_; } |
| 40 std::string filename() const { return filename_; } |
| 41 BytecodeExpectationsPrinter::ConstantPoolType const_pool_type() const { |
| 42 return const_pool_type_; |
| 43 } |
| 44 |
| 45 private: |
| 46 bool parsing_failed_; |
| 47 bool print_help_; |
| 48 bool read_raw_js_snippet_; |
| 49 bool read_from_stdin_; |
| 50 BytecodeExpectationsPrinter::ConstantPoolType const_pool_type_; |
| 51 std::string filename_; |
| 32 }; | 52 }; |
| 33 | 53 |
| 34 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator { | 54 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator { |
| 35 public: | 55 public: |
| 36 void* Allocate(size_t length) override { | 56 void* Allocate(size_t length) override { |
| 37 void* data = AllocateUninitialized(length); | 57 void* data = AllocateUninitialized(length); |
| 38 if (data != nullptr) memset(data, 0, length); | 58 if (data != nullptr) memset(data, 0, length); |
| 39 return data; | 59 return data; |
| 40 } | 60 } |
| 41 void* AllocateUninitialized(size_t length) override { return malloc(length); } | 61 void* AllocateUninitialized(size_t length) override { return malloc(length); } |
| 42 void Free(void* data, size_t) override { free(data); } | 62 void Free(void* data, size_t) override { free(data); } |
| 43 }; | 63 }; |
| 44 | 64 |
| 45 class V8InitializationScope final { | 65 class V8InitializationScope final { |
| 46 public: | 66 public: |
| 47 explicit V8InitializationScope(const char* exec_path); | 67 explicit V8InitializationScope(const char* exec_path); |
| 48 ~V8InitializationScope(); | 68 ~V8InitializationScope(); |
| 49 | 69 |
| 50 v8::Platform* platform() const { return platform_.get(); } | 70 v8::Platform* platform() const { return platform_.get(); } |
| 51 v8::Isolate* isolate() const { return isolate_; } | 71 v8::Isolate* isolate() const { return isolate_; } |
| 52 | 72 |
| 53 private: | 73 private: |
| 54 v8::base::SmartPointer<v8::Platform> platform_; | 74 v8::base::SmartPointer<v8::Platform> platform_; |
| 55 v8::Isolate* isolate_; | 75 v8::Isolate* isolate_; |
| 56 | 76 |
| 57 DISALLOW_COPY_AND_ASSIGN(V8InitializationScope); | 77 DISALLOW_COPY_AND_ASSIGN(V8InitializationScope); |
| 58 }; | 78 }; |
| 59 | 79 |
| 60 i::Isolate* GetInternalIsolate(v8::Isolate* isolate) { | 80 BytecodeExpectationsPrinter::ConstantPoolType ParseConstantPoolType( |
| 61 return reinterpret_cast<i::Isolate*>(isolate); | 81 const char* type_string) { |
| 82 if (strcmp(type_string, "int") == 0) { |
| 83 return BytecodeExpectationsPrinter::ConstantPoolType::kInteger; |
| 84 } else if (strcmp(type_string, "double") == 0) { |
| 85 return BytecodeExpectationsPrinter::ConstantPoolType::kDouble; |
| 86 } else if (strcmp(type_string, "string") == 0) { |
| 87 return BytecodeExpectationsPrinter::ConstantPoolType::kString; |
| 88 } else if (strcmp(type_string, "mixed") == 0) { |
| 89 return BytecodeExpectationsPrinter::ConstantPoolType::kMixed; |
| 90 } |
| 91 return BytecodeExpectationsPrinter::ConstantPoolType::kUnknown; |
| 92 } |
| 93 |
| 94 // static |
| 95 ProgramOptions ProgramOptions::FromCommandLine(int argc, char** argv) { |
| 96 ProgramOptions options; |
| 97 |
| 98 if (argc <= 1) return options; |
| 99 |
| 100 for (int i = 1; i < argc; ++i) { |
| 101 if (strcmp(argv[i], "--help") == 0) { |
| 102 options.print_help_ = true; |
| 103 } else if (strcmp(argv[i], "--raw-js") == 0) { |
| 104 options.read_raw_js_snippet_ = true; |
| 105 } else if (strncmp(argv[i], "--pool-type=", 12) == 0) { |
| 106 options.const_pool_type_ = ParseConstantPoolType(argv[i] + 12); |
| 107 } else if (strcmp(argv[i], "--stdin") == 0) { |
| 108 options.read_from_stdin_ = true; |
| 109 } else if (strncmp(argv[i], "--", 2) != 0) { // It doesn't start with -- |
| 110 if (!options.filename_.empty()) { |
| 111 std::cerr << "ERROR: More than one input file specified\n"; |
| 112 options.parsing_failed_ = true; |
| 113 break; |
| 114 } |
| 115 options.filename_ = argv[i]; |
| 116 } else { |
| 117 std::cerr << "ERROR: Unknonwn option " << argv[i] << "\n"; |
| 118 options.parsing_failed_ = true; |
| 119 break; |
| 120 } |
| 121 } |
| 122 |
| 123 return options; |
| 124 } |
| 125 |
| 126 bool ProgramOptions::Validate() const { |
| 127 if (parsing_failed_) return false; |
| 128 if (print_help_) return true; |
| 129 |
| 130 if (const_pool_type_ == |
| 131 BytecodeExpectationsPrinter::ConstantPoolType::kUnknown) { |
| 132 std::cerr << "ERROR: Unknown constant pool type.\n"; |
| 133 return false; |
| 134 } |
| 135 |
| 136 if (!read_from_stdin_ && filename_.empty()) { |
| 137 std::cerr << "ERROR: No input file specified.\n"; |
| 138 return false; |
| 139 } |
| 140 |
| 141 if (read_from_stdin_ && !filename_.empty()) { |
| 142 std::cerr << "ERROR: Reading from stdin, but input files supplied.\n"; |
| 143 return false; |
| 144 } |
| 145 |
| 146 return true; |
| 62 } | 147 } |
| 63 | 148 |
| 64 V8InitializationScope::V8InitializationScope(const char* exec_path) | 149 V8InitializationScope::V8InitializationScope(const char* exec_path) |
| 65 : platform_(v8::platform::CreateDefaultPlatform()) { | 150 : platform_(v8::platform::CreateDefaultPlatform()) { |
| 66 i::FLAG_ignition = true; | 151 i::FLAG_ignition = true; |
| 67 i::FLAG_always_opt = false; | 152 i::FLAG_always_opt = false; |
| 68 i::FLAG_allow_natives_syntax = true; | 153 i::FLAG_allow_natives_syntax = true; |
| 69 | 154 |
| 70 v8::V8::InitializeICU(); | 155 v8::V8::InitializeICU(); |
| 71 v8::V8::InitializeExternalStartupData(exec_path); | 156 v8::V8::InitializeExternalStartupData(exec_path); |
| 72 v8::V8::InitializePlatform(platform_.get()); | 157 v8::V8::InitializePlatform(platform_.get()); |
| 73 v8::V8::Initialize(); | 158 v8::V8::Initialize(); |
| 74 | 159 |
| 75 ArrayBufferAllocator allocator; | 160 ArrayBufferAllocator allocator; |
| 76 v8::Isolate::CreateParams create_params; | 161 v8::Isolate::CreateParams create_params; |
| 77 create_params.array_buffer_allocator = &allocator; | 162 create_params.array_buffer_allocator = &allocator; |
| 78 | 163 |
| 79 isolate_ = v8::Isolate::New(create_params); | 164 isolate_ = v8::Isolate::New(create_params); |
| 80 GetInternalIsolate(isolate_)->interpreter()->Initialize(); | |
| 81 } | 165 } |
| 82 | 166 |
| 83 V8InitializationScope::~V8InitializationScope() { | 167 V8InitializationScope::~V8InitializationScope() { |
| 84 isolate_->Dispose(); | 168 isolate_->Dispose(); |
| 85 v8::V8::Dispose(); | 169 v8::V8::Dispose(); |
| 86 v8::V8::ShutdownPlatform(); | 170 v8::V8::ShutdownPlatform(); |
| 87 } | 171 } |
| 88 | 172 |
| 89 v8::Local<v8::String> V8StringFromUTF8(v8::Isolate* isolate, const char* data) { | 173 std::string ReadRawJSSnippet(std::istream& stream) { // NOLINT |
| 90 return v8::String::NewFromUtf8(isolate, data, v8::NewStringType::kNormal) | 174 std::stringstream body_buffer; |
| 91 .ToLocalChecked(); | 175 CHECK(body_buffer << stream.rdbuf()); |
| 176 return body_buffer.str(); |
| 92 } | 177 } |
| 93 | 178 |
| 94 std::string WrapCodeInFunction(const char* function_name, | 179 bool ReadNextSnippet(std::istream& stream, std::string* string_out) { // NOLINT |
| 95 const std::string& function_body) { | 180 std::string line; |
| 96 std::ostringstream program_stream; | 181 bool found_begin_snippet = false; |
| 97 program_stream << "function " << function_name << "() {" << function_body | 182 string_out->clear(); |
| 98 << "}\n" | 183 while (std::getline(stream, line)) { |
| 99 << function_name << "();"; | 184 if (line == "snippet: \"") { |
| 100 | 185 found_begin_snippet = true; |
| 101 return program_stream.str(); | 186 continue; |
| 187 } |
| 188 if (!found_begin_snippet) continue; |
| 189 if (line == "\"") return true; |
| 190 CHECK_GE(line.size(), 2u); // We should have the indent |
| 191 string_out->append(line.begin() + 2, line.end()); |
| 192 *string_out += '\n'; |
| 193 } |
| 194 return false; |
| 102 } | 195 } |
| 103 | 196 |
| 104 v8::Local<v8::Value> CompileAndRun(v8::Isolate* isolate, | 197 void ExtractSnippetsFromStream(std::vector<std::string>* snippet_list, |
| 105 const v8::Local<v8::Context>& context, | 198 std::istream& body_stream, // NOLINT |
| 106 const char* program) { | 199 bool read_raw_js_snippet) { |
| 107 v8::Local<v8::String> source = V8StringFromUTF8(isolate, program); | 200 if (read_raw_js_snippet) { |
| 108 v8::Local<v8::Script> script = | 201 snippet_list->push_back(ReadRawJSSnippet(body_stream)); |
| 109 v8::Script::Compile(context, source).ToLocalChecked(); | 202 } else { |
| 110 | 203 std::string snippet; |
| 111 v8::Local<v8::Value> result; | 204 while (ReadNextSnippet(body_stream, &snippet)) { |
| 112 CHECK(script->Run(context).ToLocal(&result)); | 205 snippet_list->push_back(snippet); |
| 113 | |
| 114 return result; | |
| 115 } | |
| 116 | |
| 117 i::Handle<v8::internal::BytecodeArray> GetBytecodeArrayForGlobal( | |
| 118 v8::Isolate* isolate, const v8::Local<v8::Context>& context, | |
| 119 const char* global_name) { | |
| 120 v8::Local<v8::String> v8_global_name = V8StringFromUTF8(isolate, global_name); | |
| 121 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( | |
| 122 context->Global()->Get(context, v8_global_name).ToLocalChecked()); | |
| 123 i::Handle<i::JSFunction> js_function = | |
| 124 i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*function)); | |
| 125 | |
| 126 i::Handle<i::BytecodeArray> bytecodes = i::handle( | |
| 127 js_function->shared()->bytecode_array(), GetInternalIsolate(isolate)); | |
| 128 | |
| 129 return bytecodes; | |
| 130 } | |
| 131 | |
| 132 std::string QuoteCString(const std::string& source) { | |
| 133 std::string quoted_buffer; | |
| 134 for (char c : source) { | |
| 135 switch (c) { | |
| 136 case '"': | |
| 137 quoted_buffer += "\\\""; | |
| 138 break; | |
| 139 case '\n': | |
| 140 quoted_buffer += "\\n"; | |
| 141 break; | |
| 142 case '\t': | |
| 143 quoted_buffer += "\\t"; | |
| 144 break; | |
| 145 case '\\': | |
| 146 quoted_buffer += "\\\\"; | |
| 147 break; | |
| 148 default: | |
| 149 quoted_buffer += c; | |
| 150 break; | |
| 151 } | 206 } |
| 152 } | 207 } |
| 153 return quoted_buffer; | |
| 154 } | |
| 155 | |
| 156 void PrintBytecodeOperand(std::ostream& stream, | |
| 157 const BytecodeArrayIterator& bytecode_iter, | |
| 158 const Bytecode& bytecode, int op_index) { | |
| 159 OperandType op_type = Bytecodes::GetOperandType(bytecode, op_index); | |
| 160 OperandSize op_size = Bytecodes::GetOperandSize(bytecode, op_index); | |
| 161 | |
| 162 const char* size_tag; | |
| 163 switch (op_size) { | |
| 164 case OperandSize::kByte: | |
| 165 size_tag = "8"; | |
| 166 break; | |
| 167 case OperandSize::kShort: | |
| 168 size_tag = "16"; | |
| 169 break; | |
| 170 default: | |
| 171 UNREACHABLE(); | |
| 172 return; | |
| 173 } | |
| 174 | |
| 175 if (Bytecodes::IsRegisterOperandType(op_type)) { | |
| 176 Register register_value = bytecode_iter.GetRegisterOperand(op_index); | |
| 177 stream << 'R'; | |
| 178 if (op_size != OperandSize::kByte) stream << size_tag; | |
| 179 stream << '(' << register_value.index() << ')'; | |
| 180 } else { | |
| 181 stream << 'U' << size_tag << '('; | |
| 182 | |
| 183 if (Bytecodes::IsImmediateOperandType(op_type)) { | |
| 184 // We need a cast, otherwise the result is printed as char. | |
| 185 stream << static_cast<int>(bytecode_iter.GetImmediateOperand(op_index)); | |
| 186 } else if (Bytecodes::IsRegisterCountOperandType(op_type)) { | |
| 187 stream << bytecode_iter.GetRegisterCountOperand(op_index); | |
| 188 } else if (Bytecodes::IsIndexOperandType(op_type)) { | |
| 189 stream << bytecode_iter.GetIndexOperand(op_index); | |
| 190 } else { | |
| 191 UNREACHABLE(); | |
| 192 } | |
| 193 | |
| 194 stream << ')'; | |
| 195 } | |
| 196 } | 208 } |
| 197 | 209 |
| 198 void PrintBytecode(std::ostream& stream, | 210 bool ExtractSnippets(std::vector<std::string>* snippet_list, |
| 199 const BytecodeArrayIterator& bytecode_iter) { | 211 const ProgramOptions& options) { |
| 200 Bytecode bytecode = bytecode_iter.current_bytecode(); | 212 if (options.read_from_stdin()) { |
| 201 | 213 ExtractSnippetsFromStream(snippet_list, std::cin, |
| 202 stream << "B(" << Bytecodes::ToString(bytecode) << ')'; | 214 options.read_raw_js_snippet()); |
| 203 | 215 } else { |
| 204 int operands_count = Bytecodes::NumberOfOperands(bytecode); | 216 std::ifstream body_file(options.filename().c_str()); |
| 205 for (int op_index = 0; op_index < operands_count; ++op_index) { | 217 if (!body_file.is_open()) { |
| 206 stream << ", "; | 218 std::cerr << "ERROR: Could not open '" << options.filename() << "'.\n"; |
| 207 PrintBytecodeOperand(stream, bytecode_iter, bytecode, op_index); | 219 return false; |
| 220 } |
| 221 ExtractSnippetsFromStream(snippet_list, body_file, |
| 222 options.read_raw_js_snippet()); |
| 208 } | 223 } |
| 224 return true; |
| 209 } | 225 } |
| 210 | 226 |
| 211 void PrintV8String(std::ostream& stream, i::String* string) { | 227 void GenerateExpectationsFile( |
| 212 stream << '"'; | 228 std::ostream& stream, // NOLINT |
| 213 for (int i = 0, length = string->length(); i < length; ++i) { | 229 const std::vector<std::string>& snippet_list, |
| 214 stream << i::AsEscapedUC16ForJSON(string->Get(i)); | 230 BytecodeExpectationsPrinter::ConstantPoolType const_pool_type, |
| 215 } | 231 const char* exec_path) { |
| 216 stream << '"'; | |
| 217 } | |
| 218 | |
| 219 void PrintConstant(std::ostream& stream, | |
| 220 ConstantPoolType expected_constant_type, | |
| 221 i::Handle<i::Object> constant) { | |
| 222 switch (expected_constant_type) { | |
| 223 case kConstantPoolTypeString: | |
| 224 CHECK(constant->IsString()); | |
| 225 PrintV8String(stream, i::String::cast(*constant)); | |
| 226 break; | |
| 227 case kConstantPoolTypeInteger: | |
| 228 if (constant->IsSmi()) { | |
| 229 i::Smi::cast(*constant)->SmiPrint(stream); | |
| 230 } else if (constant->IsHeapNumber()) { | |
| 231 i::HeapNumber::cast(*constant)->HeapNumberPrint(stream); | |
| 232 } else { | |
| 233 UNREACHABLE(); | |
| 234 } | |
| 235 break; | |
| 236 case kConstantPoolTypeDouble: | |
| 237 i::HeapNumber::cast(*constant)->HeapNumberPrint(stream); | |
| 238 break; | |
| 239 case kConstantPoolTypeMixed: | |
| 240 if (constant->IsSmi()) { | |
| 241 stream << "kInstanceTypeDontCare"; | |
| 242 } else { | |
| 243 stream << "InstanceType::" | |
| 244 << i::HeapObject::cast(*constant)->map()->instance_type(); | |
| 245 } | |
| 246 break; | |
| 247 default: | |
| 248 UNREACHABLE(); | |
| 249 return; | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 void PrintFrameSize(std::ostream& stream, | |
| 254 i::Handle<i::BytecodeArray> bytecode_array) { | |
| 255 const int kPointerSize = sizeof(void*); | |
| 256 int frame_size = bytecode_array->frame_size(); | |
| 257 | |
| 258 stream << kIndent; | |
| 259 | |
| 260 DCHECK(frame_size % kPointerSize == 0); | |
| 261 if (frame_size > kPointerSize) { | |
| 262 stream << ' ' << frame_size / kPointerSize << " * kPointerSize,\n" | |
| 263 << kIndent; | |
| 264 } else if (frame_size == kPointerSize) { | |
| 265 stream << " kPointerSize,\n" << kIndent; | |
| 266 } else if (frame_size == 0) { | |
| 267 stream << " 0,\n" << kIndent; | |
| 268 } | |
| 269 | |
| 270 stream << ' ' << bytecode_array->parameter_count() << ",\n"; | |
| 271 } | |
| 272 | |
| 273 void PrintBytecodeSequence(std::ostream& stream, | |
| 274 i::Handle<i::BytecodeArray> bytecode_array) { | |
| 275 stream << kIndent << ' ' << bytecode_array->length() << ",\n" | |
| 276 << kIndent << " {\n" | |
| 277 << kIndent << " "; | |
| 278 | |
| 279 BytecodeArrayIterator bytecode_iter{bytecode_array}; | |
| 280 for (; !bytecode_iter.done(); bytecode_iter.Advance()) { | |
| 281 // Print separator before each instruction, except the first one. | |
| 282 if (bytecode_iter.current_offset() > 0) { | |
| 283 stream << ",\n" << kIndent << " "; | |
| 284 } | |
| 285 PrintBytecode(stream, bytecode_iter); | |
| 286 } | |
| 287 } | |
| 288 | |
| 289 void PrintConstantPool(std::ostream& stream, i::FixedArray* constant_pool, | |
| 290 ConstantPoolType expected_constant_type, | |
| 291 v8::Isolate* isolate) { | |
| 292 int num_constants = constant_pool->length(); | |
| 293 stream << "\n" << kIndent << " },\n" << kIndent << ' ' << num_constants; | |
| 294 if (num_constants > 0) { | |
| 295 stream << ",\n" << kIndent << " {"; | |
| 296 for (int i = 0; i < num_constants; ++i) { | |
| 297 // Print separator before each constant, except the first one | |
| 298 if (i != 0) stream << ", "; | |
| 299 PrintConstant( | |
| 300 stream, expected_constant_type, | |
| 301 i::FixedArray::get(constant_pool, i, GetInternalIsolate(isolate))); | |
| 302 } | |
| 303 stream << '}'; | |
| 304 } | |
| 305 stream << '\n'; | |
| 306 } | |
| 307 | |
| 308 void PrintBytecodeArray(std::ostream& stream, | |
| 309 i::Handle<i::BytecodeArray> bytecode_array, | |
| 310 const std::string& body, v8::Isolate* isolate, | |
| 311 ConstantPoolType constant_pool_type, | |
| 312 bool print_banner = true) { | |
| 313 if (print_banner) { | |
| 314 stream << kIndent << "// === ExpectedSnippet generated by " | |
| 315 "generate-bytecode-expectations. ===\n"; | |
| 316 } | |
| 317 | |
| 318 // Print the code snippet as a quoted C string. | |
| 319 stream << kIndent << "{" << '"' << QuoteCString(body) << "\",\n"; | |
| 320 | |
| 321 PrintFrameSize(stream, bytecode_array); | |
| 322 PrintBytecodeSequence(stream, bytecode_array); | |
| 323 PrintConstantPool(stream, bytecode_array->constant_pool(), constant_pool_type, | |
| 324 isolate); | |
| 325 | |
| 326 // TODO(ssanfilippo) print handlers. | |
| 327 i::HandlerTable* handlers = | |
| 328 i::HandlerTable::cast(bytecode_array->handler_table()); | |
| 329 CHECK_EQ(handlers->NumberOfRangeEntries(), 0); | |
| 330 | |
| 331 stream << kIndent << "}\n"; | |
| 332 } | |
| 333 | |
| 334 void PrintExpectedSnippet(ConstantPoolType constant_pool_type, char* exec_path, | |
| 335 std::string body) { | |
| 336 const char* wrapper_function_name = "__genbckexp_wrapper__"; | |
| 337 | |
| 338 V8InitializationScope platform(exec_path); | 232 V8InitializationScope platform(exec_path); |
| 339 { | 233 { |
| 340 v8::Isolate::Scope isolate_scope(platform.isolate()); | 234 v8::Isolate::Scope isolate_scope(platform.isolate()); |
| 341 v8::HandleScope handle_scope(platform.isolate()); | 235 v8::HandleScope handle_scope(platform.isolate()); |
| 342 v8::Local<v8::Context> context = v8::Context::New(platform.isolate()); | 236 v8::Local<v8::Context> context = v8::Context::New(platform.isolate()); |
| 343 v8::Context::Scope context_scope(context); | 237 v8::Context::Scope context_scope(context); |
| 344 | 238 |
| 345 std::string source_code = WrapCodeInFunction(wrapper_function_name, body); | 239 stream << "#\n# Autogenerated by generate-bytecode-expectations\n#\n\n"; |
| 346 CompileAndRun(platform.isolate(), context, source_code.c_str()); | |
| 347 | 240 |
| 348 i::Handle<i::BytecodeArray> bytecode_array = GetBytecodeArrayForGlobal( | 241 BytecodeExpectationsPrinter printer(platform.isolate(), const_pool_type); |
| 349 platform.isolate(), context, wrapper_function_name); | 242 for (const std::string& snippet : snippet_list) { |
| 350 | 243 printer.PrintExpectation(stream, snippet); |
| 351 PrintBytecodeArray(std::cout, bytecode_array, body, platform.isolate(), | 244 } |
| 352 constant_pool_type); | |
| 353 } | 245 } |
| 354 } | 246 } |
| 355 | 247 |
| 356 bool ReadFromFileOrStdin(std::string* body, const char* body_filename) { | |
| 357 std::stringstream body_buffer; | |
| 358 if (strcmp(body_filename, "-") == 0) { | |
| 359 body_buffer << std::cin.rdbuf(); | |
| 360 } else { | |
| 361 std::ifstream body_file{body_filename}; | |
| 362 if (!body_file) return false; | |
| 363 body_buffer << body_file.rdbuf(); | |
| 364 } | |
| 365 *body = body_buffer.str(); | |
| 366 return true; | |
| 367 } | |
| 368 | |
| 369 ConstantPoolType ParseConstantPoolType(const char* type_string) { | |
| 370 if (strcmp(type_string, "int") == 0) { | |
| 371 return kConstantPoolTypeInteger; | |
| 372 } else if (strcmp(type_string, "double") == 0) { | |
| 373 return kConstantPoolTypeDouble; | |
| 374 } else if (strcmp(type_string, "string") == 0) { | |
| 375 return kConstantPoolTypeString; | |
| 376 } else if (strcmp(type_string, "mixed") == 0) { | |
| 377 return kConstantPoolTypeMixed; | |
| 378 } | |
| 379 return kConstantPoolTypeUnknown; | |
| 380 } | |
| 381 | |
| 382 void PrintUsage(const char* exec_path) { | 248 void PrintUsage(const char* exec_path) { |
| 383 std::cerr << "Usage: " << exec_path | 249 std::cerr |
| 384 << " (int|double|string|mixed) [filename.js|-]\n\n" | 250 << "\nUsage: " << exec_path |
| 385 "First argument is the type of objects in the constant pool.\n\n" | 251 << " [OPTIONS]... [INPUT FILE]\n\n" |
| 386 "Omitting the second argument or - reads from standard input.\n" | 252 "Options:\n" |
| 387 "Anything else is interpreted as a filename.\n\n" | 253 " --help Print this help message.\n" |
| 388 "This tool is intended as a help in writing tests.\n" | 254 " --raw-js Read raw JavaScript, instead of the output format.\n" |
| 389 "Please, DO NOT blindly copy and paste the output " | 255 " --stdin Read from standard input instead of file.\n" |
| 390 "into the test suite.\n"; | 256 " --pool-type=(int|double|string|mixed)\n" |
| 257 " specify the type of the entries in the constant pool " |
| 258 "(default: mixed).\n" |
| 259 "\n" |
| 260 "Each raw JavaScript file is interpreted as a single snippet.\n\n" |
| 261 "This tool is intended as a help in writing tests.\n" |
| 262 "Please, DO NOT blindly copy and paste the output " |
| 263 "into the test suite.\n"; |
| 391 } | 264 } |
| 392 | 265 |
| 393 } // namespace | 266 } // namespace |
| 394 | 267 |
| 395 int main(int argc, char** argv) { | 268 int main(int argc, char** argv) { |
| 396 if (argc < 2) { | 269 ProgramOptions options = ProgramOptions::FromCommandLine(argc, argv); |
| 270 |
| 271 if (!options.Validate() || options.print_help()) { |
| 397 PrintUsage(argv[0]); | 272 PrintUsage(argv[0]); |
| 398 return 1; | 273 return options.print_help() ? 0 : 1; |
| 399 } | 274 } |
| 400 | 275 |
| 401 if (argc > 1 && strcmp(argv[1], "--help") == 0) { | 276 std::vector<std::string> snippet_list; |
| 402 PrintUsage(argv[0]); | 277 if (!ExtractSnippets(&snippet_list, options)) { |
| 403 return 0; | 278 return 2; |
| 404 } | 279 } |
| 405 | 280 |
| 406 const char* body_filename = (argc > 2 ? argv[2] : "-"); | 281 GenerateExpectationsFile(std::cout, snippet_list, options.const_pool_type(), |
| 407 const char* const_pool_type_string = argv[1]; | 282 argv[0]); |
| 408 | |
| 409 std::string body; | |
| 410 if (!ReadFromFileOrStdin(&body, body_filename)) { | |
| 411 std::cerr << "Could not open '" << body_filename << "'.\n\n"; | |
| 412 PrintUsage(argv[0]); | |
| 413 return 1; | |
| 414 } | |
| 415 | |
| 416 PrintExpectedSnippet(ParseConstantPoolType(const_pool_type_string), argv[0], | |
| 417 body); | |
| 418 } | 283 } |
| OLD | NEW |