d8.cc revision a7e24c173cf37484693b9abb38e494fa7bd7baeb
1// Copyright 2009 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#include <stdlib.h>
30#include <errno.h>
31
32#include "d8.h"
33#include "d8-debug.h"
34#include "debug.h"
35#include "api.h"
36#include "natives.h"
37#include "platform.h"
38
39
40namespace v8 {
41
42
43const char* Shell::kHistoryFileName = ".d8_history";
44const char* Shell::kPrompt = "d8> ";
45
46
47LineEditor *LineEditor::first_ = NULL;
48
49
50LineEditor::LineEditor(Type type, const char* name)
51    : type_(type),
52      name_(name),
53      next_(first_) {
54  first_ = this;
55}
56
57
58LineEditor* LineEditor::Get() {
59  LineEditor* current = first_;
60  LineEditor* best = current;
61  while (current != NULL) {
62    if (current->type_ > best->type_)
63      best = current;
64    current = current->next_;
65  }
66  return best;
67}
68
69
70class DumbLineEditor: public LineEditor {
71 public:
72  DumbLineEditor() : LineEditor(LineEditor::DUMB, "dumb") { }
73  virtual i::SmartPointer<char> Prompt(const char* prompt);
74};
75
76
77static DumbLineEditor dumb_line_editor;
78
79
80i::SmartPointer<char> DumbLineEditor::Prompt(const char* prompt) {
81  static const int kBufferSize = 256;
82  char buffer[kBufferSize];
83  printf("%s", prompt);
84  char* str = fgets(buffer, kBufferSize, stdin);
85  return i::SmartPointer<char>(str ? i::StrDup(str) : str);
86}
87
88
89CounterMap* Shell::counter_map_;
90i::OS::MemoryMappedFile* Shell::counters_file_ = NULL;
91CounterCollection Shell::local_counters_;
92CounterCollection* Shell::counters_ = &local_counters_;
93Persistent<Context> Shell::utility_context_;
94Persistent<Context> Shell::evaluation_context_;
95
96
97bool CounterMap::Match(void* key1, void* key2) {
98  const char* name1 = reinterpret_cast<const char*>(key1);
99  const char* name2 = reinterpret_cast<const char*>(key2);
100  return strcmp(name1, name2) == 0;
101}
102
103
104// Converts a V8 value to a C string.
105const char* ToCString(const v8::String::Utf8Value& value) {
106  return *value ? *value : "<string conversion failed>";
107}
108
109
110// Executes a string within the current v8 context.
111bool Shell::ExecuteString(Handle<String> source,
112                          Handle<Value> name,
113                          bool print_result,
114                          bool report_exceptions) {
115  HandleScope handle_scope;
116  TryCatch try_catch;
117  if (i::FLAG_debugger) {
118    // When debugging make exceptions appear to be uncaught.
119    try_catch.SetVerbose(true);
120  }
121  Handle<Script> script = Script::Compile(source, name);
122  if (script.IsEmpty()) {
123    // Print errors that happened during compilation.
124    if (report_exceptions && !i::FLAG_debugger)
125      ReportException(&try_catch);
126    return false;
127  } else {
128    Handle<Value> result = script->Run();
129    if (result.IsEmpty()) {
130      // Print errors that happened during execution.
131      if (report_exceptions && !i::FLAG_debugger)
132        ReportException(&try_catch);
133      return false;
134    } else {
135      if (print_result && !result->IsUndefined()) {
136        // If all went well and the result wasn't undefined then print
137        // the returned value.
138        v8::String::Utf8Value str(result);
139        const char* cstr = ToCString(str);
140        printf("%s\n", cstr);
141      }
142      return true;
143    }
144  }
145}
146
147
148Handle<Value> Shell::Print(const Arguments& args) {
149  Handle<Value> val = Write(args);
150  printf("\n");
151  return val;
152}
153
154
155Handle<Value> Shell::Write(const Arguments& args) {
156  for (int i = 0; i < args.Length(); i++) {
157    HandleScope handle_scope;
158    if (i != 0) {
159      printf(" ");
160    }
161    v8::String::Utf8Value str(args[i]);
162    fwrite(*str, sizeof(**str), str.length(), stdout);
163  }
164  return Undefined();
165}
166
167
168Handle<Value> Shell::Read(const Arguments& args) {
169  String::Utf8Value file(args[0]);
170  if (*file == NULL) {
171    return ThrowException(String::New("Error loading file"));
172  }
173  Handle<String> source = ReadFile(*file);
174  if (source.IsEmpty()) {
175    return ThrowException(String::New("Error loading file"));
176  }
177  return source;
178}
179
180
181Handle<Value> Shell::ReadLine(const Arguments& args) {
182  i::SmartPointer<char> line(i::ReadLine(""));
183  if (*line == NULL) {
184    return Null();
185  }
186  size_t len = strlen(*line);
187  if (len > 0 && line[len - 1] == '\n') {
188    --len;
189  }
190  return String::New(*line, len);
191}
192
193
194Handle<Value> Shell::Load(const Arguments& args) {
195  for (int i = 0; i < args.Length(); i++) {
196    HandleScope handle_scope;
197    String::Utf8Value file(args[i]);
198    if (*file == NULL) {
199      return ThrowException(String::New("Error loading file"));
200    }
201    Handle<String> source = ReadFile(*file);
202    if (source.IsEmpty()) {
203      return ThrowException(String::New("Error loading file"));
204    }
205    if (!ExecuteString(source, String::New(*file), false, false)) {
206      return ThrowException(String::New("Error executing  file"));
207    }
208  }
209  return Undefined();
210}
211
212
213Handle<Value> Shell::Yield(const Arguments& args) {
214  v8::Unlocker unlocker;
215  return Undefined();
216}
217
218
219Handle<Value> Shell::Quit(const Arguments& args) {
220  int exit_code = args[0]->Int32Value();
221  OnExit();
222  exit(exit_code);
223  return Undefined();
224}
225
226
227Handle<Value> Shell::Version(const Arguments& args) {
228  return String::New(V8::GetVersion());
229}
230
231
232void Shell::ReportException(v8::TryCatch* try_catch) {
233  HandleScope handle_scope;
234  v8::String::Utf8Value exception(try_catch->Exception());
235  const char* exception_string = ToCString(exception);
236  Handle<Message> message = try_catch->Message();
237  if (message.IsEmpty()) {
238    // V8 didn't provide any extra information about this error; just
239    // print the exception.
240    printf("%s\n", exception_string);
241  } else {
242    // Print (filename):(line number): (message).
243    v8::String::Utf8Value filename(message->GetScriptResourceName());
244    const char* filename_string = ToCString(filename);
245    int linenum = message->GetLineNumber();
246    printf("%s:%i: %s\n", filename_string, linenum, exception_string);
247    // Print line of source code.
248    v8::String::Utf8Value sourceline(message->GetSourceLine());
249    const char* sourceline_string = ToCString(sourceline);
250    printf("%s\n", sourceline_string);
251    // Print wavy underline (GetUnderline is deprecated).
252    int start = message->GetStartColumn();
253    for (int i = 0; i < start; i++) {
254      printf(" ");
255    }
256    int end = message->GetEndColumn();
257    for (int i = start; i < end; i++) {
258      printf("^");
259    }
260    printf("\n");
261  }
262}
263
264
265Handle<Array> Shell::GetCompletions(Handle<String> text, Handle<String> full) {
266  HandleScope handle_scope;
267  Context::Scope context_scope(utility_context_);
268  Handle<Object> global = utility_context_->Global();
269  Handle<Value> fun = global->Get(String::New("GetCompletions"));
270  static const int kArgc = 3;
271  Handle<Value> argv[kArgc] = { evaluation_context_->Global(), text, full };
272  Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
273  return handle_scope.Close(Handle<Array>::Cast(val));
274}
275
276
277#ifdef ENABLE_DEBUGGER_SUPPORT
278Handle<Object> Shell::DebugMessageDetails(Handle<String> message) {
279  Context::Scope context_scope(utility_context_);
280  Handle<Object> global = utility_context_->Global();
281  Handle<Value> fun = global->Get(String::New("DebugMessageDetails"));
282  static const int kArgc = 1;
283  Handle<Value> argv[kArgc] = { message };
284  Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
285  return Handle<Object>::Cast(val);
286}
287
288
289Handle<Value> Shell::DebugCommandToJSONRequest(Handle<String> command) {
290  Context::Scope context_scope(utility_context_);
291  Handle<Object> global = utility_context_->Global();
292  Handle<Value> fun = global->Get(String::New("DebugCommandToJSONRequest"));
293  static const int kArgc = 1;
294  Handle<Value> argv[kArgc] = { command };
295  Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv);
296  return val;
297}
298#endif
299
300
301int32_t* Counter::Bind(const char* name, bool is_histogram) {
302  int i;
303  for (i = 0; i < kMaxNameSize - 1 && name[i]; i++)
304    name_[i] = static_cast<char>(name[i]);
305  name_[i] = '\0';
306  is_histogram_ = is_histogram;
307  return ptr();
308}
309
310
311void Counter::AddSample(int32_t sample) {
312  count_++;
313  sample_total_ += sample;
314}
315
316
317CounterCollection::CounterCollection() {
318  magic_number_ = 0xDEADFACE;
319  max_counters_ = kMaxCounters;
320  max_name_size_ = Counter::kMaxNameSize;
321  counters_in_use_ = 0;
322}
323
324
325Counter* CounterCollection::GetNextCounter() {
326  if (counters_in_use_ == kMaxCounters) return NULL;
327  return &counters_[counters_in_use_++];
328}
329
330
331void Shell::MapCounters(const char* name) {
332  counters_file_ = i::OS::MemoryMappedFile::create(name,
333    sizeof(CounterCollection), &local_counters_);
334  void* memory = (counters_file_ == NULL) ?
335      NULL : counters_file_->memory();
336  if (memory == NULL) {
337    printf("Could not map counters file %s\n", name);
338    exit(1);
339  }
340  counters_ = static_cast<CounterCollection*>(memory);
341  V8::SetCounterFunction(LookupCounter);
342  V8::SetCreateHistogramFunction(CreateHistogram);
343  V8::SetAddHistogramSampleFunction(AddHistogramSample);
344}
345
346
347int CounterMap::Hash(const char* name) {
348  int h = 0;
349  int c;
350  while ((c = *name++) != 0) {
351    h += h << 5;
352    h += c;
353  }
354  return h;
355}
356
357
358Counter* Shell::GetCounter(const char* name, bool is_histogram) {
359  Counter* counter = counter_map_->Lookup(name);
360
361  if (counter == NULL) {
362    counter = counters_->GetNextCounter();
363    if (counter != NULL) {
364      counter_map_->Set(name, counter);
365      counter->Bind(name, is_histogram);
366    }
367  } else {
368    ASSERT(counter->is_histogram() == is_histogram);
369  }
370  return counter;
371}
372
373
374int* Shell::LookupCounter(const char* name) {
375  Counter* counter = GetCounter(name, false);
376
377  if (counter != NULL) {
378    return counter->ptr();
379  } else {
380    return NULL;
381  }
382}
383
384
385void* Shell::CreateHistogram(const char* name,
386                             int min,
387                             int max,
388                             size_t buckets) {
389  return GetCounter(name, true);
390}
391
392
393void Shell::AddHistogramSample(void* histogram, int sample) {
394  Counter* counter = reinterpret_cast<Counter*>(histogram);
395  counter->AddSample(sample);
396}
397
398
399void Shell::Initialize() {
400  Shell::counter_map_ = new CounterMap();
401  // Set up counters
402  if (i::FLAG_map_counters != NULL)
403    MapCounters(i::FLAG_map_counters);
404  if (i::FLAG_dump_counters) {
405    V8::SetCounterFunction(LookupCounter);
406    V8::SetCreateHistogramFunction(CreateHistogram);
407    V8::SetAddHistogramSampleFunction(AddHistogramSample);
408  }
409
410  // Initialize the global objects
411  HandleScope scope;
412  Handle<ObjectTemplate> global_template = ObjectTemplate::New();
413  global_template->Set(String::New("print"), FunctionTemplate::New(Print));
414  global_template->Set(String::New("write"), FunctionTemplate::New(Write));
415  global_template->Set(String::New("read"), FunctionTemplate::New(Read));
416  global_template->Set(String::New("readline"),
417                       FunctionTemplate::New(ReadLine));
418  global_template->Set(String::New("load"), FunctionTemplate::New(Load));
419  global_template->Set(String::New("quit"), FunctionTemplate::New(Quit));
420  global_template->Set(String::New("version"), FunctionTemplate::New(Version));
421
422  Handle<ObjectTemplate> os_templ = ObjectTemplate::New();
423  AddOSMethods(os_templ);
424  global_template->Set(String::New("os"), os_templ);
425
426  utility_context_ = Context::New(NULL, global_template);
427  utility_context_->SetSecurityToken(Undefined());
428  Context::Scope utility_scope(utility_context_);
429
430  i::JSArguments js_args = i::FLAG_js_arguments;
431  i::Handle<i::FixedArray> arguments_array =
432      i::Factory::NewFixedArray(js_args.argc());
433  for (int j = 0; j < js_args.argc(); j++) {
434    i::Handle<i::String> arg =
435        i::Factory::NewStringFromUtf8(i::CStrVector(js_args[j]));
436    arguments_array->set(j, *arg);
437  }
438  i::Handle<i::JSArray> arguments_jsarray =
439      i::Factory::NewJSArrayWithElements(arguments_array);
440  global_template->Set(String::New("arguments"),
441                       Utils::ToLocal(arguments_jsarray));
442
443#ifdef ENABLE_DEBUGGER_SUPPORT
444  // Install the debugger object in the utility scope
445  i::Debug::Load();
446  i::JSObject* debug = i::Debug::debug_context()->global();
447  utility_context_->Global()->Set(String::New("$debug"),
448                                  Utils::ToLocal(&debug));
449#endif
450
451  // Run the d8 shell utility script in the utility context
452  int source_index = i::NativesCollection<i::D8>::GetIndex("d8");
453  i::Vector<const char> shell_source
454      = i::NativesCollection<i::D8>::GetScriptSource(source_index);
455  i::Vector<const char> shell_source_name
456      = i::NativesCollection<i::D8>::GetScriptName(source_index);
457  Handle<String> source = String::New(shell_source.start(),
458                                      shell_source.length());
459  Handle<String> name = String::New(shell_source_name.start(),
460                                    shell_source_name.length());
461  Handle<Script> script = Script::Compile(source, name);
462  script->Run();
463
464  // Mark the d8 shell script as native to avoid it showing up as normal source
465  // in the debugger.
466  i::Handle<i::JSFunction> script_fun = Utils::OpenHandle(*script);
467  i::Handle<i::Script> script_object =
468      i::Handle<i::Script>(i::Script::cast(script_fun->shared()->script()));
469  script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE));
470
471  // Create the evaluation context
472  evaluation_context_ = Context::New(NULL, global_template);
473  evaluation_context_->SetSecurityToken(Undefined());
474
475#ifdef ENABLE_DEBUGGER_SUPPORT
476  // Set the security token of the debug context to allow access.
477  i::Debug::debug_context()->set_security_token(i::Heap::undefined_value());
478
479  // Start the debugger agent if requested.
480  if (i::FLAG_debugger_agent) {
481    v8::Debug::EnableAgent("d8 shell", i::FLAG_debugger_port);
482  }
483
484  // Start the in-process debugger if requested.
485  if (i::FLAG_debugger && !i::FLAG_debugger_agent) {
486    v8::Debug::SetDebugEventListener(HandleDebugEvent);
487  }
488#endif
489}
490
491
492void Shell::OnExit() {
493  if (i::FLAG_dump_counters) {
494    ::printf("+----------------------------------------+-------------+\n");
495    ::printf("| Name                                   | Value       |\n");
496    ::printf("+----------------------------------------+-------------+\n");
497    for (CounterMap::Iterator i(counter_map_); i.More(); i.Next()) {
498      Counter* counter = i.CurrentValue();
499      if (counter->is_histogram()) {
500        ::printf("| c:%-36s | %11i |\n", i.CurrentKey(), counter->count());
501        ::printf("| t:%-36s | %11i |\n",
502                 i.CurrentKey(),
503                 counter->sample_total());
504      } else {
505        ::printf("| %-38s | %11i |\n", i.CurrentKey(), counter->count());
506      }
507    }
508    ::printf("+----------------------------------------+-------------+\n");
509  }
510  if (counters_file_ != NULL)
511    delete counters_file_;
512}
513
514
515static char* ReadChars(const char* name, int* size_out) {
516  v8::Unlocker unlocker;  // Release the V8 lock while reading files.
517  FILE* file = i::OS::FOpen(name, "rb");
518  if (file == NULL) return NULL;
519
520  fseek(file, 0, SEEK_END);
521  int size = ftell(file);
522  rewind(file);
523
524  char* chars = new char[size + 1];
525  chars[size] = '\0';
526  for (int i = 0; i < size;) {
527    int read = fread(&chars[i], 1, size - i, file);
528    i += read;
529  }
530  fclose(file);
531  *size_out = size;
532  return chars;
533}
534
535
536static char* ReadToken(char* data, char token) {
537  char* next = i::OS::StrChr(data, token);
538  if (next != NULL) {
539    *next = '\0';
540    return (next + 1);
541  }
542
543  return NULL;
544}
545
546
547static char* ReadLine(char* data) {
548  return ReadToken(data, '\n');
549}
550
551
552static char* ReadWord(char* data) {
553  return ReadToken(data, ' ');
554}
555
556
557// Reads a file into a v8 string.
558Handle<String> Shell::ReadFile(const char* name) {
559  int size = 0;
560  char* chars = ReadChars(name, &size);
561  if (chars == NULL) return Handle<String>();
562  Handle<String> result = String::New(chars);
563  delete[] chars;
564  return result;
565}
566
567
568void Shell::RunShell() {
569  LineEditor* editor = LineEditor::Get();
570  printf("V8 version %s [console: %s]\n", V8::GetVersion(), editor->name());
571  editor->Open();
572  while (true) {
573    Locker locker;
574    HandleScope handle_scope;
575    Context::Scope context_scope(evaluation_context_);
576    i::SmartPointer<char> input = editor->Prompt(Shell::kPrompt);
577    if (input.is_empty())
578      break;
579    editor->AddHistory(*input);
580    Handle<String> name = String::New("(d8)");
581    ExecuteString(String::New(*input), name, true, true);
582  }
583  editor->Close();
584  printf("\n");
585}
586
587
588class ShellThread : public i::Thread {
589 public:
590  ShellThread(int no, i::Vector<const char> files)
591    : no_(no), files_(files) { }
592  virtual void Run();
593 private:
594  int no_;
595  i::Vector<const char> files_;
596};
597
598
599void ShellThread::Run() {
600  // Prepare the context for this thread.
601  Locker locker;
602  HandleScope scope;
603  Handle<ObjectTemplate> global_template = ObjectTemplate::New();
604  global_template->Set(String::New("print"),
605                       FunctionTemplate::New(Shell::Print));
606  global_template->Set(String::New("write"),
607                       FunctionTemplate::New(Shell::Write));
608  global_template->Set(String::New("read"),
609                       FunctionTemplate::New(Shell::Read));
610  global_template->Set(String::New("readline"),
611                       FunctionTemplate::New(Shell::ReadLine));
612  global_template->Set(String::New("load"),
613                       FunctionTemplate::New(Shell::Load));
614  global_template->Set(String::New("yield"),
615                       FunctionTemplate::New(Shell::Yield));
616  global_template->Set(String::New("version"),
617                       FunctionTemplate::New(Shell::Version));
618
619  char* ptr = const_cast<char*>(files_.start());
620  while ((ptr != NULL) && (*ptr != '\0')) {
621    // For each newline-separated line.
622    char* next_line = ReadLine(ptr);
623
624    if (*ptr == '#') {
625      // Skip comment lines.
626      ptr = next_line;
627      continue;
628    }
629
630    Persistent<Context> thread_context = Context::New(NULL, global_template);
631    thread_context->SetSecurityToken(Undefined());
632    Context::Scope context_scope(thread_context);
633
634    while ((ptr != NULL) && (*ptr != '\0')) {
635      char* filename = ptr;
636      ptr = ReadWord(ptr);
637
638      // Skip empty strings.
639      if (strlen(filename) == 0) {
640        break;
641      }
642
643      Handle<String> str = Shell::ReadFile(filename);
644      if (str.IsEmpty()) {
645        printf("WARNING: %s not found\n", filename);
646        break;
647      }
648
649      Shell::ExecuteString(str, String::New(filename), false, false);
650    }
651
652    thread_context.Dispose();
653    ptr = next_line;
654  }
655}
656
657
658int Shell::Main(int argc, char* argv[]) {
659  i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
660  if (i::FLAG_help) {
661    return 1;
662  }
663  Initialize();
664  bool run_shell = (argc == 1);
665
666  // Default use preemption if threads are created.
667  bool use_preemption = true;
668
669  // Default to use lowest possible thread preemption interval to test as many
670  // edgecases as possible.
671  int preemption_interval = 1;
672
673  i::List<i::Thread*> threads(1);
674
675  {
676    // Acquire the V8 lock once initialization has finished. Since the thread
677    // below may spawn new threads accessing V8 holding the V8 lock here is
678    // mandatory.
679    Locker locker;
680    Context::Scope context_scope(evaluation_context_);
681    for (int i = 1; i < argc; i++) {
682      char* str = argv[i];
683      if (strcmp(str, "--shell") == 0) {
684        run_shell = true;
685      } else if (strcmp(str, "--preemption") == 0) {
686        use_preemption = true;
687      } else if (strcmp(str, "--no-preemption") == 0) {
688        use_preemption = false;
689      } else if (strcmp(str, "--preemption-interval") == 0) {
690        if (i + 1 < argc) {
691          char* end = NULL;
692          preemption_interval = strtol(argv[++i], &end, 10);  // NOLINT
693          if (preemption_interval <= 0 || *end != '\0' || errno == ERANGE) {
694            printf("Invalid value for --preemption-interval '%s'\n", argv[i]);
695            return 1;
696          }
697        } else {
698          printf("Missing value for --preemption-interval\n");
699          return 1;
700       }
701      } else if (strcmp(str, "-f") == 0) {
702        // Ignore any -f flags for compatibility with other stand-alone
703        // JavaScript engines.
704        continue;
705      } else if (strncmp(str, "--", 2) == 0) {
706        printf("Warning: unknown flag %s.\nTry --help for options\n", str);
707      } else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
708        // Execute argument given to -e option directly.
709        v8::HandleScope handle_scope;
710        v8::Handle<v8::String> file_name = v8::String::New("unnamed");
711        v8::Handle<v8::String> source = v8::String::New(argv[i + 1]);
712        if (!ExecuteString(source, file_name, false, true)) {
713          OnExit();
714          return 1;
715        }
716        i++;
717      } else if (strcmp(str, "-p") == 0 && i + 1 < argc) {
718        int size = 0;
719        const char* files = ReadChars(argv[++i], &size);
720        if (files == NULL) return 1;
721        ShellThread* thread =
722            new ShellThread(threads.length(),
723                            i::Vector<const char>(files, size));
724        thread->Start();
725        threads.Add(thread);
726      } else {
727        // Use all other arguments as names of files to load and run.
728        HandleScope handle_scope;
729        Handle<String> file_name = v8::String::New(str);
730        Handle<String> source = ReadFile(str);
731        if (source.IsEmpty()) {
732          printf("Error reading '%s'\n", str);
733          return 1;
734        }
735        if (!ExecuteString(source, file_name, false, true)) {
736          OnExit();
737          return 1;
738        }
739      }
740    }
741
742    // Start preemption if threads have been created and preemption is enabled.
743    if (threads.length() > 0 && use_preemption) {
744      Locker::StartPreemption(preemption_interval);
745    }
746
747#ifdef ENABLE_DEBUGGER_SUPPORT
748    // Run the remote debugger if requested.
749    if (i::FLAG_remote_debugger) {
750      RunRemoteDebugger(i::FLAG_debugger_port);
751      return 0;
752    }
753#endif
754  }
755  if (run_shell)
756    RunShell();
757  for (int i = 0; i < threads.length(); i++) {
758    i::Thread* thread = threads[i];
759    thread->Join();
760    delete thread;
761  }
762  OnExit();
763  return 0;
764}
765
766
767}  // namespace v8
768
769
770int main(int argc, char* argv[]) {
771  return v8::Shell::Main(argc, argv);
772}
773