Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: samples/shell.cc

Issue 7348008: Merge up to 8597 to experimental/gc from the bleeding edge. (Closed) Base URL: http://v8.googlecode.com/svn/branches/experimental/gc/
Patch Set: '' Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « samples/process.cc ('k') | src/SConscript » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 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 23 matching lines...) Expand all
34 #include <fcntl.h> 34 #include <fcntl.h>
35 #include <string.h> 35 #include <string.h>
36 #include <stdio.h> 36 #include <stdio.h>
37 #include <stdlib.h> 37 #include <stdlib.h>
38 38
39 // When building with V8 in a shared library we cannot use functions which 39 // When building with V8 in a shared library we cannot use functions which
40 // is not explicitly a part of the public V8 API. This extensive use of 40 // is not explicitly a part of the public V8 API. This extensive use of
41 // #ifndef USING_V8_SHARED/#endif is a hack until we can resolve whether to 41 // #ifndef USING_V8_SHARED/#endif is a hack until we can resolve whether to
42 // still use the shell sample for testing or change to use the developer 42 // still use the shell sample for testing or change to use the developer
43 // shell d8 TODO(1272). 43 // shell d8 TODO(1272).
44 #ifndef USING_V8_SHARED 44 #if !(defined(USING_V8_SHARED) || defined(V8_SHARED))
45 #include "../src/v8.h" 45 #include "../src/v8.h"
46 #endif // USING_V8_SHARED 46 #endif // USING_V8_SHARED
47 47
48 #if !defined(_WIN32) && !defined(_WIN64) 48 #if !defined(_WIN32) && !defined(_WIN64)
49 #include <unistd.h> // NOLINT 49 #include <unistd.h> // NOLINT
50 #endif 50 #endif
51 51
52 static void ExitShell(int exit_code) { 52 static void ExitShell(int exit_code) {
53 // Use _exit instead of exit to avoid races between isolate 53 // Use _exit instead of exit to avoid races between isolate
54 // threads and static destructors. 54 // threads and static destructors.
(...skipping 24 matching lines...) Expand all
79 v8::Handle<v8::Value> PixelArray(const v8::Arguments& args); 79 v8::Handle<v8::Value> PixelArray(const v8::Arguments& args);
80 v8::Handle<v8::String> ReadFile(const char* name); 80 v8::Handle<v8::String> ReadFile(const char* name);
81 void ReportException(v8::TryCatch* handler); 81 void ReportException(v8::TryCatch* handler);
82 82
83 83
84 static bool last_run = true; 84 static bool last_run = true;
85 85
86 class SourceGroup { 86 class SourceGroup {
87 public: 87 public:
88 SourceGroup() : 88 SourceGroup() :
89 #ifndef USING_V8_SHARED 89 #if !(defined(USING_V8_SHARED) || defined(V8_SHARED))
90 next_semaphore_(v8::internal::OS::CreateSemaphore(0)), 90 next_semaphore_(v8::internal::OS::CreateSemaphore(0)),
91 done_semaphore_(v8::internal::OS::CreateSemaphore(0)), 91 done_semaphore_(v8::internal::OS::CreateSemaphore(0)),
92 thread_(NULL), 92 thread_(NULL),
93 #endif // USING_V8_SHARED 93 #endif // USING_V8_SHARED
94 argv_(NULL), 94 argv_(NULL),
95 begin_offset_(0), 95 begin_offset_(0),
96 end_offset_(0) { } 96 end_offset_(0) { }
97 97
98 void Begin(char** argv, int offset) { 98 void Begin(char** argv, int offset) {
99 argv_ = const_cast<const char**>(argv); 99 argv_ = const_cast<const char**>(argv);
(...skipping 27 matching lines...) Expand all
127 continue; 127 continue;
128 } 128 }
129 if (!ExecuteString(source, file_name, false, true)) { 129 if (!ExecuteString(source, file_name, false, true)) {
130 ExitShell(1); 130 ExitShell(1);
131 return; 131 return;
132 } 132 }
133 } 133 }
134 } 134 }
135 } 135 }
136 136
137 #ifndef USING_V8_SHARED 137 #if !(defined(USING_V8_SHARED) || defined(V8_SHARED))
138 void StartExecuteInThread() { 138 void StartExecuteInThread() {
139 if (thread_ == NULL) { 139 if (thread_ == NULL) {
140 thread_ = new IsolateThread(this); 140 thread_ = new IsolateThread(this);
141 thread_->Start(); 141 thread_->Start();
142 } 142 }
143 next_semaphore_->Signal(); 143 next_semaphore_->Signal();
144 } 144 }
145 145
146 void WaitForThread() { 146 void WaitForThread() {
147 if (thread_ == NULL) return; 147 if (thread_ == NULL) return;
148 if (last_run) { 148 if (last_run) {
149 thread_->Join(); 149 thread_->Join();
150 thread_ = NULL; 150 thread_ = NULL;
151 } else { 151 } else {
152 done_semaphore_->Wait(); 152 done_semaphore_->Wait();
153 } 153 }
154 } 154 }
155 #endif // USING_V8_SHARED 155 #endif // USING_V8_SHARED
156 156
157 private: 157 private:
158 #ifndef USING_V8_SHARED 158 #if !(defined(USING_V8_SHARED) || defined(V8_SHARED))
159 static v8::internal::Thread::Options GetThreadOptions() { 159 static v8::internal::Thread::Options GetThreadOptions() {
160 v8::internal::Thread::Options options; 160 v8::internal::Thread::Options options;
161 options.name = "IsolateThread"; 161 options.name = "IsolateThread";
162 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less 162 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less
163 // which is not enough to parse the big literal expressions used in tests. 163 // which is not enough to parse the big literal expressions used in tests.
164 // The stack size should be at least StackGuard::kLimitSize + some 164 // The stack size should be at least StackGuard::kLimitSize + some
165 // OS-specific padding for thread startup code. 165 // OS-specific padding for thread startup code.
166 options.stack_size = 2 << 20; // 2 Mb seems to be enough 166 options.stack_size = 2 << 20; // 2 Mb seems to be enough
167 return options; 167 return options;
168 } 168 }
169 169
170 class IsolateThread : public v8::internal::Thread { 170 class IsolateThread : public v8::internal::Thread {
171 public: 171 public:
172 explicit IsolateThread(SourceGroup* group) 172 explicit IsolateThread(SourceGroup* group)
173 : v8::internal::Thread(NULL, GetThreadOptions()), group_(group) {} 173 : v8::internal::Thread(GetThreadOptions()), group_(group) {}
174 174
175 virtual void Run() { 175 virtual void Run() {
176 group_->ExecuteInThread(); 176 group_->ExecuteInThread();
177 } 177 }
178 178
179 private: 179 private:
180 SourceGroup* group_; 180 SourceGroup* group_;
181 }; 181 };
182 182
183 void ExecuteInThread() { 183 void ExecuteInThread() {
(...skipping 22 matching lines...) Expand all
206 206
207 const char** argv_; 207 const char** argv_;
208 int begin_offset_; 208 int begin_offset_;
209 int end_offset_; 209 int end_offset_;
210 }; 210 };
211 211
212 212
213 static SourceGroup* isolate_sources = NULL; 213 static SourceGroup* isolate_sources = NULL;
214 214
215 215
216 #ifdef COMPRESS_STARTUP_DATA_BZ2
217 class BZip2Decompressor : public v8::StartupDataDecompressor {
218 public:
219 virtual ~BZip2Decompressor() { }
220
221 protected:
222 virtual int DecompressData(char* raw_data,
223 int* raw_data_size,
224 const char* compressed_data,
225 int compressed_data_size) {
226 ASSERT_EQ(v8::StartupData::kBZip2,
227 v8::V8::GetCompressedStartupDataAlgorithm());
228 unsigned int decompressed_size = *raw_data_size;
229 int result =
230 BZ2_bzBuffToBuffDecompress(raw_data,
231 &decompressed_size,
232 const_cast<char*>(compressed_data),
233 compressed_data_size,
234 0, 1);
235 if (result == BZ_OK) {
236 *raw_data_size = decompressed_size;
237 }
238 return result;
239 }
240 };
241 #endif
242
243
216 int RunMain(int argc, char* argv[]) { 244 int RunMain(int argc, char* argv[]) {
217 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); 245 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
218 v8::HandleScope handle_scope; 246 v8::HandleScope handle_scope;
219 v8::Persistent<v8::Context> context = CreateShellContext(); 247 v8::Persistent<v8::Context> context = CreateShellContext();
220 // Enter the newly created execution environment. 248 // Enter the newly created execution environment.
221 context->Enter(); 249 context->Enter();
222 if (context.IsEmpty()) { 250 if (context.IsEmpty()) {
223 printf("Error creating context\n"); 251 printf("Error creating context\n");
224 return 1; 252 return 1;
225 } 253 }
226 254
227 bool run_shell = (argc == 1); 255 bool run_shell = (argc == 1);
228 int num_isolates = 1; 256 int num_isolates = 1;
229 for (int i = 1; i < argc; i++) { 257 for (int i = 1; i < argc; i++) {
230 if (strcmp(argv[i], "--isolate") == 0) { 258 if (strcmp(argv[i], "--isolate") == 0) {
231 #ifndef USING_V8_SHARED 259 #if !(defined(USING_V8_SHARED) || defined(V8_SHARED))
232 ++num_isolates; 260 ++num_isolates;
233 #else // USING_V8_SHARED 261 #else // USING_V8_SHARED
234 printf("Error: --isolate not supported when linked with shared " 262 printf("Error: --isolate not supported when linked with shared "
235 "library\n"); 263 "library\n");
236 ExitShell(1); 264 ExitShell(1);
237 #endif // USING_V8_SHARED 265 #endif // USING_V8_SHARED
238 } 266 }
239 } 267 }
240 if (isolate_sources == NULL) { 268 if (isolate_sources == NULL) {
241 isolate_sources = new SourceGroup[num_isolates]; 269 isolate_sources = new SourceGroup[num_isolates];
(...skipping 10 matching lines...) Expand all
252 } else if (strcmp(str, "-f") == 0) { 280 } else if (strcmp(str, "-f") == 0) {
253 // Ignore any -f flags for compatibility with the other stand- 281 // Ignore any -f flags for compatibility with the other stand-
254 // alone JavaScript engines. 282 // alone JavaScript engines.
255 continue; 283 continue;
256 } else if (strncmp(str, "--", 2) == 0) { 284 } else if (strncmp(str, "--", 2) == 0) {
257 printf("Warning: unknown flag %s.\nTry --help for options\n", str); 285 printf("Warning: unknown flag %s.\nTry --help for options\n", str);
258 } 286 }
259 } 287 }
260 current->End(argc); 288 current->End(argc);
261 } 289 }
262 #ifndef USING_V8_SHARED 290 #if !(defined(USING_V8_SHARED) || defined(V8_SHARED))
263 for (int i = 1; i < num_isolates; ++i) { 291 for (int i = 1; i < num_isolates; ++i) {
264 isolate_sources[i].StartExecuteInThread(); 292 isolate_sources[i].StartExecuteInThread();
265 } 293 }
266 #endif // USING_V8_SHARED 294 #endif // USING_V8_SHARED
267 isolate_sources[0].Execute(); 295 isolate_sources[0].Execute();
268 if (run_shell) RunShell(context); 296 if (run_shell) RunShell(context);
269 #ifndef USING_V8_SHARED 297 #if !(defined(USING_V8_SHARED) || defined(V8_SHARED))
270 for (int i = 1; i < num_isolates; ++i) { 298 for (int i = 1; i < num_isolates; ++i) {
271 isolate_sources[i].WaitForThread(); 299 isolate_sources[i].WaitForThread();
272 } 300 }
273 #endif // USING_V8_SHARED 301 #endif // USING_V8_SHARED
274 if (last_run) { 302 if (last_run) {
275 delete[] isolate_sources; 303 delete[] isolate_sources;
276 isolate_sources = NULL; 304 isolate_sources = NULL;
277 } 305 }
278 context->Exit(); 306 context->Exit();
279 context.Dispose(); 307 context.Dispose();
(...skipping 16 matching lines...) Expand all
296 argv[i] = NULL; 324 argv[i] = NULL;
297 } else if (strcmp(argv[i], "--noalways-opt") == 0) { 325 } else if (strcmp(argv[i], "--noalways-opt") == 0) {
298 // No support for stressing if we can't use --always-opt. 326 // No support for stressing if we can't use --always-opt.
299 FLAG_stress_opt = false; 327 FLAG_stress_opt = false;
300 FLAG_stress_deopt = false; 328 FLAG_stress_deopt = false;
301 break; 329 break;
302 } 330 }
303 } 331 }
304 332
305 #ifdef COMPRESS_STARTUP_DATA_BZ2 333 #ifdef COMPRESS_STARTUP_DATA_BZ2
306 ASSERT_EQ(v8::StartupData::kBZip2, 334 BZip2Decompressor startup_data_decompressor;
307 v8::V8::GetCompressedStartupDataAlgorithm()); 335 int bz2_result = startup_data_decompressor.Decompress();
308 int compressed_data_count = v8::V8::GetCompressedStartupDataCount(); 336 if (bz2_result != BZ_OK) {
309 v8::StartupData* compressed_data = new v8::StartupData[compressed_data_count]; 337 fprintf(stderr, "bzip error code: %d\n", bz2_result);
310 v8::V8::GetCompressedStartupData(compressed_data); 338 exit(1);
311 for (int i = 0; i < compressed_data_count; ++i) {
312 char* decompressed = new char[compressed_data[i].raw_size];
313 unsigned int decompressed_size = compressed_data[i].raw_size;
314 int result =
315 BZ2_bzBuffToBuffDecompress(decompressed,
316 &decompressed_size,
317 const_cast<char*>(compressed_data[i].data),
318 compressed_data[i].compressed_size,
319 0, 1);
320 if (result != BZ_OK) {
321 fprintf(stderr, "bzip error code: %d\n", result);
322 exit(1);
323 }
324 compressed_data[i].data = decompressed;
325 compressed_data[i].raw_size = decompressed_size;
326 } 339 }
327 v8::V8::SetDecompressedStartupData(compressed_data); 340 #endif
328 #endif // COMPRESS_STARTUP_DATA_BZ2
329 341
330 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); 342 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
331 int result = 0; 343 int result = 0;
332 if (FLAG_stress_opt || FLAG_stress_deopt) { 344 if (FLAG_stress_opt || FLAG_stress_deopt) {
333 v8::Testing::SetStressRunType(FLAG_stress_opt 345 v8::Testing::SetStressRunType(FLAG_stress_opt
334 ? v8::Testing::kStressTypeOpt 346 ? v8::Testing::kStressTypeOpt
335 : v8::Testing::kStressTypeDeopt); 347 : v8::Testing::kStressTypeDeopt);
336 int stress_runs = v8::Testing::GetStressRuns(); 348 int stress_runs = v8::Testing::GetStressRuns();
337 for (int i = 0; i < stress_runs && result == 0; i++) { 349 for (int i = 0; i < stress_runs && result == 0; i++) {
338 printf("============ Stress %d/%d ============\n", 350 printf("============ Stress %d/%d ============\n",
339 i + 1, stress_runs); 351 i + 1, stress_runs);
340 v8::Testing::PrepareStressRun(i); 352 v8::Testing::PrepareStressRun(i);
341 last_run = (i == stress_runs - 1); 353 last_run = (i == stress_runs - 1);
342 result = RunMain(argc, argv); 354 result = RunMain(argc, argv);
343 } 355 }
344 printf("======== Full Deoptimization =======\n"); 356 printf("======== Full Deoptimization =======\n");
345 v8::Testing::DeoptimizeAll(); 357 v8::Testing::DeoptimizeAll();
346 } else { 358 } else {
347 result = RunMain(argc, argv); 359 result = RunMain(argc, argv);
348 } 360 }
349 v8::V8::Dispose(); 361 v8::V8::Dispose();
350 362
351 #ifdef COMPRESS_STARTUP_DATA_BZ2
352 for (int i = 0; i < compressed_data_count; ++i) {
353 delete[] compressed_data[i].data;
354 }
355 delete[] compressed_data;
356 #endif // COMPRESS_STARTUP_DATA_BZ2
357
358 return result; 363 return result;
359 } 364 }
360 365
361 366
362 // Extracts a C string from a V8 Utf8Value. 367 // Extracts a C string from a V8 Utf8Value.
363 const char* ToCString(const v8::String::Utf8Value& value) { 368 const char* ToCString(const v8::String::Utf8Value& value) {
364 return *value ? *value : "<string conversion failed>"; 369 return *value ? *value : "<string conversion failed>";
365 } 370 }
366 371
367 372
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 490
486 491
487 void ExternalArrayWeakCallback(v8::Persistent<v8::Value> object, void* data) { 492 void ExternalArrayWeakCallback(v8::Persistent<v8::Value> object, void* data) {
488 free(data); 493 free(data);
489 object.Dispose(); 494 object.Dispose();
490 } 495 }
491 496
492 497
493 v8::Handle<v8::Value> CreateExternalArray(const v8::Arguments& args, 498 v8::Handle<v8::Value> CreateExternalArray(const v8::Arguments& args,
494 v8::ExternalArrayType type, 499 v8::ExternalArrayType type,
495 int element_size) { 500 size_t element_size) {
501 assert(element_size == 1 ||
502 element_size == 2 ||
503 element_size == 4 ||
504 element_size == 8);
496 if (args.Length() != 1) { 505 if (args.Length() != 1) {
497 return v8::ThrowException( 506 return v8::ThrowException(
498 v8::String::New("Array constructor needs one parameter.")); 507 v8::String::New("Array constructor needs one parameter."));
499 } 508 }
500 int length = args[0]->Int32Value(); 509 static const int kMaxLength = 0x3fffffff;
501 void* data = malloc(length * element_size); 510 size_t length = 0;
502 memset(data, 0, length * element_size); 511 if (args[0]->IsUint32()) {
512 length = args[0]->Uint32Value();
513 } else if (args[0]->IsNumber()) {
514 double raw_length = args[0]->NumberValue();
515 if (raw_length < 0) {
516 return v8::ThrowException(
517 v8::String::New("Array length must not be negative."));
518 }
519 if (raw_length > kMaxLength) {
520 return v8::ThrowException(
521 v8::String::New("Array length exceeds maximum length."));
522 }
523 length = static_cast<size_t>(raw_length);
524 } else {
525 return v8::ThrowException(
526 v8::String::New("Array length must be a number."));
527 }
528 if (length > static_cast<size_t>(kMaxLength)) {
529 return v8::ThrowException(
530 v8::String::New("Array length exceeds maximum length."));
531 }
532 void* data = calloc(length, element_size);
533 if (data == NULL) {
534 return v8::ThrowException(v8::String::New("Memory allocation failed."));
535 }
503 v8::Handle<v8::Object> array = v8::Object::New(); 536 v8::Handle<v8::Object> array = v8::Object::New();
504 v8::Persistent<v8::Object> persistent_array = 537 v8::Persistent<v8::Object> persistent_array =
505 v8::Persistent<v8::Object>::New(array); 538 v8::Persistent<v8::Object>::New(array);
506 persistent_array.MakeWeak(data, ExternalArrayWeakCallback); 539 persistent_array.MakeWeak(data, ExternalArrayWeakCallback);
507 persistent_array.MarkIndependent(); 540 persistent_array.MarkIndependent();
508 array->SetIndexedPropertiesToExternalArrayData(data, type, length); 541 array->SetIndexedPropertiesToExternalArrayData(data, type, length);
509 array->Set(v8::String::New("length"), v8::Int32::New(length), 542 array->Set(v8::String::New("length"), v8::Int32::New(length),
510 v8::ReadOnly); 543 v8::ReadOnly);
511 array->Set(v8::String::New("BYTES_PER_ELEMENT"), 544 array->Set(v8::String::New("BYTES_PER_ELEMENT"),
512 v8::Int32::New(element_size)); 545 v8::Int32::New(element_size));
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 printf("^"); 704 printf("^");
672 } 705 }
673 printf("\n"); 706 printf("\n");
674 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); 707 v8::String::Utf8Value stack_trace(try_catch->StackTrace());
675 if (stack_trace.length() > 0) { 708 if (stack_trace.length() > 0) {
676 const char* stack_trace_string = ToCString(stack_trace); 709 const char* stack_trace_string = ToCString(stack_trace);
677 printf("%s\n", stack_trace_string); 710 printf("%s\n", stack_trace_string);
678 } 711 }
679 } 712 }
680 } 713 }
OLDNEW
« no previous file with comments | « samples/process.cc ('k') | src/SConscript » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698