OLD | NEW |
---|---|
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 15 matching lines...) Expand all Loading... | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include <v8.h> | 28 #include <v8.h> |
29 #include <v8-testing.h> | 29 #include <v8-testing.h> |
30 #include <assert.h> | 30 #include <assert.h> |
31 #include <fcntl.h> | 31 #include <fcntl.h> |
32 #include <string.h> | 32 #include <string.h> |
33 #include <stdio.h> | 33 #include <stdio.h> |
34 #include <stdlib.h> | 34 #include <stdlib.h> |
35 | 35 |
36 // When building with V8 in a shared library we cannot use functions which | |
37 // is not explicitly a part of the public V8 API. This extensive use of | |
38 // #ifndef USING_V8_SHARED/#endif is a hack until we can resolve whether to | |
39 // still use the shell sample for testing or change to use the and the | |
Mads Ager (chromium)
2011/03/24 10:59:03
the and the -> the
| |
40 // developer shell d8 TODO(1272). | |
41 #ifndef USING_V8_SHARED | |
36 #include "../src/v8.h" | 42 #include "../src/v8.h" |
43 #endif // USING_V8_SHARED | |
37 | 44 |
38 #if !defined(_WIN32) && !defined(_WIN64) | 45 #if !defined(_WIN32) && !defined(_WIN64) |
39 #include <unistd.h> // NOLINT | 46 #include <unistd.h> // NOLINT |
40 #endif | 47 #endif |
41 | 48 |
42 static void ExitShell(int exit_code) { | 49 static void ExitShell(int exit_code) { |
43 // Use _exit instead of exit to avoid races between isolate | 50 // Use _exit instead of exit to avoid races between isolate |
44 // threads and static destructors. | 51 // threads and static destructors. |
45 fflush(stdout); | 52 fflush(stdout); |
46 fflush(stderr); | 53 fflush(stderr); |
(...skipping 12 matching lines...) Expand all Loading... | |
59 v8::Handle<v8::Value> Quit(const v8::Arguments& args); | 66 v8::Handle<v8::Value> Quit(const v8::Arguments& args); |
60 v8::Handle<v8::Value> Version(const v8::Arguments& args); | 67 v8::Handle<v8::Value> Version(const v8::Arguments& args); |
61 v8::Handle<v8::String> ReadFile(const char* name); | 68 v8::Handle<v8::String> ReadFile(const char* name); |
62 void ReportException(v8::TryCatch* handler); | 69 void ReportException(v8::TryCatch* handler); |
63 | 70 |
64 | 71 |
65 static bool last_run = true; | 72 static bool last_run = true; |
66 | 73 |
67 class SourceGroup { | 74 class SourceGroup { |
68 public: | 75 public: |
69 SourceGroup() : argv_(NULL), | 76 SourceGroup() : |
70 begin_offset_(0), | 77 #ifndef USING_V8_SHARED |
71 end_offset_(0), | |
72 next_semaphore_(v8::internal::OS::CreateSemaphore(0)), | 78 next_semaphore_(v8::internal::OS::CreateSemaphore(0)), |
73 done_semaphore_(v8::internal::OS::CreateSemaphore(0)), | 79 done_semaphore_(v8::internal::OS::CreateSemaphore(0)), |
74 thread_(NULL) { } | 80 thread_(NULL), |
81 #endif // USING_V8_SHARED | |
82 argv_(NULL), | |
83 begin_offset_(0), | |
84 end_offset_(0) { } | |
75 | 85 |
76 void Begin(char** argv, int offset) { | 86 void Begin(char** argv, int offset) { |
77 argv_ = const_cast<const char**>(argv); | 87 argv_ = const_cast<const char**>(argv); |
78 begin_offset_ = offset; | 88 begin_offset_ = offset; |
79 } | 89 } |
80 | 90 |
81 void End(int offset) { end_offset_ = offset; } | 91 void End(int offset) { end_offset_ = offset; } |
82 | 92 |
83 void Execute() { | 93 void Execute() { |
84 for (int i = begin_offset_; i < end_offset_; ++i) { | 94 for (int i = begin_offset_; i < end_offset_; ++i) { |
(...skipping 19 matching lines...) Expand all Loading... | |
104 printf("Error reading '%s'\n", arg); | 114 printf("Error reading '%s'\n", arg); |
105 } | 115 } |
106 if (!ExecuteString(source, file_name, false, true)) { | 116 if (!ExecuteString(source, file_name, false, true)) { |
107 ExitShell(1); | 117 ExitShell(1); |
108 return; | 118 return; |
109 } | 119 } |
110 } | 120 } |
111 } | 121 } |
112 } | 122 } |
113 | 123 |
124 #ifndef USING_V8_SHARED | |
114 void StartExecuteInThread() { | 125 void StartExecuteInThread() { |
115 if (thread_ == NULL) { | 126 if (thread_ == NULL) { |
116 thread_ = new IsolateThread(this); | 127 thread_ = new IsolateThread(this); |
117 thread_->Start(); | 128 thread_->Start(); |
118 } | 129 } |
119 next_semaphore_->Signal(); | 130 next_semaphore_->Signal(); |
120 } | 131 } |
121 | 132 |
122 void WaitForThread() { | 133 void WaitForThread() { |
123 if (thread_ == NULL) return; | 134 if (thread_ == NULL) return; |
124 if (last_run) { | 135 if (last_run) { |
125 thread_->Join(); | 136 thread_->Join(); |
126 thread_ = NULL; | 137 thread_ = NULL; |
127 } else { | 138 } else { |
128 done_semaphore_->Wait(); | 139 done_semaphore_->Wait(); |
129 } | 140 } |
130 } | 141 } |
142 #endif // USING_V8_SHARED | |
131 | 143 |
132 private: | 144 private: |
145 #ifndef USING_V8_SHARED | |
133 static v8::internal::Thread::Options GetThreadOptions() { | 146 static v8::internal::Thread::Options GetThreadOptions() { |
134 v8::internal::Thread::Options options; | 147 v8::internal::Thread::Options options; |
135 options.name = "IsolateThread"; | 148 options.name = "IsolateThread"; |
136 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less | 149 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less |
137 // which is not enough to parse the big literal expressions used in tests. | 150 // which is not enough to parse the big literal expressions used in tests. |
138 // The stack size should be at least StackGuard::kLimitSize + some | 151 // The stack size should be at least StackGuard::kLimitSize + some |
139 // OS-specific padding for thread startup code. | 152 // OS-specific padding for thread startup code. |
140 options.stack_size = 2 << 20; // 2 Mb seems to be enough | 153 options.stack_size = 2 << 20; // 2 Mb seems to be enough |
141 return options; | 154 return options; |
142 } | 155 } |
(...skipping 23 matching lines...) Expand all Loading... | |
166 v8::Context::Scope cscope(context); | 179 v8::Context::Scope cscope(context); |
167 Execute(); | 180 Execute(); |
168 } | 181 } |
169 context.Dispose(); | 182 context.Dispose(); |
170 } | 183 } |
171 if (done_semaphore_ != NULL) done_semaphore_->Signal(); | 184 if (done_semaphore_ != NULL) done_semaphore_->Signal(); |
172 } while (!last_run); | 185 } while (!last_run); |
173 isolate->Dispose(); | 186 isolate->Dispose(); |
174 } | 187 } |
175 | 188 |
189 v8::internal::Semaphore* next_semaphore_; | |
190 v8::internal::Semaphore* done_semaphore_; | |
191 v8::internal::Thread* thread_; | |
192 #endif // USING_V8_SHARED | |
193 | |
176 const char** argv_; | 194 const char** argv_; |
177 int begin_offset_; | 195 int begin_offset_; |
178 int end_offset_; | 196 int end_offset_; |
179 v8::internal::Semaphore* next_semaphore_; | |
180 v8::internal::Semaphore* done_semaphore_; | |
181 v8::internal::Thread* thread_; | |
182 }; | 197 }; |
183 | 198 |
184 | 199 |
185 static SourceGroup* isolate_sources = NULL; | 200 static SourceGroup* isolate_sources = NULL; |
186 | 201 |
187 | 202 |
188 int RunMain(int argc, char* argv[]) { | 203 int RunMain(int argc, char* argv[]) { |
189 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 204 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
190 v8::HandleScope handle_scope; | 205 v8::HandleScope handle_scope; |
191 v8::Persistent<v8::Context> context = CreateShellContext(); | 206 v8::Persistent<v8::Context> context = CreateShellContext(); |
192 // Enter the newly created execution environment. | 207 // Enter the newly created execution environment. |
193 context->Enter(); | 208 context->Enter(); |
194 if (context.IsEmpty()) { | 209 if (context.IsEmpty()) { |
195 printf("Error creating context\n"); | 210 printf("Error creating context\n"); |
196 return 1; | 211 return 1; |
197 } | 212 } |
198 | 213 |
199 bool run_shell = (argc == 1); | 214 bool run_shell = (argc == 1); |
200 int num_isolates = 1; | 215 int num_isolates = 1; |
201 for (int i = 1; i < argc; i++) { | 216 for (int i = 1; i < argc; i++) { |
202 if (strcmp(argv[i], "--isolate") == 0) ++num_isolates; | 217 if (strcmp(argv[i], "--isolate") == 0) { |
218 #ifndef USING_V8_SHARED | |
219 ++num_isolates; | |
220 #else // USING_V8_SHARED | |
221 printf("Error: --isolate not supported when linked with shared " | |
222 "library\n"); | |
223 ExitShell(1); | |
224 #endif // USING_V8_SHARED | |
225 } | |
203 } | 226 } |
204 if (isolate_sources == NULL) { | 227 if (isolate_sources == NULL) { |
205 isolate_sources = new SourceGroup[num_isolates]; | 228 isolate_sources = new SourceGroup[num_isolates]; |
206 SourceGroup* current = isolate_sources; | 229 SourceGroup* current = isolate_sources; |
207 current->Begin(argv, 1); | 230 current->Begin(argv, 1); |
208 for (int i = 1; i < argc; i++) { | 231 for (int i = 1; i < argc; i++) { |
209 const char* str = argv[i]; | 232 const char* str = argv[i]; |
210 if (strcmp(str, "--isolate") == 0) { | 233 if (strcmp(str, "--isolate") == 0) { |
211 current->End(i); | 234 current->End(i); |
212 current++; | 235 current++; |
213 current->Begin(argv, i + 1); | 236 current->Begin(argv, i + 1); |
214 } else if (strcmp(str, "--shell") == 0) { | 237 } else if (strcmp(str, "--shell") == 0) { |
215 run_shell = true; | 238 run_shell = true; |
216 } else if (strcmp(str, "-f") == 0) { | 239 } else if (strcmp(str, "-f") == 0) { |
217 // Ignore any -f flags for compatibility with the other stand- | 240 // Ignore any -f flags for compatibility with the other stand- |
218 // alone JavaScript engines. | 241 // alone JavaScript engines. |
219 continue; | 242 continue; |
220 } else if (strncmp(str, "--", 2) == 0) { | 243 } else if (strncmp(str, "--", 2) == 0) { |
221 printf("Warning: unknown flag %s.\nTry --help for options\n", str); | 244 printf("Warning: unknown flag %s.\nTry --help for options\n", str); |
222 } | 245 } |
223 } | 246 } |
224 current->End(argc); | 247 current->End(argc); |
225 } | 248 } |
249 #ifndef USING_V8_SHARED | |
226 for (int i = 1; i < num_isolates; ++i) { | 250 for (int i = 1; i < num_isolates; ++i) { |
227 isolate_sources[i].StartExecuteInThread(); | 251 isolate_sources[i].StartExecuteInThread(); |
228 } | 252 } |
253 #endif // USING_V8_SHARED | |
229 isolate_sources[0].Execute(); | 254 isolate_sources[0].Execute(); |
230 if (run_shell) RunShell(context); | 255 if (run_shell) RunShell(context); |
256 #ifndef USING_V8_SHARED | |
231 for (int i = 1; i < num_isolates; ++i) { | 257 for (int i = 1; i < num_isolates; ++i) { |
232 isolate_sources[i].WaitForThread(); | 258 isolate_sources[i].WaitForThread(); |
233 } | 259 } |
260 #endif // USING_V8_SHARED | |
234 if (last_run) { | 261 if (last_run) { |
235 delete[] isolate_sources; | 262 delete[] isolate_sources; |
236 isolate_sources = NULL; | 263 isolate_sources = NULL; |
237 } | 264 } |
238 context->Exit(); | 265 context->Exit(); |
239 context.Dispose(); | 266 context.Dispose(); |
240 return 0; | 267 return 0; |
241 } | 268 } |
242 | 269 |
243 | 270 |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
498 printf("^"); | 525 printf("^"); |
499 } | 526 } |
500 printf("\n"); | 527 printf("\n"); |
501 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); | 528 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
502 if (stack_trace.length() > 0) { | 529 if (stack_trace.length() > 0) { |
503 const char* stack_trace_string = ToCString(stack_trace); | 530 const char* stack_trace_string = ToCString(stack_trace); |
504 printf("%s\n", stack_trace_string); | 531 printf("%s\n", stack_trace_string); |
505 } | 532 } |
506 } | 533 } |
507 } | 534 } |
OLD | NEW |