1 // Copyright 2012 the V8 project authors. All rights reserved. 2 // Redistribution and use in source and binary forms, with or without 3 // modification, are permitted provided that the following conditions are 4 // met: 5 // 6 // * Redistributions of source code must retain the above copyright 7 // notice, this list of conditions and the following disclaimer. 8 // * Redistributions in binary form must reproduce the above 9 // copyright notice, this list of conditions and the following 10 // disclaimer in the documentation and/or other materials provided 11 // with the distribution. 12 // * Neither the name of Google Inc. nor the names of its 13 // contributors may be used to endorse or promote products derived 14 // from this software without specific prior written permission. 15 // 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28 29 // Defined when linking against shared lib on Windows. 30 #if defined(USING_V8_SHARED) && !defined(V8_SHARED) 31 #define V8_SHARED 32 #endif 33 34 #ifdef COMPRESS_STARTUP_DATA_BZ2 35 #include <bzlib.h> 36 #endif 37 38 #include <errno.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <sys/stat.h> 42 43 #ifdef V8_SHARED 44 #include <assert.h> 45 #endif // V8_SHARED 46 47 #ifndef V8_SHARED 48 #include <algorithm> 49 #endif // !V8_SHARED 50 51 #ifdef V8_SHARED 52 #include "../include/v8-testing.h" 53 #endif // V8_SHARED 54 55 #ifdef ENABLE_VTUNE_JIT_INTERFACE 56 #include "third_party/vtune/v8-vtune.h" 57 #endif 58 59 #include "d8.h" 60 61 #ifndef V8_SHARED 62 #include "api.h" 63 #include "checks.h" 64 #include "d8-debug.h" 65 #include "debug.h" 66 #include "natives.h" 67 #include "platform.h" 68 #include "v8.h" 69 #endif // V8_SHARED 70 71 #if !defined(_WIN32) && !defined(_WIN64) 72 #include <unistd.h> // NOLINT 73 #endif 74 75 #ifndef ASSERT 76 #define ASSERT(condition) assert(condition) 77 #endif 78 79 namespace v8 { 80 81 82 static Handle<Value> Throw(const char* message) { 83 return ThrowException(String::New(message)); 84 } 85 86 87 88 class PerIsolateData { 89 public: 90 explicit PerIsolateData(Isolate* isolate) : isolate_(isolate), realms_(NULL) { 91 HandleScope scope(isolate); 92 isolate->SetData(this); 93 } 94 95 ~PerIsolateData() { 96 isolate_->SetData(NULL); // Not really needed, just to be sure... 97 } 98 99 inline static PerIsolateData* Get(Isolate* isolate) { 100 return reinterpret_cast<PerIsolateData*>(isolate->GetData()); 101 } 102 103 class RealmScope { 104 public: 105 explicit RealmScope(PerIsolateData* data); 106 ~RealmScope(); 107 private: 108 PerIsolateData* data_; 109 }; 110 111 private: 112 friend class Shell; 113 friend class RealmScope; 114 Isolate* isolate_; 115 int realm_count_; 116 int realm_current_; 117 int realm_switch_; 118 Persistent<Context>* realms_; 119 Persistent<Value> realm_shared_; 120 121 int RealmFind(Handle<Context> context); 122 }; 123 124 125 LineEditor *LineEditor::current_ = NULL; 126 127 128 LineEditor::LineEditor(Type type, const char* name) 129 : type_(type), name_(name) { 130 if (current_ == NULL || current_->type_ < type) current_ = this; 131 } 132 133 134 class DumbLineEditor: public LineEditor { 135 public: 136 explicit DumbLineEditor(Isolate* isolate) 137 : LineEditor(LineEditor::DUMB, "dumb"), isolate_(isolate) { } 138 virtual Handle<String> Prompt(const char* prompt); 139 private: 140 Isolate* isolate_; 141 }; 142 143 144 Handle<String> DumbLineEditor::Prompt(const char* prompt) { 145 printf("%s", prompt); 146 #if defined(__native_client__) 147 // Native Client libc is used to being embedded in Chrome and 148 // has trouble recognizing when to flush. 149 fflush(stdout); 150 #endif 151 return Shell::ReadFromStdin(isolate_); 152 } 153 154 155 #ifndef V8_SHARED 156 CounterMap* Shell::counter_map_; 157 i::OS::MemoryMappedFile* Shell::counters_file_ = NULL; 158 CounterCollection Shell::local_counters_; 159 CounterCollection* Shell::counters_ = &local_counters_; 160 i::Mutex* Shell::context_mutex_(i::OS::CreateMutex()); 161 Persistent<Context> Shell::utility_context_; 162 #endif // V8_SHARED 163 164 Persistent<Context> Shell::evaluation_context_; 165 ShellOptions Shell::options; 166 const char* Shell::kPrompt = "d8> "; 167 168 169 const int MB = 1024 * 1024; 170 171 172 #ifndef V8_SHARED 173 bool CounterMap::Match(void* key1, void* key2) { 174 const char* name1 = reinterpret_cast<const char*>(key1); 175 const char* name2 = reinterpret_cast<const char*>(key2); 176 return strcmp(name1, name2) == 0; 177 } 178 #endif // V8_SHARED 179 180 181 // Converts a V8 value to a C string. 182 const char* Shell::ToCString(const v8::String::Utf8Value& value) { 183 return *value ? *value : "<string conversion failed>"; 184 } 185 186 187 // Executes a string within the current v8 context. 188 bool Shell::ExecuteString(Isolate* isolate, 189 Handle<String> source, 190 Handle<Value> name, 191 bool print_result, 192 bool report_exceptions) { 193 #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) 194 bool FLAG_debugger = i::FLAG_debugger; 195 #else 196 bool FLAG_debugger = false; 197 #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT 198 HandleScope handle_scope(isolate); 199 TryCatch try_catch; 200 options.script_executed = true; 201 if (FLAG_debugger) { 202 // When debugging make exceptions appear to be uncaught. 203 try_catch.SetVerbose(true); 204 } 205 Handle<Script> script = Script::New(source, name); 206 if (script.IsEmpty()) { 207 // Print errors that happened during compilation. 208 if (report_exceptions && !FLAG_debugger) 209 ReportException(isolate, &try_catch); 210 return false; 211 } else { 212 PerIsolateData* data = PerIsolateData::Get(isolate); 213 Local<Context> realm = 214 Local<Context>::New(isolate, data->realms_[data->realm_current_]); 215 realm->Enter(); 216 Handle<Value> result = script->Run(); 217 realm->Exit(); 218 data->realm_current_ = data->realm_switch_; 219 if (result.IsEmpty()) { 220 ASSERT(try_catch.HasCaught()); 221 // Print errors that happened during execution. 222 if (report_exceptions && !FLAG_debugger) 223 ReportException(isolate, &try_catch); 224 return false; 225 } else { 226 ASSERT(!try_catch.HasCaught()); 227 if (print_result) { 228 #if !defined(V8_SHARED) 229 if (options.test_shell) { 230 #endif 231 if (!result->IsUndefined()) { 232 // If all went well and the result wasn't undefined then print 233 // the returned value. 234 v8::String::Utf8Value str(result); 235 fwrite(*str, sizeof(**str), str.length(), stdout); 236 printf("\n"); 237 } 238 #if !defined(V8_SHARED) 239 } else { 240 v8::TryCatch try_catch; 241 v8::Local<v8::Context> context = 242 v8::Local<v8::Context>::New(isolate, utility_context_); 243 v8::Context::Scope context_scope(context); 244 Handle<Object> global = context->Global(); 245 Handle<Value> fun = global->Get(String::New("Stringify")); 246 Handle<Value> argv[1] = { result }; 247 Handle<Value> s = Handle<Function>::Cast(fun)->Call(global, 1, argv); 248 if (try_catch.HasCaught()) return true; 249 v8::String::Utf8Value str(s); 250 fwrite(*str, sizeof(**str), str.length(), stdout); 251 printf("\n"); 252 } 253 #endif 254 } 255 return true; 256 } 257 } 258 } 259 260 261 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) { 262 data_->realm_count_ = 1; 263 data_->realm_current_ = 0; 264 data_->realm_switch_ = 0; 265 data_->realms_ = new Persistent<Context>[1]; 266 data_->realms_[0].Reset(data_->isolate_, Context::GetEntered()); 267 data_->realm_shared_.Clear(); 268 } 269 270 271 PerIsolateData::RealmScope::~RealmScope() { 272 // Drop realms to avoid keeping them alive. 273 for (int i = 0; i < data_->realm_count_; ++i) 274 data_->realms_[i].Dispose(data_->isolate_); 275 delete[] data_->realms_; 276 if (!data_->realm_shared_.IsEmpty()) 277 data_->realm_shared_.Dispose(data_->isolate_); 278 } 279 280 281 int PerIsolateData::RealmFind(Handle<Context> context) { 282 for (int i = 0; i < realm_count_; ++i) { 283 if (realms_[i] == context) return i; 284 } 285 return -1; 286 } 287 288 289 // Realm.current() returns the index of the currently active realm. 290 void Shell::RealmCurrent(const v8::FunctionCallbackInfo<v8::Value>& args) { 291 Isolate* isolate = args.GetIsolate(); 292 PerIsolateData* data = PerIsolateData::Get(isolate); 293 int index = data->RealmFind(Context::GetEntered()); 294 if (index == -1) return; 295 args.GetReturnValue().Set(index); 296 } 297 298 299 // Realm.owner(o) returns the index of the realm that created o. 300 void Shell::RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args) { 301 Isolate* isolate = args.GetIsolate(); 302 PerIsolateData* data = PerIsolateData::Get(isolate); 303 if (args.Length() < 1 || !args[0]->IsObject()) { 304 Throw("Invalid argument"); 305 return; 306 } 307 int index = data->RealmFind(args[0]->ToObject()->CreationContext()); 308 if (index == -1) return; 309 args.GetReturnValue().Set(index); 310 } 311 312 313 // Realm.global(i) returns the global object of realm i. 314 // (Note that properties of global objects cannot be read/written cross-realm.) 315 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) { 316 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate()); 317 if (args.Length() < 1 || !args[0]->IsNumber()) { 318 Throw("Invalid argument"); 319 return; 320 } 321 int index = args[0]->Uint32Value(); 322 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { 323 Throw("Invalid realm index"); 324 return; 325 } 326 args.GetReturnValue().Set( 327 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global()); 328 } 329 330 331 // Realm.create() creates a new realm and returns its index. 332 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) { 333 Isolate* isolate = args.GetIsolate(); 334 PerIsolateData* data = PerIsolateData::Get(isolate); 335 Persistent<Context>* old_realms = data->realms_; 336 int index = data->realm_count_; 337 data->realms_ = new Persistent<Context>[++data->realm_count_]; 338 for (int i = 0; i < index; ++i) { 339 data->realms_[i].Reset(isolate, old_realms[i]); 340 } 341 delete[] old_realms; 342 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); 343 data->realms_[index].Reset( 344 isolate, Context::New(isolate, NULL, global_template)); 345 args.GetReturnValue().Set(index); 346 } 347 348 349 // Realm.dispose(i) disposes the reference to the realm i. 350 void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) { 351 Isolate* isolate = args.GetIsolate(); 352 PerIsolateData* data = PerIsolateData::Get(isolate); 353 if (args.Length() < 1 || !args[0]->IsNumber()) { 354 Throw("Invalid argument"); 355 return; 356 } 357 int index = args[0]->Uint32Value(); 358 if (index >= data->realm_count_ || data->realms_[index].IsEmpty() || 359 index == 0 || 360 index == data->realm_current_ || index == data->realm_switch_) { 361 Throw("Invalid realm index"); 362 return; 363 } 364 data->realms_[index].Dispose(isolate); 365 data->realms_[index].Clear(); 366 } 367 368 369 // Realm.switch(i) switches to the realm i for consecutive interactive inputs. 370 void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) { 371 Isolate* isolate = args.GetIsolate(); 372 PerIsolateData* data = PerIsolateData::Get(isolate); 373 if (args.Length() < 1 || !args[0]->IsNumber()) { 374 Throw("Invalid argument"); 375 return; 376 } 377 int index = args[0]->Uint32Value(); 378 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { 379 Throw("Invalid realm index"); 380 return; 381 } 382 data->realm_switch_ = index; 383 } 384 385 386 // Realm.eval(i, s) evaluates s in realm i and returns the result. 387 void Shell::RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args) { 388 Isolate* isolate = args.GetIsolate(); 389 PerIsolateData* data = PerIsolateData::Get(isolate); 390 if (args.Length() < 2 || !args[0]->IsNumber() || !args[1]->IsString()) { 391 Throw("Invalid argument"); 392 return; 393 } 394 int index = args[0]->Uint32Value(); 395 if (index >= data->realm_count_ || data->realms_[index].IsEmpty()) { 396 Throw("Invalid realm index"); 397 return; 398 } 399 Handle<Script> script = Script::New(args[1]->ToString()); 400 if (script.IsEmpty()) return; 401 Local<Context> realm = Local<Context>::New(isolate, data->realms_[index]); 402 realm->Enter(); 403 Handle<Value> result = script->Run(); 404 realm->Exit(); 405 args.GetReturnValue().Set(result); 406 } 407 408 409 // Realm.shared is an accessor for a single shared value across realms. 410 void Shell::RealmSharedGet(Local<String> property, 411 const PropertyCallbackInfo<Value>& info) { 412 Isolate* isolate = info.GetIsolate(); 413 PerIsolateData* data = PerIsolateData::Get(isolate); 414 if (data->realm_shared_.IsEmpty()) return; 415 info.GetReturnValue().Set(data->realm_shared_); 416 } 417 418 void Shell::RealmSharedSet(Local<String> property, 419 Local<Value> value, 420 const PropertyCallbackInfo<void>& info) { 421 Isolate* isolate = info.GetIsolate(); 422 PerIsolateData* data = PerIsolateData::Get(isolate); 423 if (!data->realm_shared_.IsEmpty()) data->realm_shared_.Dispose(isolate); 424 data->realm_shared_.Reset(isolate, value); 425 } 426 427 428 void Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) { 429 Write(args); 430 printf("\n"); 431 fflush(stdout); 432 } 433 434 435 void Shell::Write(const v8::FunctionCallbackInfo<v8::Value>& args) { 436 for (int i = 0; i < args.Length(); i++) { 437 HandleScope handle_scope(args.GetIsolate()); 438 if (i != 0) { 439 printf(" "); 440 } 441 442 // Explicitly catch potential exceptions in toString(). 443 v8::TryCatch try_catch; 444 Handle<String> str_obj = args[i]->ToString(); 445 if (try_catch.HasCaught()) { 446 try_catch.ReThrow(); 447 return; 448 } 449 450 v8::String::Utf8Value str(str_obj); 451 int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), stdout)); 452 if (n != str.length()) { 453 printf("Error in fwrite\n"); 454 Exit(1); 455 } 456 } 457 } 458 459 460 void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) { 461 String::Utf8Value file(args[0]); 462 if (*file == NULL) { 463 Throw("Error loading file"); 464 return; 465 } 466 Handle<String> source = ReadFile(args.GetIsolate(), *file); 467 if (source.IsEmpty()) { 468 Throw("Error loading file"); 469 return; 470 } 471 args.GetReturnValue().Set(source); 472 } 473 474 475 Handle<String> Shell::ReadFromStdin(Isolate* isolate) { 476 static const int kBufferSize = 256; 477 char buffer[kBufferSize]; 478 Handle<String> accumulator = String::New(""); 479 int length; 480 while (true) { 481 // Continue reading if the line ends with an escape '\\' or the line has 482 // not been fully read into the buffer yet (does not end with '\n'). 483 // If fgets gets an error, just give up. 484 char* input = NULL; 485 { // Release lock for blocking input. 486 Unlocker unlock(isolate); 487 input = fgets(buffer, kBufferSize, stdin); 488 } 489 if (input == NULL) return Handle<String>(); 490 length = static_cast<int>(strlen(buffer)); 491 if (length == 0) { 492 return accumulator; 493 } else if (buffer[length-1] != '\n') { 494 accumulator = String::Concat(accumulator, String::New(buffer, length)); 495 } else if (length > 1 && buffer[length-2] == '\\') { 496 buffer[length-2] = '\n'; 497 accumulator = String::Concat(accumulator, String::New(buffer, length-1)); 498 } else { 499 return String::Concat(accumulator, String::New(buffer, length-1)); 500 } 501 } 502 } 503 504 505 void Shell::Load(const v8::FunctionCallbackInfo<v8::Value>& args) { 506 for (int i = 0; i < args.Length(); i++) { 507 HandleScope handle_scope(args.GetIsolate()); 508 String::Utf8Value file(args[i]); 509 if (*file == NULL) { 510 Throw("Error loading file"); 511 return; 512 } 513 Handle<String> source = ReadFile(args.GetIsolate(), *file); 514 if (source.IsEmpty()) { 515 Throw("Error loading file"); 516 return; 517 } 518 if (!ExecuteString(args.GetIsolate(), 519 source, 520 String::New(*file), 521 false, 522 true)) { 523 Throw("Error executing file"); 524 return; 525 } 526 } 527 } 528 529 530 void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { 531 int exit_code = args[0]->Int32Value(); 532 OnExit(); 533 exit(exit_code); 534 } 535 536 537 void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) { 538 args.GetReturnValue().Set(String::New(V8::GetVersion())); 539 } 540 541 542 void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) { 543 HandleScope handle_scope(isolate); 544 #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) 545 Handle<Context> utility_context; 546 bool enter_context = !Context::InContext(); 547 if (enter_context) { 548 utility_context = Local<Context>::New(isolate, utility_context_); 549 utility_context->Enter(); 550 } 551 #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT 552 v8::String::Utf8Value exception(try_catch->Exception()); 553 const char* exception_string = ToCString(exception); 554 Handle<Message> message = try_catch->Message(); 555 if (message.IsEmpty()) { 556 // V8 didn't provide any extra information about this error; just 557 // print the exception. 558 printf("%s\n", exception_string); 559 } else { 560 // Print (filename):(line number): (message). 561 v8::String::Utf8Value filename(message->GetScriptResourceName()); 562 const char* filename_string = ToCString(filename); 563 int linenum = message->GetLineNumber(); 564 printf("%s:%i: %s\n", filename_string, linenum, exception_string); 565 // Print line of source code. 566 v8::String::Utf8Value sourceline(message->GetSourceLine()); 567 const char* sourceline_string = ToCString(sourceline); 568 printf("%s\n", sourceline_string); 569 // Print wavy underline (GetUnderline is deprecated). 570 int start = message->GetStartColumn(); 571 for (int i = 0; i < start; i++) { 572 printf(" "); 573 } 574 int end = message->GetEndColumn(); 575 for (int i = start; i < end; i++) { 576 printf("^"); 577 } 578 printf("\n"); 579 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); 580 if (stack_trace.length() > 0) { 581 const char* stack_trace_string = ToCString(stack_trace); 582 printf("%s\n", stack_trace_string); 583 } 584 } 585 printf("\n"); 586 #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) 587 if (enter_context) utility_context->Exit(); 588 #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT 589 } 590 591 592 #ifndef V8_SHARED 593 Handle<Array> Shell::GetCompletions(Isolate* isolate, 594 Handle<String> text, 595 Handle<String> full) { 596 HandleScope handle_scope(isolate); 597 v8::Local<v8::Context> utility_context = 598 v8::Local<v8::Context>::New(isolate, utility_context_); 599 v8::Context::Scope context_scope(utility_context); 600 Handle<Object> global = utility_context->Global(); 601 Handle<Value> fun = global->Get(String::New("GetCompletions")); 602 static const int kArgc = 3; 603 v8::Local<v8::Context> evaluation_context = 604 v8::Local<v8::Context>::New(isolate, evaluation_context_); 605 Handle<Value> argv[kArgc] = { evaluation_context->Global(), text, full }; 606 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); 607 return handle_scope.Close(Handle<Array>::Cast(val)); 608 } 609 610 611 #ifdef ENABLE_DEBUGGER_SUPPORT 612 Handle<Object> Shell::DebugMessageDetails(Isolate* isolate, 613 Handle<String> message) { 614 HandleScope handle_scope(isolate); 615 v8::Local<v8::Context> context = 616 v8::Local<v8::Context>::New(isolate, utility_context_); 617 v8::Context::Scope context_scope(context); 618 Handle<Object> global = context->Global(); 619 Handle<Value> fun = global->Get(String::New("DebugMessageDetails")); 620 static const int kArgc = 1; 621 Handle<Value> argv[kArgc] = { message }; 622 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); 623 return Handle<Object>::Cast(val); 624 } 625 626 627 Handle<Value> Shell::DebugCommandToJSONRequest(Isolate* isolate, 628 Handle<String> command) { 629 HandleScope handle_scope(isolate); 630 v8::Local<v8::Context> context = 631 v8::Local<v8::Context>::New(isolate, utility_context_); 632 v8::Context::Scope context_scope(context); 633 Handle<Object> global = context->Global(); 634 Handle<Value> fun = global->Get(String::New("DebugCommandToJSONRequest")); 635 static const int kArgc = 1; 636 Handle<Value> argv[kArgc] = { command }; 637 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); 638 return val; 639 } 640 641 642 void Shell::DispatchDebugMessages() { 643 Isolate* isolate = v8::Isolate::GetCurrent(); 644 HandleScope handle_scope(isolate); 645 v8::Local<v8::Context> context = 646 v8::Local<v8::Context>::New(isolate, Shell::evaluation_context_); 647 v8::Context::Scope context_scope(context); 648 v8::Debug::ProcessDebugMessages(); 649 } 650 #endif // ENABLE_DEBUGGER_SUPPORT 651 #endif // V8_SHARED 652 653 654 #ifndef V8_SHARED 655 int32_t* Counter::Bind(const char* name, bool is_histogram) { 656 int i; 657 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) 658 name_[i] = static_cast<char>(name[i]); 659 name_[i] = '\0'; 660 is_histogram_ = is_histogram; 661 return ptr(); 662 } 663 664 665 void Counter::AddSample(int32_t sample) { 666 count_++; 667 sample_total_ += sample; 668 } 669 670 671 CounterCollection::CounterCollection() { 672 magic_number_ = 0xDEADFACE; 673 max_counters_ = kMaxCounters; 674 max_name_size_ = Counter::kMaxNameSize; 675 counters_in_use_ = 0; 676 } 677 678 679 Counter* CounterCollection::GetNextCounter() { 680 if (counters_in_use_ == kMaxCounters) return NULL; 681 return &counters_[counters_in_use_++]; 682 } 683 684 685 void Shell::MapCounters(const char* name) { 686 counters_file_ = i::OS::MemoryMappedFile::create( 687 name, sizeof(CounterCollection), &local_counters_); 688 void* memory = (counters_file_ == NULL) ? 689 NULL : counters_file_->memory(); 690 if (memory == NULL) { 691 printf("Could not map counters file %s\n", name); 692 Exit(1); 693 } 694 counters_ = static_cast<CounterCollection*>(memory); 695 V8::SetCounterFunction(LookupCounter); 696 V8::SetCreateHistogramFunction(CreateHistogram); 697 V8::SetAddHistogramSampleFunction(AddHistogramSample); 698 } 699 700 701 int CounterMap::Hash(const char* name) { 702 int h = 0; 703 int c; 704 while ((c = *name++) != 0) { 705 h += h << 5; 706 h += c; 707 } 708 return h; 709 } 710 711 712 Counter* Shell::GetCounter(const char* name, bool is_histogram) { 713 Counter* counter = counter_map_->Lookup(name); 714 715 if (counter == NULL) { 716 counter = counters_->GetNextCounter(); 717 if (counter != NULL) { 718 counter_map_->Set(name, counter); 719 counter->Bind(name, is_histogram); 720 } 721 } else { 722 ASSERT(counter->is_histogram() == is_histogram); 723 } 724 return counter; 725 } 726 727 728 int* Shell::LookupCounter(const char* name) { 729 Counter* counter = GetCounter(name, false); 730 731 if (counter != NULL) { 732 return counter->ptr(); 733 } else { 734 return NULL; 735 } 736 } 737 738 739 void* Shell::CreateHistogram(const char* name, 740 int min, 741 int max, 742 size_t buckets) { 743 return GetCounter(name, true); 744 } 745 746 747 void Shell::AddHistogramSample(void* histogram, int sample) { 748 Counter* counter = reinterpret_cast<Counter*>(histogram); 749 counter->AddSample(sample); 750 } 751 752 753 void Shell::InstallUtilityScript(Isolate* isolate) { 754 Locker lock(isolate); 755 HandleScope scope(isolate); 756 // If we use the utility context, we have to set the security tokens so that 757 // utility, evaluation and debug context can all access each other. 758 v8::Local<v8::Context> utility_context = 759 v8::Local<v8::Context>::New(isolate, utility_context_); 760 v8::Local<v8::Context> evaluation_context = 761 v8::Local<v8::Context>::New(isolate, evaluation_context_); 762 utility_context->SetSecurityToken(Undefined(isolate)); 763 evaluation_context->SetSecurityToken(Undefined(isolate)); 764 v8::Context::Scope context_scope(utility_context); 765 766 #ifdef ENABLE_DEBUGGER_SUPPORT 767 if (i::FLAG_debugger) printf("JavaScript debugger enabled\n"); 768 // Install the debugger object in the utility scope 769 i::Debug* debug = i::Isolate::Current()->debug(); 770 debug->Load(); 771 i::Handle<i::JSObject> js_debug 772 = i::Handle<i::JSObject>(debug->debug_context()->global_object()); 773 utility_context->Global()->Set(String::New("$debug"), 774 Utils::ToLocal(js_debug)); 775 debug->debug_context()->set_security_token(HEAP->undefined_value()); 776 #endif // ENABLE_DEBUGGER_SUPPORT 777 778 // Run the d8 shell utility script in the utility context 779 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); 780 i::Vector<const char> shell_source = 781 i::NativesCollection<i::D8>::GetRawScriptSource(source_index); 782 i::Vector<const char> shell_source_name = 783 i::NativesCollection<i::D8>::GetScriptName(source_index); 784 Handle<String> source = String::New(shell_source.start(), 785 shell_source.length()); 786 Handle<String> name = String::New(shell_source_name.start(), 787 shell_source_name.length()); 788 Handle<Script> script = Script::Compile(source, name); 789 script->Run(); 790 // Mark the d8 shell script as native to avoid it showing up as normal source 791 // in the debugger. 792 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); 793 i::Handle<i::Script> script_object = compiled_script->IsJSFunction() 794 ? i::Handle<i::Script>(i::Script::cast( 795 i::JSFunction::cast(*compiled_script)->shared()->script())) 796 : i::Handle<i::Script>(i::Script::cast( 797 i::SharedFunctionInfo::cast(*compiled_script)->script())); 798 script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE)); 799 800 #ifdef ENABLE_DEBUGGER_SUPPORT 801 // Start the in-process debugger if requested. 802 if (i::FLAG_debugger && !i::FLAG_debugger_agent) { 803 v8::Debug::SetDebugEventListener2(HandleDebugEvent); 804 } 805 #endif // ENABLE_DEBUGGER_SUPPORT 806 } 807 #endif // V8_SHARED 808 809 810 #ifdef COMPRESS_STARTUP_DATA_BZ2 811 class BZip2Decompressor : public v8::StartupDataDecompressor { 812 public: 813 virtual ~BZip2Decompressor() { } 814 815 protected: 816 virtual int DecompressData(char* raw_data, 817 int* raw_data_size, 818 const char* compressed_data, 819 int compressed_data_size) { 820 ASSERT_EQ(v8::StartupData::kBZip2, 821 v8::V8::GetCompressedStartupDataAlgorithm()); 822 unsigned int decompressed_size = *raw_data_size; 823 int result = 824 BZ2_bzBuffToBuffDecompress(raw_data, 825 &decompressed_size, 826 const_cast<char*>(compressed_data), 827 compressed_data_size, 828 0, 1); 829 if (result == BZ_OK) { 830 *raw_data_size = decompressed_size; 831 } 832 return result; 833 } 834 }; 835 #endif 836 837 838 Handle<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { 839 Handle<ObjectTemplate> global_template = ObjectTemplate::New(); 840 global_template->Set(String::New("print"), FunctionTemplate::New(Print)); 841 global_template->Set(String::New("write"), FunctionTemplate::New(Write)); 842 global_template->Set(String::New("read"), FunctionTemplate::New(Read)); 843 global_template->Set(String::New("readbuffer"), 844 FunctionTemplate::New(ReadBuffer)); 845 global_template->Set(String::New("readline"), 846 FunctionTemplate::New(ReadLine)); 847 global_template->Set(String::New("load"), FunctionTemplate::New(Load)); 848 global_template->Set(String::New("quit"), FunctionTemplate::New(Quit)); 849 global_template->Set(String::New("version"), FunctionTemplate::New(Version)); 850 851 // Bind the Realm object. 852 Handle<ObjectTemplate> realm_template = ObjectTemplate::New(); 853 realm_template->Set(String::New("current"), 854 FunctionTemplate::New(RealmCurrent)); 855 realm_template->Set(String::New("owner"), 856 FunctionTemplate::New(RealmOwner)); 857 realm_template->Set(String::New("global"), 858 FunctionTemplate::New(RealmGlobal)); 859 realm_template->Set(String::New("create"), 860 FunctionTemplate::New(RealmCreate)); 861 realm_template->Set(String::New("dispose"), 862 FunctionTemplate::New(RealmDispose)); 863 realm_template->Set(String::New("switch"), 864 FunctionTemplate::New(RealmSwitch)); 865 realm_template->Set(String::New("eval"), 866 FunctionTemplate::New(RealmEval)); 867 realm_template->SetAccessor(String::New("shared"), 868 RealmSharedGet, RealmSharedSet); 869 global_template->Set(String::New("Realm"), realm_template); 870 871 #if !defined(V8_SHARED) && !defined(_WIN32) && !defined(_WIN64) 872 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(); 873 AddOSMethods(os_templ); 874 global_template->Set(String::New("os"), os_templ); 875 #endif // V8_SHARED 876 877 return global_template; 878 } 879 880 881 void Shell::Initialize(Isolate* isolate) { 882 #ifdef COMPRESS_STARTUP_DATA_BZ2 883 BZip2Decompressor startup_data_decompressor; 884 int bz2_result = startup_data_decompressor.Decompress(); 885 if (bz2_result != BZ_OK) { 886 fprintf(stderr, "bzip error code: %d\n", bz2_result); 887 Exit(1); 888 } 889 #endif 890 891 #ifndef V8_SHARED 892 Shell::counter_map_ = new CounterMap(); 893 // Set up counters 894 if (i::StrLength(i::FLAG_map_counters) != 0) 895 MapCounters(i::FLAG_map_counters); 896 if (i::FLAG_dump_counters || i::FLAG_track_gc_object_stats) { 897 V8::SetCounterFunction(LookupCounter); 898 V8::SetCreateHistogramFunction(CreateHistogram); 899 V8::SetAddHistogramSampleFunction(AddHistogramSample); 900 } 901 #endif // V8_SHARED 902 } 903 904 905 void Shell::InitializeDebugger(Isolate* isolate) { 906 if (options.test_shell) return; 907 #ifndef V8_SHARED 908 Locker lock(isolate); 909 HandleScope scope(isolate); 910 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); 911 utility_context_.Reset(isolate, 912 Context::New(isolate, NULL, global_template)); 913 914 #ifdef ENABLE_DEBUGGER_SUPPORT 915 // Start the debugger agent if requested. 916 if (i::FLAG_debugger_agent) { 917 v8::Debug::EnableAgent("d8 shell", i::FLAG_debugger_port, true); 918 v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true); 919 } 920 #endif // ENABLE_DEBUGGER_SUPPORT 921 #endif // V8_SHARED 922 } 923 924 925 Local<Context> Shell::CreateEvaluationContext(Isolate* isolate) { 926 #ifndef V8_SHARED 927 // This needs to be a critical section since this is not thread-safe 928 i::ScopedLock lock(context_mutex_); 929 #endif // V8_SHARED 930 // Initialize the global objects 931 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); 932 HandleScope handle_scope(isolate); 933 Local<Context> context = Context::New(isolate, NULL, global_template); 934 ASSERT(!context.IsEmpty()); 935 Context::Scope scope(context); 936 937 #ifndef V8_SHARED 938 i::Factory* factory = i::Isolate::Current()->factory(); 939 i::JSArguments js_args = i::FLAG_js_arguments; 940 i::Handle<i::FixedArray> arguments_array = 941 factory->NewFixedArray(js_args.argc()); 942 for (int j = 0; j < js_args.argc(); j++) { 943 i::Handle<i::String> arg = 944 factory->NewStringFromUtf8(i::CStrVector(js_args[j])); 945 arguments_array->set(j, *arg); 946 } 947 i::Handle<i::JSArray> arguments_jsarray = 948 factory->NewJSArrayWithElements(arguments_array); 949 context->Global()->Set(String::New("arguments"), 950 Utils::ToLocal(arguments_jsarray)); 951 #endif // V8_SHARED 952 return handle_scope.Close(context); 953 } 954 955 956 void Shell::Exit(int exit_code) { 957 // Use _exit instead of exit to avoid races between isolate 958 // threads and static destructors. 959 fflush(stdout); 960 fflush(stderr); 961 _exit(exit_code); 962 } 963 964 965 #ifndef V8_SHARED 966 struct CounterAndKey { 967 Counter* counter; 968 const char* key; 969 }; 970 971 972 inline bool operator<(const CounterAndKey& lhs, const CounterAndKey& rhs) { 973 return strcmp(lhs.key, rhs.key) < 0; 974 } 975 #endif // V8_SHARED 976 977 978 void Shell::OnExit() { 979 LineEditor* line_editor = LineEditor::Get(); 980 if (line_editor) line_editor->Close(); 981 #ifndef V8_SHARED 982 if (i::FLAG_dump_counters) { 983 int number_of_counters = 0; 984 for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) { 985 number_of_counters++; 986 } 987 CounterAndKey* counters = new CounterAndKey[number_of_counters]; 988 int j = 0; 989 for (CounterMap::Iterator i(counter_map_); i.More(); i.Next(), j++) { 990 counters[j].counter = i.CurrentValue(); 991 counters[j].key = i.CurrentKey(); 992 } 993 std::sort(counters, counters + number_of_counters); 994 printf("+----------------------------------------------------------------+" 995 "-------------+\n"); 996 printf("| Name |" 997 " Value |\n"); 998 printf("+----------------------------------------------------------------+" 999 "-------------+\n"); 1000 for (j = 0; j < number_of_counters; j++) { 1001 Counter* counter = counters[j].counter; 1002 const char* key = counters[j].key; 1003 if (counter->is_histogram()) { 1004 printf("| c:%-60s | %11i |\n", key, counter->count()); 1005 printf("| t:%-60s | %11i |\n", key, counter->sample_total()); 1006 } else { 1007 printf("| %-62s | %11i |\n", key, counter->count()); 1008 } 1009 } 1010 printf("+----------------------------------------------------------------+" 1011 "-------------+\n"); 1012 delete [] counters; 1013 } 1014 delete context_mutex_; 1015 delete counters_file_; 1016 delete counter_map_; 1017 #endif // V8_SHARED 1018 } 1019 1020 1021 1022 static FILE* FOpen(const char* path, const char* mode) { 1023 #if defined(_MSC_VER) && (defined(_WIN32) || defined(_WIN64)) 1024 FILE* result; 1025 if (fopen_s(&result, path, mode) == 0) { 1026 return result; 1027 } else { 1028 return NULL; 1029 } 1030 #else 1031 FILE* file = fopen(path, mode); 1032 if (file == NULL) return NULL; 1033 struct stat file_stat; 1034 if (fstat(fileno(file), &file_stat) != 0) return NULL; 1035 bool is_regular_file = ((file_stat.st_mode & S_IFREG) != 0); 1036 if (is_regular_file) return file; 1037 fclose(file); 1038 return NULL; 1039 #endif 1040 } 1041 1042 1043 static char* ReadChars(Isolate* isolate, const char* name, int* size_out) { 1044 // Release the V8 lock while reading files. 1045 v8::Unlocker unlocker(isolate); 1046 FILE* file = FOpen(name, "rb"); 1047 if (file == NULL) return NULL; 1048 1049 fseek(file, 0, SEEK_END); 1050 int size = ftell(file); 1051 rewind(file); 1052 1053 char* chars = new char[size + 1]; 1054 chars[size] = '\0'; 1055 for (int i = 0; i < size;) { 1056 int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); 1057 i += read; 1058 } 1059 fclose(file); 1060 *size_out = size; 1061 return chars; 1062 } 1063 1064 static void ReadBufferWeakCallback(v8::Isolate* isolate, 1065 Persistent<ArrayBuffer>* array_buffer, 1066 uint8_t* data) { 1067 size_t byte_length = 1068 Local<ArrayBuffer>::New(isolate, *array_buffer)->ByteLength(); 1069 isolate->AdjustAmountOfExternalAllocatedMemory( 1070 -static_cast<intptr_t>(byte_length)); 1071 1072 delete[] data; 1073 array_buffer->Dispose(); 1074 } 1075 1076 1077 void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { 1078 ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT 1079 String::Utf8Value filename(args[0]); 1080 int length; 1081 if (*filename == NULL) { 1082 Throw("Error loading file"); 1083 return; 1084 } 1085 1086 Isolate* isolate = args.GetIsolate(); 1087 uint8_t* data = reinterpret_cast<uint8_t*>( 1088 ReadChars(args.GetIsolate(), *filename, &length)); 1089 if (data == NULL) { 1090 Throw("Error reading file"); 1091 return; 1092 } 1093 Handle<v8::ArrayBuffer> buffer = ArrayBuffer::New(data, length); 1094 v8::Persistent<v8::ArrayBuffer> weak_handle(isolate, buffer); 1095 weak_handle.MakeWeak(data, ReadBufferWeakCallback); 1096 weak_handle.MarkIndependent(); 1097 isolate->AdjustAmountOfExternalAllocatedMemory(length); 1098 1099 args.GetReturnValue().Set(buffer); 1100 } 1101 1102 1103 #ifndef V8_SHARED 1104 static char* ReadToken(char* data, char token) { 1105 char* next = i::OS::StrChr(data, token); 1106 if (next != NULL) { 1107 *next = '\0'; 1108 return (next + 1); 1109 } 1110 1111 return NULL; 1112 } 1113 1114 1115 static char* ReadLine(char* data) { 1116 return ReadToken(data, '\n'); 1117 } 1118 1119 1120 static char* ReadWord(char* data) { 1121 return ReadToken(data, ' '); 1122 } 1123 #endif // V8_SHARED 1124 1125 1126 // Reads a file into a v8 string. 1127 Handle<String> Shell::ReadFile(Isolate* isolate, const char* name) { 1128 int size = 0; 1129 char* chars = ReadChars(isolate, name, &size); 1130 if (chars == NULL) return Handle<String>(); 1131 Handle<String> result = String::New(chars, size); 1132 delete[] chars; 1133 return result; 1134 } 1135 1136 1137 void Shell::RunShell(Isolate* isolate) { 1138 Locker locker(isolate); 1139 HandleScope outer_scope(isolate); 1140 v8::Local<v8::Context> context = 1141 v8::Local<v8::Context>::New(isolate, evaluation_context_); 1142 v8::Context::Scope context_scope(context); 1143 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); 1144 Handle<String> name = String::New("(d8)"); 1145 LineEditor* console = LineEditor::Get(); 1146 printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name()); 1147 console->Open(isolate); 1148 while (true) { 1149 HandleScope inner_scope(isolate); 1150 Handle<String> input = console->Prompt(Shell::kPrompt); 1151 if (input.IsEmpty()) break; 1152 ExecuteString(isolate, input, name, true, true); 1153 } 1154 printf("\n"); 1155 } 1156 1157 1158 #ifndef V8_SHARED 1159 class ShellThread : public i::Thread { 1160 public: 1161 // Takes ownership of the underlying char array of |files|. 1162 ShellThread(Isolate* isolate, char* files) 1163 : Thread("d8:ShellThread"), 1164 isolate_(isolate), files_(files) { } 1165 1166 ~ShellThread() { 1167 delete[] files_; 1168 } 1169 1170 virtual void Run(); 1171 private: 1172 Isolate* isolate_; 1173 char* files_; 1174 }; 1175 1176 1177 void ShellThread::Run() { 1178 char* ptr = files_; 1179 while ((ptr != NULL) && (*ptr != '\0')) { 1180 // For each newline-separated line. 1181 char* next_line = ReadLine(ptr); 1182 1183 if (*ptr == '#') { 1184 // Skip comment lines. 1185 ptr = next_line; 1186 continue; 1187 } 1188 1189 // Prepare the context for this thread. 1190 Locker locker(isolate_); 1191 HandleScope outer_scope(isolate_); 1192 Local<Context> thread_context = 1193 Shell::CreateEvaluationContext(isolate_); 1194 Context::Scope context_scope(thread_context); 1195 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate_)); 1196 1197 while ((ptr != NULL) && (*ptr != '\0')) { 1198 HandleScope inner_scope(isolate_); 1199 char* filename = ptr; 1200 ptr = ReadWord(ptr); 1201 1202 // Skip empty strings. 1203 if (strlen(filename) == 0) { 1204 continue; 1205 } 1206 1207 Handle<String> str = Shell::ReadFile(isolate_, filename); 1208 if (str.IsEmpty()) { 1209 printf("File '%s' not found\n", filename); 1210 Shell::Exit(1); 1211 } 1212 1213 Shell::ExecuteString(isolate_, str, String::New(filename), false, false); 1214 } 1215 1216 ptr = next_line; 1217 } 1218 } 1219 #endif // V8_SHARED 1220 1221 1222 SourceGroup::~SourceGroup() { 1223 #ifndef V8_SHARED 1224 delete next_semaphore_; 1225 next_semaphore_ = NULL; 1226 delete done_semaphore_; 1227 done_semaphore_ = NULL; 1228 delete thread_; 1229 thread_ = NULL; 1230 #endif // V8_SHARED 1231 } 1232 1233 1234 void SourceGroup::Execute(Isolate* isolate) { 1235 for (int i = begin_offset_; i < end_offset_; ++i) { 1236 const char* arg = argv_[i]; 1237 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { 1238 // Execute argument given to -e option directly. 1239 HandleScope handle_scope(isolate); 1240 Handle<String> file_name = String::New("unnamed"); 1241 Handle<String> source = String::New(argv_[i + 1]); 1242 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { 1243 Shell::Exit(1); 1244 } 1245 ++i; 1246 } else if (arg[0] == '-') { 1247 // Ignore other options. They have been parsed already. 1248 } else { 1249 // Use all other arguments as names of files to load and run. 1250 HandleScope handle_scope(isolate); 1251 Handle<String> file_name = String::New(arg); 1252 Handle<String> source = ReadFile(isolate, arg); 1253 if (source.IsEmpty()) { 1254 printf("Error reading '%s'\n", arg); 1255 Shell::Exit(1); 1256 } 1257 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { 1258 Shell::Exit(1); 1259 } 1260 } 1261 } 1262 } 1263 1264 1265 Handle<String> SourceGroup::ReadFile(Isolate* isolate, const char* name) { 1266 int size; 1267 char* chars = ReadChars(isolate, name, &size); 1268 if (chars == NULL) return Handle<String>(); 1269 Handle<String> result = String::New(chars, size); 1270 delete[] chars; 1271 return result; 1272 } 1273 1274 1275 #ifndef V8_SHARED 1276 i::Thread::Options SourceGroup::GetThreadOptions() { 1277 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less 1278 // which is not enough to parse the big literal expressions used in tests. 1279 // The stack size should be at least StackGuard::kLimitSize + some 1280 // OS-specific padding for thread startup code. 2Mbytes seems to be enough. 1281 return i::Thread::Options("IsolateThread", 2 * MB); 1282 } 1283 1284 1285 void SourceGroup::ExecuteInThread() { 1286 Isolate* isolate = Isolate::New(); 1287 do { 1288 if (next_semaphore_ != NULL) next_semaphore_->Wait(); 1289 { 1290 Isolate::Scope iscope(isolate); 1291 Locker lock(isolate); 1292 { 1293 HandleScope scope(isolate); 1294 PerIsolateData data(isolate); 1295 Local<Context> context = Shell::CreateEvaluationContext(isolate); 1296 { 1297 Context::Scope cscope(context); 1298 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); 1299 Execute(isolate); 1300 } 1301 } 1302 if (Shell::options.send_idle_notification) { 1303 const int kLongIdlePauseInMs = 1000; 1304 V8::ContextDisposedNotification(); 1305 V8::IdleNotification(kLongIdlePauseInMs); 1306 } 1307 } 1308 if (done_semaphore_ != NULL) done_semaphore_->Signal(); 1309 } while (!Shell::options.last_run); 1310 isolate->Dispose(); 1311 } 1312 1313 1314 void SourceGroup::StartExecuteInThread() { 1315 if (thread_ == NULL) { 1316 thread_ = new IsolateThread(this); 1317 thread_->Start(); 1318 } 1319 next_semaphore_->Signal(); 1320 } 1321 1322 1323 void SourceGroup::WaitForThread() { 1324 if (thread_ == NULL) return; 1325 if (Shell::options.last_run) { 1326 thread_->Join(); 1327 } else { 1328 done_semaphore_->Wait(); 1329 } 1330 } 1331 #endif // V8_SHARED 1332 1333 1334 bool Shell::SetOptions(int argc, char* argv[]) { 1335 for (int i = 0; i < argc; i++) { 1336 if (strcmp(argv[i], "--stress-opt") == 0) { 1337 options.stress_opt = true; 1338 argv[i] = NULL; 1339 } else if (strcmp(argv[i], "--stress-deopt") == 0) { 1340 options.stress_deopt = true; 1341 argv[i] = NULL; 1342 } else if (strcmp(argv[i], "--noalways-opt") == 0) { 1343 // No support for stressing if we can't use --always-opt. 1344 options.stress_opt = false; 1345 options.stress_deopt = false; 1346 } else if (strcmp(argv[i], "--shell") == 0) { 1347 options.interactive_shell = true; 1348 argv[i] = NULL; 1349 } else if (strcmp(argv[i], "--test") == 0) { 1350 options.test_shell = true; 1351 argv[i] = NULL; 1352 } else if (strcmp(argv[i], "--send-idle-notification") == 0) { 1353 options.send_idle_notification = true; 1354 argv[i] = NULL; 1355 } else if (strcmp(argv[i], "--preemption") == 0) { 1356 #ifdef V8_SHARED 1357 printf("D8 with shared library does not support multi-threading\n"); 1358 return false; 1359 #else 1360 options.use_preemption = true; 1361 argv[i] = NULL; 1362 #endif // V8_SHARED 1363 } else if (strcmp(argv[i], "--nopreemption") == 0) { 1364 #ifdef V8_SHARED 1365 printf("D8 with shared library does not support multi-threading\n"); 1366 return false; 1367 #else 1368 options.use_preemption = false; 1369 argv[i] = NULL; 1370 #endif // V8_SHARED 1371 } else if (strcmp(argv[i], "--preemption-interval") == 0) { 1372 #ifdef V8_SHARED 1373 printf("D8 with shared library does not support multi-threading\n"); 1374 return false; 1375 #else 1376 if (++i < argc) { 1377 argv[i-1] = NULL; 1378 char* end = NULL; 1379 options.preemption_interval = strtol(argv[i], &end, 10); // NOLINT 1380 if (options.preemption_interval <= 0 1381 || *end != '\0' 1382 || errno == ERANGE) { 1383 printf("Invalid value for --preemption-interval '%s'\n", argv[i]); 1384 return false; 1385 } 1386 argv[i] = NULL; 1387 } else { 1388 printf("Missing value for --preemption-interval\n"); 1389 return false; 1390 } 1391 #endif // V8_SHARED 1392 } else if (strcmp(argv[i], "-f") == 0) { 1393 // Ignore any -f flags for compatibility with other stand-alone 1394 // JavaScript engines. 1395 continue; 1396 } else if (strcmp(argv[i], "--isolate") == 0) { 1397 #ifdef V8_SHARED 1398 printf("D8 with shared library does not support multi-threading\n"); 1399 return false; 1400 #endif // V8_SHARED 1401 options.num_isolates++; 1402 } else if (strcmp(argv[i], "-p") == 0) { 1403 #ifdef V8_SHARED 1404 printf("D8 with shared library does not support multi-threading\n"); 1405 return false; 1406 #else 1407 options.num_parallel_files++; 1408 #endif // V8_SHARED 1409 } else if (strcmp(argv[i], "--dump-heap-constants") == 0) { 1410 #ifdef V8_SHARED 1411 printf("D8 with shared library does not support constant dumping\n"); 1412 return false; 1413 #else 1414 options.dump_heap_constants = true; 1415 argv[i] = NULL; 1416 #endif 1417 } 1418 #ifdef V8_SHARED 1419 else if (strcmp(argv[i], "--dump-counters") == 0) { 1420 printf("D8 with shared library does not include counters\n"); 1421 return false; 1422 } else if (strcmp(argv[i], "--debugger") == 0) { 1423 printf("Javascript debugger not included\n"); 1424 return false; 1425 } 1426 #endif // V8_SHARED 1427 } 1428 1429 #ifndef V8_SHARED 1430 // Run parallel threads if we are not using --isolate 1431 options.parallel_files = new char*[options.num_parallel_files]; 1432 int parallel_files_set = 0; 1433 for (int i = 1; i < argc; i++) { 1434 if (argv[i] == NULL) continue; 1435 if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) { 1436 if (options.num_isolates > 1) { 1437 printf("-p is not compatible with --isolate\n"); 1438 return false; 1439 } 1440 argv[i] = NULL; 1441 i++; 1442 options.parallel_files[parallel_files_set] = argv[i]; 1443 parallel_files_set++; 1444 argv[i] = NULL; 1445 } 1446 } 1447 if (parallel_files_set != options.num_parallel_files) { 1448 printf("-p requires a file containing a list of files as parameter\n"); 1449 return false; 1450 } 1451 #endif // V8_SHARED 1452 1453 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); 1454 1455 // Set up isolated source groups. 1456 options.isolate_sources = new SourceGroup[options.num_isolates]; 1457 SourceGroup* current = options.isolate_sources; 1458 current->Begin(argv, 1); 1459 for (int i = 1; i < argc; i++) { 1460 const char* str = argv[i]; 1461 if (strcmp(str, "--isolate") == 0) { 1462 current->End(i); 1463 current++; 1464 current->Begin(argv, i + 1); 1465 } else if (strncmp(argv[i], "--", 2) == 0) { 1466 printf("Warning: unknown flag %s.\nTry --help for options\n", argv[i]); 1467 } 1468 } 1469 current->End(argc); 1470 1471 return true; 1472 } 1473 1474 1475 int Shell::RunMain(Isolate* isolate, int argc, char* argv[]) { 1476 #ifndef V8_SHARED 1477 i::List<i::Thread*> threads(1); 1478 if (options.parallel_files != NULL) { 1479 for (int i = 0; i < options.num_parallel_files; i++) { 1480 char* files = NULL; 1481 { Locker lock(isolate); 1482 int size = 0; 1483 files = ReadChars(isolate, options.parallel_files[i], &size); 1484 } 1485 if (files == NULL) { 1486 printf("File list '%s' not found\n", options.parallel_files[i]); 1487 Exit(1); 1488 } 1489 ShellThread* thread = new ShellThread(isolate, files); 1490 thread->Start(); 1491 threads.Add(thread); 1492 } 1493 } 1494 for (int i = 1; i < options.num_isolates; ++i) { 1495 options.isolate_sources[i].StartExecuteInThread(); 1496 } 1497 #endif // V8_SHARED 1498 { // NOLINT 1499 Locker lock(isolate); 1500 { 1501 HandleScope scope(isolate); 1502 Local<Context> context = CreateEvaluationContext(isolate); 1503 if (options.last_run) { 1504 // Keep using the same context in the interactive shell. 1505 evaluation_context_.Reset(isolate, context); 1506 #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) 1507 // If the interactive debugger is enabled make sure to activate 1508 // it before running the files passed on the command line. 1509 if (i::FLAG_debugger) { 1510 InstallUtilityScript(isolate); 1511 } 1512 #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT 1513 } 1514 { 1515 Context::Scope cscope(context); 1516 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); 1517 options.isolate_sources[0].Execute(isolate); 1518 } 1519 } 1520 if (!options.last_run) { 1521 if (options.send_idle_notification) { 1522 const int kLongIdlePauseInMs = 1000; 1523 V8::ContextDisposedNotification(); 1524 V8::IdleNotification(kLongIdlePauseInMs); 1525 } 1526 } 1527 1528 #ifndef V8_SHARED 1529 // Start preemption if threads have been created and preemption is enabled. 1530 if (threads.length() > 0 1531 && options.use_preemption) { 1532 Locker::StartPreemption(options.preemption_interval); 1533 } 1534 #endif // V8_SHARED 1535 } 1536 1537 #ifndef V8_SHARED 1538 for (int i = 1; i < options.num_isolates; ++i) { 1539 options.isolate_sources[i].WaitForThread(); 1540 } 1541 1542 for (int i = 0; i < threads.length(); i++) { 1543 i::Thread* thread = threads[i]; 1544 thread->Join(); 1545 delete thread; 1546 } 1547 1548 if (threads.length() > 0 && options.use_preemption) { 1549 Locker lock(isolate); 1550 Locker::StopPreemption(); 1551 } 1552 #endif // V8_SHARED 1553 return 0; 1554 } 1555 1556 1557 #ifdef V8_SHARED 1558 static void SetStandaloneFlagsViaCommandLine() { 1559 int fake_argc = 3; 1560 char **fake_argv = new char*[3]; 1561 fake_argv[0] = NULL; 1562 fake_argv[1] = strdup("--harmony-typed-arrays"); 1563 fake_argv[2] = strdup("--trace-hydrogen-file=hydrogen.cfg"); 1564 v8::V8::SetFlagsFromCommandLine(&fake_argc, fake_argv, false); 1565 free(fake_argv[1]); 1566 delete[] fake_argv; 1567 } 1568 #endif 1569 1570 1571 #ifndef V8_SHARED 1572 static void DumpHeapConstants(i::Isolate* isolate) { 1573 i::Heap* heap = isolate->heap(); 1574 1575 // Dump the INSTANCE_TYPES table to the console. 1576 printf("# List of known V8 instance types.\n"); 1577 #define DUMP_TYPE(T) printf(" %d: \"%s\",\n", i::T, #T); 1578 printf("INSTANCE_TYPES = {\n"); 1579 INSTANCE_TYPE_LIST(DUMP_TYPE) 1580 printf("}\n"); 1581 #undef DUMP_TYPE 1582 1583 // Dump the KNOWN_MAP table to the console. 1584 printf("\n# List of known V8 maps.\n"); 1585 #define ROOT_LIST_CASE(type, name, camel_name) \ 1586 if (n == NULL && o == heap->name()) n = #camel_name; 1587 #define STRUCT_LIST_CASE(upper_name, camel_name, name) \ 1588 if (n == NULL && o == heap->name##_map()) n = #camel_name "Map"; 1589 i::HeapObjectIterator it(heap->map_space()); 1590 printf("KNOWN_MAPS = {\n"); 1591 for (i::Object* o = it.Next(); o != NULL; o = it.Next()) { 1592 i::Map* m = i::Map::cast(o); 1593 const char* n = NULL; 1594 intptr_t p = reinterpret_cast<intptr_t>(m) & 0xfffff; 1595 int t = m->instance_type(); 1596 ROOT_LIST(ROOT_LIST_CASE) 1597 STRUCT_LIST(STRUCT_LIST_CASE) 1598 if (n == NULL) continue; 1599 printf(" 0x%05" V8PRIxPTR ": (%d, \"%s\"),\n", p, t, n); 1600 } 1601 printf("}\n"); 1602 #undef STRUCT_LIST_CASE 1603 #undef ROOT_LIST_CASE 1604 1605 // Dump the KNOWN_OBJECTS table to the console. 1606 printf("\n# List of known V8 objects.\n"); 1607 #define ROOT_LIST_CASE(type, name, camel_name) \ 1608 if (n == NULL && o == heap->name()) n = #camel_name; 1609 i::OldSpaces spit(heap); 1610 printf("KNOWN_OBJECTS = {\n"); 1611 for (i::PagedSpace* s = spit.next(); s != NULL; s = spit.next()) { 1612 i::HeapObjectIterator it(s); 1613 const char* sname = AllocationSpaceName(s->identity()); 1614 for (i::Object* o = it.Next(); o != NULL; o = it.Next()) { 1615 const char* n = NULL; 1616 intptr_t p = reinterpret_cast<intptr_t>(o) & 0xfffff; 1617 ROOT_LIST(ROOT_LIST_CASE) 1618 if (n == NULL) continue; 1619 printf(" (\"%s\", 0x%05" V8PRIxPTR "): \"%s\",\n", sname, p, n); 1620 } 1621 } 1622 printf("}\n"); 1623 #undef ROOT_LIST_CASE 1624 } 1625 #endif // V8_SHARED 1626 1627 1628 class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator { 1629 public: 1630 virtual void* Allocate(size_t length) { 1631 void* result = malloc(length); 1632 memset(result, 0, length); 1633 return result; 1634 } 1635 virtual void* AllocateUninitialized(size_t length) { 1636 return malloc(length); 1637 } 1638 virtual void Free(void* data, size_t) { free(data); } 1639 // TODO(dslomov): Remove when v8:2823 is fixed. 1640 virtual void Free(void* data) { 1641 #ifndef V8_SHARED 1642 UNREACHABLE(); 1643 #endif 1644 } 1645 }; 1646 1647 1648 int Shell::Main(int argc, char* argv[]) { 1649 if (!SetOptions(argc, argv)) return 1; 1650 v8::V8::InitializeICU(); 1651 #ifndef V8_SHARED 1652 i::FLAG_harmony_array_buffer = true; 1653 i::FLAG_harmony_typed_arrays = true; 1654 i::FLAG_trace_hydrogen_file = "hydrogen.cfg"; 1655 #else 1656 SetStandaloneFlagsViaCommandLine(); 1657 #endif 1658 ShellArrayBufferAllocator array_buffer_allocator; 1659 v8::V8::SetArrayBufferAllocator(&array_buffer_allocator); 1660 int result = 0; 1661 Isolate* isolate = Isolate::GetCurrent(); 1662 DumbLineEditor dumb_line_editor(isolate); 1663 { 1664 Initialize(isolate); 1665 #ifdef ENABLE_VTUNE_JIT_INTERFACE 1666 vTune::InitializeVtuneForV8(); 1667 #endif 1668 PerIsolateData data(isolate); 1669 InitializeDebugger(isolate); 1670 1671 #ifndef V8_SHARED 1672 if (options.dump_heap_constants) { 1673 DumpHeapConstants(reinterpret_cast<i::Isolate*>(isolate)); 1674 return 0; 1675 } 1676 #endif 1677 1678 if (options.stress_opt || options.stress_deopt) { 1679 Testing::SetStressRunType(options.stress_opt 1680 ? Testing::kStressTypeOpt 1681 : Testing::kStressTypeDeopt); 1682 int stress_runs = Testing::GetStressRuns(); 1683 for (int i = 0; i < stress_runs && result == 0; i++) { 1684 printf("============ Stress %d/%d ============\n", i + 1, stress_runs); 1685 Testing::PrepareStressRun(i); 1686 options.last_run = (i == stress_runs - 1); 1687 result = RunMain(isolate, argc, argv); 1688 } 1689 printf("======== Full Deoptimization =======\n"); 1690 Testing::DeoptimizeAll(); 1691 #if !defined(V8_SHARED) 1692 } else if (i::FLAG_stress_runs > 0) { 1693 int stress_runs = i::FLAG_stress_runs; 1694 for (int i = 0; i < stress_runs && result == 0; i++) { 1695 printf("============ Run %d/%d ============\n", i + 1, stress_runs); 1696 options.last_run = (i == stress_runs - 1); 1697 result = RunMain(isolate, argc, argv); 1698 } 1699 #endif 1700 } else { 1701 result = RunMain(isolate, argc, argv); 1702 } 1703 1704 1705 #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) 1706 // Run remote debugger if requested, but never on --test 1707 if (i::FLAG_remote_debugger && !options.test_shell) { 1708 InstallUtilityScript(isolate); 1709 RunRemoteDebugger(isolate, i::FLAG_debugger_port); 1710 return 0; 1711 } 1712 #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT 1713 1714 // Run interactive shell if explicitly requested or if no script has been 1715 // executed, but never on --test 1716 1717 if (( options.interactive_shell || !options.script_executed ) 1718 && !options.test_shell ) { 1719 #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) 1720 if (!i::FLAG_debugger) { 1721 InstallUtilityScript(isolate); 1722 } 1723 #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT 1724 RunShell(isolate); 1725 } 1726 } 1727 V8::Dispose(); 1728 1729 OnExit(); 1730 1731 return result; 1732 } 1733 1734 } // namespace v8 1735 1736 1737 #ifndef GOOGLE3 1738 int main(int argc, char* argv[]) { 1739 return v8::Shell::Main(argc, argv); 1740 } 1741 #endif 1742