OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 // Defined when linking against shared lib on Windows. | 6 // Defined when linking against shared lib on Windows. |
7 #if defined(USING_V8_SHARED) && !defined(V8_SHARED) | 7 #if defined(USING_V8_SHARED) && !defined(V8_SHARED) |
8 #define V8_SHARED | 8 #define V8_SHARED |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 } | 214 } |
215 } | 215 } |
216 temp_isolate->Dispose(); | 216 temp_isolate->Dispose(); |
217 delete[] source_buffer; | 217 delete[] source_buffer; |
218 delete[] name_buffer; | 218 delete[] name_buffer; |
219 return result; | 219 return result; |
220 } | 220 } |
221 | 221 |
222 | 222 |
223 // Compile a string within the current v8 context. | 223 // Compile a string within the current v8 context. |
224 Local<UnboundScript> Shell::CompileString( | 224 Local<Script> Shell::CompileString( |
225 Isolate* isolate, Local<String> source, Local<Value> name, | 225 Isolate* isolate, Local<String> source, Local<Value> name, |
226 ScriptCompiler::CompileOptions compile_options) { | 226 ScriptCompiler::CompileOptions compile_options, SourceType source_type) { |
227 ScriptOrigin origin(name); | 227 ScriptOrigin origin(name); |
228 if (compile_options == ScriptCompiler::kNoCompileOptions) { | 228 if (compile_options == ScriptCompiler::kNoCompileOptions) { |
229 ScriptCompiler::Source script_source(source, origin); | 229 ScriptCompiler::Source script_source(source, origin); |
230 return ScriptCompiler::CompileUnbound(isolate, &script_source, | 230 return source_type == SCRIPT |
231 compile_options); | 231 ? ScriptCompiler::Compile(isolate, &script_source, |
| 232 compile_options) |
| 233 : ScriptCompiler::CompileModule(isolate, &script_source, |
| 234 compile_options); |
232 } | 235 } |
233 | 236 |
234 ScriptCompiler::CachedData* data = | 237 ScriptCompiler::CachedData* data = |
235 CompileForCachedData(source, name, compile_options); | 238 CompileForCachedData(source, name, compile_options); |
236 ScriptCompiler::Source cached_source(source, origin, data); | 239 ScriptCompiler::Source cached_source(source, origin, data); |
237 if (compile_options == ScriptCompiler::kProduceCodeCache) { | 240 if (compile_options == ScriptCompiler::kProduceCodeCache) { |
238 compile_options = ScriptCompiler::kConsumeCodeCache; | 241 compile_options = ScriptCompiler::kConsumeCodeCache; |
239 } else if (compile_options == ScriptCompiler::kProduceParserCache) { | 242 } else if (compile_options == ScriptCompiler::kProduceParserCache) { |
240 compile_options = ScriptCompiler::kConsumeParserCache; | 243 compile_options = ScriptCompiler::kConsumeParserCache; |
241 } else { | 244 } else { |
242 DCHECK(false); // A new compile option? | 245 DCHECK(false); // A new compile option? |
243 } | 246 } |
244 if (data == NULL) compile_options = ScriptCompiler::kNoCompileOptions; | 247 if (data == NULL) compile_options = ScriptCompiler::kNoCompileOptions; |
245 return ScriptCompiler::CompileUnbound(isolate, &cached_source, | 248 return source_type == SCRIPT |
246 compile_options); | 249 ? ScriptCompiler::Compile(isolate, &cached_source, compile_options) |
| 250 : ScriptCompiler::CompileModule(isolate, &cached_source, |
| 251 compile_options); |
247 } | 252 } |
248 | 253 |
249 | 254 |
250 // Executes a string within the current v8 context. | 255 // Executes a string within the current v8 context. |
251 bool Shell::ExecuteString(Isolate* isolate, | 256 bool Shell::ExecuteString(Isolate* isolate, Handle<String> source, |
252 Handle<String> source, | 257 Handle<Value> name, bool print_result, |
253 Handle<Value> name, | 258 bool report_exceptions, SourceType source_type) { |
254 bool print_result, | |
255 bool report_exceptions) { | |
256 #ifndef V8_SHARED | 259 #ifndef V8_SHARED |
257 bool FLAG_debugger = i::FLAG_debugger; | 260 bool FLAG_debugger = i::FLAG_debugger; |
258 #else | 261 #else |
259 bool FLAG_debugger = false; | 262 bool FLAG_debugger = false; |
260 #endif // !V8_SHARED | 263 #endif // !V8_SHARED |
261 HandleScope handle_scope(isolate); | 264 HandleScope handle_scope(isolate); |
262 TryCatch try_catch; | 265 TryCatch try_catch; |
263 options.script_executed = true; | 266 options.script_executed = true; |
264 if (FLAG_debugger) { | 267 if (FLAG_debugger) { |
265 // When debugging make exceptions appear to be uncaught. | 268 // When debugging make exceptions appear to be uncaught. |
266 try_catch.SetVerbose(true); | 269 try_catch.SetVerbose(true); |
267 } | 270 } |
268 | 271 |
269 Handle<UnboundScript> script = | 272 Handle<Value> result; |
270 Shell::CompileString(isolate, source, name, options.compile_options); | 273 { |
271 if (script.IsEmpty()) { | 274 PerIsolateData* data = PerIsolateData::Get(isolate); |
272 // Print errors that happened during compilation. | 275 Local<Context> realm = |
| 276 Local<Context>::New(isolate, data->realms_[data->realm_current_]); |
| 277 Context::Scope context_scope(realm); |
| 278 Handle<Script> script = Shell::CompileString( |
| 279 isolate, source, name, options.compile_options, source_type); |
| 280 if (script.IsEmpty()) { |
| 281 // Print errors that happened during compilation. |
| 282 if (report_exceptions && !FLAG_debugger) |
| 283 ReportException(isolate, &try_catch); |
| 284 return false; |
| 285 } |
| 286 result = script->Run(); |
| 287 data->realm_current_ = data->realm_switch_; |
| 288 } |
| 289 if (result.IsEmpty()) { |
| 290 DCHECK(try_catch.HasCaught()); |
| 291 // Print errors that happened during execution. |
273 if (report_exceptions && !FLAG_debugger) | 292 if (report_exceptions && !FLAG_debugger) |
274 ReportException(isolate, &try_catch); | 293 ReportException(isolate, &try_catch); |
275 return false; | 294 return false; |
276 } else { | 295 } |
277 PerIsolateData* data = PerIsolateData::Get(isolate); | 296 DCHECK(!try_catch.HasCaught()); |
278 Local<Context> realm = | 297 if (print_result) { |
279 Local<Context>::New(isolate, data->realms_[data->realm_current_]); | 298 #if !defined(V8_SHARED) |
280 realm->Enter(); | 299 if (options.test_shell) { |
281 Handle<Value> result = script->BindToCurrentContext()->Run(); | 300 #endif |
282 realm->Exit(); | 301 if (!result->IsUndefined()) { |
283 data->realm_current_ = data->realm_switch_; | 302 // If all went well and the result wasn't undefined then print |
284 if (result.IsEmpty()) { | 303 // the returned value. |
285 DCHECK(try_catch.HasCaught()); | 304 v8::String::Utf8Value str(result); |
286 // Print errors that happened during execution. | 305 fwrite(*str, sizeof(**str), str.length(), stdout); |
287 if (report_exceptions && !FLAG_debugger) | 306 printf("\n"); |
288 ReportException(isolate, &try_catch); | 307 } |
289 return false; | 308 #if !defined(V8_SHARED) |
290 } else { | 309 } else { |
291 DCHECK(!try_catch.HasCaught()); | 310 v8::TryCatch try_catch; |
292 if (print_result) { | 311 v8::Local<v8::Context> context = |
293 #if !defined(V8_SHARED) | 312 v8::Local<v8::Context>::New(isolate, utility_context_); |
294 if (options.test_shell) { | 313 v8::Context::Scope context_scope(context); |
| 314 Handle<Object> global = context->Global(); |
| 315 Handle<Value> fun = |
| 316 global->Get(String::NewFromUtf8(isolate, "Stringify")); |
| 317 Handle<Value> argv[1] = {result}; |
| 318 Handle<Value> s = Handle<Function>::Cast(fun)->Call(global, 1, argv); |
| 319 if (try_catch.HasCaught()) return true; |
| 320 v8::String::Utf8Value str(s); |
| 321 fwrite(*str, sizeof(**str), str.length(), stdout); |
| 322 printf("\n"); |
| 323 } |
295 #endif | 324 #endif |
296 if (!result->IsUndefined()) { | |
297 // If all went well and the result wasn't undefined then print | |
298 // the returned value. | |
299 v8::String::Utf8Value str(result); | |
300 fwrite(*str, sizeof(**str), str.length(), stdout); | |
301 printf("\n"); | |
302 } | |
303 #if !defined(V8_SHARED) | |
304 } else { | |
305 v8::TryCatch try_catch; | |
306 v8::Local<v8::Context> context = | |
307 v8::Local<v8::Context>::New(isolate, utility_context_); | |
308 v8::Context::Scope context_scope(context); | |
309 Handle<Object> global = context->Global(); | |
310 Handle<Value> fun = | |
311 global->Get(String::NewFromUtf8(isolate, "Stringify")); | |
312 Handle<Value> argv[1] = { result }; | |
313 Handle<Value> s = Handle<Function>::Cast(fun)->Call(global, 1, argv); | |
314 if (try_catch.HasCaught()) return true; | |
315 v8::String::Utf8Value str(s); | |
316 fwrite(*str, sizeof(**str), str.length(), stdout); | |
317 printf("\n"); | |
318 } | |
319 #endif | |
320 } | |
321 return true; | |
322 } | |
323 } | 325 } |
| 326 return true; |
324 } | 327 } |
325 | 328 |
326 | 329 |
327 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) { | 330 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) { |
328 data_->realm_count_ = 1; | 331 data_->realm_count_ = 1; |
329 data_->realm_current_ = 0; | 332 data_->realm_current_ = 0; |
330 data_->realm_switch_ = 0; | 333 data_->realm_switch_ = 0; |
331 data_->realms_ = new Persistent<Context>[1]; | 334 data_->realms_ = new Persistent<Context>[1]; |
332 data_->realms_[0].Reset(data_->isolate_, | 335 data_->realms_[0].Reset(data_->isolate_, |
333 data_->isolate_->GetEnteredContext()); | 336 data_->isolate_->GetEnteredContext()); |
(...skipping 848 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1182 delete thread_; | 1185 delete thread_; |
1183 thread_ = NULL; | 1186 thread_ = NULL; |
1184 #endif // !V8_SHARED | 1187 #endif // !V8_SHARED |
1185 } | 1188 } |
1186 | 1189 |
1187 | 1190 |
1188 void SourceGroup::Execute(Isolate* isolate) { | 1191 void SourceGroup::Execute(Isolate* isolate) { |
1189 bool exception_was_thrown = false; | 1192 bool exception_was_thrown = false; |
1190 for (int i = begin_offset_; i < end_offset_; ++i) { | 1193 for (int i = begin_offset_; i < end_offset_; ++i) { |
1191 const char* arg = argv_[i]; | 1194 const char* arg = argv_[i]; |
| 1195 Shell::SourceType source_type = Shell::SCRIPT; |
1192 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { | 1196 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { |
1193 // Execute argument given to -e option directly. | 1197 // Execute argument given to -e option directly. |
1194 HandleScope handle_scope(isolate); | 1198 HandleScope handle_scope(isolate); |
1195 Handle<String> file_name = String::NewFromUtf8(isolate, "unnamed"); | 1199 Handle<String> file_name = String::NewFromUtf8(isolate, "unnamed"); |
1196 Handle<String> source = String::NewFromUtf8(isolate, argv_[i + 1]); | 1200 Handle<String> source = String::NewFromUtf8(isolate, argv_[i + 1]); |
1197 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { | 1201 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { |
1198 exception_was_thrown = true; | 1202 exception_was_thrown = true; |
1199 break; | 1203 break; |
1200 } | 1204 } |
1201 ++i; | 1205 ++i; |
| 1206 continue; |
| 1207 } else if (strcmp(arg, "--module") == 0 && i + 1 < end_offset_) { |
| 1208 // Treat the next file as a module. |
| 1209 source_type = Shell::MODULE; |
| 1210 arg = argv_[++i]; |
1202 } else if (arg[0] == '-') { | 1211 } else if (arg[0] == '-') { |
1203 // Ignore other options. They have been parsed already. | 1212 // Ignore other options. They have been parsed already. |
1204 } else { | 1213 continue; |
1205 // Use all other arguments as names of files to load and run. | 1214 } |
1206 HandleScope handle_scope(isolate); | 1215 |
1207 Handle<String> file_name = String::NewFromUtf8(isolate, arg); | 1216 // Use all other arguments as names of files to load and run. |
1208 Handle<String> source = ReadFile(isolate, arg); | 1217 HandleScope handle_scope(isolate); |
1209 if (source.IsEmpty()) { | 1218 Handle<String> file_name = String::NewFromUtf8(isolate, arg); |
1210 printf("Error reading '%s'\n", arg); | 1219 Handle<String> source = ReadFile(isolate, arg); |
1211 Shell::Exit(1); | 1220 if (source.IsEmpty()) { |
1212 } | 1221 printf("Error reading '%s'\n", arg); |
1213 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { | 1222 Shell::Exit(1); |
1214 exception_was_thrown = true; | 1223 } |
1215 break; | 1224 if (!Shell::ExecuteString(isolate, source, file_name, false, true, |
1216 } | 1225 source_type)) { |
| 1226 exception_was_thrown = true; |
| 1227 break; |
1217 } | 1228 } |
1218 } | 1229 } |
1219 if (exception_was_thrown != Shell::options.expected_to_throw) { | 1230 if (exception_was_thrown != Shell::options.expected_to_throw) { |
1220 Shell::Exit(1); | 1231 Shell::Exit(1); |
1221 } | 1232 } |
1222 } | 1233 } |
1223 | 1234 |
1224 | 1235 |
1225 Handle<String> SourceGroup::ReadFile(Isolate* isolate, const char* name) { | 1236 Handle<String> SourceGroup::ReadFile(Isolate* isolate, const char* name) { |
1226 int size; | 1237 int size; |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1391 } else { | 1402 } else { |
1392 printf("Unknown option to --cache.\n"); | 1403 printf("Unknown option to --cache.\n"); |
1393 return false; | 1404 return false; |
1394 } | 1405 } |
1395 argv[i] = NULL; | 1406 argv[i] = NULL; |
1396 } | 1407 } |
1397 } | 1408 } |
1398 | 1409 |
1399 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); | 1410 v8::V8::SetFlagsFromCommandLine(&argc, argv, true); |
1400 | 1411 |
| 1412 bool enable_harmony_modules = false; |
| 1413 |
1401 // Set up isolated source groups. | 1414 // Set up isolated source groups. |
1402 options.isolate_sources = new SourceGroup[options.num_isolates]; | 1415 options.isolate_sources = new SourceGroup[options.num_isolates]; |
1403 SourceGroup* current = options.isolate_sources; | 1416 SourceGroup* current = options.isolate_sources; |
1404 current->Begin(argv, 1); | 1417 current->Begin(argv, 1); |
1405 for (int i = 1; i < argc; i++) { | 1418 for (int i = 1; i < argc; i++) { |
1406 const char* str = argv[i]; | 1419 const char* str = argv[i]; |
1407 if (strcmp(str, "--isolate") == 0) { | 1420 if (strcmp(str, "--isolate") == 0) { |
1408 current->End(i); | 1421 current->End(i); |
1409 current++; | 1422 current++; |
1410 current->Begin(argv, i + 1); | 1423 current->Begin(argv, i + 1); |
| 1424 } else if (strcmp(str, "--module") == 0) { |
| 1425 // Pass on to SourceGroup, which understands this option. |
| 1426 enable_harmony_modules = true; |
1411 } else if (strncmp(argv[i], "--", 2) == 0) { | 1427 } else if (strncmp(argv[i], "--", 2) == 0) { |
1412 printf("Warning: unknown flag %s.\nTry --help for options\n", argv[i]); | 1428 printf("Warning: unknown flag %s.\nTry --help for options\n", argv[i]); |
1413 } | 1429 } |
1414 } | 1430 } |
1415 current->End(argc); | 1431 current->End(argc); |
1416 | 1432 |
1417 if (!logfile_per_isolate && options.num_isolates) { | 1433 if (!logfile_per_isolate && options.num_isolates) { |
1418 SetFlagsFromString("--nologfile_per_isolate"); | 1434 SetFlagsFromString("--nologfile_per_isolate"); |
1419 } | 1435 } |
1420 | 1436 |
| 1437 if (enable_harmony_modules) { |
| 1438 SetFlagsFromString("--harmony-modules"); |
| 1439 } |
| 1440 |
1421 return true; | 1441 return true; |
1422 } | 1442 } |
1423 | 1443 |
1424 | 1444 |
1425 int Shell::RunMain(Isolate* isolate, int argc, char* argv[]) { | 1445 int Shell::RunMain(Isolate* isolate, int argc, char* argv[]) { |
1426 #ifndef V8_SHARED | 1446 #ifndef V8_SHARED |
1427 for (int i = 1; i < options.num_isolates; ++i) { | 1447 for (int i = 1; i < options.num_isolates; ++i) { |
1428 options.isolate_sources[i].StartExecuteInThread(); | 1448 options.isolate_sources[i].StartExecuteInThread(); |
1429 } | 1449 } |
1430 #endif // !V8_SHARED | 1450 #endif // !V8_SHARED |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1750 } | 1770 } |
1751 | 1771 |
1752 } // namespace v8 | 1772 } // namespace v8 |
1753 | 1773 |
1754 | 1774 |
1755 #ifndef GOOGLE3 | 1775 #ifndef GOOGLE3 |
1756 int main(int argc, char* argv[]) { | 1776 int main(int argc, char* argv[]) { |
1757 return v8::Shell::Main(argc, argv); | 1777 return v8::Shell::Main(argc, argv); |
1758 } | 1778 } |
1759 #endif | 1779 #endif |
OLD | NEW |