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 20 matching lines...) Expand all Loading... |
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> Print(const v8::Arguments& args); | 40 v8::Handle<v8::Value> Print(const v8::Arguments& args); |
| 41 v8::Handle<v8::Value> Read(const v8::Arguments& args); |
41 v8::Handle<v8::Value> Load(const v8::Arguments& args); | 42 v8::Handle<v8::Value> Load(const v8::Arguments& args); |
42 v8::Handle<v8::Value> Quit(const v8::Arguments& args); | 43 v8::Handle<v8::Value> Quit(const v8::Arguments& args); |
43 v8::Handle<v8::Value> Version(const v8::Arguments& args); | 44 v8::Handle<v8::Value> Version(const v8::Arguments& args); |
44 v8::Handle<v8::String> ReadFile(const char* name); | 45 v8::Handle<v8::String> ReadFile(const char* name); |
45 void ReportException(v8::TryCatch* handler); | 46 void ReportException(v8::TryCatch* handler); |
46 | 47 |
47 | 48 |
48 int RunMain(int argc, char* argv[]) { | 49 int RunMain(int argc, char* argv[]) { |
49 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 50 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
50 v8::HandleScope handle_scope; | 51 v8::HandleScope handle_scope; |
51 // Create a template for the global object. | 52 // Create a template for the global object. |
52 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 53 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
53 // Bind the global 'print' function to the C++ Print callback. | 54 // Bind the global 'print' function to the C++ Print callback. |
54 global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print)); | 55 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("read"), v8::FunctionTemplate::New(Read)); |
55 // Bind the global 'load' function to the C++ Load callback. | 58 // Bind the global 'load' function to the C++ Load callback. |
56 global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load)); | 59 global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load)); |
57 // Bind the 'quit' function | 60 // Bind the 'quit' function |
58 global->Set(v8::String::New("quit"), v8::FunctionTemplate::New(Quit)); | 61 global->Set(v8::String::New("quit"), v8::FunctionTemplate::New(Quit)); |
59 // Bind the 'version' function | 62 // Bind the 'version' function |
60 global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version)); | 63 global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version)); |
61 // Create a new execution environment containing the built-in | 64 // Create a new execution environment containing the built-in |
62 // functions | 65 // functions |
63 v8::Handle<v8::Context> context = v8::Context::New(NULL, global); | 66 v8::Handle<v8::Context> context = v8::Context::New(NULL, global); |
64 // Enter the newly created execution environment. | 67 // Enter the newly created execution environment. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 v8::String::Utf8Value str(args[i]); | 131 v8::String::Utf8Value str(args[i]); |
129 const char* cstr = ToCString(str); | 132 const char* cstr = ToCString(str); |
130 printf("%s", cstr); | 133 printf("%s", cstr); |
131 } | 134 } |
132 printf("\n"); | 135 printf("\n"); |
133 fflush(stdout); | 136 fflush(stdout); |
134 return v8::Undefined(); | 137 return v8::Undefined(); |
135 } | 138 } |
136 | 139 |
137 | 140 |
| 141 // 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 |
| 143 // the argument into a JavaScript string. |
| 144 v8::Handle<v8::Value> Read(const v8::Arguments& args) { |
| 145 if (args.Length() != 1) { |
| 146 return v8::ThrowException(v8::String::New("Bad parameters")); |
| 147 } |
| 148 v8::String::Utf8Value file(args[0]); |
| 149 if (*file == NULL) { |
| 150 return v8::ThrowException(v8::String::New("Error loading file")); |
| 151 } |
| 152 v8::Handle<v8::String> source = ReadFile(*file); |
| 153 if (source.IsEmpty()) { |
| 154 return v8::ThrowException(v8::String::New("Error loading file")); |
| 155 } |
| 156 return source; |
| 157 } |
| 158 |
| 159 |
138 // The callback that is invoked by v8 whenever the JavaScript 'load' | 160 // The callback that is invoked by v8 whenever the JavaScript 'load' |
139 // function is called. Loads, compiles and executes its argument | 161 // function is called. Loads, compiles and executes its argument |
140 // JavaScript file. | 162 // JavaScript file. |
141 v8::Handle<v8::Value> Load(const v8::Arguments& args) { | 163 v8::Handle<v8::Value> Load(const v8::Arguments& args) { |
142 for (int i = 0; i < args.Length(); i++) { | 164 for (int i = 0; i < args.Length(); i++) { |
143 v8::HandleScope handle_scope; | 165 v8::HandleScope handle_scope; |
144 v8::String::Utf8Value file(args[i]); | 166 v8::String::Utf8Value file(args[i]); |
145 if (*file == NULL) { | 167 if (*file == NULL) { |
146 return v8::ThrowException(v8::String::New("Error loading file")); | 168 return v8::ThrowException(v8::String::New("Error loading file")); |
147 } | 169 } |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 for (int i = 0; i < start; i++) { | 294 for (int i = 0; i < start; i++) { |
273 printf(" "); | 295 printf(" "); |
274 } | 296 } |
275 int end = message->GetEndColumn(); | 297 int end = message->GetEndColumn(); |
276 for (int i = start; i < end; i++) { | 298 for (int i = start; i < end; i++) { |
277 printf("^"); | 299 printf("^"); |
278 } | 300 } |
279 printf("\n"); | 301 printf("\n"); |
280 } | 302 } |
281 } | 303 } |
OLD | NEW |