| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 bool ExecuteString(v8::Isolate* isolate, | 51 bool ExecuteString(v8::Isolate* isolate, |
| 52 v8::Handle<v8::String> source, | 52 v8::Handle<v8::String> source, |
| 53 v8::Handle<v8::Value> name, | 53 v8::Handle<v8::Value> name, |
| 54 bool print_result, | 54 bool print_result, |
| 55 bool report_exceptions); | 55 bool report_exceptions); |
| 56 void Print(const v8::FunctionCallbackInfo<v8::Value>& args); | 56 void Print(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 57 void Read(const v8::FunctionCallbackInfo<v8::Value>& args); | 57 void Read(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 58 void Load(const v8::FunctionCallbackInfo<v8::Value>& args); | 58 void Load(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 59 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args); | 59 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 60 void Version(const v8::FunctionCallbackInfo<v8::Value>& args); | 60 void Version(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 61 v8::Handle<v8::String> ReadFile(const char* name); | 61 v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name); |
| 62 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); | 62 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); |
| 63 | 63 |
| 64 | 64 |
| 65 static bool run_shell; | 65 static bool run_shell; |
| 66 | 66 |
| 67 | 67 |
| 68 int main(int argc, char* argv[]) { | 68 int main(int argc, char* argv[]) { |
| 69 v8::V8::InitializeICU(); | 69 v8::V8::InitializeICU(); |
| 70 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 70 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
| 71 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 71 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 93 return *value ? *value : "<string conversion failed>"; | 93 return *value ? *value : "<string conversion failed>"; |
| 94 } | 94 } |
| 95 | 95 |
| 96 | 96 |
| 97 // Creates a new execution environment containing the built-in | 97 // Creates a new execution environment containing the built-in |
| 98 // functions. | 98 // functions. |
| 99 v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate) { | 99 v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate) { |
| 100 // Create a template for the global object. | 100 // Create a template for the global object. |
| 101 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 101 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| 102 // Bind the global 'print' function to the C++ Print callback. | 102 // Bind the global 'print' function to the C++ Print callback. |
| 103 global->Set(v8::String::New("print"), v8::FunctionTemplate::New(Print)); | 103 global->Set(v8::String::NewFromUtf8(isolate, "print"), |
| 104 v8::FunctionTemplate::New(Print)); |
| 104 // Bind the global 'read' function to the C++ Read callback. | 105 // Bind the global 'read' function to the C++ Read callback. |
| 105 global->Set(v8::String::New("read"), v8::FunctionTemplate::New(Read)); | 106 global->Set(v8::String::NewFromUtf8(isolate, "read"), |
| 107 v8::FunctionTemplate::New(Read)); |
| 106 // Bind the global 'load' function to the C++ Load callback. | 108 // Bind the global 'load' function to the C++ Load callback. |
| 107 global->Set(v8::String::New("load"), v8::FunctionTemplate::New(Load)); | 109 global->Set(v8::String::NewFromUtf8(isolate, "load"), |
| 110 v8::FunctionTemplate::New(Load)); |
| 108 // Bind the 'quit' function | 111 // Bind the 'quit' function |
| 109 global->Set(v8::String::New("quit"), v8::FunctionTemplate::New(Quit)); | 112 global->Set(v8::String::NewFromUtf8(isolate, "quit"), |
| 113 v8::FunctionTemplate::New(Quit)); |
| 110 // Bind the 'version' function | 114 // Bind the 'version' function |
| 111 global->Set(v8::String::New("version"), v8::FunctionTemplate::New(Version)); | 115 global->Set(v8::String::NewFromUtf8(isolate, "version"), |
| 116 v8::FunctionTemplate::New(Version)); |
| 112 | 117 |
| 113 return v8::Context::New(isolate, NULL, global); | 118 return v8::Context::New(isolate, NULL, global); |
| 114 } | 119 } |
| 115 | 120 |
| 116 | 121 |
| 117 // The callback that is invoked by v8 whenever the JavaScript 'print' | 122 // The callback that is invoked by v8 whenever the JavaScript 'print' |
| 118 // function is called. Prints its arguments on stdout separated by | 123 // function is called. Prints its arguments on stdout separated by |
| 119 // spaces and ending with a newline. | 124 // spaces and ending with a newline. |
| 120 void Print(const v8::FunctionCallbackInfo<v8::Value>& args) { | 125 void Print(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 121 bool first = true; | 126 bool first = true; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 134 fflush(stdout); | 139 fflush(stdout); |
| 135 } | 140 } |
| 136 | 141 |
| 137 | 142 |
| 138 // The callback that is invoked by v8 whenever the JavaScript 'read' | 143 // The callback that is invoked by v8 whenever the JavaScript 'read' |
| 139 // function is called. This function loads the content of the file named in | 144 // function is called. This function loads the content of the file named in |
| 140 // the argument into a JavaScript string. | 145 // the argument into a JavaScript string. |
| 141 void Read(const v8::FunctionCallbackInfo<v8::Value>& args) { | 146 void Read(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 142 if (args.Length() != 1) { | 147 if (args.Length() != 1) { |
| 143 args.GetIsolate()->ThrowException( | 148 args.GetIsolate()->ThrowException( |
| 144 v8::String::New("Bad parameters")); | 149 v8::String::NewFromUtf8(args.GetIsolate(), "Bad parameters")); |
| 145 return; | 150 return; |
| 146 } | 151 } |
| 147 v8::String::Utf8Value file(args[0]); | 152 v8::String::Utf8Value file(args[0]); |
| 148 if (*file == NULL) { | 153 if (*file == NULL) { |
| 149 args.GetIsolate()->ThrowException( | 154 args.GetIsolate()->ThrowException( |
| 150 v8::String::New("Error loading file")); | 155 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file")); |
| 151 return; | 156 return; |
| 152 } | 157 } |
| 153 v8::Handle<v8::String> source = ReadFile(*file); | 158 v8::Handle<v8::String> source = ReadFile(args.GetIsolate(), *file); |
| 154 if (source.IsEmpty()) { | 159 if (source.IsEmpty()) { |
| 155 args.GetIsolate()->ThrowException( | 160 args.GetIsolate()->ThrowException( |
| 156 v8::String::New("Error loading file")); | 161 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file")); |
| 157 return; | 162 return; |
| 158 } | 163 } |
| 159 args.GetReturnValue().Set(source); | 164 args.GetReturnValue().Set(source); |
| 160 } | 165 } |
| 161 | 166 |
| 162 | 167 |
| 163 // The callback that is invoked by v8 whenever the JavaScript 'load' | 168 // The callback that is invoked by v8 whenever the JavaScript 'load' |
| 164 // function is called. Loads, compiles and executes its argument | 169 // function is called. Loads, compiles and executes its argument |
| 165 // JavaScript file. | 170 // JavaScript file. |
| 166 void Load(const v8::FunctionCallbackInfo<v8::Value>& args) { | 171 void Load(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 167 for (int i = 0; i < args.Length(); i++) { | 172 for (int i = 0; i < args.Length(); i++) { |
| 168 v8::HandleScope handle_scope(args.GetIsolate()); | 173 v8::HandleScope handle_scope(args.GetIsolate()); |
| 169 v8::String::Utf8Value file(args[i]); | 174 v8::String::Utf8Value file(args[i]); |
| 170 if (*file == NULL) { | 175 if (*file == NULL) { |
| 171 args.GetIsolate()->ThrowException( | 176 args.GetIsolate()->ThrowException( |
| 172 v8::String::New("Error loading file")); | 177 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file")); |
| 173 return; | 178 return; |
| 174 } | 179 } |
| 175 v8::Handle<v8::String> source = ReadFile(*file); | 180 v8::Handle<v8::String> source = ReadFile(args.GetIsolate(), *file); |
| 176 if (source.IsEmpty()) { | 181 if (source.IsEmpty()) { |
| 177 args.GetIsolate()->ThrowException( | 182 args.GetIsolate()->ThrowException( |
| 178 v8::String::New("Error loading file")); | 183 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file")); |
| 179 return; | 184 return; |
| 180 } | 185 } |
| 181 if (!ExecuteString(args.GetIsolate(), | 186 if (!ExecuteString(args.GetIsolate(), |
| 182 source, | 187 source, |
| 183 v8::String::New(*file), | 188 v8::String::NewFromUtf8(args.GetIsolate(), *file), |
| 184 false, | 189 false, |
| 185 false)) { | 190 false)) { |
| 186 args.GetIsolate()->ThrowException( | 191 args.GetIsolate()->ThrowException( |
| 187 v8::String::New("Error executing file")); | 192 v8::String::NewFromUtf8(args.GetIsolate(), "Error executing file")); |
| 188 return; | 193 return; |
| 189 } | 194 } |
| 190 } | 195 } |
| 191 } | 196 } |
| 192 | 197 |
| 193 | 198 |
| 194 // The callback that is invoked by v8 whenever the JavaScript 'quit' | 199 // The callback that is invoked by v8 whenever the JavaScript 'quit' |
| 195 // function is called. Quits. | 200 // function is called. Quits. |
| 196 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { | 201 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 197 // If not arguments are given args[0] will yield undefined which | 202 // If not arguments are given args[0] will yield undefined which |
| 198 // converts to the integer value 0. | 203 // converts to the integer value 0. |
| 199 int exit_code = args[0]->Int32Value(); | 204 int exit_code = args[0]->Int32Value(); |
| 200 fflush(stdout); | 205 fflush(stdout); |
| 201 fflush(stderr); | 206 fflush(stderr); |
| 202 exit(exit_code); | 207 exit(exit_code); |
| 203 } | 208 } |
| 204 | 209 |
| 205 | 210 |
| 206 void Version(const v8::FunctionCallbackInfo<v8::Value>& args) { | 211 void Version(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 207 args.GetReturnValue().Set(v8::String::New(v8::V8::GetVersion())); | 212 args.GetReturnValue().Set( |
| 213 v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion())); |
| 208 } | 214 } |
| 209 | 215 |
| 210 | 216 |
| 211 // Reads a file into a v8 string. | 217 // Reads a file into a v8 string. |
| 212 v8::Handle<v8::String> ReadFile(const char* name) { | 218 v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name) { |
| 213 FILE* file = fopen(name, "rb"); | 219 FILE* file = fopen(name, "rb"); |
| 214 if (file == NULL) return v8::Handle<v8::String>(); | 220 if (file == NULL) return v8::Handle<v8::String>(); |
| 215 | 221 |
| 216 fseek(file, 0, SEEK_END); | 222 fseek(file, 0, SEEK_END); |
| 217 int size = ftell(file); | 223 int size = ftell(file); |
| 218 rewind(file); | 224 rewind(file); |
| 219 | 225 |
| 220 char* chars = new char[size + 1]; | 226 char* chars = new char[size + 1]; |
| 221 chars[size] = '\0'; | 227 chars[size] = '\0'; |
| 222 for (int i = 0; i < size;) { | 228 for (int i = 0; i < size;) { |
| 223 int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); | 229 int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); |
| 224 i += read; | 230 i += read; |
| 225 } | 231 } |
| 226 fclose(file); | 232 fclose(file); |
| 227 v8::Handle<v8::String> result = v8::String::New(chars, size); | 233 v8::Handle<v8::String> result = |
| 234 v8::String::NewFromUtf8(isolate, chars, v8::String::kNormalString, size); |
| 228 delete[] chars; | 235 delete[] chars; |
| 229 return result; | 236 return result; |
| 230 } | 237 } |
| 231 | 238 |
| 232 | 239 |
| 233 // Process remaining command line arguments and execute files | 240 // Process remaining command line arguments and execute files |
| 234 int RunMain(v8::Isolate* isolate, int argc, char* argv[]) { | 241 int RunMain(v8::Isolate* isolate, int argc, char* argv[]) { |
| 235 for (int i = 1; i < argc; i++) { | 242 for (int i = 1; i < argc; i++) { |
| 236 const char* str = argv[i]; | 243 const char* str = argv[i]; |
| 237 if (strcmp(str, "--shell") == 0) { | 244 if (strcmp(str, "--shell") == 0) { |
| 238 run_shell = true; | 245 run_shell = true; |
| 239 } else if (strcmp(str, "-f") == 0) { | 246 } else if (strcmp(str, "-f") == 0) { |
| 240 // Ignore any -f flags for compatibility with the other stand- | 247 // Ignore any -f flags for compatibility with the other stand- |
| 241 // alone JavaScript engines. | 248 // alone JavaScript engines. |
| 242 continue; | 249 continue; |
| 243 } else if (strncmp(str, "--", 2) == 0) { | 250 } else if (strncmp(str, "--", 2) == 0) { |
| 244 fprintf(stderr, | 251 fprintf(stderr, |
| 245 "Warning: unknown flag %s.\nTry --help for options\n", str); | 252 "Warning: unknown flag %s.\nTry --help for options\n", str); |
| 246 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { | 253 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { |
| 247 // Execute argument given to -e option directly. | 254 // Execute argument given to -e option directly. |
| 248 v8::Handle<v8::String> file_name = v8::String::New("unnamed"); | 255 v8::Handle<v8::String> file_name = |
| 249 v8::Handle<v8::String> source = v8::String::New(argv[++i]); | 256 v8::String::NewFromUtf8(isolate, "unnamed"); |
| 257 v8::Handle<v8::String> source = |
| 258 v8::String::NewFromUtf8(isolate, argv[++i]); |
| 250 if (!ExecuteString(isolate, source, file_name, false, true)) return 1; | 259 if (!ExecuteString(isolate, source, file_name, false, true)) return 1; |
| 251 } else { | 260 } else { |
| 252 // Use all other arguments as names of files to load and run. | 261 // Use all other arguments as names of files to load and run. |
| 253 v8::Handle<v8::String> file_name = v8::String::New(str); | 262 v8::Handle<v8::String> file_name = v8::String::NewFromUtf8(isolate, str); |
| 254 v8::Handle<v8::String> source = ReadFile(str); | 263 v8::Handle<v8::String> source = ReadFile(isolate, str); |
| 255 if (source.IsEmpty()) { | 264 if (source.IsEmpty()) { |
| 256 fprintf(stderr, "Error reading '%s'\n", str); | 265 fprintf(stderr, "Error reading '%s'\n", str); |
| 257 continue; | 266 continue; |
| 258 } | 267 } |
| 259 if (!ExecuteString(isolate, source, file_name, false, true)) return 1; | 268 if (!ExecuteString(isolate, source, file_name, false, true)) return 1; |
| 260 } | 269 } |
| 261 } | 270 } |
| 262 return 0; | 271 return 0; |
| 263 } | 272 } |
| 264 | 273 |
| 265 | 274 |
| 266 // The read-eval-execute loop of the shell. | 275 // The read-eval-execute loop of the shell. |
| 267 void RunShell(v8::Handle<v8::Context> context) { | 276 void RunShell(v8::Handle<v8::Context> context) { |
| 268 fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion()); | 277 fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion()); |
| 269 static const int kBufferSize = 256; | 278 static const int kBufferSize = 256; |
| 270 // Enter the execution environment before evaluating any code. | 279 // Enter the execution environment before evaluating any code. |
| 271 v8::Context::Scope context_scope(context); | 280 v8::Context::Scope context_scope(context); |
| 272 v8::Local<v8::String> name(v8::String::New("(shell)")); | 281 v8::Local<v8::String> name( |
| 282 v8::String::NewFromUtf8(context->GetIsolate(), "(shell)")); |
| 273 while (true) { | 283 while (true) { |
| 274 char buffer[kBufferSize]; | 284 char buffer[kBufferSize]; |
| 275 fprintf(stderr, "> "); | 285 fprintf(stderr, "> "); |
| 276 char* str = fgets(buffer, kBufferSize, stdin); | 286 char* str = fgets(buffer, kBufferSize, stdin); |
| 277 if (str == NULL) break; | 287 if (str == NULL) break; |
| 278 v8::HandleScope handle_scope(context->GetIsolate()); | 288 v8::HandleScope handle_scope(context->GetIsolate()); |
| 279 ExecuteString(context->GetIsolate(), | 289 ExecuteString(context->GetIsolate(), |
| 280 v8::String::New(str), | 290 v8::String::NewFromUtf8(context->GetIsolate(), str), |
| 281 name, | 291 name, |
| 282 true, | 292 true, |
| 283 true); | 293 true); |
| 284 } | 294 } |
| 285 fprintf(stderr, "\n"); | 295 fprintf(stderr, "\n"); |
| 286 } | 296 } |
| 287 | 297 |
| 288 | 298 |
| 289 // Executes a string within the current v8 context. | 299 // Executes a string within the current v8 context. |
| 290 bool ExecuteString(v8::Isolate* isolate, | 300 bool ExecuteString(v8::Isolate* isolate, |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 fprintf(stderr, "^"); | 362 fprintf(stderr, "^"); |
| 353 } | 363 } |
| 354 fprintf(stderr, "\n"); | 364 fprintf(stderr, "\n"); |
| 355 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); | 365 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); |
| 356 if (stack_trace.length() > 0) { | 366 if (stack_trace.length() > 0) { |
| 357 const char* stack_trace_string = ToCString(stack_trace); | 367 const char* stack_trace_string = ToCString(stack_trace); |
| 358 fprintf(stderr, "%s\n", stack_trace_string); | 368 fprintf(stderr, "%s\n", stack_trace_string); |
| 359 } | 369 } |
| 360 } | 370 } |
| 361 } | 371 } |
| OLD | NEW |