v8-shell.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // Support for translateCode added by Alex Warth <alex@vpri.org>
  2. // Copyright 2009 the V8 project authors. All rights reserved.
  3. // Redistribution and use in source and binary forms, with or without
  4. // modification, are permitted provided that the following conditions are
  5. // met:
  6. //
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above
  10. // copyright notice, this list of conditions and the following
  11. // disclaimer in the documentation and/or other materials provided
  12. // with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its
  14. // contributors may be used to endorse or promote products derived
  15. // from this software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. #include <v8.h>
  29. #include <fcntl.h>
  30. #include <string.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. v8::Handle<v8::Context> context;
  34. void RunShell(v8::Handle<v8::Context> context);
  35. bool ExecuteString(v8::Handle<v8::String> source,
  36. v8::Handle<v8::Value> name,
  37. bool print_result,
  38. bool report_exceptions);
  39. v8::Handle<v8::Value> Print(const v8::Arguments& args);
  40. v8::Handle<v8::Value> Read(const v8::Arguments& args);
  41. v8::Handle<v8::Value> Load(const v8::Arguments& args);
  42. v8::Handle<v8::Value> Quit(const v8::Arguments& args);
  43. v8::Handle<v8::Value> Version(const v8::Arguments& args);
  44. v8::Handle<v8::String> ReadFile(const char* name);
  45. void ReportException(v8::TryCatch* handler);
  46. int RunMain(int argc, char* argv[]) {
  47. v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
  48. v8::HandleScope handle_scope;
  49. // Create a template for the global object.
  50. v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
  51. // Bind the global 'print' function to the C++ Print callback.
  52. global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print));
  53. // Bind the global 'read' function to the C++ Read callback.
  54. global->Set(v8::String::New("read"), v8::FunctionTemplate::New(Read));
  55. // Bind the global 'load' function to the C++ Load callback.
  56. global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load));
  57. // Bind the 'quit' function
  58. global->Set(v8::String::New("quit"), v8::FunctionTemplate::New(Quit));
  59. // Bind the 'version' function
  60. global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version));
  61. // Create a new execution environment containing the built-in
  62. // functions
  63. context = v8::Context::New(NULL, global);
  64. // Enter the newly created execution environment.
  65. v8::Context::Scope context_scope(context);
  66. bool run_shell = (argc == 1);
  67. for (int i = 1; i < argc; i++) {
  68. const char* str = argv[i];
  69. if (strcmp(str, "--shell") == 0) {
  70. run_shell = true;
  71. } else if (strcmp(str, "-f") == 0) {
  72. // Ignore any -f flags for compatibility with the other stand-
  73. // alone JavaScript engines.
  74. continue;
  75. } else if (strncmp(str, "--", 2) == 0) {
  76. printf("Warning: unknown flag %s.\nTry --help for options\n", str);
  77. } else if (strcmp(str, "-e") == 0 && i + 1 < argc) {
  78. // Execute argument given to -e option directly
  79. v8::HandleScope handle_scope;
  80. v8::Handle<v8::String> file_name = v8::String::New("unnamed");
  81. v8::Handle<v8::String> source = v8::String::New(argv[i + 1]);
  82. if (!ExecuteString(source, file_name, false, true))
  83. return 1;
  84. i++;
  85. } else {
  86. // Use all other arguments as names of files to load and run.
  87. v8::HandleScope handle_scope;
  88. v8::Handle<v8::String> file_name = v8::String::New(str);
  89. v8::Handle<v8::String> source = ReadFile(str);
  90. if (source.IsEmpty()) {
  91. printf("Error reading '%s'\n", str);
  92. return 1;
  93. }
  94. if (!ExecuteString(source, file_name, false, true))
  95. return 1;
  96. }
  97. }
  98. if (run_shell) RunShell(context);
  99. return 0;
  100. }
  101. int main(int argc, char* argv[]) {
  102. int result = RunMain(argc, argv);
  103. v8::V8::Dispose();
  104. return result;
  105. }
  106. // Extracts a C string from a V8 Utf8Value.
  107. const char* ToCString(const v8::String::Utf8Value& value) {
  108. return *value ? *value : "<string conversion failed>";
  109. }
  110. // The callback that is invoked by v8 whenever the JavaScript 'print'
  111. // function is called. Prints its arguments on stdout separated by
  112. // spaces and ending with a newline.
  113. v8::Handle<v8::Value> Print(const v8::Arguments& args) {
  114. bool first = true;
  115. for (int i = 0; i < args.Length(); i++) {
  116. v8::HandleScope handle_scope;
  117. if (first) {
  118. first = false;
  119. } else {
  120. printf(" ");
  121. }
  122. v8::String::Utf8Value str(args[i]);
  123. const char* cstr = ToCString(str);
  124. printf("%s", cstr);
  125. }
  126. printf("\n");
  127. fflush(stdout);
  128. return v8::Undefined();
  129. }
  130. // The callback that is invoked by v8 whenever the JavaScript 'read'
  131. // function is called. This function loads the content of the file named in
  132. // the argument into a JavaScript string.
  133. v8::Handle<v8::Value> Read(const v8::Arguments& args) {
  134. if (args.Length() != 1) {
  135. return v8::ThrowException(v8::String::New("Bad parameters"));
  136. }
  137. v8::String::Utf8Value file(args[0]);
  138. if (*file == NULL) {
  139. return v8::ThrowException(v8::String::New("Error loading file"));
  140. }
  141. v8::Handle<v8::String> source = ReadFile(*file);
  142. if (source.IsEmpty()) {
  143. return v8::ThrowException(v8::String::New("Error loading file"));
  144. }
  145. return source;
  146. }
  147. // The callback that is invoked by v8 whenever the JavaScript 'load'
  148. // function is called. Loads, compiles and executes its argument
  149. // JavaScript file.
  150. v8::Handle<v8::Value> Load(const v8::Arguments& args) {
  151. for (int i = 0; i < args.Length(); i++) {
  152. v8::HandleScope handle_scope;
  153. v8::String::Utf8Value file(args[i]);
  154. if (*file == NULL) {
  155. return v8::ThrowException(v8::String::New("Error loading file"));
  156. }
  157. v8::Handle<v8::String> source = ReadFile(*file);
  158. if (source.IsEmpty()) {
  159. return v8::ThrowException(v8::String::New("Error loading file"));
  160. }
  161. if (!ExecuteString(source, v8::String::New(*file), false, false)) {
  162. return v8::ThrowException(v8::String::New("Error executing file"));
  163. }
  164. }
  165. return v8::Undefined();
  166. }
  167. // The callback that is invoked by v8 whenever the JavaScript 'quit'
  168. // function is called. Quits.
  169. v8::Handle<v8::Value> Quit(const v8::Arguments& args) {
  170. // If not arguments are given args[0] will yield undefined which
  171. // converts to the integer value 0.
  172. int exit_code = args[0]->Int32Value();
  173. exit(exit_code);
  174. return v8::Undefined();
  175. }
  176. v8::Handle<v8::Value> Version(const v8::Arguments& args) {
  177. return v8::String::New(v8::V8::GetVersion());
  178. }
  179. // Reads a file into a v8 string.
  180. v8::Handle<v8::String> ReadFile(const char* name) {
  181. FILE* file = fopen(name, "rb");
  182. if (file == NULL) return v8::Handle<v8::String>();
  183. fseek(file, 0, SEEK_END);
  184. int size = ftell(file);
  185. rewind(file);
  186. char* chars = new char[size + 1];
  187. chars[size] = '\0';
  188. for (int i = 0; i < size;) {
  189. int read = fread(&chars[i], 1, size - i, file);
  190. i += read;
  191. }
  192. fclose(file);
  193. v8::Handle<v8::String> result = v8::String::New(chars, size);
  194. delete[] chars;
  195. return result;
  196. }
  197. // The read-eval-execute loop of the shell.
  198. void RunShell(v8::Handle<v8::Context> context) {
  199. printf("V8 version %s\n", v8::V8::GetVersion());
  200. static const int kBufferSize = 256;
  201. while (true) {
  202. char buffer[kBufferSize];
  203. printf("> ");
  204. char* str = fgets(buffer, kBufferSize, stdin);
  205. if (str == NULL) break;
  206. v8::HandleScope handle_scope;
  207. ExecuteString(v8::String::New(str),
  208. v8::String::New("(shell)"),
  209. true,
  210. true);
  211. }
  212. printf("\n");
  213. }
  214. bool iExecuteString(v8::Handle<v8::String> source,
  215. v8::Handle<v8::Value> name,
  216. v8::Handle<v8::Value> &result,
  217. bool report_exceptions) {
  218. v8::HandleScope handle_scope;
  219. v8::TryCatch try_catch;
  220. v8::Handle<v8::Script> script = v8::Script::Compile(source, name);
  221. if (script.IsEmpty()) {
  222. // Print errors that happened during compilation.
  223. if (report_exceptions)
  224. ReportException(&try_catch);
  225. return false;
  226. } else {
  227. result = script->Run();
  228. if (result.IsEmpty()) {
  229. // Print errors that happened during execution.
  230. if (report_exceptions)
  231. ReportException(&try_catch);
  232. return false;
  233. } else {
  234. return true;
  235. }
  236. }
  237. }
  238. // Executes a string within the current v8 context.
  239. bool ExecuteString(v8::Handle<v8::String> source,
  240. v8::Handle<v8::Value> name,
  241. bool print_result,
  242. bool report_exceptions) {
  243. v8::Handle<v8::Value> result;
  244. context->Global()->Set(v8::String::New("__xy7z"), source);
  245. bool ok = true;
  246. ok = iExecuteString(v8::String::New("if ((function() { return this.translateCode })()) __xy7z = translateCode(__xy7z)"),
  247. name, result, false);
  248. if (ok)
  249. ok = iExecuteString(v8::String::New("__xy7z = eval(__xy7z)"), name, result, report_exceptions);
  250. else {
  251. iExecuteString(v8::String::New("translateCode = undefined"), name, result, report_exceptions);
  252. printf("error: translateCode failed and has been set to undefined in order to make the shell operational again\n");
  253. }
  254. if (ok && print_result && !result->IsUndefined())
  255. iExecuteString(v8::String::New("print(__xy7z)"), name, result, report_exceptions);
  256. return ok;
  257. }
  258. void ReportException(v8::TryCatch* try_catch) {
  259. v8::HandleScope handle_scope;
  260. v8::String::Utf8Value exception(try_catch->Exception());
  261. const char* exception_string = ToCString(exception);
  262. v8::Handle<v8::Message> message = try_catch->Message();
  263. if (message.IsEmpty()) {
  264. // V8 didn't provide any extra information about this error; just
  265. // print the exception.
  266. printf("%s\n", exception_string);
  267. } else {
  268. // Print (filename):(line number): (message).
  269. v8::String::Utf8Value filename(message->GetScriptResourceName());
  270. const char* filename_string = ToCString(filename);
  271. int linenum = message->GetLineNumber();
  272. printf("%s:%i: %s\n", filename_string, linenum, exception_string);
  273. // Print line of source code.
  274. v8::String::Utf8Value sourceline(message->GetSourceLine());
  275. const char* sourceline_string = ToCString(sourceline);
  276. printf("%s\n", sourceline_string);
  277. // Print wavy underline (GetUnderline is deprecated).
  278. int start = message->GetStartColumn();
  279. for (int i = 0; i < start; i++) {
  280. printf(" ");
  281. }
  282. int end = message->GetEndColumn();
  283. for (int i = start; i < end; i++) {
  284. printf("^");
  285. }
  286. printf("\n");
  287. }
  288. }