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

Side by Side Diff: samples/shell.cc

Issue 435003: Patch for allowing several V8 instances in process:... (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 11 years 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 | « include/v8.h ('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 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
11 // with the distribution. 11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its 12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived 13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission. 14 // from this software without specific prior written permission.
15 // 15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #define V8_TEST
27 28
29 #ifdef V8_TEST
30 #define WIN32
31 #include <../src/v8.h>
32 #include <../src/v8-global-context.h>
33 #else
28 #include <v8.h> 34 #include <v8.h>
29 #include <fcntl.h> 35 #include <fcntl.h>
30 #include <string.h> 36 #include <string.h>
31 #include <stdio.h> 37 #include <stdio.h>
32 #include <stdlib.h> 38 #include <stdlib.h>
33 39 #endif
34 40
35 void RunShell(v8::Handle<v8::Context> context); 41 void RunShell(v8::Handle<v8::Context> context);
36 bool ExecuteString(v8::Handle<v8::String> source, 42 bool ExecuteString(v8::Handle<v8::String> source,
37 v8::Handle<v8::Value> name, 43 v8::Handle<v8::Value> name,
38 bool print_result, 44 bool print_result,
39 bool report_exceptions); 45 bool report_exceptions);
40 v8::Handle<v8::Value> Print(const v8::Arguments& args); 46 v8::Handle<v8::Value> Print(const v8::Arguments& args);
41 v8::Handle<v8::Value> Read(const v8::Arguments& args); 47 v8::Handle<v8::Value> Read(const v8::Arguments& args);
42 v8::Handle<v8::Value> Load(const v8::Arguments& args); 48 v8::Handle<v8::Value> Load(const v8::Arguments& args);
43 v8::Handle<v8::Value> Quit(const v8::Arguments& args); 49 v8::Handle<v8::Value> Quit(const v8::Arguments& args);
44 v8::Handle<v8::Value> Version(const v8::Arguments& args); 50 v8::Handle<v8::Value> Version(const v8::Arguments& args);
45 v8::Handle<v8::String> ReadFile(const char* name); 51 v8::Handle<v8::String> ReadFile(const char* name);
46 void ReportException(v8::TryCatch* handler); 52 void ReportException(v8::TryCatch* handler);
47 53
48 54
49 int RunMain(int argc, char* argv[]) { 55 int RunMain(int argc, char* argv[]) {
50 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
51 v8::HandleScope handle_scope; 56 v8::HandleScope handle_scope;
52 // Create a template for the global object. 57 // Create a template for the global object.
53 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); 58 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
54 // Bind the global 'print' function to the C++ Print callback. 59 // Bind the global 'print' function to the C++ Print callback.
55 global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print)); 60 global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print));
56 // Bind the global 'read' function to the C++ Read callback. 61 // Bind the global 'read' function to the C++ Read callback.
57 global->Set(v8::String::New("read"), v8::FunctionTemplate::New(Read)); 62 global->Set(v8::String::New("read"), v8::FunctionTemplate::New(Read));
58 // Bind the global 'load' function to the C++ Load callback. 63 // Bind the global 'load' function to the C++ Load callback.
59 global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load)); 64 global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load));
60 // Bind the 'quit' function 65 // Bind the 'quit' function
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 return 1; 100 return 1;
96 } 101 }
97 if (!ExecuteString(source, file_name, false, true)) 102 if (!ExecuteString(source, file_name, false, true))
98 return 1; 103 return 1;
99 } 104 }
100 } 105 }
101 if (run_shell) RunShell(context); 106 if (run_shell) RunShell(context);
102 return 0; 107 return 0;
103 } 108 }
104 109
110 #ifdef V8_TEST
111 v8::AllowSeveralV8InstancesInProcess v8_please;
112
113 class V8Runner: public v8::internal::Thread {
114 public:
115 V8Runner(int argc, char* argv[])
116 :argc_(argc), argv_(argv) {
117 }
118
119 int result() const { return result_; }
120 private:
121 int argc_;
122 char** const argv_;
123 int result_;
124
125 virtual void Run() {
126 v8::V8ContextProvider v8_context_provider;
127 v8::V8ContextBinder v8_context_binder(v8_context_provider);
128 result_ = RunMain(argc_, argv_);
129 v8::V8::Dispose();
130 }
131
132 DISALLOW_COPY_AND_ASSIGN(V8Runner);
133 };
134 #endif
105 135
106 int main(int argc, char* argv[]) { 136 int main(int argc, char* argv[]) {
137 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
138 #ifdef V8_TEST
139 const int kAdditionalParallelThreads = 0;
140 V8Runner** runners = new V8Runner*[kAdditionalParallelThreads];
141 for (int i = 0; i < kAdditionalParallelThreads; ++i) {
142 runners[i] = new V8Runner(argc, argv);
143 runners[i]->Start();
144 }
145 #endif
146
107 int result = RunMain(argc, argv); 147 int result = RunMain(argc, argv);
108 v8::V8::Dispose(); 148 v8::V8::Dispose();
109 return result; 149
150 bool allResultsTheSame = true;
151 #ifdef V8_TEST
152 for (int i = 0; i < kAdditionalParallelThreads; ++i) {
153 runners[i]->Join();
154 if (result != runners[i]->result()) {
155 allResultsTheSame = false;
156 }
157 }
158 #endif
159 return allResultsTheSame ? result : -1;
110 } 160 }
111 161
112 162
113 // Extracts a C string from a V8 Utf8Value. 163 // Extracts a C string from a V8 Utf8Value.
114 const char* ToCString(const v8::String::Utf8Value& value) { 164 const char* ToCString(const v8::String::Utf8Value& value) {
115 return *value ? *value : "<string conversion failed>"; 165 return *value ? *value : "<string conversion failed>";
116 } 166 }
117 167
118 168
119 // The callback that is invoked by v8 whenever the JavaScript 'print' 169 // The callback that is invoked by v8 whenever the JavaScript 'print'
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 for (int i = 0; i < start; i++) { 344 for (int i = 0; i < start; i++) {
295 printf(" "); 345 printf(" ");
296 } 346 }
297 int end = message->GetEndColumn(); 347 int end = message->GetEndColumn();
298 for (int i = start; i < end; i++) { 348 for (int i = start; i < end; i++) {
299 printf("^"); 349 printf("^");
300 } 350 }
301 printf("\n"); 351 printf("\n");
302 } 352 }
303 } 353 }
OLDNEW
« no previous file with comments | « include/v8.h ('k') | src/SConscript » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698