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

Side by Side Diff: samples/shell.cc

Issue 6624071: [Isolates] Use _exit() instead of exit() in shell to avoid shutdown (Closed)
Patch Set: Created 9 years, 9 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 23 matching lines...) Expand all
34 #include <stdlib.h> 34 #include <stdlib.h>
35 35
36 #include "../src/v8.h" 36 #include "../src/v8.h"
37 37
38 // TODO(isolates): 38 // TODO(isolates):
39 // o Either use V8 internal platform stuff for every platform or 39 // o Either use V8 internal platform stuff for every platform or
40 // re-implement it. 40 // re-implement it.
41 // o Do not assume not WIN32 implies pthreads. 41 // o Do not assume not WIN32 implies pthreads.
42 #ifndef WIN32 42 #ifndef WIN32
43 #include <pthread.h> // NOLINT 43 #include <pthread.h> // NOLINT
44 #include <unistd.h> // NOLINT
44 #endif 45 #endif
45 46
47 static void ExitShell(int exit_code) {
48 // Use _exit instead of exit to avoid races between isolate
49 // threads and static destructors.
50 fflush(stdout);
51 fflush(stderr);
52 _exit(exit_code);
53 }
54
46 v8::Persistent<v8::Context> CreateShellContext(); 55 v8::Persistent<v8::Context> CreateShellContext();
47 void RunShell(v8::Handle<v8::Context> context); 56 void RunShell(v8::Handle<v8::Context> context);
48 bool ExecuteString(v8::Handle<v8::String> source, 57 bool ExecuteString(v8::Handle<v8::String> source,
49 v8::Handle<v8::Value> name, 58 v8::Handle<v8::Value> name,
50 bool print_result, 59 bool print_result,
51 bool report_exceptions); 60 bool report_exceptions);
52 v8::Handle<v8::Value> Print(const v8::Arguments& args); 61 v8::Handle<v8::Value> Print(const v8::Arguments& args);
53 v8::Handle<v8::Value> Read(const v8::Arguments& args); 62 v8::Handle<v8::Value> Read(const v8::Arguments& args);
54 v8::Handle<v8::Value> Load(const v8::Arguments& args); 63 v8::Handle<v8::Value> Load(const v8::Arguments& args);
55 v8::Handle<v8::Value> Quit(const v8::Arguments& args); 64 v8::Handle<v8::Value> Quit(const v8::Arguments& args);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 96
88 void Execute() { 97 void Execute() {
89 for (int i = begin_offset_; i < end_offset_; ++i) { 98 for (int i = begin_offset_; i < end_offset_; ++i) {
90 const char* arg = argv_[i]; 99 const char* arg = argv_[i];
91 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { 100 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) {
92 // Execute argument given to -e option directly. 101 // Execute argument given to -e option directly.
93 v8::HandleScope handle_scope; 102 v8::HandleScope handle_scope;
94 v8::Handle<v8::String> file_name = v8::String::New("unnamed"); 103 v8::Handle<v8::String> file_name = v8::String::New("unnamed");
95 v8::Handle<v8::String> source = v8::String::New(argv_[i + 1]); 104 v8::Handle<v8::String> source = v8::String::New(argv_[i + 1]);
96 if (!ExecuteString(source, file_name, false, true)) { 105 if (!ExecuteString(source, file_name, false, true)) {
97 exit(1); 106 ExitShell(1);
98 return; 107 return;
99 } 108 }
100 ++i; 109 ++i;
101 } else if (arg[0] == '-') { 110 } else if (arg[0] == '-') {
102 // Ignore other options. They have been parsed already. 111 // Ignore other options. They have been parsed already.
103 } else { 112 } else {
104 // Use all other arguments as names of files to load and run. 113 // Use all other arguments as names of files to load and run.
105 v8::HandleScope handle_scope; 114 v8::HandleScope handle_scope;
106 v8::Handle<v8::String> file_name = v8::String::New(arg); 115 v8::Handle<v8::String> file_name = v8::String::New(arg);
107 v8::Handle<v8::String> source = ReadFile(arg); 116 v8::Handle<v8::String> source = ReadFile(arg);
108 if (source.IsEmpty()) { 117 if (source.IsEmpty()) {
109 printf("Error reading '%s'\n", arg); 118 printf("Error reading '%s'\n", arg);
110 } 119 }
111 if (!ExecuteString(source, file_name, false, true)) { 120 if (!ExecuteString(source, file_name, false, true)) {
112 exit(1); 121 ExitShell(1);
113 return; 122 return;
114 } 123 }
115 } 124 }
116 } 125 }
117 } 126 }
118 127
119 #ifdef WIN32 128 #ifdef WIN32
120 void StartExecuteInThread() { ExecuteInThread(); } 129 void StartExecuteInThread() { ExecuteInThread(); }
121 void WaitForThread() {} 130 void WaitForThread() {}
122 131
123 #else 132 #else
124 void StartExecuteInThread() { 133 void StartExecuteInThread() {
125 if (thread_ == 0) { 134 if (thread_ == 0) {
126 pthread_attr_t attr; 135 pthread_attr_t attr;
127 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less 136 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less
128 // which is not enough to parse the big literal expressions used in tests. 137 // which is not enough to parse the big literal expressions used in tests.
129 // The stack size should be at least StackGuard::kLimitSize + some 138 // The stack size should be at least StackGuard::kLimitSize + some
130 // OS-specific padding for thread startup code. 139 // OS-specific padding for thread startup code.
131 size_t stacksize = 2 << 20; // 2 Mb seems to be enough 140 size_t stacksize = 2 << 20; // 2 Mb seems to be enough
132 pthread_attr_init(&attr); 141 pthread_attr_init(&attr);
133 pthread_attr_setstacksize(&attr, stacksize); 142 pthread_attr_setstacksize(&attr, stacksize);
134 int error = pthread_create(&thread_, &attr, &IsolateThreadEntry, this); 143 int error = pthread_create(&thread_, &attr, &IsolateThreadEntry, this);
135 if (error != 0) { 144 if (error != 0) {
136 printf("Error creating isolate thread.\n"); 145 fprintf(stderr, "Error creating isolate thread.\n");
137 exit(1); 146 ExitShell(1);
138 } 147 }
139 } 148 }
140 next_semaphore_->Signal(); 149 next_semaphore_->Signal();
141 } 150 }
142 151
143 void WaitForThread() { 152 void WaitForThread() {
144 if (thread_ == 0) return; 153 if (thread_ == 0) return;
145 if (last_run) { 154 if (last_run) {
146 pthread_join(thread_, NULL); 155 pthread_join(thread_, NULL);
147 thread_ = 0; 156 thread_ = 0;
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 return v8::Undefined(); 384 return v8::Undefined();
376 } 385 }
377 386
378 387
379 // The callback that is invoked by v8 whenever the JavaScript 'quit' 388 // The callback that is invoked by v8 whenever the JavaScript 'quit'
380 // function is called. Quits. 389 // function is called. Quits.
381 v8::Handle<v8::Value> Quit(const v8::Arguments& args) { 390 v8::Handle<v8::Value> Quit(const v8::Arguments& args) {
382 // If not arguments are given args[0] will yield undefined which 391 // If not arguments are given args[0] will yield undefined which
383 // converts to the integer value 0. 392 // converts to the integer value 0.
384 int exit_code = args[0]->Int32Value(); 393 int exit_code = args[0]->Int32Value();
385 exit(exit_code); 394 ExitShell(exit_code);
386 return v8::Undefined(); 395 return v8::Undefined();
387 } 396 }
388 397
389 398
390 v8::Handle<v8::Value> Version(const v8::Arguments& args) { 399 v8::Handle<v8::Value> Version(const v8::Arguments& args) {
391 return v8::String::New(v8::V8::GetVersion()); 400 return v8::String::New(v8::V8::GetVersion());
392 } 401 }
393 402
394 403
395 // Reads a file into a v8 string. 404 // Reads a file into a v8 string.
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
500 printf("^"); 509 printf("^");
501 } 510 }
502 printf("\n"); 511 printf("\n");
503 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); 512 v8::String::Utf8Value stack_trace(try_catch->StackTrace());
504 if (stack_trace.length() > 0) { 513 if (stack_trace.length() > 0) {
505 const char* stack_trace_string = ToCString(stack_trace); 514 const char* stack_trace_string = ToCString(stack_trace);
506 printf("%s\n", stack_trace_string); 515 printf("%s\n", stack_trace_string);
507 } 516 }
508 } 517 }
509 } 518 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698