OLD | NEW |
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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 * is called again. See how "--callback" command-line parameter in this sample | 91 * is called again. See how "--callback" command-line parameter in this sample |
92 * fixes this issue. | 92 * fixes this issue. |
93 */ | 93 */ |
94 | 94 |
95 enum MainCycleType { | 95 enum MainCycleType { |
96 CycleInCpp, | 96 CycleInCpp, |
97 CycleInJs | 97 CycleInJs |
98 }; | 98 }; |
99 | 99 |
100 const char* ToCString(const v8::String::Utf8Value& value); | 100 const char* ToCString(const v8::String::Utf8Value& value); |
101 void ReportException(v8::TryCatch* handler); | 101 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); |
102 v8::Handle<v8::String> ReadFile(const char* name); | 102 v8::Handle<v8::String> ReadFile(const char* name); |
103 v8::Handle<v8::String> ReadLine(); | 103 v8::Handle<v8::String> ReadLine(); |
104 | 104 |
105 v8::Handle<v8::Value> Print(const v8::Arguments& args); | 105 v8::Handle<v8::Value> Print(const v8::Arguments& args); |
106 v8::Handle<v8::Value> ReadLine(const v8::Arguments& args); | 106 v8::Handle<v8::Value> ReadLine(const v8::Arguments& args); |
107 bool RunCppCycle(v8::Handle<v8::Script> script, v8::Local<v8::Context> context, | 107 bool RunCppCycle(v8::Handle<v8::Script> script, |
| 108 v8::Local<v8::Context> context, |
108 bool report_exceptions); | 109 bool report_exceptions); |
109 | 110 |
110 | 111 |
111 #ifdef ENABLE_DEBUGGER_SUPPORT | 112 #ifdef ENABLE_DEBUGGER_SUPPORT |
112 v8::Persistent<v8::Context> debug_message_context; | 113 v8::Persistent<v8::Context> debug_message_context; |
113 | 114 |
114 void DispatchDebugMessages() { | 115 void DispatchDebugMessages() { |
115 // We are in some random thread. We should already have v8::Locker acquired | 116 // We are in some random thread. We should already have v8::Locker acquired |
116 // (we requested this when registered this callback). We was called | 117 // (we requested this when registered this callback). We was called |
117 // because new debug messages arrived; they may have already been processed, | 118 // because new debug messages arrived; they may have already been processed, |
118 // but we shouldn't worry about this. | 119 // but we shouldn't worry about this. |
119 // | 120 // |
120 // All we have to do is to set context and call ProcessDebugMessages. | 121 // All we have to do is to set context and call ProcessDebugMessages. |
121 // | 122 // |
122 // We should decide which V8 context to use here. This is important for | 123 // We should decide which V8 context to use here. This is important for |
123 // "evaluate" command, because it must be executed some context. | 124 // "evaluate" command, because it must be executed some context. |
124 // In our sample we have only one context, so there is nothing really to | 125 // In our sample we have only one context, so there is nothing really to |
125 // think about. | 126 // think about. |
126 v8::Context::Scope scope(debug_message_context); | 127 v8::Context::Scope scope(debug_message_context); |
127 | 128 |
128 v8::Debug::ProcessDebugMessages(); | 129 v8::Debug::ProcessDebugMessages(); |
129 } | 130 } |
130 #endif // ENABLE_DEBUGGER_SUPPORT | 131 #endif // ENABLE_DEBUGGER_SUPPORT |
131 | 132 |
132 | 133 |
133 int RunMain(int argc, char* argv[]) { | 134 int RunMain(int argc, char* argv[]) { |
134 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 135 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
135 v8::HandleScope handle_scope; | 136 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 137 v8::HandleScope handle_scope(isolate); |
136 | 138 |
137 v8::Handle<v8::String> script_source(NULL); | 139 v8::Handle<v8::String> script_source(NULL); |
138 v8::Handle<v8::Value> script_name(NULL); | 140 v8::Handle<v8::Value> script_name(NULL); |
139 int script_param_counter = 0; | 141 int script_param_counter = 0; |
140 | 142 |
141 #ifdef ENABLE_DEBUGGER_SUPPORT | 143 #ifdef ENABLE_DEBUGGER_SUPPORT |
142 int port_number = -1; | 144 int port_number = -1; |
143 bool wait_for_connection = false; | 145 bool wait_for_connection = false; |
144 bool support_callback = false; | 146 bool support_callback = false; |
145 #endif // ENABLE_DEBUGGER_SUPPORT | 147 #endif // ENABLE_DEBUGGER_SUPPORT |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 } | 208 } |
207 | 209 |
208 // Create a new execution environment containing the built-in | 210 // Create a new execution environment containing the built-in |
209 // functions | 211 // functions |
210 v8::Handle<v8::Context> context = v8::Context::New(NULL, global); | 212 v8::Handle<v8::Context> context = v8::Context::New(NULL, global); |
211 // Enter the newly created execution environment. | 213 // Enter the newly created execution environment. |
212 v8::Context::Scope context_scope(context); | 214 v8::Context::Scope context_scope(context); |
213 | 215 |
214 #ifdef ENABLE_DEBUGGER_SUPPORT | 216 #ifdef ENABLE_DEBUGGER_SUPPORT |
215 debug_message_context = | 217 debug_message_context = |
216 v8::Persistent<v8::Context>::New(context->GetIsolate(), context); | 218 v8::Persistent<v8::Context>::New(isolate, context); |
217 | 219 |
218 v8::Locker locker(context->GetIsolate()); | 220 v8::Locker locker(isolate); |
219 | 221 |
220 if (support_callback) { | 222 if (support_callback) { |
221 v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true); | 223 v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true); |
222 } | 224 } |
223 | 225 |
224 if (port_number != -1) { | 226 if (port_number != -1) { |
225 v8::Debug::EnableAgent("lineprocessor", port_number, wait_for_connection); | 227 v8::Debug::EnableAgent("lineprocessor", port_number, wait_for_connection); |
226 } | 228 } |
227 #endif // ENABLE_DEBUGGER_SUPPORT | 229 #endif // ENABLE_DEBUGGER_SUPPORT |
228 | 230 |
229 bool report_exceptions = true; | 231 bool report_exceptions = true; |
230 | 232 |
231 v8::Handle<v8::Script> script; | 233 v8::Handle<v8::Script> script; |
232 { | 234 { |
233 // Compile script in try/catch context. | 235 // Compile script in try/catch context. |
234 v8::TryCatch try_catch; | 236 v8::TryCatch try_catch; |
235 script = v8::Script::Compile(script_source, script_name); | 237 script = v8::Script::Compile(script_source, script_name); |
236 if (script.IsEmpty()) { | 238 if (script.IsEmpty()) { |
237 // Print errors that happened during compilation. | 239 // Print errors that happened during compilation. |
238 if (report_exceptions) | 240 if (report_exceptions) |
239 ReportException(&try_catch); | 241 ReportException(isolate, &try_catch); |
240 return 1; | 242 return 1; |
241 } | 243 } |
242 } | 244 } |
243 | 245 |
244 { | 246 { |
245 v8::TryCatch try_catch; | 247 v8::TryCatch try_catch; |
246 | 248 |
247 script->Run(); | 249 script->Run(); |
248 if (try_catch.HasCaught()) { | 250 if (try_catch.HasCaught()) { |
249 if (report_exceptions) | 251 if (report_exceptions) |
250 ReportException(&try_catch); | 252 ReportException(isolate, &try_catch); |
251 return 1; | 253 return 1; |
252 } | 254 } |
253 } | 255 } |
254 | 256 |
255 if (cycle_type == CycleInCpp) { | 257 if (cycle_type == CycleInCpp) { |
256 bool res = RunCppCycle(script, v8::Context::GetCurrent(), | 258 bool res = RunCppCycle(script, |
| 259 v8::Context::GetCurrent(), |
257 report_exceptions); | 260 report_exceptions); |
258 return !res; | 261 return !res; |
259 } else { | 262 } else { |
260 // All is already done. | 263 // All is already done. |
261 } | 264 } |
262 return 0; | 265 return 0; |
263 } | 266 } |
264 | 267 |
265 | 268 |
266 bool RunCppCycle(v8::Handle<v8::Script> script, v8::Local<v8::Context> context, | 269 bool RunCppCycle(v8::Handle<v8::Script> script, |
| 270 v8::Local<v8::Context> context, |
267 bool report_exceptions) { | 271 bool report_exceptions) { |
| 272 v8::Isolate* isolate = context->GetIsolate(); |
268 #ifdef ENABLE_DEBUGGER_SUPPORT | 273 #ifdef ENABLE_DEBUGGER_SUPPORT |
269 v8::Locker lock(v8::Isolate::GetCurrent()); | 274 v8::Locker lock(isolate); |
270 #endif // ENABLE_DEBUGGER_SUPPORT | 275 #endif // ENABLE_DEBUGGER_SUPPORT |
271 | 276 |
272 v8::Handle<v8::String> fun_name = v8::String::New("ProcessLine"); | 277 v8::Handle<v8::String> fun_name = v8::String::New("ProcessLine"); |
273 v8::Handle<v8::Value> process_val = | 278 v8::Handle<v8::Value> process_val = context->Global()->Get(fun_name); |
274 v8::Context::GetCurrent()->Global()->Get(fun_name); | |
275 | 279 |
276 // If there is no Process function, or if it is not a function, | 280 // If there is no Process function, or if it is not a function, |
277 // bail out | 281 // bail out |
278 if (!process_val->IsFunction()) { | 282 if (!process_val->IsFunction()) { |
279 printf("Error: Script does not declare 'ProcessLine' global function.\n"); | 283 printf("Error: Script does not declare 'ProcessLine' global function.\n"); |
280 return 1; | 284 return 1; |
281 } | 285 } |
282 | 286 |
283 // It is a function; cast it to a Function | 287 // It is a function; cast it to a Function |
284 v8::Handle<v8::Function> process_fun = | 288 v8::Handle<v8::Function> process_fun = |
285 v8::Handle<v8::Function>::Cast(process_val); | 289 v8::Handle<v8::Function>::Cast(process_val); |
286 | 290 |
287 | 291 |
288 while (!feof(stdin)) { | 292 while (!feof(stdin)) { |
289 v8::HandleScope handle_scope; | 293 v8::HandleScope handle_scope(isolate); |
290 | 294 |
291 v8::Handle<v8::String> input_line = ReadLine(); | 295 v8::Handle<v8::String> input_line = ReadLine(); |
292 if (input_line == v8::Undefined()) { | 296 if (input_line == v8::Undefined()) { |
293 continue; | 297 continue; |
294 } | 298 } |
295 | 299 |
296 const int argc = 1; | 300 const int argc = 1; |
297 v8::Handle<v8::Value> argv[argc] = { input_line }; | 301 v8::Handle<v8::Value> argv[argc] = { input_line }; |
298 | 302 |
299 v8::Handle<v8::Value> result; | 303 v8::Handle<v8::Value> result; |
300 { | 304 { |
301 v8::TryCatch try_catch; | 305 v8::TryCatch try_catch; |
302 result = process_fun->Call(v8::Context::GetCurrent()->Global(), | 306 result = process_fun->Call(v8::Context::GetCurrent()->Global(), |
303 argc, argv); | 307 argc, argv); |
304 if (try_catch.HasCaught()) { | 308 if (try_catch.HasCaught()) { |
305 if (report_exceptions) | 309 if (report_exceptions) |
306 ReportException(&try_catch); | 310 ReportException(isolate, &try_catch); |
307 return false; | 311 return false; |
308 } | 312 } |
309 } | 313 } |
310 v8::String::Utf8Value str(result); | 314 v8::String::Utf8Value str(result); |
311 const char* cstr = ToCString(str); | 315 const char* cstr = ToCString(str); |
312 printf("%s\n", cstr); | 316 printf("%s\n", cstr); |
313 } | 317 } |
314 | 318 |
315 return true; | 319 return true; |
316 } | 320 } |
(...skipping 26 matching lines...) Expand all Loading... |
343 int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); | 347 int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); |
344 i += read; | 348 i += read; |
345 } | 349 } |
346 fclose(file); | 350 fclose(file); |
347 v8::Handle<v8::String> result = v8::String::New(chars, size); | 351 v8::Handle<v8::String> result = v8::String::New(chars, size); |
348 delete[] chars; | 352 delete[] chars; |
349 return result; | 353 return result; |
350 } | 354 } |
351 | 355 |
352 | 356 |
353 void ReportException(v8::TryCatch* try_catch) { | 357 void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) { |
354 v8::HandleScope handle_scope; | 358 v8::HandleScope handle_scope(isolate); |
355 v8::String::Utf8Value exception(try_catch->Exception()); | 359 v8::String::Utf8Value exception(try_catch->Exception()); |
356 const char* exception_string = ToCString(exception); | 360 const char* exception_string = ToCString(exception); |
357 v8::Handle<v8::Message> message = try_catch->Message(); | 361 v8::Handle<v8::Message> message = try_catch->Message(); |
358 if (message.IsEmpty()) { | 362 if (message.IsEmpty()) { |
359 // V8 didn't provide any extra information about this error; just | 363 // V8 didn't provide any extra information about this error; just |
360 // print the exception. | 364 // print the exception. |
361 printf("%s\n", exception_string); | 365 printf("%s\n", exception_string); |
362 } else { | 366 } else { |
363 // Print (filename):(line number): (message). | 367 // Print (filename):(line number): (message). |
364 v8::String::Utf8Value filename(message->GetScriptResourceName()); | 368 v8::String::Utf8Value filename(message->GetScriptResourceName()); |
(...skipping 17 matching lines...) Expand all Loading... |
382 } | 386 } |
383 } | 387 } |
384 | 388 |
385 | 389 |
386 // The callback that is invoked by v8 whenever the JavaScript 'print' | 390 // The callback that is invoked by v8 whenever the JavaScript 'print' |
387 // function is called. Prints its arguments on stdout separated by | 391 // function is called. Prints its arguments on stdout separated by |
388 // spaces and ending with a newline. | 392 // spaces and ending with a newline. |
389 v8::Handle<v8::Value> Print(const v8::Arguments& args) { | 393 v8::Handle<v8::Value> Print(const v8::Arguments& args) { |
390 bool first = true; | 394 bool first = true; |
391 for (int i = 0; i < args.Length(); i++) { | 395 for (int i = 0; i < args.Length(); i++) { |
392 v8::HandleScope handle_scope; | 396 v8::HandleScope handle_scope(args.GetIsolate()); |
393 if (first) { | 397 if (first) { |
394 first = false; | 398 first = false; |
395 } else { | 399 } else { |
396 printf(" "); | 400 printf(" "); |
397 } | 401 } |
398 v8::String::Utf8Value str(args[i]); | 402 v8::String::Utf8Value str(args[i]); |
399 const char* cstr = ToCString(str); | 403 const char* cstr = ToCString(str); |
400 printf("%s", cstr); | 404 printf("%s", cstr); |
401 } | 405 } |
402 printf("\n"); | 406 printf("\n"); |
(...skipping 28 matching lines...) Expand all Loading... |
431 } | 435 } |
432 // Remove newline char | 436 // Remove newline char |
433 for (char* pos = buffer; *pos != '\0'; pos++) { | 437 for (char* pos = buffer; *pos != '\0'; pos++) { |
434 if (*pos == '\n') { | 438 if (*pos == '\n') { |
435 *pos = '\0'; | 439 *pos = '\0'; |
436 break; | 440 break; |
437 } | 441 } |
438 } | 442 } |
439 return v8::String::New(buffer); | 443 return v8::String::New(buffer); |
440 } | 444 } |
OLD | NEW |