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 namespace v8::internal::interpreter; |
21 | 19 |
22 namespace { | 20 namespace { |
23 | 21 |
24 const char* kIndent = " "; | 22 BytecodeExpectationsPrinter::ConstantPoolType ParseConstantPoolType( |
23 const char* type_string) { | |
24 if (strcmp(type_string, "int") == 0) { | |
25 return BytecodeExpectationsPrinter::ConstantPoolType::kInteger; | |
26 } else if (strcmp(type_string, "double") == 0) { | |
27 return BytecodeExpectationsPrinter::ConstantPoolType::kDouble; | |
28 } else if (strcmp(type_string, "string") == 0) { | |
29 return BytecodeExpectationsPrinter::ConstantPoolType::kString; | |
30 } else if (strcmp(type_string, "mixed") == 0) { | |
31 return BytecodeExpectationsPrinter::ConstantPoolType::kMixed; | |
32 } | |
33 return BytecodeExpectationsPrinter::ConstantPoolType::kUnknown; | |
34 } | |
rmcilroy
2016/02/15 11:15:19
nit - move below V8InitializationScope
Stefano Sanfilippo
2016/02/15 13:26:33
Done.
| |
25 | 35 |
26 enum ConstantPoolType { | 36 struct ProgramOptions { |
27 kConstantPoolTypeUnknown, | 37 static ProgramOptions FromCommandLine(int argc, char** argv); |
28 kConstantPoolTypeString, | 38 bool Validate() const; |
29 kConstantPoolTypeInteger, | 39 |
30 kConstantPoolTypeDouble, | 40 bool has_unknown_options() const { return has_unknown_options_; } |
31 kConstantPoolTypeMixed, | 41 bool print_help() const { return print_help_; } |
42 bool read_raw_js_snippet() const { return read_raw_js_snippet_; } | |
43 bool read_from_stdin() const { return read_from_stdin_; } | |
44 std::string filename() const { return filename_; } | |
45 BytecodeExpectationsPrinter::ConstantPoolType const_pool_type() const { | |
46 return const_pool_type_; | |
47 } | |
48 | |
49 private: | |
50 bool has_unknown_options_ = false; | |
51 bool print_help_ = false; | |
52 bool read_raw_js_snippet_ = false; | |
53 bool read_from_stdin_ = false; | |
54 std::string filename_; | |
55 BytecodeExpectationsPrinter::ConstantPoolType const_pool_type_ = | |
56 BytecodeExpectationsPrinter::ConstantPoolType::kMixed; | |
rmcilroy
2016/02/15 11:15:19
Don't use default values in fields, initialize the
Stefano Sanfilippo
2016/02/15 13:26:34
Done.
| |
32 }; | 57 }; |
33 | 58 |
34 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator { | 59 class ArrayBufferAllocator final : public v8::ArrayBuffer::Allocator { |
35 public: | 60 public: |
36 void* Allocate(size_t length) override { | 61 void* Allocate(size_t length) override { |
37 void* data = AllocateUninitialized(length); | 62 void* data = AllocateUninitialized(length); |
38 if (data != nullptr) memset(data, 0, length); | 63 if (data != nullptr) memset(data, 0, length); |
39 return data; | 64 return data; |
40 } | 65 } |
41 void* AllocateUninitialized(size_t length) override { return malloc(length); } | 66 void* AllocateUninitialized(size_t length) override { return malloc(length); } |
42 void Free(void* data, size_t) override { free(data); } | 67 void Free(void* data, size_t) override { free(data); } |
43 }; | 68 }; |
44 | 69 |
45 class V8InitializationScope final { | 70 class V8InitializationScope final { |
46 public: | 71 public: |
47 explicit V8InitializationScope(const char* exec_path); | 72 explicit V8InitializationScope(const char* exec_path); |
48 ~V8InitializationScope(); | 73 ~V8InitializationScope(); |
49 | 74 |
50 v8::Platform* platform() const { return platform_.get(); } | 75 v8::Platform* platform() const { return platform_.get(); } |
51 v8::Isolate* isolate() const { return isolate_; } | 76 v8::Isolate* isolate() const { return isolate_; } |
52 | 77 |
53 private: | 78 private: |
54 v8::base::SmartPointer<v8::Platform> platform_; | 79 v8::base::SmartPointer<v8::Platform> platform_; |
55 v8::Isolate* isolate_; | 80 v8::Isolate* isolate_; |
56 | 81 |
57 DISALLOW_COPY_AND_ASSIGN(V8InitializationScope); | 82 DISALLOW_COPY_AND_ASSIGN(V8InitializationScope); |
58 }; | 83 }; |
59 | 84 |
85 /* static */ | |
rmcilroy
2016/02/15 11:15:19
nit - line comments instead of block comments (i.e
Stefano Sanfilippo
2016/02/15 13:26:33
Done.
| |
86 ProgramOptions ProgramOptions::FromCommandLine(int argc, char** argv) { | |
87 ProgramOptions options; | |
88 | |
89 if (argc <= 1) return options; | |
90 | |
91 bool expecting_const_pool_type = false; | |
92 for (int i = 1; i < argc; ++i) { | |
93 if (expecting_const_pool_type) { | |
94 options.const_pool_type_ = ParseConstantPoolType(argv[i]); | |
95 expecting_const_pool_type = false; | |
96 } else if (strcmp(argv[i], "--help") == 0) { | |
97 options.print_help_ = true; | |
98 } else if (strcmp(argv[i], "--raw-js") == 0) { | |
99 options.read_raw_js_snippet_ = true; | |
100 } else if (strcmp(argv[i], "--pool-type") == 0) { | |
rmcilroy
2016/02/15 11:15:20
Could you just make this a --pool-type=..." argume
Stefano Sanfilippo
2016/02/15 13:26:33
Done.
| |
101 expecting_const_pool_type = true; | |
102 } else if (strcmp(argv[i], "--stdin") == 0) { | |
103 options.read_from_stdin_ = true; | |
104 } else if (strncmp(argv[i], "--", 2) != 0) { // It doesn't start with -- | |
105 options.filename_ = argv[i]; | |
rmcilroy
2016/02/15 11:15:19
check options.filename_ is empty beforehand (i.e.,
Stefano Sanfilippo
2016/02/15 13:26:33
Done.
| |
106 } else { | |
107 std::cerr << "ERROR: Unknonwn option " << argv[i] << "\n"; | |
108 options.has_unknown_options_ = true; | |
109 } | |
110 } | |
111 | |
112 return options; | |
113 } | |
114 | |
115 bool ProgramOptions::Validate() const { | |
116 if (has_unknown_options_) return false; | |
117 if (print_help_) return true; | |
118 | |
119 if (const_pool_type_ == | |
120 BytecodeExpectationsPrinter::ConstantPoolType::kUnknown) { | |
121 std::cerr << "ERROR: Unknown constant pool type.\n"; | |
122 return false; | |
123 } | |
124 | |
125 if (!read_from_stdin_ && filename_.empty()) { | |
126 std::cerr << "ERROR: No input file specified.\n"; | |
127 return false; | |
128 } | |
129 | |
130 if (read_from_stdin_ && !filename_.empty()) { | |
131 std::cerr << "ERROR: Reading from stdin, but input files supplied.\n"; | |
132 return false; | |
133 } | |
134 | |
135 return true; | |
136 } | |
137 | |
60 i::Isolate* GetInternalIsolate(v8::Isolate* isolate) { | 138 i::Isolate* GetInternalIsolate(v8::Isolate* isolate) { |
61 return reinterpret_cast<i::Isolate*>(isolate); | 139 return reinterpret_cast<i::Isolate*>(isolate); |
62 } | 140 } |
63 | 141 |
64 V8InitializationScope::V8InitializationScope(const char* exec_path) | 142 V8InitializationScope::V8InitializationScope(const char* exec_path) |
65 : platform_(v8::platform::CreateDefaultPlatform()) { | 143 : platform_(v8::platform::CreateDefaultPlatform()) { |
66 i::FLAG_ignition = true; | 144 i::FLAG_ignition = true; |
67 i::FLAG_always_opt = false; | 145 i::FLAG_always_opt = false; |
68 i::FLAG_allow_natives_syntax = true; | 146 i::FLAG_allow_natives_syntax = true; |
69 | 147 |
70 v8::V8::InitializeICU(); | 148 v8::V8::InitializeICU(); |
71 v8::V8::InitializeExternalStartupData(exec_path); | 149 v8::V8::InitializeExternalStartupData(exec_path); |
72 v8::V8::InitializePlatform(platform_.get()); | 150 v8::V8::InitializePlatform(platform_.get()); |
73 v8::V8::Initialize(); | 151 v8::V8::Initialize(); |
74 | 152 |
75 ArrayBufferAllocator allocator; | 153 ArrayBufferAllocator allocator; |
76 v8::Isolate::CreateParams create_params; | 154 v8::Isolate::CreateParams create_params; |
77 create_params.array_buffer_allocator = &allocator; | 155 create_params.array_buffer_allocator = &allocator; |
78 | 156 |
79 isolate_ = v8::Isolate::New(create_params); | 157 isolate_ = v8::Isolate::New(create_params); |
80 GetInternalIsolate(isolate_)->interpreter()->Initialize(); | 158 GetInternalIsolate(isolate_)->interpreter()->Initialize(); |
rmcilroy
2016/02/15 11:15:19
this shouldn't be necessary anymore given some pre
Stefano Sanfilippo
2016/02/15 13:26:34
Done.
| |
81 } | 159 } |
82 | 160 |
83 V8InitializationScope::~V8InitializationScope() { | 161 V8InitializationScope::~V8InitializationScope() { |
84 isolate_->Dispose(); | 162 isolate_->Dispose(); |
85 v8::V8::Dispose(); | 163 v8::V8::Dispose(); |
86 v8::V8::ShutdownPlatform(); | 164 v8::V8::ShutdownPlatform(); |
87 } | 165 } |
88 | 166 |
89 v8::Local<v8::String> V8StringFromUTF8(v8::Isolate* isolate, const char* data) { | 167 std::string ReadRawJSSnippet(std::istream& stream) { |
rmcilroy
2016/02/15 11:15:19
All references must be const (https://google.githu
Stefano Sanfilippo
2016/02/15 13:26:34
As discussed, I think it's best if we keep it cons
| |
90 return v8::String::NewFromUtf8(isolate, data, v8::NewStringType::kNormal) | 168 std::stringstream body_buffer; |
91 .ToLocalChecked(); | 169 CHECK(body_buffer << stream.rdbuf()); |
170 return body_buffer.str(); | |
92 } | 171 } |
93 | 172 |
94 std::string WrapCodeInFunction(const char* function_name, | 173 bool ReadNextSnippet(std::string* string, std::istream& stream) { |
rmcilroy
2016/02/15 11:15:20
ditto. Also rename string->string_out and put it a
Stefano Sanfilippo
2016/02/15 13:26:33
Done.
| |
95 const std::string& function_body) { | 174 std::string line; |
96 std::ostringstream program_stream; | 175 bool found_begin_snippet = false; |
97 program_stream << "function " << function_name << "() {" << function_body | 176 string->clear(); |
98 << "}\n" | 177 while (std::getline(stream, line)) { |
99 << function_name << "();"; | 178 if (line == "snippet: \"") { |
100 | 179 found_begin_snippet = true; |
101 return program_stream.str(); | 180 continue; |
181 } | |
182 if (!found_begin_snippet) continue; | |
183 if (line == "\"") return true; | |
184 // Skip two leading spaces we added, if the line is not shorter | |
185 if (line.size() >= 2) { | |
186 string->append(line.begin() + 2, line.end()); | |
187 } else { | |
188 *string += line; | |
rmcilroy
2016/02/15 11:15:20
What string gets added by this? Maybe just only ap
Stefano Sanfilippo
2016/02/15 13:26:34
This is done to preserve the original format, i.e.
| |
189 } | |
190 *string += '\n'; | |
191 } | |
192 return false; | |
102 } | 193 } |
103 | 194 |
104 v8::Local<v8::Value> CompileAndRun(v8::Isolate* isolate, | 195 bool ExtractSnippets(std::vector<std::string>* snippet_list, |
105 const v8::Local<v8::Context>& context, | 196 const ProgramOptions& options) { |
106 const char* program) { | 197 if (options.read_from_stdin()) { |
107 v8::Local<v8::String> source = V8StringFromUTF8(isolate, program); | 198 snippet_list->push_back(ReadRawJSSnippet(std::cin)); |
rmcilroy
2016/02/15 11:15:19
You can pull this into the code below (e.g., if op
Stefano Sanfilippo
2016/02/15 13:26:33
Done, although in a slightly different way because
| |
108 v8::Local<v8::Script> script = | 199 return true; |
109 v8::Script::Compile(context, source).ToLocalChecked(); | 200 } |
110 | 201 |
111 v8::Local<v8::Value> result; | 202 std::string snippet; |
112 CHECK(script->Run(context).ToLocal(&result)); | 203 std::ifstream body_file{options.filename()}; |
rmcilroy
2016/02/15 11:15:19
Please don't use curly bracket initialization for
Stefano Sanfilippo
2016/02/15 13:26:34
Done.
| |
204 if (!body_file.is_open()) { | |
205 std::cerr << "ERROR: Could not open '" << options.filename() << "'.\n"; | |
206 return false; | |
207 } | |
208 if (options.read_raw_js_snippet()) { | |
209 snippet_list->push_back(ReadRawJSSnippet(body_file)); | |
210 } else { | |
211 while (ReadNextSnippet(&snippet, body_file)) { | |
212 snippet_list->push_back(snippet); | |
213 } | |
214 } | |
113 | 215 |
114 return result; | 216 return true; |
115 } | 217 } |
116 | 218 |
117 i::Handle<v8::internal::BytecodeArray> GetBytecodeArrayForGlobal( | 219 void GenerateExpectationsFile( |
118 v8::Isolate* isolate, const v8::Local<v8::Context>& context, | 220 std::ostream& stream, const std::vector<std::string>& snippet_list, |
119 const char* global_name) { | 221 BytecodeExpectationsPrinter::ConstantPoolType const_pool_type, |
120 v8::Local<v8::String> v8_global_name = V8StringFromUTF8(isolate, global_name); | 222 const char* exec_path) { |
121 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( | 223 V8InitializationScope platform(exec_path); |
122 context->Global()->Get(context, v8_global_name).ToLocalChecked()); | 224 { |
123 i::Handle<i::JSFunction> js_function = | 225 v8::Isolate::Scope isolate_scope{platform.isolate()}; |
124 i::Handle<i::JSFunction>::cast(v8::Utils::OpenHandle(*function)); | 226 v8::HandleScope handle_scope{platform.isolate()}; |
227 v8::Local<v8::Context> context = v8::Context::New(platform.isolate()); | |
228 v8::Context::Scope context_scope{context}; | |
125 | 229 |
126 i::Handle<i::BytecodeArray> bytecodes = i::handle( | 230 stream << "#\n# Autogenerated by generate-bytecode-expectations\n#\n\n"; |
127 js_function->shared()->bytecode_array(), GetInternalIsolate(isolate)); | |
128 | 231 |
129 return bytecodes; | 232 BytecodeExpectationsPrinter printer(platform.isolate(), const_pool_type); |
130 } | 233 for (const std::string& snippet : snippet_list) { |
131 | 234 printer.PrintExpectation(stream, snippet); |
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 } | 235 } |
152 } | 236 } |
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 } | 237 } |
197 | 238 |
198 void PrintBytecode(std::ostream& stream, | |
199 const BytecodeArrayIterator& bytecode_iter) { | |
200 Bytecode bytecode = bytecode_iter.current_bytecode(); | |
201 | |
202 stream << "B(" << Bytecodes::ToString(bytecode) << ')'; | |
203 | |
204 int operands_count = Bytecodes::NumberOfOperands(bytecode); | |
205 for (int op_index = 0; op_index < operands_count; ++op_index) { | |
206 stream << ", "; | |
207 PrintBytecodeOperand(stream, bytecode_iter, bytecode, op_index); | |
208 } | |
209 } | |
210 | |
211 void PrintV8String(std::ostream& stream, i::String* string) { | |
212 stream << '"'; | |
213 for (int i = 0, length = string->length(); i < length; ++i) { | |
214 stream << i::AsEscapedUC16ForJSON(string->Get(i)); | |
215 } | |
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); | |
339 { | |
340 v8::Isolate::Scope isolate_scope(platform.isolate()); | |
341 v8::HandleScope handle_scope(platform.isolate()); | |
342 v8::Local<v8::Context> context = v8::Context::New(platform.isolate()); | |
343 v8::Context::Scope context_scope(context); | |
344 | |
345 std::string source_code = WrapCodeInFunction(wrapper_function_name, body); | |
346 CompileAndRun(platform.isolate(), context, source_code.c_str()); | |
347 | |
348 i::Handle<i::BytecodeArray> bytecode_array = GetBytecodeArrayForGlobal( | |
349 platform.isolate(), context, wrapper_function_name); | |
350 | |
351 PrintBytecodeArray(std::cout, bytecode_array, body, platform.isolate(), | |
352 constant_pool_type); | |
353 } | |
354 } | |
355 | |
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) { | 239 void PrintUsage(const char* exec_path) { |
383 std::cerr << "Usage: " << exec_path | 240 std::cerr |
384 << " (int|double|string|mixed) [filename.js|-]\n\n" | 241 << "\nUsage: " << exec_path |
385 "First argument is the type of objects in the constant pool.\n\n" | 242 << " [OPTIONS]... [INPUT FILE]\n\n" |
386 "Omitting the second argument or - reads from standard input.\n" | 243 "Options:\n" |
387 "Anything else is interpreted as a filename.\n\n" | 244 " --help Print this help message.\n" |
388 "This tool is intended as a help in writing tests.\n" | 245 " --raw-js Read raw JavaScript, instead of the output format.\n" |
389 "Please, DO NOT blindly copy and paste the output " | 246 " --stdin Read from standard input instead of file.\n" |
390 "into the test suite.\n"; | 247 " --pool-type (int|double|string|mixed)\n" |
248 " specify the type of the entries in the constant pool " | |
249 "(default: mixed).\n" | |
250 "\n" | |
251 "Each raw JavaScript file is interpreted as a single snippet.\n\n" | |
252 "This tool is intended as a help in writing tests.\n" | |
253 "Please, DO NOT blindly copy and paste the output " | |
254 "into the test suite.\n"; | |
391 } | 255 } |
392 | 256 |
393 } // namespace | 257 } // namespace |
394 | 258 |
395 int main(int argc, char** argv) { | 259 int main(int argc, char** argv) { |
396 if (argc < 2) { | 260 ProgramOptions options = ProgramOptions::FromCommandLine(argc, argv); |
261 | |
262 if (!options.Validate() || options.print_help()) { | |
397 PrintUsage(argv[0]); | 263 PrintUsage(argv[0]); |
398 return 1; | 264 return options.print_help() ? 0 : 1; |
399 } | 265 } |
400 | 266 |
401 if (argc > 1 && strcmp(argv[1], "--help") == 0) { | 267 std::vector<std::string> snippet_list; |
402 PrintUsage(argv[0]); | 268 if (!ExtractSnippets(&snippet_list, options)) { |
403 return 0; | 269 return 2; |
404 } | 270 } |
405 | 271 |
406 const char* body_filename = (argc > 2 ? argv[2] : "-"); | 272 GenerateExpectationsFile(std::cout, snippet_list, options.const_pool_type(), |
407 const char* const_pool_type_string = argv[1]; | 273 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 } | 274 } |
OLD | NEW |