1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 // Defined when linking against shared lib on Windows.
7 #if defined(USING_V8_SHARED) && !defined(V8_SHARED)
11 #ifdef COMPRESS_STARTUP_DATA_BZ2
29 #include "include/v8-testing.h"
32 #ifdef ENABLE_VTUNE_JIT_INTERFACE
33 #include "src/third_party/vtune/v8-vtune.h"
40 #include "src/checks.h"
42 #include "src/d8-debug.h"
43 #include "src/debug.h"
44 #include "src/natives.h"
45 #include "src/platform.h"
49 #if !defined(_WIN32) && !defined(_WIN64)
50 #include <unistd.h> // NOLINT
54 #define ASSERT(condition) assert(condition)
60 static Handle<Value> Throw(Isolate* isolate, const char* message) {
61 return isolate->ThrowException(String::NewFromUtf8(isolate, message));
66 class PerIsolateData {
68 explicit PerIsolateData(Isolate* isolate) : isolate_(isolate), realms_(NULL) {
69 HandleScope scope(isolate);
70 isolate->SetData(0, this);
74 isolate_->SetData(0, NULL); // Not really needed, just to be sure...
77 inline static PerIsolateData* Get(Isolate* isolate) {
78 return reinterpret_cast<PerIsolateData*>(isolate->GetData(0));
83 explicit RealmScope(PerIsolateData* data);
86 PerIsolateData* data_;
91 friend class RealmScope;
96 Persistent<Context>* realms_;
97 Persistent<Value> realm_shared_;
99 int RealmIndexOrThrow(const v8::FunctionCallbackInfo<v8::Value>& args,
101 int RealmFind(Handle<Context> context);
105 LineEditor *LineEditor::current_ = NULL;
108 LineEditor::LineEditor(Type type, const char* name)
109 : type_(type), name_(name) {
110 if (current_ == NULL || current_->type_ < type) current_ = this;
114 class DumbLineEditor: public LineEditor {
116 explicit DumbLineEditor(Isolate* isolate)
117 : LineEditor(LineEditor::DUMB, "dumb"), isolate_(isolate) { }
118 virtual Handle<String> Prompt(const char* prompt);
124 Handle<String> DumbLineEditor::Prompt(const char* prompt) {
125 printf("%s", prompt);
126 #if defined(__native_client__)
127 // Native Client libc is used to being embedded in Chrome and
128 // has trouble recognizing when to flush.
131 return Shell::ReadFromStdin(isolate_);
136 CounterMap* Shell::counter_map_;
137 i::OS::MemoryMappedFile* Shell::counters_file_ = NULL;
138 CounterCollection Shell::local_counters_;
139 CounterCollection* Shell::counters_ = &local_counters_;
140 i::Mutex Shell::context_mutex_;
141 const i::TimeTicks Shell::kInitialTicks = i::TimeTicks::HighResolutionNow();
142 Persistent<Context> Shell::utility_context_;
145 Persistent<Context> Shell::evaluation_context_;
146 ShellOptions Shell::options;
147 const char* Shell::kPrompt = "d8> ";
151 const int MB = 1024 * 1024;
153 bool CounterMap::Match(void* key1, void* key2) {
154 const char* name1 = reinterpret_cast<const char*>(key1);
155 const char* name2 = reinterpret_cast<const char*>(key2);
156 return strcmp(name1, name2) == 0;
161 // Converts a V8 value to a C string.
162 const char* Shell::ToCString(const v8::String::Utf8Value& value) {
163 return *value ? *value : "<string conversion failed>";
167 // Executes a string within the current v8 context.
168 bool Shell::ExecuteString(Isolate* isolate,
169 Handle<String> source,
172 bool report_exceptions) {
174 bool FLAG_debugger = i::FLAG_debugger;
176 bool FLAG_debugger = false;
178 HandleScope handle_scope(isolate);
180 options.script_executed = true;
182 // When debugging make exceptions appear to be uncaught.
183 try_catch.SetVerbose(true);
185 ScriptOrigin origin(name);
186 ScriptCompiler::Source script_source(source, origin);
187 Handle<UnboundScript> script =
188 ScriptCompiler::CompileUnbound(isolate, &script_source);
189 if (script.IsEmpty()) {
190 // Print errors that happened during compilation.
191 if (report_exceptions && !FLAG_debugger)
192 ReportException(isolate, &try_catch);
195 PerIsolateData* data = PerIsolateData::Get(isolate);
196 Local<Context> realm =
197 Local<Context>::New(isolate, data->realms_[data->realm_current_]);
199 Handle<Value> result = script->BindToCurrentContext()->Run();
201 data->realm_current_ = data->realm_switch_;
202 if (result.IsEmpty()) {
203 ASSERT(try_catch.HasCaught());
204 // Print errors that happened during execution.
205 if (report_exceptions && !FLAG_debugger)
206 ReportException(isolate, &try_catch);
209 ASSERT(!try_catch.HasCaught());
211 #if !defined(V8_SHARED)
212 if (options.test_shell) {
214 if (!result->IsUndefined()) {
215 // If all went well and the result wasn't undefined then print
216 // the returned value.
217 v8::String::Utf8Value str(result);
218 fwrite(*str, sizeof(**str), str.length(), stdout);
221 #if !defined(V8_SHARED)
223 v8::TryCatch try_catch;
224 v8::Local<v8::Context> context =
225 v8::Local<v8::Context>::New(isolate, utility_context_);
226 v8::Context::Scope context_scope(context);
227 Handle<Object> global = context->Global();
229 global->Get(String::NewFromUtf8(isolate, "Stringify"));
230 Handle<Value> argv[1] = { result };
231 Handle<Value> s = Handle<Function>::Cast(fun)->Call(global, 1, argv);
232 if (try_catch.HasCaught()) return true;
233 v8::String::Utf8Value str(s);
234 fwrite(*str, sizeof(**str), str.length(), stdout);
245 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) {
246 data_->realm_count_ = 1;
247 data_->realm_current_ = 0;
248 data_->realm_switch_ = 0;
249 data_->realms_ = new Persistent<Context>[1];
250 data_->realms_[0].Reset(data_->isolate_,
251 data_->isolate_->GetEnteredContext());
255 PerIsolateData::RealmScope::~RealmScope() {
256 // Drop realms to avoid keeping them alive.
257 for (int i = 0; i < data_->realm_count_; ++i)
258 data_->realms_[i].Reset();
259 delete[] data_->realms_;
260 if (!data_->realm_shared_.IsEmpty())
261 data_->realm_shared_.Reset();
265 int PerIsolateData::RealmFind(Handle<Context> context) {
266 for (int i = 0; i < realm_count_; ++i) {
267 if (realms_[i] == context) return i;
273 int PerIsolateData::RealmIndexOrThrow(
274 const v8::FunctionCallbackInfo<v8::Value>& args,
276 if (args.Length() < arg_offset || !args[arg_offset]->IsNumber()) {
277 Throw(args.GetIsolate(), "Invalid argument");
280 int index = args[arg_offset]->Int32Value();
282 index >= realm_count_ ||
283 realms_[index].IsEmpty()) {
284 Throw(args.GetIsolate(), "Invalid realm index");
292 // performance.now() returns a time stamp as double, measured in milliseconds.
293 void Shell::PerformanceNow(const v8::FunctionCallbackInfo<v8::Value>& args) {
294 i::TimeDelta delta = i::TimeTicks::HighResolutionNow() - kInitialTicks;
295 args.GetReturnValue().Set(delta.InMillisecondsF());
300 // Realm.current() returns the index of the currently active realm.
301 void Shell::RealmCurrent(const v8::FunctionCallbackInfo<v8::Value>& args) {
302 Isolate* isolate = args.GetIsolate();
303 PerIsolateData* data = PerIsolateData::Get(isolate);
304 int index = data->RealmFind(isolate->GetEnteredContext());
305 if (index == -1) return;
306 args.GetReturnValue().Set(index);
310 // Realm.owner(o) returns the index of the realm that created o.
311 void Shell::RealmOwner(const v8::FunctionCallbackInfo<v8::Value>& args) {
312 Isolate* isolate = args.GetIsolate();
313 PerIsolateData* data = PerIsolateData::Get(isolate);
314 if (args.Length() < 1 || !args[0]->IsObject()) {
315 Throw(args.GetIsolate(), "Invalid argument");
318 int index = data->RealmFind(args[0]->ToObject()->CreationContext());
319 if (index == -1) return;
320 args.GetReturnValue().Set(index);
324 // Realm.global(i) returns the global object of realm i.
325 // (Note that properties of global objects cannot be read/written cross-realm.)
326 void Shell::RealmGlobal(const v8::FunctionCallbackInfo<v8::Value>& args) {
327 PerIsolateData* data = PerIsolateData::Get(args.GetIsolate());
328 int index = data->RealmIndexOrThrow(args, 0);
329 if (index == -1) return;
330 args.GetReturnValue().Set(
331 Local<Context>::New(args.GetIsolate(), data->realms_[index])->Global());
335 // Realm.create() creates a new realm and returns its index.
336 void Shell::RealmCreate(const v8::FunctionCallbackInfo<v8::Value>& args) {
337 Isolate* isolate = args.GetIsolate();
338 PerIsolateData* data = PerIsolateData::Get(isolate);
339 Persistent<Context>* old_realms = data->realms_;
340 int index = data->realm_count_;
341 data->realms_ = new Persistent<Context>[++data->realm_count_];
342 for (int i = 0; i < index; ++i) {
343 data->realms_[i].Reset(isolate, old_realms[i]);
346 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
347 data->realms_[index].Reset(
348 isolate, Context::New(isolate, NULL, global_template));
349 args.GetReturnValue().Set(index);
353 // Realm.dispose(i) disposes the reference to the realm i.
354 void Shell::RealmDispose(const v8::FunctionCallbackInfo<v8::Value>& args) {
355 Isolate* isolate = args.GetIsolate();
356 PerIsolateData* data = PerIsolateData::Get(isolate);
357 int index = data->RealmIndexOrThrow(args, 0);
358 if (index == -1) return;
360 index == data->realm_current_ || index == data->realm_switch_) {
361 Throw(args.GetIsolate(), "Invalid realm index");
364 data->realms_[index].Reset();
368 // Realm.switch(i) switches to the realm i for consecutive interactive inputs.
369 void Shell::RealmSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) {
370 Isolate* isolate = args.GetIsolate();
371 PerIsolateData* data = PerIsolateData::Get(isolate);
372 int index = data->RealmIndexOrThrow(args, 0);
373 if (index == -1) return;
374 data->realm_switch_ = index;
378 // Realm.eval(i, s) evaluates s in realm i and returns the result.
379 void Shell::RealmEval(const v8::FunctionCallbackInfo<v8::Value>& args) {
380 Isolate* isolate = args.GetIsolate();
381 PerIsolateData* data = PerIsolateData::Get(isolate);
382 int index = data->RealmIndexOrThrow(args, 0);
383 if (index == -1) return;
384 if (args.Length() < 2 || !args[1]->IsString()) {
385 Throw(args.GetIsolate(), "Invalid argument");
388 ScriptCompiler::Source script_source(args[1]->ToString());
389 Handle<UnboundScript> script = ScriptCompiler::CompileUnbound(
390 isolate, &script_source);
391 if (script.IsEmpty()) return;
392 Local<Context> realm = Local<Context>::New(isolate, data->realms_[index]);
394 Handle<Value> result = script->BindToCurrentContext()->Run();
396 args.GetReturnValue().Set(result);
400 // Realm.shared is an accessor for a single shared value across realms.
401 void Shell::RealmSharedGet(Local<String> property,
402 const PropertyCallbackInfo<Value>& info) {
403 Isolate* isolate = info.GetIsolate();
404 PerIsolateData* data = PerIsolateData::Get(isolate);
405 if (data->realm_shared_.IsEmpty()) return;
406 info.GetReturnValue().Set(data->realm_shared_);
409 void Shell::RealmSharedSet(Local<String> property,
411 const PropertyCallbackInfo<void>& info) {
412 Isolate* isolate = info.GetIsolate();
413 PerIsolateData* data = PerIsolateData::Get(isolate);
414 data->realm_shared_.Reset(isolate, value);
418 void Shell::Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
425 void Shell::Write(const v8::FunctionCallbackInfo<v8::Value>& args) {
426 for (int i = 0; i < args.Length(); i++) {
427 HandleScope handle_scope(args.GetIsolate());
432 // Explicitly catch potential exceptions in toString().
433 v8::TryCatch try_catch;
434 Handle<String> str_obj = args[i]->ToString();
435 if (try_catch.HasCaught()) {
440 v8::String::Utf8Value str(str_obj);
441 int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), stdout));
442 if (n != str.length()) {
443 printf("Error in fwrite\n");
450 void Shell::Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
451 String::Utf8Value file(args[0]);
453 Throw(args.GetIsolate(), "Error loading file");
456 Handle<String> source = ReadFile(args.GetIsolate(), *file);
457 if (source.IsEmpty()) {
458 Throw(args.GetIsolate(), "Error loading file");
461 args.GetReturnValue().Set(source);
465 Handle<String> Shell::ReadFromStdin(Isolate* isolate) {
466 static const int kBufferSize = 256;
467 char buffer[kBufferSize];
468 Handle<String> accumulator = String::NewFromUtf8(isolate, "");
471 // Continue reading if the line ends with an escape '\\' or the line has
472 // not been fully read into the buffer yet (does not end with '\n').
473 // If fgets gets an error, just give up.
475 input = fgets(buffer, kBufferSize, stdin);
476 if (input == NULL) return Handle<String>();
477 length = static_cast<int>(strlen(buffer));
480 } else if (buffer[length-1] != '\n') {
481 accumulator = String::Concat(
483 String::NewFromUtf8(isolate, buffer, String::kNormalString, length));
484 } else if (length > 1 && buffer[length-2] == '\\') {
485 buffer[length-2] = '\n';
486 accumulator = String::Concat(
487 accumulator, String::NewFromUtf8(isolate, buffer,
488 String::kNormalString, length - 1));
490 return String::Concat(
491 accumulator, String::NewFromUtf8(isolate, buffer,
492 String::kNormalString, length - 1));
498 void Shell::Load(const v8::FunctionCallbackInfo<v8::Value>& args) {
499 for (int i = 0; i < args.Length(); i++) {
500 HandleScope handle_scope(args.GetIsolate());
501 String::Utf8Value file(args[i]);
503 Throw(args.GetIsolate(), "Error loading file");
506 Handle<String> source = ReadFile(args.GetIsolate(), *file);
507 if (source.IsEmpty()) {
508 Throw(args.GetIsolate(), "Error loading file");
511 if (!ExecuteString(args.GetIsolate(),
513 String::NewFromUtf8(args.GetIsolate(), *file),
516 Throw(args.GetIsolate(), "Error executing file");
523 void Shell::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
524 int exit_code = args[0]->Int32Value();
530 void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) {
531 args.GetReturnValue().Set(
532 String::NewFromUtf8(args.GetIsolate(), V8::GetVersion()));
536 void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) {
537 HandleScope handle_scope(isolate);
539 Handle<Context> utility_context;
540 bool enter_context = !isolate->InContext();
542 utility_context = Local<Context>::New(isolate, utility_context_);
543 utility_context->Enter();
546 v8::String::Utf8Value exception(try_catch->Exception());
547 const char* exception_string = ToCString(exception);
548 Handle<Message> message = try_catch->Message();
549 if (message.IsEmpty()) {
550 // V8 didn't provide any extra information about this error; just
551 // print the exception.
552 printf("%s\n", exception_string);
554 // Print (filename):(line number): (message).
555 v8::String::Utf8Value filename(message->GetScriptResourceName());
556 const char* filename_string = ToCString(filename);
557 int linenum = message->GetLineNumber();
558 printf("%s:%i: %s\n", filename_string, linenum, exception_string);
559 // Print line of source code.
560 v8::String::Utf8Value sourceline(message->GetSourceLine());
561 const char* sourceline_string = ToCString(sourceline);
562 printf("%s\n", sourceline_string);
563 // Print wavy underline (GetUnderline is deprecated).
564 int start = message->GetStartColumn();
565 for (int i = 0; i < start; i++) {
568 int end = message->GetEndColumn();
569 for (int i = start; i < end; i++) {
573 v8::String::Utf8Value stack_trace(try_catch->StackTrace());
574 if (stack_trace.length() > 0) {
575 const char* stack_trace_string = ToCString(stack_trace);
576 printf("%s\n", stack_trace_string);
581 if (enter_context) utility_context->Exit();
587 Handle<Array> Shell::GetCompletions(Isolate* isolate,
589 Handle<String> full) {
590 EscapableHandleScope handle_scope(isolate);
591 v8::Local<v8::Context> utility_context =
592 v8::Local<v8::Context>::New(isolate, utility_context_);
593 v8::Context::Scope context_scope(utility_context);
594 Handle<Object> global = utility_context->Global();
596 global->Get(String::NewFromUtf8(isolate, "GetCompletions"));
597 static const int kArgc = 3;
598 v8::Local<v8::Context> evaluation_context =
599 v8::Local<v8::Context>::New(isolate, evaluation_context_);
600 Handle<Value> argv[kArgc] = { evaluation_context->Global(), text, full };
601 Local<Value> val = Local<Function>::Cast(fun)->Call(global, kArgc, argv);
602 return handle_scope.Escape(Local<Array>::Cast(val));
606 Local<Object> Shell::DebugMessageDetails(Isolate* isolate,
607 Handle<String> message) {
608 EscapableHandleScope handle_scope(isolate);
609 v8::Local<v8::Context> context =
610 v8::Local<v8::Context>::New(isolate, utility_context_);
611 v8::Context::Scope context_scope(context);
612 Handle<Object> global = context->Global();
614 global->Get(String::NewFromUtf8(isolate, "DebugMessageDetails"));
615 static const int kArgc = 1;
616 Handle<Value> argv[kArgc] = { message };
617 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
618 return handle_scope.Escape(Local<Object>(Handle<Object>::Cast(val)));
622 Local<Value> Shell::DebugCommandToJSONRequest(Isolate* isolate,
623 Handle<String> command) {
624 EscapableHandleScope handle_scope(isolate);
625 v8::Local<v8::Context> context =
626 v8::Local<v8::Context>::New(isolate, utility_context_);
627 v8::Context::Scope context_scope(context);
628 Handle<Object> global = context->Global();
630 global->Get(String::NewFromUtf8(isolate, "DebugCommandToJSONRequest"));
631 static const int kArgc = 1;
632 Handle<Value> argv[kArgc] = { command };
633 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
634 return handle_scope.Escape(Local<Value>(val));
638 int32_t* Counter::Bind(const char* name, bool is_histogram) {
640 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++)
641 name_[i] = static_cast<char>(name[i]);
643 is_histogram_ = is_histogram;
648 void Counter::AddSample(int32_t sample) {
650 sample_total_ += sample;
654 CounterCollection::CounterCollection() {
655 magic_number_ = 0xDEADFACE;
656 max_counters_ = kMaxCounters;
657 max_name_size_ = Counter::kMaxNameSize;
658 counters_in_use_ = 0;
662 Counter* CounterCollection::GetNextCounter() {
663 if (counters_in_use_ == kMaxCounters) return NULL;
664 return &counters_[counters_in_use_++];
668 void Shell::MapCounters(v8::Isolate* isolate, const char* name) {
669 counters_file_ = i::OS::MemoryMappedFile::create(
670 name, sizeof(CounterCollection), &local_counters_);
671 void* memory = (counters_file_ == NULL) ?
672 NULL : counters_file_->memory();
673 if (memory == NULL) {
674 printf("Could not map counters file %s\n", name);
677 counters_ = static_cast<CounterCollection*>(memory);
678 isolate->SetCounterFunction(LookupCounter);
679 isolate->SetCreateHistogramFunction(CreateHistogram);
680 isolate->SetAddHistogramSampleFunction(AddHistogramSample);
684 int CounterMap::Hash(const char* name) {
687 while ((c = *name++) != 0) {
695 Counter* Shell::GetCounter(const char* name, bool is_histogram) {
696 Counter* counter = counter_map_->Lookup(name);
698 if (counter == NULL) {
699 counter = counters_->GetNextCounter();
700 if (counter != NULL) {
701 counter_map_->Set(name, counter);
702 counter->Bind(name, is_histogram);
705 ASSERT(counter->is_histogram() == is_histogram);
711 int* Shell::LookupCounter(const char* name) {
712 Counter* counter = GetCounter(name, false);
714 if (counter != NULL) {
715 return counter->ptr();
722 void* Shell::CreateHistogram(const char* name,
726 return GetCounter(name, true);
730 void Shell::AddHistogramSample(void* histogram, int sample) {
731 Counter* counter = reinterpret_cast<Counter*>(histogram);
732 counter->AddSample(sample);
736 void Shell::InstallUtilityScript(Isolate* isolate) {
737 HandleScope scope(isolate);
738 // If we use the utility context, we have to set the security tokens so that
739 // utility, evaluation and debug context can all access each other.
740 v8::Local<v8::Context> utility_context =
741 v8::Local<v8::Context>::New(isolate, utility_context_);
742 v8::Local<v8::Context> evaluation_context =
743 v8::Local<v8::Context>::New(isolate, evaluation_context_);
744 utility_context->SetSecurityToken(Undefined(isolate));
745 evaluation_context->SetSecurityToken(Undefined(isolate));
746 v8::Context::Scope context_scope(utility_context);
748 if (i::FLAG_debugger) printf("JavaScript debugger enabled\n");
749 // Install the debugger object in the utility scope
750 i::Debug* debug = reinterpret_cast<i::Isolate*>(isolate)->debug();
752 i::Handle<i::Context> debug_context = debug->debug_context();
753 i::Handle<i::JSObject> js_debug
754 = i::Handle<i::JSObject>(debug_context->global_object());
755 utility_context->Global()->Set(String::NewFromUtf8(isolate, "$debug"),
756 Utils::ToLocal(js_debug));
757 debug_context->set_security_token(
758 reinterpret_cast<i::Isolate*>(isolate)->heap()->undefined_value());
760 // Run the d8 shell utility script in the utility context
761 int source_index = i::NativesCollection<i::D8>::GetIndex("d8");
762 i::Vector<const char> shell_source =
763 i::NativesCollection<i::D8>::GetRawScriptSource(source_index);
764 i::Vector<const char> shell_source_name =
765 i::NativesCollection<i::D8>::GetScriptName(source_index);
766 Handle<String> source =
767 String::NewFromUtf8(isolate, shell_source.start(), String::kNormalString,
768 shell_source.length());
769 Handle<String> name =
770 String::NewFromUtf8(isolate, shell_source_name.start(),
771 String::kNormalString, shell_source_name.length());
772 ScriptOrigin origin(name);
773 Handle<Script> script = Script::Compile(source, &origin);
775 // Mark the d8 shell script as native to avoid it showing up as normal source
777 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script);
778 i::Handle<i::Script> script_object = compiled_script->IsJSFunction()
779 ? i::Handle<i::Script>(i::Script::cast(
780 i::JSFunction::cast(*compiled_script)->shared()->script()))
781 : i::Handle<i::Script>(i::Script::cast(
782 i::SharedFunctionInfo::cast(*compiled_script)->script()));
783 script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE));
785 // Start the in-process debugger if requested.
786 if (i::FLAG_debugger) v8::Debug::SetDebugEventListener(HandleDebugEvent);
791 #ifdef COMPRESS_STARTUP_DATA_BZ2
792 class BZip2Decompressor : public v8::StartupDataDecompressor {
794 virtual ~BZip2Decompressor() { }
797 virtual int DecompressData(char* raw_data,
799 const char* compressed_data,
800 int compressed_data_size) {
801 ASSERT_EQ(v8::StartupData::kBZip2,
802 v8::V8::GetCompressedStartupDataAlgorithm());
803 unsigned int decompressed_size = *raw_data_size;
805 BZ2_bzBuffToBuffDecompress(raw_data,
807 const_cast<char*>(compressed_data),
808 compressed_data_size,
810 if (result == BZ_OK) {
811 *raw_data_size = decompressed_size;
819 Handle<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
820 Handle<ObjectTemplate> global_template = ObjectTemplate::New(isolate);
821 global_template->Set(String::NewFromUtf8(isolate, "print"),
822 FunctionTemplate::New(isolate, Print));
823 global_template->Set(String::NewFromUtf8(isolate, "write"),
824 FunctionTemplate::New(isolate, Write));
825 global_template->Set(String::NewFromUtf8(isolate, "read"),
826 FunctionTemplate::New(isolate, Read));
827 global_template->Set(String::NewFromUtf8(isolate, "readbuffer"),
828 FunctionTemplate::New(isolate, ReadBuffer));
829 global_template->Set(String::NewFromUtf8(isolate, "readline"),
830 FunctionTemplate::New(isolate, ReadLine));
831 global_template->Set(String::NewFromUtf8(isolate, "load"),
832 FunctionTemplate::New(isolate, Load));
833 global_template->Set(String::NewFromUtf8(isolate, "quit"),
834 FunctionTemplate::New(isolate, Quit));
835 global_template->Set(String::NewFromUtf8(isolate, "version"),
836 FunctionTemplate::New(isolate, Version));
838 // Bind the Realm object.
839 Handle<ObjectTemplate> realm_template = ObjectTemplate::New(isolate);
840 realm_template->Set(String::NewFromUtf8(isolate, "current"),
841 FunctionTemplate::New(isolate, RealmCurrent));
842 realm_template->Set(String::NewFromUtf8(isolate, "owner"),
843 FunctionTemplate::New(isolate, RealmOwner));
844 realm_template->Set(String::NewFromUtf8(isolate, "global"),
845 FunctionTemplate::New(isolate, RealmGlobal));
846 realm_template->Set(String::NewFromUtf8(isolate, "create"),
847 FunctionTemplate::New(isolate, RealmCreate));
848 realm_template->Set(String::NewFromUtf8(isolate, "dispose"),
849 FunctionTemplate::New(isolate, RealmDispose));
850 realm_template->Set(String::NewFromUtf8(isolate, "switch"),
851 FunctionTemplate::New(isolate, RealmSwitch));
852 realm_template->Set(String::NewFromUtf8(isolate, "eval"),
853 FunctionTemplate::New(isolate, RealmEval));
854 realm_template->SetAccessor(String::NewFromUtf8(isolate, "shared"),
855 RealmSharedGet, RealmSharedSet);
856 global_template->Set(String::NewFromUtf8(isolate, "Realm"), realm_template);
859 Handle<ObjectTemplate> performance_template = ObjectTemplate::New(isolate);
860 performance_template->Set(String::NewFromUtf8(isolate, "now"),
861 FunctionTemplate::New(isolate, PerformanceNow));
862 global_template->Set(String::NewFromUtf8(isolate, "performance"),
863 performance_template);
866 #if !defined(V8_SHARED) && !defined(_WIN32) && !defined(_WIN64)
867 Handle<ObjectTemplate> os_templ = ObjectTemplate::New(isolate);
868 AddOSMethods(isolate, os_templ);
869 global_template->Set(String::NewFromUtf8(isolate, "os"), os_templ);
870 #endif // !V8_SHARED && !_WIN32 && !_WIN64
872 return global_template;
876 void Shell::Initialize(Isolate* isolate) {
877 #ifdef COMPRESS_STARTUP_DATA_BZ2
878 BZip2Decompressor startup_data_decompressor;
879 int bz2_result = startup_data_decompressor.Decompress();
880 if (bz2_result != BZ_OK) {
881 fprintf(stderr, "bzip error code: %d\n", bz2_result);
887 Shell::counter_map_ = new CounterMap();
889 if (i::StrLength(i::FLAG_map_counters) != 0)
890 MapCounters(isolate, i::FLAG_map_counters);
891 if (i::FLAG_dump_counters || i::FLAG_track_gc_object_stats) {
892 V8::SetCounterFunction(LookupCounter);
893 V8::SetCreateHistogramFunction(CreateHistogram);
894 V8::SetAddHistogramSampleFunction(AddHistogramSample);
900 void Shell::InitializeDebugger(Isolate* isolate) {
901 if (options.test_shell) return;
903 HandleScope scope(isolate);
904 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
905 utility_context_.Reset(isolate,
906 Context::New(isolate, NULL, global_template));
911 Local<Context> Shell::CreateEvaluationContext(Isolate* isolate) {
913 // This needs to be a critical section since this is not thread-safe
914 i::LockGuard<i::Mutex> lock_guard(&context_mutex_);
916 // Initialize the global objects
917 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
918 EscapableHandleScope handle_scope(isolate);
919 Local<Context> context = Context::New(isolate, NULL, global_template);
920 ASSERT(!context.IsEmpty());
921 Context::Scope scope(context);
924 i::Factory* factory = reinterpret_cast<i::Isolate*>(isolate)->factory();
925 i::JSArguments js_args = i::FLAG_js_arguments;
926 i::Handle<i::FixedArray> arguments_array =
927 factory->NewFixedArray(js_args.argc);
928 for (int j = 0; j < js_args.argc; j++) {
929 i::Handle<i::String> arg =
930 factory->NewStringFromUtf8(i::CStrVector(js_args[j])).ToHandleChecked();
931 arguments_array->set(j, *arg);
933 i::Handle<i::JSArray> arguments_jsarray =
934 factory->NewJSArrayWithElements(arguments_array);
935 context->Global()->Set(String::NewFromUtf8(isolate, "arguments"),
936 Utils::ToLocal(arguments_jsarray));
938 return handle_scope.Escape(context);
942 void Shell::Exit(int exit_code) {
943 // Use _exit instead of exit to avoid races between isolate
944 // threads and static destructors.
952 struct CounterAndKey {
958 inline bool operator<(const CounterAndKey& lhs, const CounterAndKey& rhs) {
959 return strcmp(lhs.key, rhs.key) < 0;
964 void Shell::OnExit() {
965 LineEditor* line_editor = LineEditor::Get();
966 if (line_editor) line_editor->Close();
968 if (i::FLAG_dump_counters) {
969 int number_of_counters = 0;
970 for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) {
971 number_of_counters++;
973 CounterAndKey* counters = new CounterAndKey[number_of_counters];
975 for (CounterMap::Iterator i(counter_map_); i.More(); i.Next(), j++) {
976 counters[j].counter = i.CurrentValue();
977 counters[j].key = i.CurrentKey();
979 std::sort(counters, counters + number_of_counters);
980 printf("+----------------------------------------------------------------+"
984 printf("+----------------------------------------------------------------+"
986 for (j = 0; j < number_of_counters; j++) {
987 Counter* counter = counters[j].counter;
988 const char* key = counters[j].key;
989 if (counter->is_histogram()) {
990 printf("| c:%-60s | %11i |\n", key, counter->count());
991 printf("| t:%-60s | %11i |\n", key, counter->sample_total());
993 printf("| %-62s | %11i |\n", key, counter->count());
996 printf("+----------------------------------------------------------------+"
1000 delete counters_file_;
1001 delete counter_map_;
1002 #endif // !V8_SHARED
1007 static FILE* FOpen(const char* path, const char* mode) {
1008 #if defined(_MSC_VER) && (defined(_WIN32) || defined(_WIN64))
1010 if (fopen_s(&result, path, mode) == 0) {
1016 FILE* file = fopen(path, mode);
1017 if (file == NULL) return NULL;
1018 struct stat file_stat;
1019 if (fstat(fileno(file), &file_stat) != 0) return NULL;
1020 bool is_regular_file = ((file_stat.st_mode & S_IFREG) != 0);
1021 if (is_regular_file) return file;
1028 static char* ReadChars(Isolate* isolate, const char* name, int* size_out) {
1029 FILE* file = FOpen(name, "rb");
1030 if (file == NULL) return NULL;
1032 fseek(file, 0, SEEK_END);
1033 int size = ftell(file);
1036 char* chars = new char[size + 1];
1038 for (int i = 0; i < size;) {
1039 int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
1048 struct DataAndPersistent {
1050 Persistent<ArrayBuffer> handle;
1054 static void ReadBufferWeakCallback(
1055 const v8::WeakCallbackData<ArrayBuffer, DataAndPersistent>& data) {
1056 size_t byte_length = data.GetValue()->ByteLength();
1057 data.GetIsolate()->AdjustAmountOfExternalAllocatedMemory(
1058 -static_cast<intptr_t>(byte_length));
1060 delete[] data.GetParameter()->data;
1061 data.GetParameter()->handle.Reset();
1062 delete data.GetParameter();
1066 void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) {
1067 ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT
1068 String::Utf8Value filename(args[0]);
1070 if (*filename == NULL) {
1071 Throw(args.GetIsolate(), "Error loading file");
1075 Isolate* isolate = args.GetIsolate();
1076 DataAndPersistent* data = new DataAndPersistent;
1077 data->data = reinterpret_cast<uint8_t*>(
1078 ReadChars(args.GetIsolate(), *filename, &length));
1079 if (data->data == NULL) {
1081 Throw(args.GetIsolate(), "Error reading file");
1084 Handle<v8::ArrayBuffer> buffer =
1085 ArrayBuffer::New(isolate, data->data, length);
1086 data->handle.Reset(isolate, buffer);
1087 data->handle.SetWeak(data, ReadBufferWeakCallback);
1088 data->handle.MarkIndependent();
1089 isolate->AdjustAmountOfExternalAllocatedMemory(length);
1091 args.GetReturnValue().Set(buffer);
1095 // Reads a file into a v8 string.
1096 Handle<String> Shell::ReadFile(Isolate* isolate, const char* name) {
1098 char* chars = ReadChars(isolate, name, &size);
1099 if (chars == NULL) return Handle<String>();
1100 Handle<String> result =
1101 String::NewFromUtf8(isolate, chars, String::kNormalString, size);
1107 void Shell::RunShell(Isolate* isolate) {
1108 HandleScope outer_scope(isolate);
1109 v8::Local<v8::Context> context =
1110 v8::Local<v8::Context>::New(isolate, evaluation_context_);
1111 v8::Context::Scope context_scope(context);
1112 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
1113 Handle<String> name = String::NewFromUtf8(isolate, "(d8)");
1114 LineEditor* console = LineEditor::Get();
1115 printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name());
1116 console->Open(isolate);
1118 HandleScope inner_scope(isolate);
1119 Handle<String> input = console->Prompt(Shell::kPrompt);
1120 if (input.IsEmpty()) break;
1121 ExecuteString(isolate, input, name, true, true);
1127 SourceGroup::~SourceGroup() {
1131 #endif // !V8_SHARED
1135 void SourceGroup::Execute(Isolate* isolate) {
1136 bool exception_was_thrown = false;
1137 for (int i = begin_offset_; i < end_offset_; ++i) {
1138 const char* arg = argv_[i];
1139 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) {
1140 // Execute argument given to -e option directly.
1141 HandleScope handle_scope(isolate);
1142 Handle<String> file_name = String::NewFromUtf8(isolate, "unnamed");
1143 Handle<String> source = String::NewFromUtf8(isolate, argv_[i + 1]);
1144 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) {
1145 exception_was_thrown = true;
1149 } else if (arg[0] == '-') {
1150 // Ignore other options. They have been parsed already.
1152 // Use all other arguments as names of files to load and run.
1153 HandleScope handle_scope(isolate);
1154 Handle<String> file_name = String::NewFromUtf8(isolate, arg);
1155 Handle<String> source = ReadFile(isolate, arg);
1156 if (source.IsEmpty()) {
1157 printf("Error reading '%s'\n", arg);
1160 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) {
1161 exception_was_thrown = true;
1166 if (exception_was_thrown != Shell::options.expected_to_throw) {
1172 Handle<String> SourceGroup::ReadFile(Isolate* isolate, const char* name) {
1174 char* chars = ReadChars(isolate, name, &size);
1175 if (chars == NULL) return Handle<String>();
1176 Handle<String> result =
1177 String::NewFromUtf8(isolate, chars, String::kNormalString, size);
1184 i::Thread::Options SourceGroup::GetThreadOptions() {
1185 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less
1186 // which is not enough to parse the big literal expressions used in tests.
1187 // The stack size should be at least StackGuard::kLimitSize + some
1188 // OS-specific padding for thread startup code. 2Mbytes seems to be enough.
1189 return i::Thread::Options("IsolateThread", 2 * MB);
1193 void SourceGroup::ExecuteInThread() {
1194 Isolate* isolate = Isolate::New();
1196 next_semaphore_.Wait();
1198 Isolate::Scope iscope(isolate);
1200 HandleScope scope(isolate);
1201 PerIsolateData data(isolate);
1202 Local<Context> context = Shell::CreateEvaluationContext(isolate);
1204 Context::Scope cscope(context);
1205 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
1209 if (Shell::options.send_idle_notification) {
1210 const int kLongIdlePauseInMs = 1000;
1211 V8::ContextDisposedNotification();
1212 V8::IdleNotification(kLongIdlePauseInMs);
1214 if (Shell::options.invoke_weak_callbacks) {
1215 // By sending a low memory notifications, we will try hard to collect
1216 // all garbage and will therefore also invoke all weak callbacks of
1217 // actually unreachable persistent handles.
1218 V8::LowMemoryNotification();
1221 done_semaphore_.Signal();
1222 } while (!Shell::options.last_run);
1228 void SourceGroup::StartExecuteInThread() {
1229 if (thread_ == NULL) {
1230 thread_ = new IsolateThread(this);
1233 next_semaphore_.Signal();
1237 void SourceGroup::WaitForThread() {
1238 if (thread_ == NULL) return;
1239 if (Shell::options.last_run) {
1242 done_semaphore_.Wait();
1245 #endif // !V8_SHARED
1248 void SetFlagsFromString(const char* flags) {
1249 v8::V8::SetFlagsFromString(flags, static_cast<int>(strlen(flags)));
1253 bool Shell::SetOptions(int argc, char* argv[]) {
1254 bool logfile_per_isolate = false;
1255 for (int i = 0; i < argc; i++) {
1256 if (strcmp(argv[i], "--stress-opt") == 0) {
1257 options.stress_opt = true;
1259 } else if (strcmp(argv[i], "--nostress-opt") == 0) {
1260 options.stress_opt = false;
1262 } else if (strcmp(argv[i], "--stress-deopt") == 0) {
1263 options.stress_deopt = true;
1265 } else if (strcmp(argv[i], "--mock-arraybuffer-allocator") == 0) {
1266 options.mock_arraybuffer_allocator = true;
1268 } else if (strcmp(argv[i], "--noalways-opt") == 0) {
1269 // No support for stressing if we can't use --always-opt.
1270 options.stress_opt = false;
1271 options.stress_deopt = false;
1272 } else if (strcmp(argv[i], "--logfile-per-isolate") == 0) {
1273 logfile_per_isolate = true;
1275 } else if (strcmp(argv[i], "--shell") == 0) {
1276 options.interactive_shell = true;
1278 } else if (strcmp(argv[i], "--test") == 0) {
1279 options.test_shell = true;
1281 } else if (strcmp(argv[i], "--send-idle-notification") == 0) {
1282 options.send_idle_notification = true;
1284 } else if (strcmp(argv[i], "--invoke-weak-callbacks") == 0) {
1285 options.invoke_weak_callbacks = true;
1286 // TODO(jochen) See issue 3351
1287 options.send_idle_notification = true;
1289 } else if (strcmp(argv[i], "-f") == 0) {
1290 // Ignore any -f flags for compatibility with other stand-alone
1291 // JavaScript engines.
1293 } else if (strcmp(argv[i], "--isolate") == 0) {
1295 printf("D8 with shared library does not support multi-threading\n");
1298 options.num_isolates++;
1299 } else if (strcmp(argv[i], "--dump-heap-constants") == 0) {
1301 printf("D8 with shared library does not support constant dumping\n");
1304 options.dump_heap_constants = true;
1307 } else if (strcmp(argv[i], "--throws") == 0) {
1308 options.expected_to_throw = true;
1310 } else if (strncmp(argv[i], "--icu-data-file=", 16) == 0) {
1311 options.icu_data_file = argv[i] + 16;
1314 } else if (strcmp(argv[i], "--dump-counters") == 0) {
1315 printf("D8 with shared library does not include counters\n");
1317 } else if (strcmp(argv[i], "--debugger") == 0) {
1318 printf("Javascript debugger not included\n");
1324 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
1326 // Set up isolated source groups.
1327 options.isolate_sources = new SourceGroup[options.num_isolates];
1328 SourceGroup* current = options.isolate_sources;
1329 current->Begin(argv, 1);
1330 for (int i = 1; i < argc; i++) {
1331 const char* str = argv[i];
1332 if (strcmp(str, "--isolate") == 0) {
1335 current->Begin(argv, i + 1);
1336 } else if (strncmp(argv[i], "--", 2) == 0) {
1337 printf("Warning: unknown flag %s.\nTry --help for options\n", argv[i]);
1342 if (!logfile_per_isolate && options.num_isolates) {
1343 SetFlagsFromString("--nologfile_per_isolate");
1350 int Shell::RunMain(Isolate* isolate, int argc, char* argv[]) {
1352 for (int i = 1; i < options.num_isolates; ++i) {
1353 options.isolate_sources[i].StartExecuteInThread();
1355 #endif // !V8_SHARED
1357 HandleScope scope(isolate);
1358 Local<Context> context = CreateEvaluationContext(isolate);
1359 if (options.last_run && options.use_interactive_shell()) {
1360 // Keep using the same context in the interactive shell.
1361 evaluation_context_.Reset(isolate, context);
1363 // If the interactive debugger is enabled make sure to activate
1364 // it before running the files passed on the command line.
1365 if (i::FLAG_debugger) {
1366 InstallUtilityScript(isolate);
1368 #endif // !V8_SHARED
1371 Context::Scope cscope(context);
1372 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
1373 options.isolate_sources[0].Execute(isolate);
1376 if (options.send_idle_notification) {
1377 const int kLongIdlePauseInMs = 1000;
1378 V8::ContextDisposedNotification();
1379 V8::IdleNotification(kLongIdlePauseInMs);
1381 if (options.invoke_weak_callbacks) {
1382 // By sending a low memory notifications, we will try hard to collect all
1383 // garbage and will therefore also invoke all weak callbacks of actually
1384 // unreachable persistent handles.
1385 V8::LowMemoryNotification();
1389 for (int i = 1; i < options.num_isolates; ++i) {
1390 options.isolate_sources[i].WaitForThread();
1392 #endif // !V8_SHARED
1398 static void DumpHeapConstants(i::Isolate* isolate) {
1399 i::Heap* heap = isolate->heap();
1401 // Dump the INSTANCE_TYPES table to the console.
1402 printf("# List of known V8 instance types.\n");
1403 #define DUMP_TYPE(T) printf(" %d: \"%s\",\n", i::T, #T);
1404 printf("INSTANCE_TYPES = {\n");
1405 INSTANCE_TYPE_LIST(DUMP_TYPE)
1409 // Dump the KNOWN_MAP table to the console.
1410 printf("\n# List of known V8 maps.\n");
1411 #define ROOT_LIST_CASE(type, name, camel_name) \
1412 if (n == NULL && o == heap->name()) n = #camel_name;
1413 #define STRUCT_LIST_CASE(upper_name, camel_name, name) \
1414 if (n == NULL && o == heap->name##_map()) n = #camel_name "Map";
1415 i::HeapObjectIterator it(heap->map_space());
1416 printf("KNOWN_MAPS = {\n");
1417 for (i::Object* o = it.Next(); o != NULL; o = it.Next()) {
1418 i::Map* m = i::Map::cast(o);
1419 const char* n = NULL;
1420 intptr_t p = reinterpret_cast<intptr_t>(m) & 0xfffff;
1421 int t = m->instance_type();
1422 ROOT_LIST(ROOT_LIST_CASE)
1423 STRUCT_LIST(STRUCT_LIST_CASE)
1424 if (n == NULL) continue;
1425 printf(" 0x%05" V8PRIxPTR ": (%d, \"%s\"),\n", p, t, n);
1428 #undef STRUCT_LIST_CASE
1429 #undef ROOT_LIST_CASE
1431 // Dump the KNOWN_OBJECTS table to the console.
1432 printf("\n# List of known V8 objects.\n");
1433 #define ROOT_LIST_CASE(type, name, camel_name) \
1434 if (n == NULL && o == heap->name()) n = #camel_name;
1435 i::OldSpaces spit(heap);
1436 printf("KNOWN_OBJECTS = {\n");
1437 for (i::PagedSpace* s = spit.next(); s != NULL; s = spit.next()) {
1438 i::HeapObjectIterator it(s);
1439 const char* sname = AllocationSpaceName(s->identity());
1440 for (i::Object* o = it.Next(); o != NULL; o = it.Next()) {
1441 const char* n = NULL;
1442 intptr_t p = reinterpret_cast<intptr_t>(o) & 0xfffff;
1443 ROOT_LIST(ROOT_LIST_CASE)
1444 if (n == NULL) continue;
1445 printf(" (\"%s\", 0x%05" V8PRIxPTR "): \"%s\",\n", sname, p, n);
1449 #undef ROOT_LIST_CASE
1451 #endif // !V8_SHARED
1454 class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
1456 virtual void* Allocate(size_t length) {
1457 void* data = AllocateUninitialized(length);
1458 return data == NULL ? data : memset(data, 0, length);
1460 virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
1461 virtual void Free(void* data, size_t) { free(data); }
1465 class MockArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
1467 virtual void* Allocate(size_t) V8_OVERRIDE {
1470 virtual void* AllocateUninitialized(size_t length) V8_OVERRIDE {
1473 virtual void Free(void* p, size_t) V8_OVERRIDE {
1479 int Shell::Main(int argc, char* argv[]) {
1480 if (!SetOptions(argc, argv)) return 1;
1481 v8::V8::InitializeICU(options.icu_data_file);
1482 SetFlagsFromString("--trace-hydrogen-file=hydrogen.cfg");
1483 SetFlagsFromString("--redirect-code-traces-to=code.asm");
1484 ShellArrayBufferAllocator array_buffer_allocator;
1485 MockArrayBufferAllocator mock_arraybuffer_allocator;
1486 if (options.mock_arraybuffer_allocator) {
1487 v8::V8::SetArrayBufferAllocator(&mock_arraybuffer_allocator);
1489 v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
1492 Isolate* isolate = Isolate::New();
1494 v8::ResourceConstraints constraints;
1495 constraints.ConfigureDefaults(i::OS::TotalPhysicalMemory(),
1496 i::OS::MaxVirtualMemory(),
1497 i::OS::NumberOfProcessorsOnline());
1498 v8::SetResourceConstraints(isolate, &constraints);
1500 DumbLineEditor dumb_line_editor(isolate);
1502 Isolate::Scope scope(isolate);
1503 Initialize(isolate);
1504 #ifdef ENABLE_VTUNE_JIT_INTERFACE
1505 vTune::InitializeVtuneForV8();
1507 PerIsolateData data(isolate);
1508 InitializeDebugger(isolate);
1511 if (options.dump_heap_constants) {
1512 DumpHeapConstants(reinterpret_cast<i::Isolate*>(isolate));
1517 if (options.stress_opt || options.stress_deopt) {
1518 Testing::SetStressRunType(options.stress_opt
1519 ? Testing::kStressTypeOpt
1520 : Testing::kStressTypeDeopt);
1521 int stress_runs = Testing::GetStressRuns();
1522 for (int i = 0; i < stress_runs && result == 0; i++) {
1523 printf("============ Stress %d/%d ============\n", i + 1, stress_runs);
1524 Testing::PrepareStressRun(i);
1525 options.last_run = (i == stress_runs - 1);
1526 result = RunMain(isolate, argc, argv);
1528 printf("======== Full Deoptimization =======\n");
1529 Testing::DeoptimizeAll();
1530 #if !defined(V8_SHARED)
1531 } else if (i::FLAG_stress_runs > 0) {
1532 int stress_runs = i::FLAG_stress_runs;
1533 for (int i = 0; i < stress_runs && result == 0; i++) {
1534 printf("============ Run %d/%d ============\n", i + 1, stress_runs);
1535 options.last_run = (i == stress_runs - 1);
1536 result = RunMain(isolate, argc, argv);
1540 result = RunMain(isolate, argc, argv);
1543 // Run interactive shell if explicitly requested or if no script has been
1544 // executed, but never on --test
1545 if (options.use_interactive_shell()) {
1547 if (!i::FLAG_debugger) {
1548 InstallUtilityScript(isolate);
1550 #endif // !V8_SHARED
1566 int main(int argc, char* argv[]) {
1567 return v8::Shell::Main(argc, argv);