OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 #if !defined(_WIN32) && !defined(_WIN64) | 59 #if !defined(_WIN32) && !defined(_WIN64) |
60 #include <unistd.h> // NOLINT | 60 #include <unistd.h> // NOLINT |
61 #endif | 61 #endif |
62 | 62 |
63 #ifndef ASSERT | 63 #ifndef ASSERT |
64 #define ASSERT(condition) assert(condition) | 64 #define ASSERT(condition) assert(condition) |
65 #endif | 65 #endif |
66 | 66 |
67 namespace v8 { | 67 namespace v8 { |
68 | 68 |
69 | |
70 #ifndef V8_SHARED | |
71 LineEditor *LineEditor::first_ = NULL; | 69 LineEditor *LineEditor::first_ = NULL; |
72 const char* Shell::kHistoryFileName = ".d8_history"; | |
73 const int Shell::kMaxHistoryEntries = 1000; | |
74 | 70 |
75 | 71 |
76 LineEditor::LineEditor(Type type, const char* name) | 72 LineEditor::LineEditor(Type type, const char* name) |
77 : type_(type), | 73 : type_(type), |
78 name_(name), | 74 name_(name), |
79 next_(first_) { | 75 next_(first_) { |
80 first_ = this; | 76 first_ = this; |
81 } | 77 } |
82 | 78 |
83 | 79 |
84 LineEditor* LineEditor::Get() { | 80 LineEditor* LineEditor::Get() { |
85 LineEditor* current = first_; | 81 LineEditor* current = first_; |
86 LineEditor* best = current; | 82 LineEditor* best = current; |
87 while (current != NULL) { | 83 while (current != NULL) { |
88 if (current->type_ > best->type_) | 84 if (current->type_ > best->type_) |
89 best = current; | 85 best = current; |
90 current = current->next_; | 86 current = current->next_; |
91 } | 87 } |
92 return best; | 88 return best; |
93 } | 89 } |
94 | 90 |
95 | 91 |
96 class DumbLineEditor: public LineEditor { | 92 class DumbLineEditor: public LineEditor { |
97 public: | 93 public: |
98 DumbLineEditor() : LineEditor(LineEditor::DUMB, "dumb") { } | 94 DumbLineEditor() : LineEditor(LineEditor::DUMB, "dumb") { } |
99 virtual i::SmartArrayPointer<char> Prompt(const char* prompt); | 95 virtual Handle<String> Prompt(const char* prompt); |
100 }; | 96 }; |
101 | 97 |
102 | 98 |
103 static DumbLineEditor dumb_line_editor; | 99 static DumbLineEditor dumb_line_editor; |
104 | 100 |
105 | 101 |
106 i::SmartArrayPointer<char> DumbLineEditor::Prompt(const char* prompt) { | 102 Handle<String> DumbLineEditor::Prompt(const char* prompt) { |
107 static const int kBufferSize = 256; | |
108 char buffer[kBufferSize]; | |
109 printf("%s", prompt); | 103 printf("%s", prompt); |
110 char* str = fgets(buffer, kBufferSize, stdin); | 104 return Shell::ReadFromStdin(); |
111 return i::SmartArrayPointer<char>(str ? i::StrDup(str) : str); | |
112 } | 105 } |
113 | 106 |
114 | 107 |
| 108 #ifndef V8_SHARED |
115 CounterMap* Shell::counter_map_; | 109 CounterMap* Shell::counter_map_; |
116 i::OS::MemoryMappedFile* Shell::counters_file_ = NULL; | 110 i::OS::MemoryMappedFile* Shell::counters_file_ = NULL; |
117 CounterCollection Shell::local_counters_; | 111 CounterCollection Shell::local_counters_; |
118 CounterCollection* Shell::counters_ = &local_counters_; | 112 CounterCollection* Shell::counters_ = &local_counters_; |
119 i::Mutex* Shell::context_mutex_(i::OS::CreateMutex()); | 113 i::Mutex* Shell::context_mutex_(i::OS::CreateMutex()); |
120 Persistent<Context> Shell::utility_context_; | 114 Persistent<Context> Shell::utility_context_; |
121 LineEditor* Shell::console = NULL; | |
122 #endif // V8_SHARED | 115 #endif // V8_SHARED |
123 | 116 |
| 117 LineEditor* Shell::console = NULL; |
124 Persistent<Context> Shell::evaluation_context_; | 118 Persistent<Context> Shell::evaluation_context_; |
125 ShellOptions Shell::options; | 119 ShellOptions Shell::options; |
126 const char* Shell::kPrompt = "d8> "; | 120 const char* Shell::kPrompt = "d8> "; |
127 | 121 |
128 | 122 |
129 #ifndef V8_SHARED | 123 #ifndef V8_SHARED |
130 bool CounterMap::Match(void* key1, void* key2) { | 124 bool CounterMap::Match(void* key1, void* key2) { |
131 const char* name1 = reinterpret_cast<const char*>(key1); | 125 const char* name1 = reinterpret_cast<const char*>(key1); |
132 const char* name2 = reinterpret_cast<const char*>(key2); | 126 const char* name2 = reinterpret_cast<const char*>(key2); |
133 return strcmp(name1, name2) == 0; | 127 return strcmp(name1, name2) == 0; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 return ThrowException(String::New("Error loading file")); | 225 return ThrowException(String::New("Error loading file")); |
232 } | 226 } |
233 Handle<String> source = ReadFile(*file); | 227 Handle<String> source = ReadFile(*file); |
234 if (source.IsEmpty()) { | 228 if (source.IsEmpty()) { |
235 return ThrowException(String::New("Error loading file")); | 229 return ThrowException(String::New("Error loading file")); |
236 } | 230 } |
237 return source; | 231 return source; |
238 } | 232 } |
239 | 233 |
240 | 234 |
241 Handle<Value> Shell::ReadLine(const Arguments& args) { | 235 Handle<String> Shell::ReadFromStdin() { |
242 static const int kBufferSize = 256; | 236 static const int kBufferSize = 256; |
243 char buffer[kBufferSize]; | 237 char buffer[kBufferSize]; |
244 Handle<String> accumulator = String::New(""); | 238 Handle<String> accumulator = String::New(""); |
245 int length; | 239 int length; |
246 while (true) { | 240 while (true) { |
247 // Continue reading if the line ends with an escape '\\' or the line has | 241 // Continue reading if the line ends with an escape '\\' or the line has |
248 // not been fully read into the buffer yet (does not end with '\n'). | 242 // not been fully read into the buffer yet (does not end with '\n'). |
249 // If fgets gets an error, just give up. | 243 // If fgets gets an error, just give up. |
250 if (fgets(buffer, kBufferSize, stdin) == NULL) return Null(); | 244 if (fgets(buffer, kBufferSize, stdin) == NULL) return Handle<String>(); |
251 length = static_cast<int>(strlen(buffer)); | 245 length = static_cast<int>(strlen(buffer)); |
252 if (length == 0) { | 246 if (length == 0) { |
253 return accumulator; | 247 return accumulator; |
254 } else if (buffer[length-1] != '\n') { | 248 } else if (buffer[length-1] != '\n') { |
255 accumulator = String::Concat(accumulator, String::New(buffer, length)); | 249 accumulator = String::Concat(accumulator, String::New(buffer, length)); |
256 } else if (length > 1 && buffer[length-2] == '\\') { | 250 } else if (length > 1 && buffer[length-2] == '\\') { |
257 buffer[length-2] = '\n'; | 251 buffer[length-2] = '\n'; |
258 accumulator = String::Concat(accumulator, String::New(buffer, length-1)); | 252 accumulator = String::Concat(accumulator, String::New(buffer, length-1)); |
259 } else { | 253 } else { |
260 return String::Concat(accumulator, String::New(buffer, length-1)); | 254 return String::Concat(accumulator, String::New(buffer, length-1)); |
(...skipping 779 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1040 delete[] chars; | 1034 delete[] chars; |
1041 return result; | 1035 return result; |
1042 } | 1036 } |
1043 | 1037 |
1044 | 1038 |
1045 void Shell::RunShell() { | 1039 void Shell::RunShell() { |
1046 Locker locker; | 1040 Locker locker; |
1047 Context::Scope context_scope(evaluation_context_); | 1041 Context::Scope context_scope(evaluation_context_); |
1048 HandleScope outer_scope; | 1042 HandleScope outer_scope; |
1049 Handle<String> name = String::New("(d8)"); | 1043 Handle<String> name = String::New("(d8)"); |
1050 #ifndef V8_SHARED | |
1051 console = LineEditor::Get(); | 1044 console = LineEditor::Get(); |
1052 printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name()); | 1045 printf("V8 version %s [console: %s]\n", V8::GetVersion(), console->name()); |
1053 console->Open(); | 1046 console->Open(); |
1054 while (true) { | 1047 while (true) { |
1055 i::SmartArrayPointer<char> input = console->Prompt(Shell::kPrompt); | |
1056 if (input.is_empty()) break; | |
1057 console->AddHistory(*input); | |
1058 HandleScope inner_scope; | 1048 HandleScope inner_scope; |
1059 ExecuteString(String::New(*input), name, true, true); | 1049 Handle<String> input = console->Prompt(Shell::kPrompt); |
| 1050 if (input.IsEmpty()) break; |
| 1051 ExecuteString(input, name, true, true); |
1060 } | 1052 } |
1061 #else | |
1062 printf("V8 version %s [D8 light using shared library]\n", V8::GetVersion()); | |
1063 static const int kBufferSize = 256; | |
1064 while (true) { | |
1065 char buffer[kBufferSize]; | |
1066 printf("%s", Shell::kPrompt); | |
1067 if (fgets(buffer, kBufferSize, stdin) == NULL) break; | |
1068 HandleScope inner_scope; | |
1069 ExecuteString(String::New(buffer), name, true, true); | |
1070 } | |
1071 #endif // V8_SHARED | |
1072 printf("\n"); | 1053 printf("\n"); |
1073 } | 1054 } |
1074 | 1055 |
1075 | 1056 |
1076 #ifndef V8_SHARED | 1057 #ifndef V8_SHARED |
1077 class ShellThread : public i::Thread { | 1058 class ShellThread : public i::Thread { |
1078 public: | 1059 public: |
1079 // Takes ownership of the underlying char array of |files|. | 1060 // Takes ownership of the underlying char array of |files|. |
1080 ShellThread(int no, char* files) | 1061 ShellThread(int no, char* files) |
1081 : Thread("d8:ShellThread"), | 1062 : Thread("d8:ShellThread"), |
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1504 } | 1485 } |
1505 | 1486 |
1506 } // namespace v8 | 1487 } // namespace v8 |
1507 | 1488 |
1508 | 1489 |
1509 #ifndef GOOGLE3 | 1490 #ifndef GOOGLE3 |
1510 int main(int argc, char* argv[]) { | 1491 int main(int argc, char* argv[]) { |
1511 return v8::Shell::Main(argc, argv); | 1492 return v8::Shell::Main(argc, argv); |
1512 } | 1493 } |
1513 #endif | 1494 #endif |
OLD | NEW |