Revision: 2671 Author: [email protected] Date: Thu Aug 13 00:07:41 2009 Log: Add a readline() command to d8. This reads a single line, stripping the new-line at the end. This is the other half of what is required to make the Debian Language Shootout code work correctly: http://code.google.com/p/v8/issues/detail?id=353
http://code.google.com/p/v8/source/detail?r=2671 Modified: /branches/bleeding_edge/src/d8.cc /branches/bleeding_edge/src/d8.h ======================================= --- /branches/bleeding_edge/src/d8.cc Wed Aug 12 04:52:22 2009 +++ /branches/bleeding_edge/src/d8.cc Thu Aug 13 00:07:41 2009 @@ -167,9 +167,6 @@ Handle<Value> Shell::Read(const Arguments& args) { - if (args.Length() != 1) { - return ThrowException(String::New("Bad parameters")); - } String::Utf8Value file(args[0]); if (*file == NULL) { return ThrowException(String::New("Error loading file")); @@ -180,6 +177,19 @@ } return source; } + + +Handle<Value> Shell::ReadLine(const Arguments& args) { + char line_buf[256]; + if (fgets(line_buf, sizeof(line_buf), stdin) == NULL) { + return ThrowException(String::New("Error reading line")); + } + int len = strlen(line_buf); + if (line_buf[len - 1] == '\n') { + --len; + } + return String::New(line_buf, len); +} Handle<Value> Shell::Load(const Arguments& args) { @@ -404,6 +414,8 @@ global_template->Set(String::New("print"), FunctionTemplate::New(Print)); global_template->Set(String::New("write"), FunctionTemplate::New(Write)); global_template->Set(String::New("read"), FunctionTemplate::New(Read)); + global_template->Set(String::New("readline"), + FunctionTemplate::New(ReadLine)); global_template->Set(String::New("load"), FunctionTemplate::New(Load)); global_template->Set(String::New("quit"), FunctionTemplate::New(Quit)); global_template->Set(String::New("version"), FunctionTemplate::New(Version)); @@ -596,6 +608,8 @@ FunctionTemplate::New(Shell::Write)); global_template->Set(String::New("read"), FunctionTemplate::New(Shell::Read)); + global_template->Set(String::New("readline"), + FunctionTemplate::New(Shell::ReadLine)); global_template->Set(String::New("load"), FunctionTemplate::New(Shell::Load)); global_template->Set(String::New("yield"), ======================================= --- /branches/bleeding_edge/src/d8.h Wed Aug 12 04:52:22 2009 +++ /branches/bleeding_edge/src/d8.h Thu Aug 13 00:07:41 2009 @@ -143,6 +143,7 @@ static Handle<Value> Quit(const Arguments& args); static Handle<Value> Version(const Arguments& args); static Handle<Value> Read(const Arguments& args); + static Handle<Value> ReadLine(const Arguments& args); static Handle<Value> Load(const Arguments& args); // The OS object on the global object contains methods for performing // operating system calls: --~--~---------~--~----~------------~-------~--~----~ v8-dev mailing list [email protected] http://groups.google.com/group/v8-dev -~----------~----~----~----~------~----~------~--~---
