Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1311)

Side by Side Diff: src/d8.cc

Issue 7134040: added --stress-opt option to d8 (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« src/d8.h ('K') | « src/d8.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2009 the V8 project authors. All rights reserved. 1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 23 matching lines...) Expand all
34 34
35 #include "v8.h" 35 #include "v8.h"
36 36
37 #include "d8.h" 37 #include "d8.h"
38 #include "d8-debug.h" 38 #include "d8-debug.h"
39 #include "debug.h" 39 #include "debug.h"
40 #include "api.h" 40 #include "api.h"
41 #include "natives.h" 41 #include "natives.h"
42 #include "platform.h" 42 #include "platform.h"
43 43
44
45 namespace v8 { 44 namespace v8 {
46 45
47
48 const char* Shell::kHistoryFileName = ".d8_history"; 46 const char* Shell::kHistoryFileName = ".d8_history";
49 const char* Shell::kPrompt = "d8> "; 47 const char* Shell::kPrompt = "d8> ";
50 48
51
52 LineEditor *LineEditor::first_ = NULL; 49 LineEditor *LineEditor::first_ = NULL;
53 50
54 51 LineEditor::LineEditor(Type type, const char* name) :
55 LineEditor::LineEditor(Type type, const char* name) 52 type_(type), name_(name), next_(first_) {
56 : type_(type),
57 name_(name),
58 next_(first_) {
59 first_ = this; 53 first_ = this;
60 } 54 }
61 55
62
63 LineEditor* LineEditor::Get() { 56 LineEditor* LineEditor::Get() {
64 LineEditor* current = first_; 57 LineEditor* current = first_;
65 LineEditor* best = current; 58 LineEditor* best = current;
66 while (current != NULL) { 59 while (current != NULL) {
67 if (current->type_ > best->type_) 60 if (current->type_ > best->type_)
68 best = current; 61 best = current;
69 current = current->next_; 62 current = current->next_;
70 } 63 }
71 return best; 64 return best;
72 } 65 }
73 66
74
75 class DumbLineEditor: public LineEditor { 67 class DumbLineEditor: public LineEditor {
76 public: 68 public:
77 DumbLineEditor() : LineEditor(LineEditor::DUMB, "dumb") { } 69 DumbLineEditor() :
70 LineEditor(LineEditor::DUMB, "dumb") {
71 }
78 virtual i::SmartPointer<char> Prompt(const char* prompt); 72 virtual i::SmartPointer<char> Prompt(const char* prompt);
79 }; 73 };
80 74
81
82 static DumbLineEditor dumb_line_editor; 75 static DumbLineEditor dumb_line_editor;
83 76
84
85 i::SmartPointer<char> DumbLineEditor::Prompt(const char* prompt) { 77 i::SmartPointer<char> DumbLineEditor::Prompt(const char* prompt) {
86 static const int kBufferSize = 256; 78 static const int kBufferSize = 256;
87 char buffer[kBufferSize]; 79 char buffer[kBufferSize];
88 printf("%s", prompt); 80 printf("%s", prompt);
89 char* str = fgets(buffer, kBufferSize, stdin); 81 char* str = fgets(buffer, kBufferSize, stdin);
90 return i::SmartPointer<char>(str ? i::StrDup(str) : str); 82 return i::SmartPointer<char>(str ? i::StrDup(str) : str);
91 } 83 }
92 84
93
94 CounterMap* Shell::counter_map_; 85 CounterMap* Shell::counter_map_;
95 i::OS::MemoryMappedFile* Shell::counters_file_ = NULL; 86 i::OS::MemoryMappedFile* Shell::counters_file_ = NULL;
96 CounterCollection Shell::local_counters_; 87 CounterCollection Shell::local_counters_;
97 CounterCollection* Shell::counters_ = &local_counters_; 88 CounterCollection* Shell::counters_ = &local_counters_;
98 Persistent<Context> Shell::utility_context_; 89 Persistent<Context> Shell::utility_context_;
99 Persistent<Context> Shell::evaluation_context_; 90 Persistent<Context> Shell::evaluation_context_;
100 91
101
102 bool CounterMap::Match(void* key1, void* key2) { 92 bool CounterMap::Match(void* key1, void* key2) {
103 const char* name1 = reinterpret_cast<const char*>(key1); 93 const char* name1 = reinterpret_cast<const char*> (key1);
104 const char* name2 = reinterpret_cast<const char*>(key2); 94 const char* name2 = reinterpret_cast<const char*> (key2);
105 return strcmp(name1, name2) == 0; 95 return strcmp(name1, name2) == 0;
106 } 96 }
107 97
108
109 // Converts a V8 value to a C string. 98 // Converts a V8 value to a C string.
110 const char* Shell::ToCString(const v8::String::Utf8Value& value) { 99 const char* Shell::ToCString(const v8::String::Utf8Value& value) {
111 return *value ? *value : "<string conversion failed>"; 100 return *value ? *value : "<string conversion failed>";
112 } 101 }
113 102
114
115 // Executes a string within the current v8 context. 103 // Executes a string within the current v8 context.
116 bool Shell::ExecuteString(Handle<String> source, 104 bool Shell::ExecuteString(Handle<String> source, Handle<Value> name,
117 Handle<Value> name, 105 bool print_result, bool report_exceptions) {
118 bool print_result,
119 bool report_exceptions) {
120 HandleScope handle_scope; 106 HandleScope handle_scope;
121 TryCatch try_catch; 107 TryCatch try_catch;
122 if (i::FLAG_debugger) { 108 if (i::FLAG_debugger) {
123 // When debugging make exceptions appear to be uncaught. 109 // When debugging make exceptions appear to be uncaught.
124 try_catch.SetVerbose(true); 110 try_catch.SetVerbose(true);
125 } 111 }
126 Handle<Script> script = Script::Compile(source, name); 112 Handle<Script> script = Script::Compile(source, name);
127 if (script.IsEmpty()) { 113 if (script.IsEmpty()) {
128 // Print errors that happened during compilation. 114 // Print errors that happened during compilation.
129 if (report_exceptions && !i::FLAG_debugger) 115 if (report_exceptions && !i::FLAG_debugger)
(...skipping 14 matching lines...) Expand all
144 // the returned value. 130 // the returned value.
145 v8::String::Utf8Value str(result); 131 v8::String::Utf8Value str(result);
146 const char* cstr = ToCString(str); 132 const char* cstr = ToCString(str);
147 printf("%s\n", cstr); 133 printf("%s\n", cstr);
148 } 134 }
149 return true; 135 return true;
150 } 136 }
151 } 137 }
152 } 138 }
153 139
154
155 Handle<Value> Shell::Print(const Arguments& args) { 140 Handle<Value> Shell::Print(const Arguments& args) {
156 Handle<Value> val = Write(args); 141 Handle<Value> val = Write(args);
157 printf("\n"); 142 printf("\n");
158 return val; 143 return val;
159 } 144 }
160 145
161
Søren Thygesen Gjesse 2011/06/09 10:05:48 We always try to keep two blank lines between func
162 Handle<Value> Shell::Write(const Arguments& args) { 146 Handle<Value> Shell::Write(const Arguments& args) {
163 for (int i = 0; i < args.Length(); i++) { 147 for (int i = 0; i < args.Length(); i++) {
164 HandleScope handle_scope; 148 HandleScope handle_scope;
165 if (i != 0) { 149 if (i != 0) {
166 printf(" "); 150 printf(" ");
167 } 151 }
168 v8::String::Utf8Value str(args[i]); 152 v8::String::Utf8Value str(args[i]);
169 int n = fwrite(*str, sizeof(**str), str.length(), stdout); 153 int n = fwrite(*str, sizeof(**str), str.length(), stdout);
170 if (n != str.length()) { 154 if (n != str.length()) {
171 printf("Error in fwrite\n"); 155 printf("Error in fwrite\n");
172 exit(1); 156 exit(1);
173 } 157 }
174 } 158 }
175 return Undefined(); 159 return Undefined();
176 } 160 }
177 161
178
179 Handle<Value> Shell::Read(const Arguments& args) { 162 Handle<Value> Shell::Read(const Arguments& args) {
180 String::Utf8Value file(args[0]); 163 String::Utf8Value file(args[0]);
181 if (*file == NULL) { 164 if (*file == NULL) {
182 return ThrowException(String::New("Error loading file")); 165 return ThrowException(String::New("Error loading file"));
183 } 166 }
184 Handle<String> source = ReadFile(*file); 167 Handle<String> source = ReadFile(*file);
185 if (source.IsEmpty()) { 168 if (source.IsEmpty()) {
186 return ThrowException(String::New("Error loading file")); 169 return ThrowException(String::New("Error loading file"));
187 } 170 }
188 return source; 171 return source;
189 } 172 }
190 173
191
192 Handle<Value> Shell::ReadLine(const Arguments& args) { 174 Handle<Value> Shell::ReadLine(const Arguments& args) {
193 i::SmartPointer<char> line(i::ReadLine("")); 175 i::SmartPointer<char> line(i::ReadLine(""));
194 if (*line == NULL) { 176 if (*line == NULL) {
195 return Null(); 177 return Null();
196 } 178 }
197 size_t len = strlen(*line); 179 size_t len = strlen(*line);
198 if (len > 0 && line[len - 1] == '\n') { 180 if (len > 0 && line[len - 1] == '\n') {
199 --len; 181 --len;
200 } 182 }
201 return String::New(*line, len); 183 return String::New(*line, len);
202 } 184 }
203 185
204
205 Handle<Value> Shell::Load(const Arguments& args) { 186 Handle<Value> Shell::Load(const Arguments& args) {
206 for (int i = 0; i < args.Length(); i++) { 187 for (int i = 0; i < args.Length(); i++) {
207 HandleScope handle_scope; 188 HandleScope handle_scope;
208 String::Utf8Value file(args[i]); 189 String::Utf8Value file(args[i]);
209 if (*file == NULL) { 190 if (*file == NULL) {
210 return ThrowException(String::New("Error loading file")); 191 return ThrowException(String::New("Error loading file"));
211 } 192 }
212 Handle<String> source = ReadFile(*file); 193 Handle<String> source = ReadFile(*file);
213 if (source.IsEmpty()) { 194 if (source.IsEmpty()) {
214 return ThrowException(String::New("Error loading file")); 195 return ThrowException(String::New("Error loading file"));
215 } 196 }
216 if (!ExecuteString(source, String::New(*file), false, false)) { 197 if (!ExecuteString(source, String::New(*file), false, false)) {
217 return ThrowException(String::New("Error executing file")); 198 return ThrowException(String::New("Error executing file"));
218 } 199 }
219 } 200 }
220 return Undefined(); 201 return Undefined();
221 } 202 }
222 203
223
224 Handle<Value> Shell::CreateExternalArray(const Arguments& args, 204 Handle<Value> Shell::CreateExternalArray(const Arguments& args,
225 ExternalArrayType type, 205 ExternalArrayType type, int element_size) {
226 int element_size) {
227 if (args.Length() != 1) { 206 if (args.Length() != 1) {
228 return ThrowException( 207 return ThrowException(String::New("Array constructor needs one parameter.")) ;
229 String::New("Array constructor needs one parameter."));
230 } 208 }
231 int length = args[0]->Int32Value(); 209 int length = args[0]->Int32Value();
232 void* data = malloc(length * element_size); 210 void* data = malloc(length * element_size);
233 memset(data, 0, length * element_size); 211 memset(data, 0, length * element_size);
234 Handle<Object> array = Object::New(); 212 Handle<Object> array = Object::New();
235 Persistent<Object> persistent_array = Persistent<Object>::New(array); 213 Persistent<Object> persistent_array = Persistent<Object>::New(array);
236 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); 214 persistent_array.MakeWeak(data, ExternalArrayWeakCallback);
237 persistent_array.MarkIndependent(); 215 persistent_array.MarkIndependent();
238 array->SetIndexedPropertiesToExternalArrayData(data, type, length); 216 array->SetIndexedPropertiesToExternalArrayData(data, type, length);
239 array->Set(String::New("length"), Int32::New(length), ReadOnly); 217 array->Set(String::New("length"), Int32::New(length), ReadOnly);
240 array->Set(String::New("BYTES_PER_ELEMENT"), Int32::New(element_size)); 218 array->Set(String::New("BYTES_PER_ELEMENT"), Int32::New(element_size));
241 return array; 219 return array;
242 } 220 }
243 221
244
245 void Shell::ExternalArrayWeakCallback(Persistent<Value> object, void* data) { 222 void Shell::ExternalArrayWeakCallback(Persistent<Value> object, void* data) {
246 free(data); 223 free(data);
247 object.Dispose(); 224 object.Dispose();
248 } 225 }
249 226
250
251 Handle<Value> Shell::Int8Array(const Arguments& args) { 227 Handle<Value> Shell::Int8Array(const Arguments& args) {
252 return CreateExternalArray(args, v8::kExternalByteArray, sizeof(int8_t)); 228 return CreateExternalArray(args, v8::kExternalByteArray, sizeof(int8_t));
253 } 229 }
254 230
255
256 Handle<Value> Shell::Uint8Array(const Arguments& args) { 231 Handle<Value> Shell::Uint8Array(const Arguments& args) {
257 return CreateExternalArray(args, kExternalUnsignedByteArray, sizeof(uint8_t)); 232 return CreateExternalArray(args, kExternalUnsignedByteArray, sizeof(uint8_t));
258 } 233 }
259 234
260
261 Handle<Value> Shell::Int16Array(const Arguments& args) { 235 Handle<Value> Shell::Int16Array(const Arguments& args) {
262 return CreateExternalArray(args, kExternalShortArray, sizeof(int16_t)); 236 return CreateExternalArray(args, kExternalShortArray, sizeof(int16_t));
263 } 237 }
264 238
265
266 Handle<Value> Shell::Uint16Array(const Arguments& args) { 239 Handle<Value> Shell::Uint16Array(const Arguments& args) {
267 return CreateExternalArray(args, kExternalUnsignedShortArray, 240 return CreateExternalArray(args, kExternalUnsignedShortArray,
268 sizeof(uint16_t)); 241 sizeof(uint16_t));
269 } 242 }
270 243
271 Handle<Value> Shell::Int32Array(const Arguments& args) { 244 Handle<Value> Shell::Int32Array(const Arguments& args) {
272 return CreateExternalArray(args, kExternalIntArray, sizeof(int32_t)); 245 return CreateExternalArray(args, kExternalIntArray, sizeof(int32_t));
273 } 246 }
274 247
275
276 Handle<Value> Shell::Uint32Array(const Arguments& args) { 248 Handle<Value> Shell::Uint32Array(const Arguments& args) {
277 return CreateExternalArray(args, kExternalUnsignedIntArray, sizeof(uint32_t)); 249 return CreateExternalArray(args, kExternalUnsignedIntArray, sizeof(uint32_t));
278 } 250 }
279 251
280
281 Handle<Value> Shell::Float32Array(const Arguments& args) { 252 Handle<Value> Shell::Float32Array(const Arguments& args) {
282 return CreateExternalArray(args, kExternalFloatArray, 253 return CreateExternalArray(args, kExternalFloatArray, sizeof(float)); // NOLIN T
283 sizeof(float)); // NOLINT
284 } 254 }
285 255
286
287 Handle<Value> Shell::Float64Array(const Arguments& args) { 256 Handle<Value> Shell::Float64Array(const Arguments& args) {
288 return CreateExternalArray(args, kExternalDoubleArray, 257 return CreateExternalArray(args, kExternalDoubleArray, sizeof(double)); // NOL INT
289 sizeof(double)); // NOLINT
290 } 258 }
291 259
292
293 Handle<Value> Shell::PixelArray(const Arguments& args) { 260 Handle<Value> Shell::PixelArray(const Arguments& args) {
294 return CreateExternalArray(args, kExternalPixelArray, sizeof(uint8_t)); 261 return CreateExternalArray(args, kExternalPixelArray, sizeof(uint8_t));
295 } 262 }
296 263
297
298 Handle<Value> Shell::Yield(const Arguments& args) { 264 Handle<Value> Shell::Yield(const Arguments& args) {
299 v8::Unlocker unlocker; 265 v8::Unlocker unlocker;
300 return Undefined(); 266 return Undefined();
301 } 267 }
302 268
303
304 Handle<Value> Shell::Quit(const Arguments& args) { 269 Handle<Value> Shell::Quit(const Arguments& args) {
305 int exit_code = args[0]->Int32Value(); 270 int exit_code = args[0]->Int32Value();
306 OnExit(); 271 OnExit();
307 exit(exit_code); 272 exit(exit_code);
308 return Undefined(); 273 return Undefined();
309 } 274 }
310 275
311
312 Handle<Value> Shell::Version(const Arguments& args) { 276 Handle<Value> Shell::Version(const Arguments& args) {
313 return String::New(V8::GetVersion()); 277 return String::New(V8::GetVersion());
314 } 278 }
315 279
316
317 void Shell::ReportException(v8::TryCatch* try_catch) { 280 void Shell::ReportException(v8::TryCatch* try_catch) {
318 HandleScope handle_scope; 281 HandleScope handle_scope;
319 v8::String::Utf8Value exception(try_catch->Exception()); 282 v8::String::Utf8Value exception(try_catch->Exception());
320 const char* exception_string = ToCString(exception); 283 const char* exception_string = ToCString(exception);
321 Handle<Message> message = try_catch->Message(); 284 Handle<Message> message = try_catch->Message();
322 if (message.IsEmpty()) { 285 if (message.IsEmpty()) {
323 // V8 didn't provide any extra information about this error; just 286 // V8 didn't provide any extra information about this error; just
324 // print the exception. 287 // print the exception.
325 printf("%s\n", exception_string); 288 printf("%s\n", exception_string);
326 } else { 289 } else {
(...skipping 17 matching lines...) Expand all
344 } 307 }
345 printf("\n"); 308 printf("\n");
346 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); 309 v8::String::Utf8Value stack_trace(try_catch->StackTrace());
347 if (stack_trace.length() > 0) { 310 if (stack_trace.length() > 0) {
348 const char* stack_trace_string = ToCString(stack_trace); 311 const char* stack_trace_string = ToCString(stack_trace);
349 printf("%s\n", stack_trace_string); 312 printf("%s\n", stack_trace_string);
350 } 313 }
351 } 314 }
352 } 315 }
353 316
354
355 Handle<Array> Shell::GetCompletions(Handle<String> text, Handle<String> full) { 317 Handle<Array> Shell::GetCompletions(Handle<String> text, Handle<String> full) {
356 HandleScope handle_scope; 318 HandleScope handle_scope;
357 Context::Scope context_scope(utility_context_); 319 Context::Scope context_scope(utility_context_);
358 Handle<Object> global = utility_context_->Global(); 320 Handle<Object> global = utility_context_->Global();
359 Handle<Value> fun = global->Get(String::New("GetCompletions")); 321 Handle<Value> fun = global->Get(String::New("GetCompletions"));
360 static const int kArgc = 3; 322 static const int kArgc = 3;
361 Handle<Value> argv[kArgc] = { evaluation_context_->Global(), text, full }; 323 Handle<Value> argv[kArgc] = { evaluation_context_->Global(), text, full };
362 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); 324 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
363 return handle_scope.Close(Handle<Array>::Cast(val)); 325 return handle_scope.Close(Handle<Array>::Cast(val));
364 } 326 }
365 327
366
367 #ifdef ENABLE_DEBUGGER_SUPPORT 328 #ifdef ENABLE_DEBUGGER_SUPPORT
368 Handle<Object> Shell::DebugMessageDetails(Handle<String> message) { 329 Handle<Object> Shell::DebugMessageDetails(Handle<String> message) {
369 Context::Scope context_scope(utility_context_); 330 Context::Scope context_scope(utility_context_);
370 Handle<Object> global = utility_context_->Global(); 331 Handle<Object> global = utility_context_->Global();
371 Handle<Value> fun = global->Get(String::New("DebugMessageDetails")); 332 Handle<Value> fun = global->Get(String::New("DebugMessageDetails"));
372 static const int kArgc = 1; 333 static const int kArgc = 1;
373 Handle<Value> argv[kArgc] = { message }; 334 Handle<Value> argv[kArgc] = {message};
374 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); 335 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
375 return Handle<Object>::Cast(val); 336 return Handle<Object>::Cast(val);
376 } 337 }
377 338
378
379 Handle<Value> Shell::DebugCommandToJSONRequest(Handle<String> command) { 339 Handle<Value> Shell::DebugCommandToJSONRequest(Handle<String> command) {
380 Context::Scope context_scope(utility_context_); 340 Context::Scope context_scope(utility_context_);
381 Handle<Object> global = utility_context_->Global(); 341 Handle<Object> global = utility_context_->Global();
382 Handle<Value> fun = global->Get(String::New("DebugCommandToJSONRequest")); 342 Handle<Value> fun = global->Get(String::New("DebugCommandToJSONRequest"));
383 static const int kArgc = 1; 343 static const int kArgc = 1;
384 Handle<Value> argv[kArgc] = { command }; 344 Handle<Value> argv[kArgc] = {command};
385 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); 345 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
386 return val; 346 return val;
387 } 347 }
388 #endif 348 #endif
389 349
390
391 int32_t* Counter::Bind(const char* name, bool is_histogram) { 350 int32_t* Counter::Bind(const char* name, bool is_histogram) {
392 int i; 351 int i;
393 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) 352 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++)
394 name_[i] = static_cast<char>(name[i]); 353 name_[i] = static_cast<char> (name[i]);
395 name_[i] = '\0'; 354 name_[i] = '\0';
396 is_histogram_ = is_histogram; 355 is_histogram_ = is_histogram;
397 return ptr(); 356 return ptr();
398 } 357 }
399 358
400
401 void Counter::AddSample(int32_t sample) { 359 void Counter::AddSample(int32_t sample) {
402 count_++; 360 count_++;
403 sample_total_ += sample; 361 sample_total_ += sample;
404 } 362 }
405 363
406
407 CounterCollection::CounterCollection() { 364 CounterCollection::CounterCollection() {
408 magic_number_ = 0xDEADFACE; 365 magic_number_ = 0xDEADFACE;
409 max_counters_ = kMaxCounters; 366 max_counters_ = kMaxCounters;
410 max_name_size_ = Counter::kMaxNameSize; 367 max_name_size_ = Counter::kMaxNameSize;
411 counters_in_use_ = 0; 368 counters_in_use_ = 0;
412 } 369 }
413 370
414
415 Counter* CounterCollection::GetNextCounter() { 371 Counter* CounterCollection::GetNextCounter() {
416 if (counters_in_use_ == kMaxCounters) return NULL; 372 if (counters_in_use_ == kMaxCounters)
373 return NULL;
417 return &counters_[counters_in_use_++]; 374 return &counters_[counters_in_use_++];
418 } 375 }
419 376
420
421 void Shell::MapCounters(const char* name) { 377 void Shell::MapCounters(const char* name) {
422 counters_file_ = i::OS::MemoryMappedFile::create(name, 378 counters_file_ = i::OS::MemoryMappedFile::create(name,
423 sizeof(CounterCollection), &local_counters_); 379 sizeof(CounterCollection), &local_counters_);
424 void* memory = (counters_file_ == NULL) ? 380 void* memory = (counters_file_ == NULL) ? NULL : counters_file_->memory();
425 NULL : counters_file_->memory();
426 if (memory == NULL) { 381 if (memory == NULL) {
427 printf("Could not map counters file %s\n", name); 382 printf("Could not map counters file %s\n", name);
428 exit(1); 383 exit(1);
429 } 384 }
430 counters_ = static_cast<CounterCollection*>(memory); 385 counters_ = static_cast<CounterCollection*> (memory);
431 V8::SetCounterFunction(LookupCounter); 386 V8::SetCounterFunction(LookupCounter);
432 V8::SetCreateHistogramFunction(CreateHistogram); 387 V8::SetCreateHistogramFunction(CreateHistogram);
433 V8::SetAddHistogramSampleFunction(AddHistogramSample); 388 V8::SetAddHistogramSampleFunction(AddHistogramSample);
434 } 389 }
435 390
436
437 int CounterMap::Hash(const char* name) { 391 int CounterMap::Hash(const char* name) {
438 int h = 0; 392 int h = 0;
439 int c; 393 int c;
440 while ((c = *name++) != 0) { 394 while ((c = *name++) != 0) {
441 h += h << 5; 395 h += h << 5;
442 h += c; 396 h += c;
443 } 397 }
444 return h; 398 return h;
445 } 399 }
446 400
447
448 Counter* Shell::GetCounter(const char* name, bool is_histogram) { 401 Counter* Shell::GetCounter(const char* name, bool is_histogram) {
449 Counter* counter = counter_map_->Lookup(name); 402 Counter* counter = counter_map_->Lookup(name);
450 403
451 if (counter == NULL) { 404 if (counter == NULL) {
452 counter = counters_->GetNextCounter(); 405 counter = counters_->GetNextCounter();
453 if (counter != NULL) { 406 if (counter != NULL) {
454 counter_map_->Set(name, counter); 407 counter_map_->Set(name, counter);
455 counter->Bind(name, is_histogram); 408 counter->Bind(name, is_histogram);
456 } 409 }
457 } else { 410 } else {
458 ASSERT(counter->is_histogram() == is_histogram); 411 ASSERT(counter->is_histogram() == is_histogram);
459 } 412 }
460 return counter; 413 return counter;
461 } 414 }
462 415
463
464 int* Shell::LookupCounter(const char* name) { 416 int* Shell::LookupCounter(const char* name) {
465 Counter* counter = GetCounter(name, false); 417 Counter* counter = GetCounter(name, false);
466 418
467 if (counter != NULL) { 419 if (counter != NULL) {
468 return counter->ptr(); 420 return counter->ptr();
469 } else { 421 } else {
470 return NULL; 422 return NULL;
471 } 423 }
472 } 424 }
473 425
474 426 void* Shell::CreateHistogram(const char* name, int min, int max, size_t buckets) {
475 void* Shell::CreateHistogram(const char* name,
476 int min,
477 int max,
478 size_t buckets) {
479 return GetCounter(name, true); 427 return GetCounter(name, true);
480 } 428 }
481 429
482
483 void Shell::AddHistogramSample(void* histogram, int sample) { 430 void Shell::AddHistogramSample(void* histogram, int sample) {
484 Counter* counter = reinterpret_cast<Counter*>(histogram); 431 Counter* counter = reinterpret_cast<Counter*> (histogram);
485 counter->AddSample(sample); 432 counter->AddSample(sample);
486 } 433 }
487 434
435 void Shell::InstallUtilScript() {
436 Locker lock;
437 HandleScope scope;
438 Context::Scope utility_scope(utility_context_);
439 // Run the d8 shell utility script in the utility context
440 int source_index = i::NativesCollection<i::D8>::GetIndex("d8");
441 i::Vector<const char> shell_source =
442 i::NativesCollection<i::D8>::GetRawScriptSource(source_index);
443 i::Vector<const char> shell_source_name =
444 i::NativesCollection<i::D8>::GetScriptName(source_index);
445 Handle<String> source = String::New(shell_source.start(),
446 shell_source.length());
447 Handle<String> name = String::New(shell_source_name.start(),
448 shell_source_name.length());
449 Handle<Script> script = Script::Compile(source, name);
450 script->Run();
451 }
488 452
489 #ifdef COMPRESS_STARTUP_DATA_BZ2 453 #ifdef COMPRESS_STARTUP_DATA_BZ2
490 class BZip2Decompressor : public v8::StartupDataDecompressor { 454 class BZip2Decompressor : public v8::StartupDataDecompressor {
491 public: 455 public:
492 virtual ~BZip2Decompressor() { } 456 virtual ~BZip2Decompressor() {}
493 457
494 protected: 458 protected:
495 virtual int DecompressData(char* raw_data, 459 virtual int DecompressData(char* raw_data,
496 int* raw_data_size, 460 int* raw_data_size,
497 const char* compressed_data, 461 const char* compressed_data,
498 int compressed_data_size) { 462 int compressed_data_size) {
499 ASSERT_EQ(v8::StartupData::kBZip2, 463 ASSERT_EQ(v8::StartupData::kBZip2,
500 v8::V8::GetCompressedStartupDataAlgorithm()); 464 v8::V8::GetCompressedStartupDataAlgorithm());
501 unsigned int decompressed_size = *raw_data_size; 465 unsigned int decompressed_size = *raw_data_size;
502 int result = 466 int result =
503 BZ2_bzBuffToBuffDecompress(raw_data, 467 BZ2_bzBuffToBuffDecompress(raw_data,
504 &decompressed_size, 468 &decompressed_size,
505 const_cast<char*>(compressed_data), 469 const_cast<char*>(compressed_data),
506 compressed_data_size, 470 compressed_data_size,
507 0, 1); 471 0, 1);
508 if (result == BZ_OK) { 472 if (result == BZ_OK) {
509 *raw_data_size = decompressed_size; 473 *raw_data_size = decompressed_size;
510 } 474 }
511 return result; 475 return result;
512 } 476 }
513 }; 477 };
514 #endif 478 #endif
515 479
480 Handle<ObjectTemplate> Shell::CreateGlobalTemplate() {
481 Handle<ObjectTemplate> global_template = ObjectTemplate::New();
482 global_template->Set(String::New("print"), FunctionTemplate::New(Print));
483 global_template->Set(String::New("write"), FunctionTemplate::New(Write));
484 global_template->Set(String::New("read"), FunctionTemplate::New(Read));
485 global_template->Set(String::New("readline"), FunctionTemplate::New(ReadLine)) ;
486 global_template->Set(String::New("load"), FunctionTemplate::New(Load));
487 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit));
488 global_template->Set(String::New("version"), FunctionTemplate::New(Version));
489
490 // Bind the handlers for external arrays.
491 global_template->Set(String::New("Int8Array"),
492 FunctionTemplate::New(Int8Array));
493 global_template->Set(String::New("Uint8Array"),
494 FunctionTemplate::New(Uint8Array));
495 global_template->Set(String::New("Int16Array"),
496 FunctionTemplate::New(Int16Array));
497 global_template->Set(String::New("Uint16Array"),
498 FunctionTemplate::New(Uint16Array));
499 global_template->Set(String::New("Int32Array"),
500 FunctionTemplate::New(Int32Array));
501 global_template->Set(String::New("Uint32Array"),
502 FunctionTemplate::New(Uint32Array));
503 global_template->Set(String::New("Float32Array"),
504 FunctionTemplate::New(Float32Array));
505 global_template->Set(String::New("Float64Array"),
506 FunctionTemplate::New(Float64Array));
507 global_template->Set(String::New("PixelArray"),
508 FunctionTemplate::New(PixelArray));
509
510 #ifdef LIVE_OBJECT_LIST
511 global_template->Set(String::New("lol_is_enabled"), Boolean::New(true));
512 #else
513 global_template->Set(String::New("lol_is_enabled"), Boolean::New(false));
514 #endif
515 return global_template;
516 }
516 517
517 void Shell::Initialize() { 518 void Shell::Initialize() {
518 #ifdef COMPRESS_STARTUP_DATA_BZ2
519 BZip2Decompressor startup_data_decompressor;
520 int bz2_result = startup_data_decompressor.Decompress();
521 if (bz2_result != BZ_OK) {
522 fprintf(stderr, "bzip error code: %d\n", bz2_result);
523 exit(1);
524 }
525 #endif
526
527 Shell::counter_map_ = new CounterMap(); 519 Shell::counter_map_ = new CounterMap();
528 // Set up counters 520 // Set up counters
529 if (i::StrLength(i::FLAG_map_counters) != 0) 521 if (i::StrLength(i::FLAG_map_counters) != 0)
530 MapCounters(i::FLAG_map_counters); 522 MapCounters(i::FLAG_map_counters);
531 if (i::FLAG_dump_counters) { 523 if (i::FLAG_dump_counters) {
532 V8::SetCounterFunction(LookupCounter); 524 V8::SetCounterFunction(LookupCounter);
533 V8::SetCreateHistogramFunction(CreateHistogram); 525 V8::SetCreateHistogramFunction(CreateHistogram);
534 V8::SetAddHistogramSampleFunction(AddHistogramSample); 526 V8::SetAddHistogramSampleFunction(AddHistogramSample);
535 } 527 }
536 528
529 #ifdef COMPRESS_STARTUP_DATA_BZ2
530 BZip2Decompressor startup_data_decompressor;
531 int bz2_result = startup_data_decompressor.Decompress();
532 if (bz2_result != BZ_OK) {
533 fprintf(stderr, "bzip error code: %d\n", bz2_result);
534 exit(1);
535 }
536 #endif
537
537 // Initialize the global objects 538 // Initialize the global objects
538 HandleScope scope; 539 HandleScope scope;
539 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); 540 Handle<ObjectTemplate> global_template = CreateGlobalTemplate();
540 global_template->Set(String::New("print"), FunctionTemplate::New(Print));
541 global_template->Set(String::New("write"), FunctionTemplate::New(Write));
542 global_template->Set(String::New("read"), FunctionTemplate::New(Read));
543 global_template->Set(String::New("readline"),
544 FunctionTemplate::New(ReadLine));
545 global_template->Set(String::New("load"), FunctionTemplate::New(Load));
546 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit));
547 global_template->Set(String::New("version"), FunctionTemplate::New(Version));
548
549 // Bind the handlers for external arrays.
550 global_template->Set(String::New("Int8Array"),
551 FunctionTemplate::New(Int8Array));
552 global_template->Set(String::New("Uint8Array"),
553 FunctionTemplate::New(Uint8Array));
554 global_template->Set(String::New("Int16Array"),
555 FunctionTemplate::New(Int16Array));
556 global_template->Set(String::New("Uint16Array"),
557 FunctionTemplate::New(Uint16Array));
558 global_template->Set(String::New("Int32Array"),
559 FunctionTemplate::New(Int32Array));
560 global_template->Set(String::New("Uint32Array"),
561 FunctionTemplate::New(Uint32Array));
562 global_template->Set(String::New("Float32Array"),
563 FunctionTemplate::New(Float32Array));
564 global_template->Set(String::New("Float64Array"),
565 FunctionTemplate::New(Float64Array));
566 global_template->Set(String::New("PixelArray"),
567 FunctionTemplate::New(PixelArray));
568
569 #ifdef LIVE_OBJECT_LIST
570 global_template->Set(String::New("lol_is_enabled"), Boolean::New(true));
571 #else
572 global_template->Set(String::New("lol_is_enabled"), Boolean::New(false));
573 #endif
574 541
575 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(); 542 Handle<ObjectTemplate> os_templ = ObjectTemplate::New();
576 AddOSMethods(os_templ); 543 AddOSMethods(os_templ);
577 global_template->Set(String::New("os"), os_templ); 544 global_template->Set(String::New("os"), os_templ);
578 545
579 utility_context_ = Context::New(NULL, global_template); 546 utility_context_ = Context::New(NULL, global_template);
580 utility_context_->SetSecurityToken(Undefined()); 547 utility_context_->SetSecurityToken(Undefined());
581 Context::Scope utility_scope(utility_context_); 548 Context::Scope utility_scope(utility_context_);
582 549
583 i::JSArguments js_args = i::FLAG_js_arguments; 550 i::JSArguments js_args = i::FLAG_js_arguments;
584 i::Handle<i::FixedArray> arguments_array = 551 i::Handle<i::FixedArray> arguments_array = FACTORY->NewFixedArray(
585 FACTORY->NewFixedArray(js_args.argc()); 552 js_args.argc());
586 for (int j = 0; j < js_args.argc(); j++) { 553 for (int j = 0; j < js_args.argc(); j++) {
587 i::Handle<i::String> arg = 554 i::Handle<i::String> arg = FACTORY->NewStringFromUtf8(
588 FACTORY->NewStringFromUtf8(i::CStrVector(js_args[j])); 555 i::CStrVector(js_args[j]));
589 arguments_array->set(j, *arg); 556 arguments_array->set(j, *arg);
590 } 557 }
591 i::Handle<i::JSArray> arguments_jsarray = 558 i::Handle<i::JSArray> arguments_jsarray = FACTORY->NewJSArrayWithElements(
592 FACTORY->NewJSArrayWithElements(arguments_array); 559 arguments_array);
593 global_template->Set(String::New("arguments"), 560 global_template->Set(String::New("arguments"),
594 Utils::ToLocal(arguments_jsarray)); 561 Utils::ToLocal(arguments_jsarray));
595 562
596 #ifdef ENABLE_DEBUGGER_SUPPORT 563 #ifdef ENABLE_DEBUGGER_SUPPORT
597 // Install the debugger object in the utility scope 564 // Install the debugger object in the utility scope
598 i::Debug* debug = i::Isolate::Current()->debug(); 565 i::Debug* debug = i::Isolate::Current()->debug();
599 debug->Load(); 566 debug->Load();
600 i::Handle<i::JSObject> js_debug 567 i::Handle<i::JSObject> js_debug
601 = i::Handle<i::JSObject>(debug->debug_context()->global()); 568 = i::Handle<i::JSObject>(debug->debug_context()->global());
602 utility_context_->Global()->Set(String::New("$debug"), 569 utility_context_->Global()->Set(String::New("$debug"),
603 Utils::ToLocal(js_debug)); 570 Utils::ToLocal(js_debug));
604 #endif 571 #endif
572 }
605 573
606 // Run the d8 shell utility script in the utility context 574 void Shell::RenewEvalContext() {
607 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); 575 // Initialize the global objects
608 i::Vector<const char> shell_source 576 HandleScope scope;
609 = i::NativesCollection<i::D8>::GetRawScriptSource(source_index); 577 Handle<ObjectTemplate> global_template = CreateGlobalTemplate();
610 i::Vector<const char> shell_source_name
611 = i::NativesCollection<i::D8>::GetScriptName(source_index);
612 Handle<String> source = String::New(shell_source.start(),
613 shell_source.length());
614 Handle<String> name = String::New(shell_source_name.start(),
615 shell_source_name.length());
616 Handle<Script> script = Script::Compile(source, name);
617 script->Run();
618 578
619 // Mark the d8 shell script as native to avoid it showing up as normal source 579 // (Re-)create the evaluation context
620 // in the debugger. 580 if (!evaluation_context_.IsEmpty()) {
621 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); 581 evaluation_context_.Dispose();
622 i::Handle<i::Script> script_object = compiled_script->IsJSFunction() 582 }
623 ? i::Handle<i::Script>(i::Script::cast(
624 i::JSFunction::cast(*compiled_script)->shared()->script()))
625 : i::Handle<i::Script>(i::Script::cast(
626 i::SharedFunctionInfo::cast(*compiled_script)->script()));
627 script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE));
628
629 // Create the evaluation context
630 evaluation_context_ = Context::New(NULL, global_template); 583 evaluation_context_ = Context::New(NULL, global_template);
631 evaluation_context_->SetSecurityToken(Undefined()); 584 evaluation_context_->SetSecurityToken(Undefined());
632 585
633 #ifdef ENABLE_DEBUGGER_SUPPORT 586 #ifdef ENABLE_DEBUGGER_SUPPORT
587 i::Debug* debug = i::Isolate::Current()->debug();
588 debug->Load();
589
634 // Set the security token of the debug context to allow access. 590 // Set the security token of the debug context to allow access.
635 debug->debug_context()->set_security_token(HEAP->undefined_value()); 591 debug->debug_context()->set_security_token(HEAP->undefined_value());
636 592
637 // Start the debugger agent if requested. 593 // Start the debugger agent if requested.
638 if (i::FLAG_debugger_agent) { 594 if (i::FLAG_debugger_agent) {
639 v8::Debug::EnableAgent("d8 shell", i::FLAG_debugger_port, true); 595 v8::Debug::EnableAgent("d8 shell", i::FLAG_debugger_port, true);
640 } 596 }
641 597
642 // Start the in-process debugger if requested. 598 // Start the in-process debugger if requested.
643 if (i::FLAG_debugger && !i::FLAG_debugger_agent) { 599 if (i::FLAG_debugger && !i::FLAG_debugger_agent) {
644 v8::Debug::SetDebugEventListener(HandleDebugEvent); 600 v8::Debug::SetDebugEventListener(HandleDebugEvent);
645 } 601 }
646 #endif 602 #endif
647 } 603 }
648 604
649
650 void Shell::OnExit() { 605 void Shell::OnExit() {
651 if (i::FLAG_dump_counters) { 606 if (i::FLAG_dump_counters) {
652 ::printf("+----------------------------------------+-------------+\n"); 607 ::printf("+----------------------------------------+-------------+\n");
653 ::printf("| Name | Value |\n"); 608 ::printf("| Name | Value |\n");
654 ::printf("+----------------------------------------+-------------+\n"); 609 ::printf("+----------------------------------------+-------------+\n");
655 for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) { 610 for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) {
656 Counter* counter = i.CurrentValue(); 611 Counter* counter = i.CurrentValue();
657 if (counter->is_histogram()) { 612 if (counter->is_histogram()) {
658 ::printf("| c:%-36s | %11i |\n", i.CurrentKey(), counter->count()); 613 ::printf("| c:%-36s | %11i |\n", i.CurrentKey(), counter->count());
659 ::printf("| t:%-36s | %11i |\n", 614 ::printf("| t:%-36s | %11i |\n", i.CurrentKey(),
660 i.CurrentKey(), 615 counter->sample_total());
661 counter->sample_total());
662 } else { 616 } else {
663 ::printf("| %-38s | %11i |\n", i.CurrentKey(), counter->count()); 617 ::printf("| %-38s | %11i |\n", i.CurrentKey(), counter->count());
664 } 618 }
665 } 619 }
666 ::printf("+----------------------------------------+-------------+\n"); 620 ::printf("+----------------------------------------+-------------+\n");
667 } 621 }
668 if (counters_file_ != NULL) 622 if (counters_file_ != NULL)
669 delete counters_file_; 623 delete counters_file_;
670 } 624 }
671 625
672
673 static char* ReadChars(const char* name, int* size_out) { 626 static char* ReadChars(const char* name, int* size_out) {
674 v8::Unlocker unlocker; // Release the V8 lock while reading files. 627 v8::Unlocker unlocker; // Release the V8 lock while reading files.
675 FILE* file = i::OS::FOpen(name, "rb"); 628 FILE* file = i::OS::FOpen(name, "rb");
676 if (file == NULL) return NULL; 629 if (file == NULL)
630 return NULL;
677 631
678 fseek(file, 0, SEEK_END); 632 fseek(file, 0, SEEK_END);
679 int size = ftell(file); 633 int size = ftell(file);
680 rewind(file); 634 rewind(file);
681 635
682 char* chars = new char[size + 1]; 636 char* chars = new char[size + 1];
683 chars[size] = '\0'; 637 chars[size] = '\0';
684 for (int i = 0; i < size;) { 638 for (int i = 0; i < size;) {
685 int read = fread(&chars[i], 1, size - i, file); 639 int read = fread(&chars[i], 1, size - i, file);
686 i += read; 640 i += read;
687 } 641 }
688 fclose(file); 642 fclose(file);
689 *size_out = size; 643 *size_out = size;
690 return chars; 644 return chars;
691 } 645 }
692 646
693
694 static char* ReadToken(char* data, char token) { 647 static char* ReadToken(char* data, char token) {
695 char* next = i::OS::StrChr(data, token); 648 char* next = i::OS::StrChr(data, token);
696 if (next != NULL) { 649 if (next != NULL) {
697 *next = '\0'; 650 *next = '\0';
698 return (next + 1); 651 return (next + 1);
699 } 652 }
700 653
701 return NULL; 654 return NULL;
702 } 655 }
703 656
704
705 static char* ReadLine(char* data) { 657 static char* ReadLine(char* data) {
706 return ReadToken(data, '\n'); 658 return ReadToken(data, '\n');
707 } 659 }
708 660
709
710 static char* ReadWord(char* data) { 661 static char* ReadWord(char* data) {
711 return ReadToken(data, ' '); 662 return ReadToken(data, ' ');
712 } 663 }
713 664
714
715 // Reads a file into a v8 string. 665 // Reads a file into a v8 string.
716 Handle<String> Shell::ReadFile(const char* name) { 666 Handle<String> Shell::ReadFile(const char* name) {
717 int size = 0; 667 int size = 0;
718 char* chars = ReadChars(name, &size); 668 char* chars = ReadChars(name, &size);
719 if (chars == NULL) return Handle<String>(); 669 if (chars == NULL)
670 return Handle<String> ();
720 Handle<String> result = String::New(chars); 671 Handle<String> result = String::New(chars);
721 delete[] chars; 672 delete[] chars;
722 return result; 673 return result;
723 } 674 }
724 675
676 void Shell::RunShell() {
725 677
726 void Shell::RunShell() {
727 LineEditor* editor = LineEditor::Get(); 678 LineEditor* editor = LineEditor::Get();
728 printf("V8 version %s [console: %s]\n", V8::GetVersion(), editor->name()); 679 printf("V8 version %s [console: %s]\n", V8::GetVersion(), editor->name());
729 if (i::FLAG_debugger) { 680 if (i::FLAG_debugger) {
730 printf("JavaScript debugger enabled\n"); 681 printf("JavaScript debugger enabled\n");
731 } 682 }
732 editor->Open(); 683 editor->Open();
684
733 while (true) { 685 while (true) {
734 Locker locker; 686 Locker locker;
735 HandleScope handle_scope; 687 HandleScope handle_scope;
736 Context::Scope context_scope(evaluation_context_); 688 Context::Scope context_scope(evaluation_context_);
737 i::SmartPointer<char> input = editor->Prompt(Shell::kPrompt); 689 i::SmartPointer<char> input = editor->Prompt(Shell::kPrompt);
738 if (input.is_empty()) 690 if (input.is_empty())
739 break; 691 break;
740 editor->AddHistory(*input); 692 editor->AddHistory(*input);
741 Handle<String> name = String::New("(d8)"); 693 Handle<String> name = String::New("(d8)");
742 ExecuteString(String::New(*input), name, true, true); 694 ExecuteString(String::New(*input), name, true, true);
743 } 695 }
744 editor->Close(); 696 editor->Close();
745 printf("\n"); 697 printf("\n");
746 } 698 }
747 699
748 700 class ShellThread: public i::Thread {
749 class ShellThread : public i::Thread { 701 public:
750 public: 702 ShellThread(i::Isolate* isolate, int no, i::Vector<const char> files) :
751 ShellThread(i::Isolate* isolate, int no, i::Vector<const char> files) 703 Thread(isolate, "d8:ShellThread"), no_(no), files_(files) {
752 : Thread(isolate, "d8:ShellThread"), 704 }
753 no_(no), files_(files) { }
754 virtual void Run(); 705 virtual void Run();
755 private: 706 private:
756 int no_; 707 int no_;
757 i::Vector<const char> files_; 708 i::Vector<const char> files_;
758 }; 709 };
759 710
760
761 void ShellThread::Run() { 711 void ShellThread::Run() {
762 // Prepare the context for this thread. 712 // Prepare the context for this thread.
763 Locker locker; 713 Locker locker;
764 HandleScope scope; 714 HandleScope scope;
765 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); 715 Handle<ObjectTemplate> global_template = Shell::CreateGlobalTemplate();
766 global_template->Set(String::New("print"),
767 FunctionTemplate::New(Shell::Print));
768 global_template->Set(String::New("write"),
769 FunctionTemplate::New(Shell::Write));
770 global_template->Set(String::New("read"),
771 FunctionTemplate::New(Shell::Read));
772 global_template->Set(String::New("readline"),
773 FunctionTemplate::New(Shell::ReadLine));
774 global_template->Set(String::New("load"),
775 FunctionTemplate::New(Shell::Load));
776 global_template->Set(String::New("yield"),
777 FunctionTemplate::New(Shell::Yield));
778 global_template->Set(String::New("version"),
779 FunctionTemplate::New(Shell::Version));
780 716
781 char* ptr = const_cast<char*>(files_.start()); 717 char* ptr = const_cast<char*> (files_.start());
782 while ((ptr != NULL) && (*ptr != '\0')) { 718 while ((ptr != NULL) && (*ptr != '\0')) {
783 // For each newline-separated line. 719 // For each newline-separated line.
784 char* next_line = ReadLine(ptr); 720 char* next_line = ReadLine(ptr);
785 721
786 if (*ptr == '#') { 722 if (*ptr == '#') {
787 // Skip comment lines. 723 // Skip comment lines.
788 ptr = next_line; 724 ptr = next_line;
789 continue; 725 continue;
790 } 726 }
791 727
(...skipping 17 matching lines...) Expand all
809 } 745 }
810 746
811 Shell::ExecuteString(str, String::New(filename), false, false); 747 Shell::ExecuteString(str, String::New(filename), false, false);
812 } 748 }
813 749
814 thread_context.Dispose(); 750 thread_context.Dispose();
815 ptr = next_line; 751 ptr = next_line;
816 } 752 }
817 } 753 }
818 754
819 755 int Shell::RunMain(int argc, char* argv[]) {
820 int Shell::Main(int argc, char* argv[]) {
821 i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
822 if (i::FLAG_help) {
823 return 1;
824 }
825 Initialize();
826 bool run_shell = (argc == 1);
827 756
828 // Default use preemption if threads are created. 757 // Default use preemption if threads are created.
829 bool use_preemption = true; 758 bool use_preemption = true;
830 759
831 // Default to use lowest possible thread preemption interval to test as many 760 // Default to use lowest possible thread preemption interval to test as many
832 // edgecases as possible. 761 // edgecases as possible.
833 int preemption_interval = 1; 762 int preemption_interval = 1;
834 763
835 i::List<i::Thread*> threads(1); 764 i::List<i::Thread*> threads(1);
836 765
837 { 766 {
838 // Acquire the V8 lock once initialization has finished. Since the thread 767 // Since the thread below may spawn new threads accessing V8 holding the
839 // below may spawn new threads accessing V8 holding the V8 lock here is 768 // V8 lock here is mandatory.
840 // mandatory.
841 Locker locker; 769 Locker locker;
770
771 RenewEvalContext();
842 Context::Scope context_scope(evaluation_context_); 772 Context::Scope context_scope(evaluation_context_);
843 for (int i = 1; i < argc; i++) { 773 for (int i = 1; i < argc; i++) {
844 char* str = argv[i]; 774 char* str = argv[i];
845 if (strcmp(str, "--shell") == 0) { 775 if (strcmp(str, "--preemption") == 0) {
846 run_shell = true;
847 } else if (strcmp(str, "--preemption") == 0) {
848 use_preemption = true; 776 use_preemption = true;
849 } else if (strcmp(str, "--no-preemption") == 0) { 777 } else if (strcmp(str, "--no-preemption") == 0) {
850 use_preemption = false; 778 use_preemption = false;
851 } else if (strcmp(str, "--preemption-interval") == 0) { 779 } else if (strcmp(str, "--preemption-interval") == 0) {
852 if (i + 1 < argc) { 780 if (i + 1 < argc) {
853 char* end = NULL; 781 char* end = NULL;
854 preemption_interval = strtol(argv[++i], &end, 10); // NOLINT 782 preemption_interval = strtol(argv[++i], &end, 10); // NOLINT
855 if (preemption_interval <= 0 || *end != '\0' || errno == ERANGE) { 783 if (preemption_interval <= 0 || *end != '\0' || errno == ERANGE) {
856 printf("Invalid value for --preemption-interval '%s'\n", argv[i]); 784 printf("Invalid value for --preemption-interval '%s'\n", argv[i]);
857 return 1; 785 return 1;
858 } 786 }
859 } else { 787 } else {
860 printf("Missing value for --preemption-interval\n"); 788 printf("Missing value for --preemption-interval\n");
861 return 1; 789 return 1;
862 } 790 }
863 } else if (strcmp(str, "-f") == 0) { 791 } else if (strcmp(str, "-f") == 0) {
864 // Ignore any -f flags for compatibility with other stand-alone 792 // Ignore any -f flags for compatibility with other stand-alone
865 // JavaScript engines. 793 // JavaScript engines.
866 continue; 794 continue;
867 } else if (strncmp(str, "--", 2) == 0) { 795 } else if (strncmp(str, "--", 2) == 0) {
868 printf("Warning: unknown flag %s.\nTry --help for options\n", str); 796 printf("Warning: unknown flag %s.\nTry --help for options\n", str);
869 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { 797 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
870 // Execute argument given to -e option directly. 798 // Execute argument given to -e option directly.
871 v8::HandleScope handle_scope; 799 v8::HandleScope handle_scope;
872 v8::Handle<v8::String> file_name = v8::String::New("unnamed"); 800 v8::Handle<v8::String> file_name = v8::String::New("unnamed");
873 v8::Handle<v8::String> source = v8::String::New(argv[i + 1]); 801 v8::Handle<v8::String> source = v8::String::New(argv[++i]);
874 if (!ExecuteString(source, file_name, false, true)) { 802 if (!ExecuteString(source, file_name, false, true)) {
875 OnExit(); 803 OnExit();
876 return 1; 804 return 1;
877 } 805 }
878 i++;
879 } else if (strcmp(str, "-p") == 0 && i + 1 < argc) { 806 } else if (strcmp(str, "-p") == 0 && i + 1 < argc) {
880 int size = 0; 807 int size = 0;
881 const char* files = ReadChars(argv[++i], &size); 808 const char* files = ReadChars(argv[++i], &size);
882 if (files == NULL) return 1; 809 if (files == NULL)
883 ShellThread* thread = 810 return 1;
884 new ShellThread(i::Isolate::Current(), 811 ShellThread* thread = new ShellThread(i::Isolate::Current(),
885 threads.length(), 812 threads.length(), i::Vector<const char>(files, size));
886 i::Vector<const char>(files, size));
887 thread->Start(); 813 thread->Start();
888 threads.Add(thread); 814 threads.Add(thread);
889 } else { 815 } else {
890 // Use all other arguments as names of files to load and run. 816 // Use all other arguments as names of files to load and run.
891 HandleScope handle_scope; 817 HandleScope handle_scope;
892 Handle<String> file_name = v8::String::New(str); 818 Handle<String> file_name = v8::String::New(str);
893 Handle<String> source = ReadFile(str); 819 Handle<String> source = ReadFile(str);
894 if (source.IsEmpty()) { 820 if (source.IsEmpty()) {
895 printf("Error reading '%s'\n", str); 821 printf("Error reading '%s'\n", str);
896 return 1; 822 return 1;
897 } 823 }
898 if (!ExecuteString(source, file_name, false, true)) { 824 if (!ExecuteString(source, file_name, false, true)) {
899 OnExit(); 825 OnExit();
900 return 1; 826 return 1;
901 } 827 }
902 } 828 }
903 } 829 }
904 830
905 // Start preemption if threads have been created and preemption is enabled. 831 // Start preemption if threads have been created and preemption is enabled.
906 if (threads.length() > 0 && use_preemption) { 832 if (threads.length() > 0 && use_preemption) {
907 Locker::StartPreemption(preemption_interval); 833 Locker::StartPreemption(preemption_interval);
908 } 834 }
909 835
910 #ifdef ENABLE_DEBUGGER_SUPPORT
911 // Run the remote debugger if requested.
912 if (i::FLAG_remote_debugger) {
913 RunRemoteDebugger(i::FLAG_debugger_port);
914 return 0;
915 }
916 #endif
917 } 836 }
918 if (run_shell) 837
919 RunShell();
920 for (int i = 0; i < threads.length(); i++) { 838 for (int i = 0; i < threads.length(); i++) {
921 i::Thread* thread = threads[i]; 839 i::Thread* thread = threads[i];
922 thread->Join(); 840 thread->Join();
923 delete thread; 841 delete thread;
924 } 842 }
843
925 OnExit(); 844 OnExit();
926 return 0; 845 return 0;
846
927 } 847 }
928 848
849 int Shell::Main(int argc, char* argv[]) {
850 // Figure out if we're requested to stress the optimization
851 // infrastructure by running tests multiple times and forcing
852 // optimization in the last run.
853 bool FLAG_stress_opt = false;
854 bool FLAG_stress_deopt = false;
855 bool run_shell = (argc == 1);
929 856
930 } // namespace v8 857 for (int i = 0; i < argc; i++) {
858 if (strcmp(argv[i], "--stress-opt") == 0) {
859 FLAG_stress_opt = true;
860 argv[i] = NULL;
861 } else if (strcmp(argv[i], "--stress-deopt") == 0) {
862 FLAG_stress_deopt = true;
863 argv[i] = NULL;
864 } else if (strcmp(argv[i], "--noalways-opt") == 0) {
865 // No support for stressing if we can't use --always-opt.
866 FLAG_stress_opt = false;
867 FLAG_stress_deopt = false;
868 } else if (strcmp(argv[i], "--shell") == 0) {
869 run_shell = true;
870 argv[i] = NULL;
871 }
872 }
873
874 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
875
876 Initialize();
877
878 int result = 0;
879 if (FLAG_stress_opt || FLAG_stress_deopt) {
880 v8::Testing::SetStressRunType(
881 FLAG_stress_opt ? v8::Testing::kStressTypeOpt
882 : v8::Testing::kStressTypeDeopt);
883 int stress_runs = v8::Testing::GetStressRuns();
884 for (int i = 0; i < stress_runs && result == 0; i++) {
885 printf("============ Stress %d/%d ============\n", i + 1, stress_runs);
886 v8::Testing::PrepareStressRun(i);
887 result = RunMain(argc, argv);
888 }
889 printf("======== Full Deoptimization =======\n");
890 v8::Testing::DeoptimizeAll();
891 } else {
892 result = RunMain(argc, argv);
893 }
894
895 #ifdef ENABLE_DEBUGGER_SUPPORT
896 if (i::FLAG_remote_debugger) {
897 RunRemoteDebugger(i::FLAG_debugger_port);
898 return 0;
899 }
900 #endif
901
902 if (run_shell) {
903 InstallUtilScript();
904 RunShell();
905 }
906
907 v8::V8::Dispose();
908
909 return result;
910
911 }
912
913 } // namespace v8
931 914
932 915
933 #ifndef GOOGLE3 916 #ifndef GOOGLE3
934 int main(int argc, char* argv[]) { 917 int main(int argc, char* argv[]) {
935 return v8::Shell::Main(argc, argv); 918 return v8::Shell::Main(argc, argv);
936 } 919 }
937 #endif 920 #endif
OLDNEW
« src/d8.h ('K') | « src/d8.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698