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

Side by Side Diff: samples/shell.cc

Issue 12716010: Added a version of the v8::HandleScope constructor with an Isolate and use that consistently. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Fixed rest Created 7 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 | Annotate | Revision Log
OLDNEW
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 29 matching lines...) Expand all
40 * This sample program shows how to implement a simple javascript shell 40 * This sample program shows how to implement a simple javascript shell
41 * based on V8. This includes initializing V8 with command line options, 41 * based on V8. This includes initializing V8 with command line options,
42 * creating global functions, compiling and executing strings. 42 * creating global functions, compiling and executing strings.
43 * 43 *
44 * For a more sophisticated shell, consider using the debug shell D8. 44 * For a more sophisticated shell, consider using the debug shell D8.
45 */ 45 */
46 46
47 47
48 v8::Persistent<v8::Context> CreateShellContext(); 48 v8::Persistent<v8::Context> CreateShellContext();
49 void RunShell(v8::Handle<v8::Context> context); 49 void RunShell(v8::Handle<v8::Context> context);
50 int RunMain(int argc, char* argv[]); 50 int RunMain(v8::Isolate* isolate, int argc, char* argv[]);
51 bool ExecuteString(v8::Handle<v8::String> source, 51 bool ExecuteString(v8::Isolate* isolate,
52 v8::Handle<v8::String> source,
52 v8::Handle<v8::Value> name, 53 v8::Handle<v8::Value> name,
53 bool print_result, 54 bool print_result,
54 bool report_exceptions); 55 bool report_exceptions);
55 v8::Handle<v8::Value> Print(const v8::Arguments& args); 56 v8::Handle<v8::Value> Print(const v8::Arguments& args);
56 v8::Handle<v8::Value> Read(const v8::Arguments& args); 57 v8::Handle<v8::Value> Read(const v8::Arguments& args);
57 v8::Handle<v8::Value> Load(const v8::Arguments& args); 58 v8::Handle<v8::Value> Load(const v8::Arguments& args);
58 v8::Handle<v8::Value> Quit(const v8::Arguments& args); 59 v8::Handle<v8::Value> Quit(const v8::Arguments& args);
59 v8::Handle<v8::Value> Version(const v8::Arguments& args); 60 v8::Handle<v8::Value> Version(const v8::Arguments& args);
60 v8::Handle<v8::String> ReadFile(const char* name); 61 v8::Handle<v8::String> ReadFile(const char* name);
61 void ReportException(v8::TryCatch* handler); 62 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler);
62 63
63 64
64 static bool run_shell; 65 static bool run_shell;
65 66
66 67
67 int main(int argc, char* argv[]) { 68 int main(int argc, char* argv[]) {
68 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); 69 v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
70 v8::Isolate* isolate = v8::Isolate::GetCurrent();
69 run_shell = (argc == 1); 71 run_shell = (argc == 1);
70 int result; 72 int result;
71 { 73 {
72 v8::HandleScope handle_scope;
73 v8::Persistent<v8::Context> context = CreateShellContext(); 74 v8::Persistent<v8::Context> context = CreateShellContext();
75 v8::HandleScope handle_scope(isolate);
Michael Starzinger 2013/03/14 19:10:26 Why was this HandleScope moved down? I think it sh
Sven Panne 2013/03/15 07:59:09 Done. Ceterum censeo shell.cc esse delendam! :-P
74 if (context.IsEmpty()) { 76 if (context.IsEmpty()) {
75 fprintf(stderr, "Error creating context\n"); 77 fprintf(stderr, "Error creating context\n");
76 return 1; 78 return 1;
77 } 79 }
78 context->Enter(); 80 context->Enter();
79 result = RunMain(argc, argv); 81 result = RunMain(isolate, argc, argv);
80 if (run_shell) RunShell(context); 82 if (run_shell) RunShell(context);
81 context->Exit(); 83 context->Exit();
82 context.Dispose(context->GetIsolate()); 84 context.Dispose(isolate);
83 } 85 }
84 v8::V8::Dispose(); 86 v8::V8::Dispose();
85 return result; 87 return result;
86 } 88 }
87 89
88 90
89 // Extracts a C string from a V8 Utf8Value. 91 // Extracts a C string from a V8 Utf8Value.
90 const char* ToCString(const v8::String::Utf8Value& value) { 92 const char* ToCString(const v8::String::Utf8Value& value) {
91 return *value ? *value : "<string conversion failed>"; 93 return *value ? *value : "<string conversion failed>";
92 } 94 }
(...skipping 18 matching lines...) Expand all
111 return v8::Context::New(NULL, global); 113 return v8::Context::New(NULL, global);
112 } 114 }
113 115
114 116
115 // The callback that is invoked by v8 whenever the JavaScript 'print' 117 // The callback that is invoked by v8 whenever the JavaScript 'print'
116 // function is called. Prints its arguments on stdout separated by 118 // function is called. Prints its arguments on stdout separated by
117 // spaces and ending with a newline. 119 // spaces and ending with a newline.
118 v8::Handle<v8::Value> Print(const v8::Arguments& args) { 120 v8::Handle<v8::Value> Print(const v8::Arguments& args) {
119 bool first = true; 121 bool first = true;
120 for (int i = 0; i < args.Length(); i++) { 122 for (int i = 0; i < args.Length(); i++) {
121 v8::HandleScope handle_scope; 123 v8::HandleScope handle_scope(args.GetIsolate());
122 if (first) { 124 if (first) {
123 first = false; 125 first = false;
124 } else { 126 } else {
125 printf(" "); 127 printf(" ");
126 } 128 }
127 v8::String::Utf8Value str(args[i]); 129 v8::String::Utf8Value str(args[i]);
128 const char* cstr = ToCString(str); 130 const char* cstr = ToCString(str);
129 printf("%s", cstr); 131 printf("%s", cstr);
130 } 132 }
131 printf("\n"); 133 printf("\n");
(...skipping 19 matching lines...) Expand all
151 } 153 }
152 return source; 154 return source;
153 } 155 }
154 156
155 157
156 // The callback that is invoked by v8 whenever the JavaScript 'load' 158 // The callback that is invoked by v8 whenever the JavaScript 'load'
157 // function is called. Loads, compiles and executes its argument 159 // function is called. Loads, compiles and executes its argument
158 // JavaScript file. 160 // JavaScript file.
159 v8::Handle<v8::Value> Load(const v8::Arguments& args) { 161 v8::Handle<v8::Value> Load(const v8::Arguments& args) {
160 for (int i = 0; i < args.Length(); i++) { 162 for (int i = 0; i < args.Length(); i++) {
161 v8::HandleScope handle_scope; 163 v8::HandleScope handle_scope(args.GetIsolate());
162 v8::String::Utf8Value file(args[i]); 164 v8::String::Utf8Value file(args[i]);
163 if (*file == NULL) { 165 if (*file == NULL) {
164 return v8::ThrowException(v8::String::New("Error loading file")); 166 return v8::ThrowException(v8::String::New("Error loading file"));
165 } 167 }
166 v8::Handle<v8::String> source = ReadFile(*file); 168 v8::Handle<v8::String> source = ReadFile(*file);
167 if (source.IsEmpty()) { 169 if (source.IsEmpty()) {
168 return v8::ThrowException(v8::String::New("Error loading file")); 170 return v8::ThrowException(v8::String::New("Error loading file"));
169 } 171 }
170 if (!ExecuteString(source, v8::String::New(*file), false, false)) { 172 if (!ExecuteString(args.GetIsolate(),
173 source,
174 v8::String::New(*file),
175 false,
176 false)) {
171 return v8::ThrowException(v8::String::New("Error executing file")); 177 return v8::ThrowException(v8::String::New("Error executing file"));
172 } 178 }
173 } 179 }
174 return v8::Undefined(); 180 return v8::Undefined();
175 } 181 }
176 182
177 183
178 // The callback that is invoked by v8 whenever the JavaScript 'quit' 184 // The callback that is invoked by v8 whenever the JavaScript 'quit'
179 // function is called. Quits. 185 // function is called. Quits.
180 v8::Handle<v8::Value> Quit(const v8::Arguments& args) { 186 v8::Handle<v8::Value> Quit(const v8::Arguments& args) {
(...skipping 28 matching lines...) Expand all
209 i += read; 215 i += read;
210 } 216 }
211 fclose(file); 217 fclose(file);
212 v8::Handle<v8::String> result = v8::String::New(chars, size); 218 v8::Handle<v8::String> result = v8::String::New(chars, size);
213 delete[] chars; 219 delete[] chars;
214 return result; 220 return result;
215 } 221 }
216 222
217 223
218 // Process remaining command line arguments and execute files 224 // Process remaining command line arguments and execute files
219 int RunMain(int argc, char* argv[]) { 225 int RunMain(v8::Isolate* isolate, int argc, char* argv[]) {
220 for (int i = 1; i < argc; i++) { 226 for (int i = 1; i < argc; i++) {
221 const char* str = argv[i]; 227 const char* str = argv[i];
222 if (strcmp(str, "--shell") == 0) { 228 if (strcmp(str, "--shell") == 0) {
223 run_shell = true; 229 run_shell = true;
224 } else if (strcmp(str, "-f") == 0) { 230 } else if (strcmp(str, "-f") == 0) {
225 // Ignore any -f flags for compatibility with the other stand- 231 // Ignore any -f flags for compatibility with the other stand-
226 // alone JavaScript engines. 232 // alone JavaScript engines.
227 continue; 233 continue;
228 } else if (strncmp(str, "--", 2) == 0) { 234 } else if (strncmp(str, "--", 2) == 0) {
229 fprintf(stderr, 235 fprintf(stderr,
230 "Warning: unknown flag %s.\nTry --help for options\n", str); 236 "Warning: unknown flag %s.\nTry --help for options\n", str);
231 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { 237 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
232 // Execute argument given to -e option directly. 238 // Execute argument given to -e option directly.
233 v8::Handle<v8::String> file_name = v8::String::New("unnamed"); 239 v8::Handle<v8::String> file_name = v8::String::New("unnamed");
234 v8::Handle<v8::String> source = v8::String::New(argv[++i]); 240 v8::Handle<v8::String> source = v8::String::New(argv[++i]);
235 if (!ExecuteString(source, file_name, false, true)) return 1; 241 if (!ExecuteString(isolate, source, file_name, false, true)) return 1;
236 } else { 242 } else {
237 // Use all other arguments as names of files to load and run. 243 // Use all other arguments as names of files to load and run.
238 v8::Handle<v8::String> file_name = v8::String::New(str); 244 v8::Handle<v8::String> file_name = v8::String::New(str);
239 v8::Handle<v8::String> source = ReadFile(str); 245 v8::Handle<v8::String> source = ReadFile(str);
240 if (source.IsEmpty()) { 246 if (source.IsEmpty()) {
241 fprintf(stderr, "Error reading '%s'\n", str); 247 fprintf(stderr, "Error reading '%s'\n", str);
242 continue; 248 continue;
243 } 249 }
244 if (!ExecuteString(source, file_name, false, true)) return 1; 250 if (!ExecuteString(isolate, source, file_name, false, true)) return 1;
245 } 251 }
246 } 252 }
247 return 0; 253 return 0;
248 } 254 }
249 255
250 256
251 // The read-eval-execute loop of the shell. 257 // The read-eval-execute loop of the shell.
252 void RunShell(v8::Handle<v8::Context> context) { 258 void RunShell(v8::Handle<v8::Context> context) {
253 fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion()); 259 fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion());
254 static const int kBufferSize = 256; 260 static const int kBufferSize = 256;
255 // Enter the execution environment before evaluating any code. 261 // Enter the execution environment before evaluating any code.
256 v8::Context::Scope context_scope(context); 262 v8::Context::Scope context_scope(context);
257 v8::Local<v8::String> name(v8::String::New("(shell)")); 263 v8::Local<v8::String> name(v8::String::New("(shell)"));
258 while (true) { 264 while (true) {
259 char buffer[kBufferSize]; 265 char buffer[kBufferSize];
260 fprintf(stderr, "> "); 266 fprintf(stderr, "> ");
261 char* str = fgets(buffer, kBufferSize, stdin); 267 char* str = fgets(buffer, kBufferSize, stdin);
262 if (str == NULL) break; 268 if (str == NULL) break;
263 v8::HandleScope handle_scope; 269 v8::HandleScope handle_scope(context->GetIsolate());
264 ExecuteString(v8::String::New(str), name, true, true); 270 ExecuteString(context->GetIsolate(),
271 v8::String::New(str),
272 name,
273 true,
274 true);
265 } 275 }
266 fprintf(stderr, "\n"); 276 fprintf(stderr, "\n");
267 } 277 }
268 278
269 279
270 // Executes a string within the current v8 context. 280 // Executes a string within the current v8 context.
271 bool ExecuteString(v8::Handle<v8::String> source, 281 bool ExecuteString(v8::Isolate* isolate,
282 v8::Handle<v8::String> source,
272 v8::Handle<v8::Value> name, 283 v8::Handle<v8::Value> name,
273 bool print_result, 284 bool print_result,
274 bool report_exceptions) { 285 bool report_exceptions) {
275 v8::HandleScope handle_scope; 286 v8::HandleScope handle_scope(isolate);
276 v8::TryCatch try_catch; 287 v8::TryCatch try_catch;
277 v8::Handle<v8::Script> script = v8::Script::Compile(source, name); 288 v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
278 if (script.IsEmpty()) { 289 if (script.IsEmpty()) {
279 // Print errors that happened during compilation. 290 // Print errors that happened during compilation.
280 if (report_exceptions) 291 if (report_exceptions)
281 ReportException(&try_catch); 292 ReportException(isolate, &try_catch);
282 return false; 293 return false;
283 } else { 294 } else {
284 v8::Handle<v8::Value> result = script->Run(); 295 v8::Handle<v8::Value> result = script->Run();
285 if (result.IsEmpty()) { 296 if (result.IsEmpty()) {
286 assert(try_catch.HasCaught()); 297 assert(try_catch.HasCaught());
287 // Print errors that happened during execution. 298 // Print errors that happened during execution.
288 if (report_exceptions) 299 if (report_exceptions)
289 ReportException(&try_catch); 300 ReportException(isolate, &try_catch);
290 return false; 301 return false;
291 } else { 302 } else {
292 assert(!try_catch.HasCaught()); 303 assert(!try_catch.HasCaught());
293 if (print_result && !result->IsUndefined()) { 304 if (print_result && !result->IsUndefined()) {
294 // If all went well and the result wasn't undefined then print 305 // If all went well and the result wasn't undefined then print
295 // the returned value. 306 // the returned value.
296 v8::String::Utf8Value str(result); 307 v8::String::Utf8Value str(result);
297 const char* cstr = ToCString(str); 308 const char* cstr = ToCString(str);
298 printf("%s\n", cstr); 309 printf("%s\n", cstr);
299 } 310 }
300 return true; 311 return true;
301 } 312 }
302 } 313 }
303 } 314 }
304 315
305 316
306 void ReportException(v8::TryCatch* try_catch) { 317 void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
307 v8::HandleScope handle_scope; 318 v8::HandleScope handle_scope(isolate);
308 v8::String::Utf8Value exception(try_catch->Exception()); 319 v8::String::Utf8Value exception(try_catch->Exception());
309 const char* exception_string = ToCString(exception); 320 const char* exception_string = ToCString(exception);
310 v8::Handle<v8::Message> message = try_catch->Message(); 321 v8::Handle<v8::Message> message = try_catch->Message();
311 if (message.IsEmpty()) { 322 if (message.IsEmpty()) {
312 // V8 didn't provide any extra information about this error; just 323 // V8 didn't provide any extra information about this error; just
313 // print the exception. 324 // print the exception.
314 fprintf(stderr, "%s\n", exception_string); 325 fprintf(stderr, "%s\n", exception_string);
315 } else { 326 } else {
316 // Print (filename):(line number): (message). 327 // Print (filename):(line number): (message).
317 v8::String::Utf8Value filename(message->GetScriptResourceName()); 328 v8::String::Utf8Value filename(message->GetScriptResourceName());
(...skipping 14 matching lines...) Expand all
332 fprintf(stderr, "^"); 343 fprintf(stderr, "^");
333 } 344 }
334 fprintf(stderr, "\n"); 345 fprintf(stderr, "\n");
335 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); 346 v8::String::Utf8Value stack_trace(try_catch->StackTrace());
336 if (stack_trace.length() > 0) { 347 if (stack_trace.length() > 0) {
337 const char* stack_trace_string = ToCString(stack_trace); 348 const char* stack_trace_string = ToCString(stack_trace);
338 fprintf(stderr, "%s\n", stack_trace_string); 349 fprintf(stderr, "%s\n", stack_trace_string);
339 } 350 }
340 } 351 }
341 } 352 }
OLDNEW
« no previous file with comments | « samples/process.cc ('k') | src/api.cc » ('j') | test/cctest/test-api.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698