Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: test/shell/getline.js
- ===================================================================
- --- test/shell/getline.js (revision 0)
- +++ test/shell/getline.js (working copy)
- @@ -0,0 +1,3 @@
- +
- +var line = getline();
- +print('line:\''+line+'\'');
- Index: test/shell/getchar.js
- ===================================================================
- --- test/shell/getchar.js (revision 0)
- +++ test/shell/getchar.js (working copy)
- @@ -0,0 +1,3 @@
- +
- +var char = getchar();
- +print('char:\''+char+'\'');
- Index: samples/shell.cc
- ===================================================================
- --- samples/shell.cc (revision 13664)
- +++ samples/shell.cc (working copy)
- @@ -58,6 +58,8 @@
- v8::Handle<v8::Value> Quit(const v8::Arguments& args);
- v8::Handle<v8::Value> Version(const v8::Arguments& args);
- v8::Handle<v8::String> ReadFile(const char* name);
- +v8::Handle<v8::Value> GetChar(const v8::Arguments& args);
- +v8::Handle<v8::Value> GetLine(const v8::Arguments& args);
- void ReportException(v8::TryCatch* handler);
- @@ -99,6 +101,10 @@
- v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
- // Bind the global 'print' function to the C++ Print callback.
- global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print));
- + // Bind the global 'getchar' function to the C++ GetChar callback.
- + global->Set(v8::String::New("getchar"), v8::FunctionTemplate::New(GetChar));
- + // Bind the global 'getline' function to the C++ GetLine callback.
- + global->Set(v8::String::New("getline"), v8::FunctionTemplate::New(GetLine));
- // Bind the global 'read' function to the C++ Read callback.
- global->Set(v8::String::New("read"), v8::FunctionTemplate::New(Read));
- // Bind the global 'load' function to the C++ Load callback.
- @@ -203,6 +209,7 @@
- rewind(file);
- char* chars = new char[size + 1];
- + assert(chars);
- chars[size] = '\0';
- for (int i = 0; i < size;) {
- int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
- @@ -214,7 +221,44 @@
- return result;
- }
- +// Get one char from 'stdin'
- +v8::Handle<v8::Value> GetChar(const v8::Arguments& args) {
- + (void)args;
- + if ( feof(stdin) ) return v8::Handle<v8::String>();
- +
- + char chars[2] = {0};
- + int read = fread(&chars[0], 1, 1, stdin);
- + if ( read != 1 ) return v8::Handle<v8::String>();
- +
- + return v8::String::New(&chars[0], 1);
- +}
- +
- +// Read one line from 'stdin'
- +v8::Handle<v8::Value> GetLine(const v8::Arguments& args) {
- + (void)args;
- +
- + if ( feof(stdin) ) return v8::Handle<v8::String>();
- +
- + const int size = 4096;
- + char* chars = new char[size + 1];
- + assert(chars);
- +
- + char *line = fgets(chars, size, stdin);
- + if ( !line ) return v8::Handle<v8::String>();
- +
- + int len = strlen(line);
- + if ( len > 0 && line[len-1] == '\n' ) {
- + len = len-1;
- + line[len] = '\0';
- + }
- +
- + v8::Handle<v8::String> result = v8::String::New(line, len);
- + delete[] chars;
- +
- + return result;
- +}
- +
- // Process remaining command line arguments and execute files
- int RunMain(int argc, char* argv[]) {
- for (int i = 1; i < argc; i++) {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement