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 26 matching lines...) Expand all Loading... |
37 | 37 |
38 /** | 38 /** |
39 * This sample program shows how to implement a simple javascript shell | 39 * This sample program shows how to implement a simple javascript shell |
40 * based on V8. This includes initializing V8 with command line options, | 40 * based on V8. This includes initializing V8 with command line options, |
41 * creating global functions, compiling and executing strings. | 41 * creating global functions, compiling and executing strings. |
42 * | 42 * |
43 * For a more sophisticated shell, consider using the debug shell D8. | 43 * For a more sophisticated shell, consider using the debug shell D8. |
44 */ | 44 */ |
45 | 45 |
46 | 46 |
47 v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate); | 47 v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate); |
48 void RunShell(v8::Handle<v8::Context> context); | 48 void RunShell(v8::Local<v8::Context> context); |
49 int RunMain(v8::Isolate* isolate, int argc, char* argv[]); | 49 int RunMain(v8::Isolate* isolate, int argc, char* argv[]); |
50 bool ExecuteString(v8::Isolate* isolate, | 50 bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source, |
51 v8::Handle<v8::String> source, | 51 v8::Local<v8::Value> name, bool print_result, |
52 v8::Handle<v8::Value> name, | |
53 bool print_result, | |
54 bool report_exceptions); | 52 bool report_exceptions); |
55 void Print(const v8::FunctionCallbackInfo<v8::Value>& args); | 53 void Print(const v8::FunctionCallbackInfo<v8::Value>& args); |
56 void Read(const v8::FunctionCallbackInfo<v8::Value>& args); | 54 void Read(const v8::FunctionCallbackInfo<v8::Value>& args); |
57 void Load(const v8::FunctionCallbackInfo<v8::Value>& args); | 55 void Load(const v8::FunctionCallbackInfo<v8::Value>& args); |
58 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args); | 56 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args); |
59 void Version(const v8::FunctionCallbackInfo<v8::Value>& args); | 57 void Version(const v8::FunctionCallbackInfo<v8::Value>& args); |
60 v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name); | 58 v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name); |
61 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); | 59 void ReportException(v8::Isolate* isolate, v8::TryCatch* handler); |
62 | 60 |
63 | 61 |
64 static bool run_shell; | 62 static bool run_shell; |
65 | 63 |
66 | 64 |
67 class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | 65 class ShellArrayBufferAllocator : public v8::ArrayBuffer::Allocator { |
68 public: | 66 public: |
69 virtual void* Allocate(size_t length) { | 67 virtual void* Allocate(size_t length) { |
70 void* data = AllocateUninitialized(length); | 68 void* data = AllocateUninitialized(length); |
(...skipping 12 matching lines...) Expand all Loading... |
83 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 81 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
84 ShellArrayBufferAllocator array_buffer_allocator; | 82 ShellArrayBufferAllocator array_buffer_allocator; |
85 v8::Isolate::CreateParams create_params; | 83 v8::Isolate::CreateParams create_params; |
86 create_params.array_buffer_allocator = &array_buffer_allocator; | 84 create_params.array_buffer_allocator = &array_buffer_allocator; |
87 v8::Isolate* isolate = v8::Isolate::New(create_params); | 85 v8::Isolate* isolate = v8::Isolate::New(create_params); |
88 run_shell = (argc == 1); | 86 run_shell = (argc == 1); |
89 int result; | 87 int result; |
90 { | 88 { |
91 v8::Isolate::Scope isolate_scope(isolate); | 89 v8::Isolate::Scope isolate_scope(isolate); |
92 v8::HandleScope handle_scope(isolate); | 90 v8::HandleScope handle_scope(isolate); |
93 v8::Handle<v8::Context> context = CreateShellContext(isolate); | 91 v8::Local<v8::Context> context = CreateShellContext(isolate); |
94 if (context.IsEmpty()) { | 92 if (context.IsEmpty()) { |
95 fprintf(stderr, "Error creating context\n"); | 93 fprintf(stderr, "Error creating context\n"); |
96 return 1; | 94 return 1; |
97 } | 95 } |
98 v8::Context::Scope context_scope(context); | 96 v8::Context::Scope context_scope(context); |
99 result = RunMain(isolate, argc, argv); | 97 result = RunMain(isolate, argc, argv); |
100 if (run_shell) RunShell(context); | 98 if (run_shell) RunShell(context); |
101 } | 99 } |
102 isolate->Dispose(); | 100 isolate->Dispose(); |
103 v8::V8::Dispose(); | 101 v8::V8::Dispose(); |
104 v8::V8::ShutdownPlatform(); | 102 v8::V8::ShutdownPlatform(); |
105 delete platform; | 103 delete platform; |
106 return result; | 104 return result; |
107 } | 105 } |
108 | 106 |
109 | 107 |
110 // Extracts a C string from a V8 Utf8Value. | 108 // Extracts a C string from a V8 Utf8Value. |
111 const char* ToCString(const v8::String::Utf8Value& value) { | 109 const char* ToCString(const v8::String::Utf8Value& value) { |
112 return *value ? *value : "<string conversion failed>"; | 110 return *value ? *value : "<string conversion failed>"; |
113 } | 111 } |
114 | 112 |
115 | 113 |
116 // Creates a new execution environment containing the built-in | 114 // Creates a new execution environment containing the built-in |
117 // functions. | 115 // functions. |
118 v8::Handle<v8::Context> CreateShellContext(v8::Isolate* isolate) { | 116 v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate) { |
119 // Create a template for the global object. | 117 // Create a template for the global object. |
120 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); | 118 v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate); |
121 // Bind the global 'print' function to the C++ Print callback. | 119 // Bind the global 'print' function to the C++ Print callback. |
122 global->Set(v8::String::NewFromUtf8(isolate, "print"), | 120 global->Set( |
123 v8::FunctionTemplate::New(isolate, Print)); | 121 v8::String::NewFromUtf8(isolate, "print", v8::NewStringType::kNormal) |
| 122 .ToLocalChecked(), |
| 123 v8::FunctionTemplate::New(isolate, Print)); |
124 // Bind the global 'read' function to the C++ Read callback. | 124 // Bind the global 'read' function to the C++ Read callback. |
125 global->Set(v8::String::NewFromUtf8(isolate, "read"), | 125 global->Set(v8::String::NewFromUtf8( |
| 126 isolate, "read", v8::NewStringType::kNormal).ToLocalChecked(), |
126 v8::FunctionTemplate::New(isolate, Read)); | 127 v8::FunctionTemplate::New(isolate, Read)); |
127 // Bind the global 'load' function to the C++ Load callback. | 128 // Bind the global 'load' function to the C++ Load callback. |
128 global->Set(v8::String::NewFromUtf8(isolate, "load"), | 129 global->Set(v8::String::NewFromUtf8( |
| 130 isolate, "load", v8::NewStringType::kNormal).ToLocalChecked(), |
129 v8::FunctionTemplate::New(isolate, Load)); | 131 v8::FunctionTemplate::New(isolate, Load)); |
130 // Bind the 'quit' function | 132 // Bind the 'quit' function |
131 global->Set(v8::String::NewFromUtf8(isolate, "quit"), | 133 global->Set(v8::String::NewFromUtf8( |
| 134 isolate, "quit", v8::NewStringType::kNormal).ToLocalChecked(), |
132 v8::FunctionTemplate::New(isolate, Quit)); | 135 v8::FunctionTemplate::New(isolate, Quit)); |
133 // Bind the 'version' function | 136 // Bind the 'version' function |
134 global->Set(v8::String::NewFromUtf8(isolate, "version"), | 137 global->Set( |
135 v8::FunctionTemplate::New(isolate, Version)); | 138 v8::String::NewFromUtf8(isolate, "version", v8::NewStringType::kNormal) |
| 139 .ToLocalChecked(), |
| 140 v8::FunctionTemplate::New(isolate, Version)); |
136 | 141 |
137 return v8::Context::New(isolate, NULL, global); | 142 return v8::Context::New(isolate, NULL, global); |
138 } | 143 } |
139 | 144 |
140 | 145 |
141 // The callback that is invoked by v8 whenever the JavaScript 'print' | 146 // The callback that is invoked by v8 whenever the JavaScript 'print' |
142 // function is called. Prints its arguments on stdout separated by | 147 // function is called. Prints its arguments on stdout separated by |
143 // spaces and ending with a newline. | 148 // spaces and ending with a newline. |
144 void Print(const v8::FunctionCallbackInfo<v8::Value>& args) { | 149 void Print(const v8::FunctionCallbackInfo<v8::Value>& args) { |
145 bool first = true; | 150 bool first = true; |
(...skipping 12 matching lines...) Expand all Loading... |
158 fflush(stdout); | 163 fflush(stdout); |
159 } | 164 } |
160 | 165 |
161 | 166 |
162 // The callback that is invoked by v8 whenever the JavaScript 'read' | 167 // The callback that is invoked by v8 whenever the JavaScript 'read' |
163 // function is called. This function loads the content of the file named in | 168 // function is called. This function loads the content of the file named in |
164 // the argument into a JavaScript string. | 169 // the argument into a JavaScript string. |
165 void Read(const v8::FunctionCallbackInfo<v8::Value>& args) { | 170 void Read(const v8::FunctionCallbackInfo<v8::Value>& args) { |
166 if (args.Length() != 1) { | 171 if (args.Length() != 1) { |
167 args.GetIsolate()->ThrowException( | 172 args.GetIsolate()->ThrowException( |
168 v8::String::NewFromUtf8(args.GetIsolate(), "Bad parameters")); | 173 v8::String::NewFromUtf8(args.GetIsolate(), "Bad parameters", |
| 174 v8::NewStringType::kNormal).ToLocalChecked()); |
169 return; | 175 return; |
170 } | 176 } |
171 v8::String::Utf8Value file(args[0]); | 177 v8::String::Utf8Value file(args[0]); |
172 if (*file == NULL) { | 178 if (*file == NULL) { |
173 args.GetIsolate()->ThrowException( | 179 args.GetIsolate()->ThrowException( |
174 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file")); | 180 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file", |
| 181 v8::NewStringType::kNormal).ToLocalChecked()); |
175 return; | 182 return; |
176 } | 183 } |
177 v8::Handle<v8::String> source = ReadFile(args.GetIsolate(), *file); | 184 v8::Local<v8::String> source; |
178 if (source.IsEmpty()) { | 185 if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) { |
179 args.GetIsolate()->ThrowException( | 186 args.GetIsolate()->ThrowException( |
180 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file")); | 187 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file", |
| 188 v8::NewStringType::kNormal).ToLocalChecked()); |
181 return; | 189 return; |
182 } | 190 } |
183 args.GetReturnValue().Set(source); | 191 args.GetReturnValue().Set(source); |
184 } | 192 } |
185 | 193 |
186 | 194 |
187 // The callback that is invoked by v8 whenever the JavaScript 'load' | 195 // The callback that is invoked by v8 whenever the JavaScript 'load' |
188 // function is called. Loads, compiles and executes its argument | 196 // function is called. Loads, compiles and executes its argument |
189 // JavaScript file. | 197 // JavaScript file. |
190 void Load(const v8::FunctionCallbackInfo<v8::Value>& args) { | 198 void Load(const v8::FunctionCallbackInfo<v8::Value>& args) { |
191 for (int i = 0; i < args.Length(); i++) { | 199 for (int i = 0; i < args.Length(); i++) { |
192 v8::HandleScope handle_scope(args.GetIsolate()); | 200 v8::HandleScope handle_scope(args.GetIsolate()); |
193 v8::String::Utf8Value file(args[i]); | 201 v8::String::Utf8Value file(args[i]); |
194 if (*file == NULL) { | 202 if (*file == NULL) { |
195 args.GetIsolate()->ThrowException( | 203 args.GetIsolate()->ThrowException( |
196 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file")); | 204 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file", |
| 205 v8::NewStringType::kNormal).ToLocalChecked()); |
197 return; | 206 return; |
198 } | 207 } |
199 v8::Handle<v8::String> source = ReadFile(args.GetIsolate(), *file); | 208 v8::Local<v8::String> source; |
200 if (source.IsEmpty()) { | 209 if (!ReadFile(args.GetIsolate(), *file).ToLocal(&source)) { |
201 args.GetIsolate()->ThrowException( | 210 args.GetIsolate()->ThrowException( |
202 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file")); | 211 v8::String::NewFromUtf8(args.GetIsolate(), "Error loading file", |
| 212 v8::NewStringType::kNormal).ToLocalChecked()); |
203 return; | 213 return; |
204 } | 214 } |
205 if (!ExecuteString(args.GetIsolate(), | 215 if (!ExecuteString(args.GetIsolate(), source, args[i], false, false)) { |
206 source, | |
207 v8::String::NewFromUtf8(args.GetIsolate(), *file), | |
208 false, | |
209 false)) { | |
210 args.GetIsolate()->ThrowException( | 216 args.GetIsolate()->ThrowException( |
211 v8::String::NewFromUtf8(args.GetIsolate(), "Error executing file")); | 217 v8::String::NewFromUtf8(args.GetIsolate(), "Error executing file", |
| 218 v8::NewStringType::kNormal).ToLocalChecked()); |
212 return; | 219 return; |
213 } | 220 } |
214 } | 221 } |
215 } | 222 } |
216 | 223 |
217 | 224 |
218 // The callback that is invoked by v8 whenever the JavaScript 'quit' | 225 // The callback that is invoked by v8 whenever the JavaScript 'quit' |
219 // function is called. Quits. | 226 // function is called. Quits. |
220 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { | 227 void Quit(const v8::FunctionCallbackInfo<v8::Value>& args) { |
221 // If not arguments are given args[0] will yield undefined which | 228 // If not arguments are given args[0] will yield undefined which |
222 // converts to the integer value 0. | 229 // converts to the integer value 0. |
223 int exit_code = args[0]->Int32Value(); | 230 int exit_code = |
| 231 args[0]->Int32Value(args.GetIsolate()->GetCurrentContext()).FromMaybe(0); |
224 fflush(stdout); | 232 fflush(stdout); |
225 fflush(stderr); | 233 fflush(stderr); |
226 exit(exit_code); | 234 exit(exit_code); |
227 } | 235 } |
228 | 236 |
229 | 237 |
230 void Version(const v8::FunctionCallbackInfo<v8::Value>& args) { | 238 void Version(const v8::FunctionCallbackInfo<v8::Value>& args) { |
231 args.GetReturnValue().Set( | 239 args.GetReturnValue().Set( |
232 v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion())); | 240 v8::String::NewFromUtf8(args.GetIsolate(), v8::V8::GetVersion(), |
| 241 v8::NewStringType::kNormal).ToLocalChecked()); |
233 } | 242 } |
234 | 243 |
235 | 244 |
236 // Reads a file into a v8 string. | 245 // Reads a file into a v8 string. |
237 v8::Handle<v8::String> ReadFile(v8::Isolate* isolate, const char* name) { | 246 v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name) { |
238 FILE* file = fopen(name, "rb"); | 247 FILE* file = fopen(name, "rb"); |
239 if (file == NULL) return v8::Handle<v8::String>(); | 248 if (file == NULL) return v8::MaybeLocal<v8::String>(); |
240 | 249 |
241 fseek(file, 0, SEEK_END); | 250 fseek(file, 0, SEEK_END); |
242 size_t size = ftell(file); | 251 size_t size = ftell(file); |
243 rewind(file); | 252 rewind(file); |
244 | 253 |
245 char* chars = new char[size + 1]; | 254 char* chars = new char[size + 1]; |
246 chars[size] = '\0'; | 255 chars[size] = '\0'; |
247 for (size_t i = 0; i < size;) { | 256 for (size_t i = 0; i < size;) { |
248 i += fread(&chars[i], 1, size - i, file); | 257 i += fread(&chars[i], 1, size - i, file); |
249 if (ferror(file)) { | 258 if (ferror(file)) { |
250 fclose(file); | 259 fclose(file); |
251 return v8::Handle<v8::String>(); | 260 return v8::MaybeLocal<v8::String>(); |
252 } | 261 } |
253 } | 262 } |
254 fclose(file); | 263 fclose(file); |
255 v8::Handle<v8::String> result = v8::String::NewFromUtf8( | 264 v8::MaybeLocal<v8::String> result = v8::String::NewFromUtf8( |
256 isolate, chars, v8::String::kNormalString, static_cast<int>(size)); | 265 isolate, chars, v8::NewStringType::kNormal, static_cast<int>(size)); |
257 delete[] chars; | 266 delete[] chars; |
258 return result; | 267 return result; |
259 } | 268 } |
260 | 269 |
261 | 270 |
262 // Process remaining command line arguments and execute files | 271 // Process remaining command line arguments and execute files |
263 int RunMain(v8::Isolate* isolate, int argc, char* argv[]) { | 272 int RunMain(v8::Isolate* isolate, int argc, char* argv[]) { |
264 for (int i = 1; i < argc; i++) { | 273 for (int i = 1; i < argc; i++) { |
265 const char* str = argv[i]; | 274 const char* str = argv[i]; |
266 if (strcmp(str, "--shell") == 0) { | 275 if (strcmp(str, "--shell") == 0) { |
267 run_shell = true; | 276 run_shell = true; |
268 } else if (strcmp(str, "-f") == 0) { | 277 } else if (strcmp(str, "-f") == 0) { |
269 // Ignore any -f flags for compatibility with the other stand- | 278 // Ignore any -f flags for compatibility with the other stand- |
270 // alone JavaScript engines. | 279 // alone JavaScript engines. |
271 continue; | 280 continue; |
272 } else if (strncmp(str, "--", 2) == 0) { | 281 } else if (strncmp(str, "--", 2) == 0) { |
273 fprintf(stderr, | 282 fprintf(stderr, |
274 "Warning: unknown flag %s.\nTry --help for options\n", str); | 283 "Warning: unknown flag %s.\nTry --help for options\n", str); |
275 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { | 284 } else if (strcmp(str, "-e") == 0 && i + 1 < argc) { |
276 // Execute argument given to -e option directly. | 285 // Execute argument given to -e option directly. |
277 v8::Handle<v8::String> file_name = | 286 v8::Local<v8::String> file_name = |
278 v8::String::NewFromUtf8(isolate, "unnamed"); | 287 v8::String::NewFromUtf8(isolate, "unnamed", |
279 v8::Handle<v8::String> source = | 288 v8::NewStringType::kNormal).ToLocalChecked(); |
280 v8::String::NewFromUtf8(isolate, argv[++i]); | 289 v8::Local<v8::String> source; |
| 290 if (!v8::String::NewFromUtf8(isolate, argv[++i], |
| 291 v8::NewStringType::kNormal) |
| 292 .ToLocal(&source)) { |
| 293 return 1; |
| 294 } |
281 if (!ExecuteString(isolate, source, file_name, false, true)) return 1; | 295 if (!ExecuteString(isolate, source, file_name, false, true)) return 1; |
282 } else { | 296 } else { |
283 // Use all other arguments as names of files to load and run. | 297 // Use all other arguments as names of files to load and run. |
284 v8::Handle<v8::String> file_name = v8::String::NewFromUtf8(isolate, str); | 298 v8::Local<v8::String> file_name = |
285 v8::Handle<v8::String> source = ReadFile(isolate, str); | 299 v8::String::NewFromUtf8(isolate, str, v8::NewStringType::kNormal) |
286 if (source.IsEmpty()) { | 300 .ToLocalChecked(); |
| 301 v8::Local<v8::String> source; |
| 302 if (!ReadFile(isolate, str).ToLocal(&source)) { |
287 fprintf(stderr, "Error reading '%s'\n", str); | 303 fprintf(stderr, "Error reading '%s'\n", str); |
288 continue; | 304 continue; |
289 } | 305 } |
290 if (!ExecuteString(isolate, source, file_name, false, true)) return 1; | 306 if (!ExecuteString(isolate, source, file_name, false, true)) return 1; |
291 } | 307 } |
292 } | 308 } |
293 return 0; | 309 return 0; |
294 } | 310 } |
295 | 311 |
296 | 312 |
297 // The read-eval-execute loop of the shell. | 313 // The read-eval-execute loop of the shell. |
298 void RunShell(v8::Handle<v8::Context> context) { | 314 void RunShell(v8::Local<v8::Context> context) { |
299 fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion()); | 315 fprintf(stderr, "V8 version %s [sample shell]\n", v8::V8::GetVersion()); |
300 static const int kBufferSize = 256; | 316 static const int kBufferSize = 256; |
301 // Enter the execution environment before evaluating any code. | 317 // Enter the execution environment before evaluating any code. |
302 v8::Context::Scope context_scope(context); | 318 v8::Context::Scope context_scope(context); |
303 v8::Local<v8::String> name( | 319 v8::Local<v8::String> name( |
304 v8::String::NewFromUtf8(context->GetIsolate(), "(shell)")); | 320 v8::String::NewFromUtf8(context->GetIsolate(), "(shell)", |
| 321 v8::NewStringType::kNormal).ToLocalChecked()); |
305 while (true) { | 322 while (true) { |
306 char buffer[kBufferSize]; | 323 char buffer[kBufferSize]; |
307 fprintf(stderr, "> "); | 324 fprintf(stderr, "> "); |
308 char* str = fgets(buffer, kBufferSize, stdin); | 325 char* str = fgets(buffer, kBufferSize, stdin); |
309 if (str == NULL) break; | 326 if (str == NULL) break; |
310 v8::HandleScope handle_scope(context->GetIsolate()); | 327 v8::HandleScope handle_scope(context->GetIsolate()); |
311 ExecuteString(context->GetIsolate(), | 328 ExecuteString( |
312 v8::String::NewFromUtf8(context->GetIsolate(), str), | 329 context->GetIsolate(), |
313 name, | 330 v8::String::NewFromUtf8(context->GetIsolate(), str, |
314 true, | 331 v8::NewStringType::kNormal).ToLocalChecked(), |
315 true); | 332 name, true, true); |
316 } | 333 } |
317 fprintf(stderr, "\n"); | 334 fprintf(stderr, "\n"); |
318 } | 335 } |
319 | 336 |
320 | 337 |
321 // Executes a string within the current v8 context. | 338 // Executes a string within the current v8 context. |
322 bool ExecuteString(v8::Isolate* isolate, | 339 bool ExecuteString(v8::Isolate* isolate, v8::Local<v8::String> source, |
323 v8::Handle<v8::String> source, | 340 v8::Local<v8::Value> name, bool print_result, |
324 v8::Handle<v8::Value> name, | |
325 bool print_result, | |
326 bool report_exceptions) { | 341 bool report_exceptions) { |
327 v8::HandleScope handle_scope(isolate); | 342 v8::HandleScope handle_scope(isolate); |
328 v8::TryCatch try_catch(isolate); | 343 v8::TryCatch try_catch(isolate); |
329 v8::ScriptOrigin origin(name); | 344 v8::ScriptOrigin origin(name); |
330 v8::Handle<v8::Script> script = v8::Script::Compile(source, &origin); | 345 v8::Local<v8::Context> context(isolate->GetCurrentContext()); |
331 if (script.IsEmpty()) { | 346 v8::Local<v8::Script> script; |
| 347 if (!v8::Script::Compile(context, source, &origin).ToLocal(&script)) { |
332 // Print errors that happened during compilation. | 348 // Print errors that happened during compilation. |
333 if (report_exceptions) | 349 if (report_exceptions) |
334 ReportException(isolate, &try_catch); | 350 ReportException(isolate, &try_catch); |
335 return false; | 351 return false; |
336 } else { | 352 } else { |
337 v8::Handle<v8::Value> result = script->Run(); | 353 v8::Local<v8::Value> result; |
338 if (result.IsEmpty()) { | 354 if (!script->Run(context).ToLocal(&result)) { |
339 assert(try_catch.HasCaught()); | 355 assert(try_catch.HasCaught()); |
340 // Print errors that happened during execution. | 356 // Print errors that happened during execution. |
341 if (report_exceptions) | 357 if (report_exceptions) |
342 ReportException(isolate, &try_catch); | 358 ReportException(isolate, &try_catch); |
343 return false; | 359 return false; |
344 } else { | 360 } else { |
345 assert(!try_catch.HasCaught()); | 361 assert(!try_catch.HasCaught()); |
346 if (print_result && !result->IsUndefined()) { | 362 if (print_result && !result->IsUndefined()) { |
347 // If all went well and the result wasn't undefined then print | 363 // If all went well and the result wasn't undefined then print |
348 // the returned value. | 364 // the returned value. |
349 v8::String::Utf8Value str(result); | 365 v8::String::Utf8Value str(result); |
350 const char* cstr = ToCString(str); | 366 const char* cstr = ToCString(str); |
351 printf("%s\n", cstr); | 367 printf("%s\n", cstr); |
352 } | 368 } |
353 return true; | 369 return true; |
354 } | 370 } |
355 } | 371 } |
356 } | 372 } |
357 | 373 |
358 | 374 |
359 void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) { | 375 void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) { |
360 v8::HandleScope handle_scope(isolate); | 376 v8::HandleScope handle_scope(isolate); |
361 v8::String::Utf8Value exception(try_catch->Exception()); | 377 v8::String::Utf8Value exception(try_catch->Exception()); |
362 const char* exception_string = ToCString(exception); | 378 const char* exception_string = ToCString(exception); |
363 v8::Handle<v8::Message> message = try_catch->Message(); | 379 v8::Local<v8::Message> message = try_catch->Message(); |
364 if (message.IsEmpty()) { | 380 if (message.IsEmpty()) { |
365 // V8 didn't provide any extra information about this error; just | 381 // V8 didn't provide any extra information about this error; just |
366 // print the exception. | 382 // print the exception. |
367 fprintf(stderr, "%s\n", exception_string); | 383 fprintf(stderr, "%s\n", exception_string); |
368 } else { | 384 } else { |
369 // Print (filename):(line number): (message). | 385 // Print (filename):(line number): (message). |
370 v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName()); | 386 v8::String::Utf8Value filename(message->GetScriptOrigin().ResourceName()); |
| 387 v8::Local<v8::Context> context(isolate->GetCurrentContext()); |
371 const char* filename_string = ToCString(filename); | 388 const char* filename_string = ToCString(filename); |
372 int linenum = message->GetLineNumber(); | 389 int linenum = message->GetLineNumber(context).FromJust(); |
373 fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string); | 390 fprintf(stderr, "%s:%i: %s\n", filename_string, linenum, exception_string); |
374 // Print line of source code. | 391 // Print line of source code. |
375 v8::String::Utf8Value sourceline(message->GetSourceLine()); | 392 v8::String::Utf8Value sourceline( |
| 393 message->GetSourceLine(context).ToLocalChecked()); |
376 const char* sourceline_string = ToCString(sourceline); | 394 const char* sourceline_string = ToCString(sourceline); |
377 fprintf(stderr, "%s\n", sourceline_string); | 395 fprintf(stderr, "%s\n", sourceline_string); |
378 // Print wavy underline (GetUnderline is deprecated). | 396 // Print wavy underline (GetUnderline is deprecated). |
379 int start = message->GetStartColumn(); | 397 int start = message->GetStartColumn(context).FromJust(); |
380 for (int i = 0; i < start; i++) { | 398 for (int i = 0; i < start; i++) { |
381 fprintf(stderr, " "); | 399 fprintf(stderr, " "); |
382 } | 400 } |
383 int end = message->GetEndColumn(); | 401 int end = message->GetEndColumn(context).FromJust(); |
384 for (int i = start; i < end; i++) { | 402 for (int i = start; i < end; i++) { |
385 fprintf(stderr, "^"); | 403 fprintf(stderr, "^"); |
386 } | 404 } |
387 fprintf(stderr, "\n"); | 405 fprintf(stderr, "\n"); |
388 v8::String::Utf8Value stack_trace(try_catch->StackTrace()); | 406 v8::String::Utf8Value stack_trace( |
| 407 try_catch->StackTrace(context).ToLocalChecked()); |
389 if (stack_trace.length() > 0) { | 408 if (stack_trace.length() > 0) { |
390 const char* stack_trace_string = ToCString(stack_trace); | 409 const char* stack_trace_string = ToCString(stack_trace); |
391 fprintf(stderr, "%s\n", stack_trace_string); | 410 fprintf(stderr, "%s\n", stack_trace_string); |
392 } | 411 } |
393 } | 412 } |
394 } | 413 } |
OLD | NEW |