Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/d8.cc

Issue 2351113004: [modules] Expand API to allow linking and use it in d8 (Closed)
Patch Set: Use StrictEquals, remove check for bad instantiation behavior Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/d8.h ('k') | src/factory.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include <errno.h> 5 #include <errno.h>
6 #include <stdlib.h> 6 #include <stdlib.h>
7 #include <string.h> 7 #include <string.h>
8 #include <sys/stat.h> 8 #include <sys/stat.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
11 #include <fstream> 11 #include <fstream>
12 #include <map>
13 #include <utility>
12 #include <vector> 14 #include <vector>
13 15
14 #ifdef ENABLE_VTUNE_JIT_INTERFACE 16 #ifdef ENABLE_VTUNE_JIT_INTERFACE
15 #include "src/third_party/vtune/v8-vtune.h" 17 #include "src/third_party/vtune/v8-vtune.h"
16 #endif 18 #endif
17 19
18 #include "src/d8.h" 20 #include "src/d8.h"
19 #include "src/ostreams.h" 21 #include "src/ostreams.h"
20 22
21 #include "include/libplatform/libplatform.h" 23 #include "include/libplatform/libplatform.h"
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 MaybeLocal<Script> result = 456 MaybeLocal<Script> result =
455 ScriptCompiler::Compile(context, &cached_source, compile_options); 457 ScriptCompiler::Compile(context, &cached_source, compile_options);
456 CHECK(data == NULL || !data->rejected); 458 CHECK(data == NULL || !data->rejected);
457 return result; 459 return result;
458 } 460 }
459 461
460 462
461 // Executes a string within the current v8 context. 463 // Executes a string within the current v8 context.
462 bool Shell::ExecuteString(Isolate* isolate, Local<String> source, 464 bool Shell::ExecuteString(Isolate* isolate, Local<String> source,
463 Local<Value> name, bool print_result, 465 Local<Value> name, bool print_result,
464 bool report_exceptions, SourceType source_type) { 466 bool report_exceptions) {
465 HandleScope handle_scope(isolate); 467 HandleScope handle_scope(isolate);
466 TryCatch try_catch(isolate); 468 TryCatch try_catch(isolate);
467 try_catch.SetVerbose(true); 469 try_catch.SetVerbose(true);
468 470
469 MaybeLocal<Value> maybe_result; 471 MaybeLocal<Value> maybe_result;
470 { 472 {
471 PerIsolateData* data = PerIsolateData::Get(isolate); 473 PerIsolateData* data = PerIsolateData::Get(isolate);
472 Local<Context> realm = 474 Local<Context> realm =
473 Local<Context>::New(isolate, data->realms_[data->realm_current_]); 475 Local<Context>::New(isolate, data->realms_[data->realm_current_]);
474 Context::Scope context_scope(realm); 476 Context::Scope context_scope(realm);
475 if (source_type == SCRIPT) { 477 Local<Script> script;
476 Local<Script> script; 478 if (!Shell::CompileString(isolate, source, name, options.compile_options)
477 if (!Shell::CompileString(isolate, source, name, options.compile_options) 479 .ToLocal(&script)) {
478 .ToLocal(&script)) { 480 // Print errors that happened during compilation.
479 // Print errors that happened during compilation. 481 if (report_exceptions) ReportException(isolate, &try_catch);
480 if (report_exceptions) ReportException(isolate, &try_catch); 482 return false;
481 return false;
482 }
483 maybe_result = script->Run(realm);
484 } else {
485 DCHECK_EQ(MODULE, source_type);
486 Local<Module> module;
487 ScriptOrigin origin(name);
488 ScriptCompiler::Source script_source(source, origin);
489 // TODO(adamk): Make use of compile options for Modules.
490 if (!ScriptCompiler::CompileModule(isolate, &script_source)
491 .ToLocal(&module)) {
492 // Print errors that happened during compilation.
493 if (report_exceptions) ReportException(isolate, &try_catch);
494 return false;
495 }
496 // This can't fail until we support linking.
497 CHECK(module->Instantiate(realm));
498 maybe_result = module->Evaluate(realm);
499 } 483 }
484 maybe_result = script->Run(realm);
500 EmptyMessageQueues(isolate); 485 EmptyMessageQueues(isolate);
501 data->realm_current_ = data->realm_switch_; 486 data->realm_current_ = data->realm_switch_;
502 } 487 }
503 Local<Value> result; 488 Local<Value> result;
504 if (!maybe_result.ToLocal(&result)) { 489 if (!maybe_result.ToLocal(&result)) {
505 DCHECK(try_catch.HasCaught()); 490 DCHECK(try_catch.HasCaught());
506 // Print errors that happened during execution. 491 // Print errors that happened during execution.
507 if (report_exceptions) ReportException(isolate, &try_catch); 492 if (report_exceptions) ReportException(isolate, &try_catch);
508 return false; 493 return false;
509 } 494 }
510 DCHECK(!try_catch.HasCaught()); 495 DCHECK(!try_catch.HasCaught());
511 if (print_result) { 496 if (print_result) {
512 if (options.test_shell) { 497 if (options.test_shell) {
513 if (!result->IsUndefined()) { 498 if (!result->IsUndefined()) {
514 // If all went well and the result wasn't undefined then print 499 // If all went well and the result wasn't undefined then print
515 // the returned value. 500 // the returned value.
516 v8::String::Utf8Value str(result); 501 v8::String::Utf8Value str(result);
517 fwrite(*str, sizeof(**str), str.length(), stdout); 502 fwrite(*str, sizeof(**str), str.length(), stdout);
518 printf("\n"); 503 printf("\n");
519 } 504 }
520 } else { 505 } else {
521 v8::String::Utf8Value str(Stringify(isolate, result)); 506 v8::String::Utf8Value str(Stringify(isolate, result));
522 fwrite(*str, sizeof(**str), str.length(), stdout); 507 fwrite(*str, sizeof(**str), str.length(), stdout);
523 printf("\n"); 508 printf("\n");
524 } 509 }
525 } 510 }
526 return true; 511 return true;
527 } 512 }
528 513
514 MaybeLocal<Module> Shell::FetchModuleTree(
515 Isolate* isolate, const std::string& file_name,
516 std::map<std::string, Global<Module>>* module_map) {
517 TryCatch try_catch(isolate);
518 try_catch.SetVerbose(true);
519 Local<String> source_text = ReadFile(isolate, file_name.c_str());
520 if (source_text.IsEmpty()) {
521 printf("Error reading '%s'\n", file_name.c_str());
522 Shell::Exit(1);
523 }
524 ScriptOrigin origin(
525 String::NewFromUtf8(isolate, file_name.c_str(), NewStringType::kNormal)
526 .ToLocalChecked());
527 ScriptCompiler::Source source(source_text, origin);
528 Local<Module> module;
529 if (!ScriptCompiler::CompileModule(isolate, &source).ToLocal(&module)) {
530 ReportException(isolate, &try_catch);
531 return MaybeLocal<Module>();
532 }
533 module_map->insert(
534 std::make_pair(file_name, Global<Module>(isolate, module)));
535 for (int i = 0, length = module->GetModuleRequestsLength(); i < length; ++i) {
536 Local<String> name = module->GetModuleRequest(i);
537 // TODO(adamk): Resolve the imported module to a full path.
538 std::string str = *String::Utf8Value(name);
539 if (!module_map->count(str)) {
540 if (FetchModuleTree(isolate, str, module_map).IsEmpty()) {
541 return MaybeLocal<Module>();
542 }
543 }
544 }
545
546 return module;
547 }
548
549 namespace {
550
551 MaybeLocal<Module> ResolveModuleCallback(Local<Context> context,
552 Local<String> specifier,
553 Local<Module> referrer,
554 Local<Value> data) {
555 Isolate* isolate = context->GetIsolate();
556 auto module_map = static_cast<std::map<std::string, Global<Module>>*>(
557 External::Cast(*data)->Value());
558 std::string str_specifier = *String::Utf8Value(specifier);
559 // TODO(adamk): Resolve the specifier using the referrer
560 auto it = module_map->find(str_specifier);
561 if (it != module_map->end()) {
562 return it->second.Get(isolate);
563 }
564 return MaybeLocal<Module>();
565 }
566
567 } // anonymous namespace
568
569 bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) {
570 HandleScope handle_scope(isolate);
571
572 Local<Module> root_module;
573 std::map<std::string, Global<Module>> module_map;
574 if (!FetchModuleTree(isolate, file_name, &module_map).ToLocal(&root_module)) {
575 return false;
576 }
577
578 TryCatch try_catch(isolate);
579 try_catch.SetVerbose(true);
580
581 MaybeLocal<Value> maybe_result;
582 {
583 PerIsolateData* data = PerIsolateData::Get(isolate);
584 Local<Context> realm = data->realms_[data->realm_current_].Get(isolate);
585 Context::Scope context_scope(realm);
586
587 // This can't fail until we support linking.
588 CHECK(root_module->Instantiate(realm, ResolveModuleCallback,
589 External::New(isolate, &module_map)));
590 maybe_result = root_module->Evaluate(realm);
591 EmptyMessageQueues(isolate);
592 }
593 Local<Value> result;
594 if (!maybe_result.ToLocal(&result)) {
595 DCHECK(try_catch.HasCaught());
596 // Print errors that happened during execution.
597 ReportException(isolate, &try_catch);
598 return false;
599 }
600 DCHECK(!try_catch.HasCaught());
601 return true;
602 }
529 603
530 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) { 604 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) {
531 data_->realm_count_ = 1; 605 data_->realm_count_ = 1;
532 data_->realm_current_ = 0; 606 data_->realm_current_ = 0;
533 data_->realm_switch_ = 0; 607 data_->realm_switch_ = 0;
534 data_->realms_ = new Global<Context>[1]; 608 data_->realms_ = new Global<Context>[1];
535 data_->realms_[0].Reset(data_->isolate_, 609 data_->realms_[0].Reset(data_->isolate_,
536 data_->isolate_->GetEnteredContext()); 610 data_->isolate_->GetEnteredContext());
537 } 611 }
538 612
(...skipping 1034 matching lines...) Expand 10 before | Expand all | Expand 10 after
1573 SourceGroup::~SourceGroup() { 1647 SourceGroup::~SourceGroup() {
1574 delete thread_; 1648 delete thread_;
1575 thread_ = NULL; 1649 thread_ = NULL;
1576 } 1650 }
1577 1651
1578 1652
1579 void SourceGroup::Execute(Isolate* isolate) { 1653 void SourceGroup::Execute(Isolate* isolate) {
1580 bool exception_was_thrown = false; 1654 bool exception_was_thrown = false;
1581 for (int i = begin_offset_; i < end_offset_; ++i) { 1655 for (int i = begin_offset_; i < end_offset_; ++i) {
1582 const char* arg = argv_[i]; 1656 const char* arg = argv_[i];
1583 Shell::SourceType source_type = Shell::SCRIPT;
1584 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { 1657 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) {
1585 // Execute argument given to -e option directly. 1658 // Execute argument given to -e option directly.
1586 HandleScope handle_scope(isolate); 1659 HandleScope handle_scope(isolate);
1587 Local<String> file_name = 1660 Local<String> file_name =
1588 String::NewFromUtf8(isolate, "unnamed", NewStringType::kNormal) 1661 String::NewFromUtf8(isolate, "unnamed", NewStringType::kNormal)
1589 .ToLocalChecked(); 1662 .ToLocalChecked();
1590 Local<String> source = 1663 Local<String> source =
1591 String::NewFromUtf8(isolate, argv_[i + 1], NewStringType::kNormal) 1664 String::NewFromUtf8(isolate, argv_[i + 1], NewStringType::kNormal)
1592 .ToLocalChecked(); 1665 .ToLocalChecked();
1593 Shell::options.script_executed = true; 1666 Shell::options.script_executed = true;
1594 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { 1667 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) {
1595 exception_was_thrown = true; 1668 exception_was_thrown = true;
1596 break; 1669 break;
1597 } 1670 }
1598 ++i; 1671 ++i;
1599 continue; 1672 continue;
1600 } else if (strcmp(arg, "--module") == 0 && i + 1 < end_offset_) { 1673 } else if (strcmp(arg, "--module") == 0 && i + 1 < end_offset_) {
1601 // Treat the next file as a module. 1674 // Treat the next file as a module.
1602 source_type = Shell::MODULE;
1603 arg = argv_[++i]; 1675 arg = argv_[++i];
1676 Shell::options.script_executed = true;
1677 if (!Shell::ExecuteModule(isolate, arg)) {
1678 exception_was_thrown = true;
1679 break;
1680 }
1681 continue;
1604 } else if (arg[0] == '-') { 1682 } else if (arg[0] == '-') {
1605 // Ignore other options. They have been parsed already. 1683 // Ignore other options. They have been parsed already.
1606 continue; 1684 continue;
1607 } 1685 }
1608 1686
1609 // Use all other arguments as names of files to load and run. 1687 // Use all other arguments as names of files to load and run.
1610 HandleScope handle_scope(isolate); 1688 HandleScope handle_scope(isolate);
1611 Local<String> file_name = 1689 Local<String> file_name =
1612 String::NewFromUtf8(isolate, arg, NewStringType::kNormal) 1690 String::NewFromUtf8(isolate, arg, NewStringType::kNormal)
1613 .ToLocalChecked(); 1691 .ToLocalChecked();
1614 Local<String> source = ReadFile(isolate, arg); 1692 Local<String> source = ReadFile(isolate, arg);
1615 if (source.IsEmpty()) { 1693 if (source.IsEmpty()) {
1616 printf("Error reading '%s'\n", arg); 1694 printf("Error reading '%s'\n", arg);
1617 Shell::Exit(1); 1695 Shell::Exit(1);
1618 } 1696 }
1619 Shell::options.script_executed = true; 1697 Shell::options.script_executed = true;
1620 if (!Shell::ExecuteString(isolate, source, file_name, false, true, 1698 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) {
1621 source_type)) {
1622 exception_was_thrown = true; 1699 exception_was_thrown = true;
1623 break; 1700 break;
1624 } 1701 }
1625 } 1702 }
1626 if (exception_was_thrown != Shell::options.expected_to_throw) { 1703 if (exception_was_thrown != Shell::options.expected_to_throw) {
1627 Shell::Exit(1); 1704 Shell::Exit(1);
1628 } 1705 }
1629 } 1706 }
1630 1707
1631 1708
(...skipping 986 matching lines...) Expand 10 before | Expand all | Expand 10 after
2618 } 2695 }
2619 2696
2620 } // namespace v8 2697 } // namespace v8
2621 2698
2622 2699
2623 #ifndef GOOGLE3 2700 #ifndef GOOGLE3
2624 int main(int argc, char* argv[]) { 2701 int main(int argc, char* argv[]) {
2625 return v8::Shell::Main(argc, argv); 2702 return v8::Shell::Main(argc, argv);
2626 } 2703 }
2627 #endif 2704 #endif
OLDNEW
« no previous file with comments | « src/d8.h ('k') | src/factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698