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

Side by Side Diff: src/d8.cc

Issue 2351113004: [modules] Expand API to allow linking and use it in d8 (Closed)
Patch Set: Change return value of FetchModuleTree 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
« src/api.cc ('K') | « 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 int num_imports = module->GetModuleRequestsLength();
neis 2016/09/20 21:07:30 Please rename to length and move into the for.
adamk 2016/09/20 21:13:58 Done.
536 for (int i = 0; i < num_imports; ++i) {
537 Local<String> import_name = module->GetModuleRequest(i);
neis 2016/09/20 21:07:30 Please rename to name.
adamk 2016/09/20 21:13:58 Done.
538 // TODO(adamk): Resolve the imported module to a full path.
539 std::string str = *String::Utf8Value(import_name);
540 if (!module_map->count(str)) {
541 if (FetchModuleTree(isolate, str, module_map).IsEmpty()) {
542 return MaybeLocal<Module>();
543 }
544 }
545 }
546
547 return module;
548 }
549
550 namespace {
551
552 MaybeLocal<Module> ResolveModuleCallback(Local<Context> context,
553 Local<String> specifier,
554 Local<Module> referrer,
555 Local<Value> data) {
556 Isolate* isolate = context->GetIsolate();
557 auto module_map = static_cast<std::map<std::string, Global<Module>>*>(
558 External::Cast(*data)->Value());
559 std::string str_specifier = *String::Utf8Value(specifier);
560 // TODO(adamk): Resolve the specifier using the referrer
561 auto it = module_map->find(str_specifier);
562 if (it != module_map->end()) {
563 return it->second.Get(isolate);
564 }
565 return MaybeLocal<Module>();
566 }
567
568 } // anonymous namespace
569
570 bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) {
571 HandleScope handle_scope(isolate);
572
573 Local<Module> root_module;
574 std::map<std::string, Global<Module>> module_map;
575 if (!FetchModuleTree(isolate, file_name, &module_map).ToLocal(&root_module)) {
576 return false;
577 }
578
579 TryCatch try_catch(isolate);
580 try_catch.SetVerbose(true);
581
582 MaybeLocal<Value> maybe_result;
583 {
584 PerIsolateData* data = PerIsolateData::Get(isolate);
585 Local<Context> realm =
586 Local<Context>::New(isolate, data->realms_[data->realm_current_]);
587 Context::Scope context_scope(realm);
588
589 // This can't fail until we support linking.
590 CHECK(root_module->Instantiate(realm, ResolveModuleCallback,
591 External::New(isolate, &module_map)));
592 maybe_result = root_module->Evaluate(realm);
593 EmptyMessageQueues(isolate);
594 data->realm_current_ = data->realm_switch_;
neis 2016/09/20 21:07:30 Please remove this if possible.
adamk 2016/09/20 21:13:58 Removed this. I'm still using the current realm to
595 }
596 Local<Value> result;
597 if (!maybe_result.ToLocal(&result)) {
598 DCHECK(try_catch.HasCaught());
599 // Print errors that happened during execution.
600 ReportException(isolate, &try_catch);
601 return false;
602 }
603 DCHECK(!try_catch.HasCaught());
604 return true;
605 }
529 606
530 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) { 607 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) {
531 data_->realm_count_ = 1; 608 data_->realm_count_ = 1;
532 data_->realm_current_ = 0; 609 data_->realm_current_ = 0;
533 data_->realm_switch_ = 0; 610 data_->realm_switch_ = 0;
534 data_->realms_ = new Global<Context>[1]; 611 data_->realms_ = new Global<Context>[1];
535 data_->realms_[0].Reset(data_->isolate_, 612 data_->realms_[0].Reset(data_->isolate_,
536 data_->isolate_->GetEnteredContext()); 613 data_->isolate_->GetEnteredContext());
537 } 614 }
538 615
(...skipping 1034 matching lines...) Expand 10 before | Expand all | Expand 10 after
1573 SourceGroup::~SourceGroup() { 1650 SourceGroup::~SourceGroup() {
1574 delete thread_; 1651 delete thread_;
1575 thread_ = NULL; 1652 thread_ = NULL;
1576 } 1653 }
1577 1654
1578 1655
1579 void SourceGroup::Execute(Isolate* isolate) { 1656 void SourceGroup::Execute(Isolate* isolate) {
1580 bool exception_was_thrown = false; 1657 bool exception_was_thrown = false;
1581 for (int i = begin_offset_; i < end_offset_; ++i) { 1658 for (int i = begin_offset_; i < end_offset_; ++i) {
1582 const char* arg = argv_[i]; 1659 const char* arg = argv_[i];
1583 Shell::SourceType source_type = Shell::SCRIPT;
1584 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) { 1660 if (strcmp(arg, "-e") == 0 && i + 1 < end_offset_) {
1585 // Execute argument given to -e option directly. 1661 // Execute argument given to -e option directly.
1586 HandleScope handle_scope(isolate); 1662 HandleScope handle_scope(isolate);
1587 Local<String> file_name = 1663 Local<String> file_name =
1588 String::NewFromUtf8(isolate, "unnamed", NewStringType::kNormal) 1664 String::NewFromUtf8(isolate, "unnamed", NewStringType::kNormal)
1589 .ToLocalChecked(); 1665 .ToLocalChecked();
1590 Local<String> source = 1666 Local<String> source =
1591 String::NewFromUtf8(isolate, argv_[i + 1], NewStringType::kNormal) 1667 String::NewFromUtf8(isolate, argv_[i + 1], NewStringType::kNormal)
1592 .ToLocalChecked(); 1668 .ToLocalChecked();
1593 Shell::options.script_executed = true; 1669 Shell::options.script_executed = true;
1594 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) { 1670 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) {
1595 exception_was_thrown = true; 1671 exception_was_thrown = true;
1596 break; 1672 break;
1597 } 1673 }
1598 ++i; 1674 ++i;
1599 continue; 1675 continue;
1600 } else if (strcmp(arg, "--module") == 0 && i + 1 < end_offset_) { 1676 } else if (strcmp(arg, "--module") == 0 && i + 1 < end_offset_) {
1601 // Treat the next file as a module. 1677 // Treat the next file as a module.
1602 source_type = Shell::MODULE;
1603 arg = argv_[++i]; 1678 arg = argv_[++i];
1679 Shell::options.script_executed = true;
1680 if (!Shell::ExecuteModule(isolate, arg)) {
1681 exception_was_thrown = true;
1682 break;
1683 }
1684 continue;
1604 } else if (arg[0] == '-') { 1685 } else if (arg[0] == '-') {
1605 // Ignore other options. They have been parsed already. 1686 // Ignore other options. They have been parsed already.
1606 continue; 1687 continue;
1607 } 1688 }
1608 1689
1609 // Use all other arguments as names of files to load and run. 1690 // Use all other arguments as names of files to load and run.
1610 HandleScope handle_scope(isolate); 1691 HandleScope handle_scope(isolate);
1611 Local<String> file_name = 1692 Local<String> file_name =
1612 String::NewFromUtf8(isolate, arg, NewStringType::kNormal) 1693 String::NewFromUtf8(isolate, arg, NewStringType::kNormal)
1613 .ToLocalChecked(); 1694 .ToLocalChecked();
1614 Local<String> source = ReadFile(isolate, arg); 1695 Local<String> source = ReadFile(isolate, arg);
1615 if (source.IsEmpty()) { 1696 if (source.IsEmpty()) {
1616 printf("Error reading '%s'\n", arg); 1697 printf("Error reading '%s'\n", arg);
1617 Shell::Exit(1); 1698 Shell::Exit(1);
1618 } 1699 }
1619 Shell::options.script_executed = true; 1700 Shell::options.script_executed = true;
1620 if (!Shell::ExecuteString(isolate, source, file_name, false, true, 1701 if (!Shell::ExecuteString(isolate, source, file_name, false, true)) {
1621 source_type)) {
1622 exception_was_thrown = true; 1702 exception_was_thrown = true;
1623 break; 1703 break;
1624 } 1704 }
1625 } 1705 }
1626 if (exception_was_thrown != Shell::options.expected_to_throw) { 1706 if (exception_was_thrown != Shell::options.expected_to_throw) {
1627 Shell::Exit(1); 1707 Shell::Exit(1);
1628 } 1708 }
1629 } 1709 }
1630 1710
1631 1711
(...skipping 986 matching lines...) Expand 10 before | Expand all | Expand 10 after
2618 } 2698 }
2619 2699
2620 } // namespace v8 2700 } // namespace v8
2621 2701
2622 2702
2623 #ifndef GOOGLE3 2703 #ifndef GOOGLE3
2624 int main(int argc, char* argv[]) { 2704 int main(int argc, char* argv[]) {
2625 return v8::Shell::Main(argc, argv); 2705 return v8::Shell::Main(argc, argv);
2626 } 2706 }
2627 #endif 2707 #endif
OLDNEW
« src/api.cc ('K') | « src/d8.h ('k') | src/factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698