| 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 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 | 289 |
| 290 // It is a function; cast it to a Function | 290 // It is a function; cast it to a Function |
| 291 v8::Handle<v8::Function> process_fun = | 291 v8::Handle<v8::Function> process_fun = |
| 292 v8::Handle<v8::Function>::Cast(process_val); | 292 v8::Handle<v8::Function>::Cast(process_val); |
| 293 | 293 |
| 294 | 294 |
| 295 while (!feof(stdin)) { | 295 while (!feof(stdin)) { |
| 296 v8::HandleScope handle_scope(isolate); | 296 v8::HandleScope handle_scope(isolate); |
| 297 | 297 |
| 298 v8::Handle<v8::String> input_line = ReadLine(); | 298 v8::Handle<v8::String> input_line = ReadLine(); |
| 299 if (input_line == v8::Undefined()) { | 299 if (input_line == v8::Undefined(isolate)) { |
| 300 continue; | 300 continue; |
| 301 } | 301 } |
| 302 | 302 |
| 303 const int argc = 1; | 303 const int argc = 1; |
| 304 v8::Handle<v8::Value> argv[argc] = { input_line }; | 304 v8::Handle<v8::Value> argv[argc] = { input_line }; |
| 305 | 305 |
| 306 v8::Handle<v8::Value> result; | 306 v8::Handle<v8::Value> result; |
| 307 { | 307 { |
| 308 v8::TryCatch try_catch; | 308 v8::TryCatch try_catch; |
| 309 result = process_fun->Call(isolate->GetCurrentContext()->Global(), | 309 result = process_fun->Call(isolate->GetCurrentContext()->Global(), |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 } | 410 } |
| 411 printf("\n"); | 411 printf("\n"); |
| 412 fflush(stdout); | 412 fflush(stdout); |
| 413 } | 413 } |
| 414 | 414 |
| 415 | 415 |
| 416 // The callback that is invoked by v8 whenever the JavaScript 'read_line' | 416 // The callback that is invoked by v8 whenever the JavaScript 'read_line' |
| 417 // function is called. Reads a string from standard input and returns. | 417 // function is called. Reads a string from standard input and returns. |
| 418 void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args) { | 418 void ReadLine(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 419 if (args.Length() > 0) { | 419 if (args.Length() > 0) { |
| 420 v8::ThrowException(v8::String::New("Unexpected arguments")); | 420 args.GetIsolate()->ThrowException(v8::String::New("Unexpected arguments")); |
| 421 return; | 421 return; |
| 422 } | 422 } |
| 423 args.GetReturnValue().Set(ReadLine()); | 423 args.GetReturnValue().Set(ReadLine()); |
| 424 } | 424 } |
| 425 | 425 |
| 426 | 426 |
| 427 v8::Handle<v8::String> ReadLine() { | 427 v8::Handle<v8::String> ReadLine() { |
| 428 const int kBufferSize = 1024 + 1; | 428 const int kBufferSize = 1024 + 1; |
| 429 char buffer[kBufferSize]; | 429 char buffer[kBufferSize]; |
| 430 | 430 |
| 431 char* res; | 431 char* res; |
| 432 { | 432 { |
| 433 #ifdef ENABLE_DEBUGGER_SUPPORT | 433 #ifdef ENABLE_DEBUGGER_SUPPORT |
| 434 v8::Unlocker unlocker(v8::Isolate::GetCurrent()); | 434 v8::Unlocker unlocker(v8::Isolate::GetCurrent()); |
| 435 #endif // ENABLE_DEBUGGER_SUPPORT | 435 #endif // ENABLE_DEBUGGER_SUPPORT |
| 436 res = fgets(buffer, kBufferSize, stdin); | 436 res = fgets(buffer, kBufferSize, stdin); |
| 437 } | 437 } |
| 438 if (res == NULL) { | 438 if (res == NULL) { |
| 439 v8::Handle<v8::Primitive> t = v8::Undefined(); | 439 v8::Handle<v8::Primitive> t = v8::Undefined(v8::Isolate::GetCurrent()); |
| 440 return v8::Handle<v8::String>::Cast(t); | 440 return v8::Handle<v8::String>::Cast(t); |
| 441 } | 441 } |
| 442 // Remove newline char | 442 // Remove newline char |
| 443 for (char* pos = buffer; *pos != '\0'; pos++) { | 443 for (char* pos = buffer; *pos != '\0'; pos++) { |
| 444 if (*pos == '\n') { | 444 if (*pos == '\n') { |
| 445 *pos = '\0'; | 445 *pos = '\0'; |
| 446 break; | 446 break; |
| 447 } | 447 } |
| 448 } | 448 } |
| 449 return v8::String::New(buffer); | 449 return v8::String::New(buffer); |
| 450 } | 450 } |
| OLD | NEW |