| OLD | NEW |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 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 19 matching lines...) Expand all Loading... |
| 30 #include <string.h> | 30 #include <string.h> |
| 31 #include <stdio.h> | 31 #include <stdio.h> |
| 32 #include <stdlib.h> | 32 #include <stdlib.h> |
| 33 | 33 |
| 34 | 34 |
| 35 void RunShell(v8::Handle<v8::Context> context); | 35 void RunShell(v8::Handle<v8::Context> context); |
| 36 bool ExecuteString(v8::Handle<v8::String> source, | 36 bool ExecuteString(v8::Handle<v8::String> source, |
| 37 v8::Handle<v8::Value> name, | 37 v8::Handle<v8::Value> name, |
| 38 bool print_result, | 38 bool print_result, |
| 39 bool report_exceptions); | 39 bool report_exceptions); |
| 40 v8::Handle<v8::Value> PrintToInteger(const v8::Arguments& args); |
| 40 v8::Handle<v8::Value> Print(const v8::Arguments& args); | 41 v8::Handle<v8::Value> Print(const v8::Arguments& args); |
| 41 v8::Handle<v8::Value> Read(const v8::Arguments& args); | 42 v8::Handle<v8::Value> Read(const v8::Arguments& args); |
| 42 v8::Handle<v8::Value> Load(const v8::Arguments& args); | 43 v8::Handle<v8::Value> Load(const v8::Arguments& args); |
| 43 v8::Handle<v8::Value> Quit(const v8::Arguments& args); | 44 v8::Handle<v8::Value> Quit(const v8::Arguments& args); |
| 44 v8::Handle<v8::Value> Version(const v8::Arguments& args); | 45 v8::Handle<v8::Value> Version(const v8::Arguments& args); |
| 45 v8::Handle<v8::String> ReadFile(const char* name); | 46 v8::Handle<v8::String> ReadFile(const char* name); |
| 46 void ReportException(v8::TryCatch* handler); | 47 void ReportException(v8::TryCatch* handler); |
| 47 | 48 |
| 48 | 49 |
| 49 int RunMain(int argc, char* argv[]) { | 50 int RunMain(int argc, char* argv[]) { |
| 50 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 51 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 51 v8::HandleScope handle_scope; | 52 v8::HandleScope handle_scope; |
| 52 // Create a template for the global object. | 53 // Create a template for the global object. |
| 53 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 54 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| 54 // Bind the global 'print' function to the C++ Print callback. | 55 // Bind the global 'print' function to the C++ Print callback. |
| 55 global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print)); | 56 global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print)); |
| 56 // Bind the global 'read' function to the C++ Read callback. | 57 global->Set(v8::String::New("print2int"), v8::FunctionTemplate::New(PrintToInteg
er)); |
| 58 // Bind the global 'read' function to the C++ Read callback. |
| 57 global->Set(v8::String::New("read"), v8::FunctionTemplate::New(Read)); | 59 global->Set(v8::String::New("read"), v8::FunctionTemplate::New(Read)); |
| 58 // Bind the global 'load' function to the C++ Load callback. | 60 // Bind the global 'load' function to the C++ Load callback. |
| 59 global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load)); | 61 global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load)); |
| 60 // Bind the 'quit' function | 62 // Bind the 'quit' function |
| 61 global->Set(v8::String::New("quit"), v8::FunctionTemplate::New(Quit)); | 63 global->Set(v8::String::New("quit"), v8::FunctionTemplate::New(Quit)); |
| 62 // Bind the 'version' function | 64 // Bind the 'version' function |
| 63 global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version)); | 65 global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version)); |
| 64 // Create a new execution environment containing the built-in | 66 // Create a new execution environment containing the built-in |
| 65 // functions | 67 // functions |
| 66 v8::Handle<v8::Context> context = v8::Context::New(NULL, global); | 68 v8::Handle<v8::Context> context = v8::Context::New(NULL, global); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 v8::String::Utf8Value str(args[i]); | 133 v8::String::Utf8Value str(args[i]); |
| 132 const char* cstr = ToCString(str); | 134 const char* cstr = ToCString(str); |
| 133 printf("%s", cstr); | 135 printf("%s", cstr); |
| 134 } | 136 } |
| 135 printf("\n"); | 137 printf("\n"); |
| 136 fflush(stdout); | 138 fflush(stdout); |
| 137 return v8::Undefined(); | 139 return v8::Undefined(); |
| 138 } | 140 } |
| 139 | 141 |
| 140 | 142 |
| 143 v8::Handle<v8::Value> PrintToInteger(const v8::Arguments& args) { |
| 144 v8::HandleScope handle_scope; |
| 145 v8::String::Utf8Value str(args[0]); |
| 146 const char* cstr = ToCString(str); |
| 147 printf("%s -> %d\n", cstr, args[0]->ToInt32()->Value()); |
| 148 fflush(stdout); |
| 149 return v8::Undefined(); |
| 150 } |
| 151 |
| 152 |
| 141 // The callback that is invoked by v8 whenever the JavaScript 'read' | 153 // The callback that is invoked by v8 whenever the JavaScript 'read' |
| 142 // function is called. This function loads the content of the file named in | 154 // function is called. This function loads the content of the file named in |
| 143 // the argument into a JavaScript string. | 155 // the argument into a JavaScript string. |
| 144 v8::Handle<v8::Value> Read(const v8::Arguments& args) { | 156 v8::Handle<v8::Value> Read(const v8::Arguments& args) { |
| 145 if (args.Length() != 1) { | 157 if (args.Length() != 1) { |
| 146 return v8::ThrowException(v8::String::New("Bad parameters")); | 158 return v8::ThrowException(v8::String::New("Bad parameters")); |
| 147 } | 159 } |
| 148 v8::String::Utf8Value file(args[0]); | 160 v8::String::Utf8Value file(args[0]); |
| 149 if (*file == NULL) { | 161 if (*file == NULL) { |
| 150 return v8::ThrowException(v8::String::New("Error loading file")); | 162 return v8::ThrowException(v8::String::New("Error loading file")); |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 printf("^"); | 311 printf("^"); |
| 300 } | 312 } |
| 301 printf("\n"); | 313 printf("\n"); |
| 302 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); | 314 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
| 303 if (stack_trace.length() > 0) { | 315 if (stack_trace.length() > 0) { |
| 304 const char* stack_trace_string = ToCString(stack_trace); | 316 const char* stack_trace_string = ToCString(stack_trace); |
| 305 printf("%s\n", stack_trace_string); | 317 printf("%s\n", stack_trace_string); |
| 306 } | 318 } |
| 307 } | 319 } |
| 308 } | 320 } |
| OLD | NEW |