Advertisement
Guest User

v8-shell-getchar+getline-path

a guest
Feb 14th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. Index: test/shell/getline.js
  2. ===================================================================
  3. --- test/shell/getline.js   (revision 0)
  4. +++ test/shell/getline.js   (working copy)
  5. @@ -0,0 +1,3 @@
  6. +
  7. +var line = getline();
  8. +print('line:\''+line+'\'');
  9. Index: test/shell/getchar.js
  10. ===================================================================
  11. --- test/shell/getchar.js   (revision 0)
  12. +++ test/shell/getchar.js   (working copy)
  13. @@ -0,0 +1,3 @@
  14. +
  15. +var char = getchar();
  16. +print('char:\''+char+'\'');
  17. Index: samples/shell.cc
  18. ===================================================================
  19. --- samples/shell.cc    (revision 13664)
  20. +++ samples/shell.cc    (working copy)
  21. @@ -58,6 +58,8 @@
  22.  v8::Handle<v8::Value> Quit(const v8::Arguments& args);
  23.  v8::Handle<v8::Value> Version(const v8::Arguments& args);
  24.  v8::Handle<v8::String> ReadFile(const char* name);
  25. +v8::Handle<v8::Value> GetChar(const v8::Arguments& args);
  26. +v8::Handle<v8::Value> GetLine(const v8::Arguments& args);
  27.  void ReportException(v8::TryCatch* handler);
  28.  
  29.  
  30. @@ -99,6 +101,10 @@
  31.    v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
  32.    // Bind the global 'print' function to the C++ Print callback.
  33.    global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print));
  34. +  // Bind the global 'getchar' function to the C++ GetChar callback.
  35. +  global->Set(v8::String::New("getchar"), v8::FunctionTemplate::New(GetChar));
  36. +  // Bind the global 'getline' function to the C++ GetLine callback.
  37. +  global->Set(v8::String::New("getline"), v8::FunctionTemplate::New(GetLine));
  38.    // Bind the global 'read' function to the C++ Read callback.
  39.    global->Set(v8::String::New("read"), v8::FunctionTemplate::New(Read));
  40.    // Bind the global 'load' function to the C++ Load callback.
  41. @@ -203,6 +209,7 @@
  42.    rewind(file);
  43.  
  44.    char* chars = new char[size + 1];
  45. +  assert(chars);
  46.    chars[size] = '\0';
  47.    for (int i = 0; i < size;) {
  48.      int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
  49. @@ -214,7 +221,44 @@
  50.    return result;
  51.  }
  52.  
  53. +// Get one char from 'stdin'
  54. +v8::Handle<v8::Value> GetChar(const v8::Arguments& args) {
  55. +  (void)args;
  56.  
  57. +  if ( feof(stdin) ) return v8::Handle<v8::String>();
  58. +
  59. +  char chars[2] = {0};
  60. +  int read = fread(&chars[0], 1, 1, stdin);
  61. +  if ( read != 1 ) return v8::Handle<v8::String>();
  62. +
  63. +  return v8::String::New(&chars[0], 1);
  64. +}
  65. +
  66. +// Read one line from 'stdin'
  67. +v8::Handle<v8::Value> GetLine(const v8::Arguments& args) {
  68. +  (void)args;
  69. +
  70. +  if ( feof(stdin) ) return v8::Handle<v8::String>();
  71. +
  72. +  const int size = 4096;
  73. +  char* chars = new char[size + 1];
  74. +  assert(chars);
  75. +
  76. +  char *line = fgets(chars, size, stdin);
  77. +  if ( !line ) return v8::Handle<v8::String>();
  78. +
  79. +  int len = strlen(line);
  80. +  if ( len > 0 && line[len-1] == '\n' ) {
  81. +    len = len-1;
  82. +    line[len] = '\0';
  83. +  }
  84. +
  85. +  v8::Handle<v8::String> result = v8::String::New(line, len);
  86. +  delete[] chars;
  87. +
  88. +  return result;
  89. +}
  90. +
  91.  // Process remaining command line arguments and execute files
  92.  int RunMain(int argc, char* argv[]) {
  93.    for (int i = 1; i < argc; i++) {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement