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

Side by Side Diff: src/d8.cc

Issue 2703563002: [ESNext] Implement DynamicImportCall (Closed)
Patch Set: fixes Created 3 years, 9 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.h » ('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>
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 int name_length = 0; 433 int name_length = 0;
434 uint16_t* name_buffer = NULL; 434 uint16_t* name_buffer = NULL;
435 if (name->IsString()) { 435 if (name->IsString()) {
436 Local<String> name_string = Local<String>::Cast(name); 436 Local<String> name_string = Local<String>::Cast(name);
437 name_length = name_string->Length(); 437 name_length = name_string->Length();
438 name_buffer = new uint16_t[name_length]; 438 name_buffer = new uint16_t[name_length];
439 name_string->Write(name_buffer, 0, name_length); 439 name_string->Write(name_buffer, 0, name_length);
440 } 440 }
441 Isolate::CreateParams create_params; 441 Isolate::CreateParams create_params;
442 create_params.array_buffer_allocator = Shell::array_buffer_allocator; 442 create_params.array_buffer_allocator = Shell::array_buffer_allocator;
443 create_params.host_import_module_dynamically_callback_ =
444 Shell::HostImportModuleDynamically;
443 Isolate* temp_isolate = Isolate::New(create_params); 445 Isolate* temp_isolate = Isolate::New(create_params);
444 ScriptCompiler::CachedData* result = NULL; 446 ScriptCompiler::CachedData* result = NULL;
445 { 447 {
446 Isolate::Scope isolate_scope(temp_isolate); 448 Isolate::Scope isolate_scope(temp_isolate);
447 HandleScope handle_scope(temp_isolate); 449 HandleScope handle_scope(temp_isolate);
448 Context::Scope context_scope(Context::New(temp_isolate)); 450 Context::Scope context_scope(Context::New(temp_isolate));
449 Local<String> source_copy = 451 Local<String> source_copy =
450 v8::String::NewFromTwoByte(temp_isolate, source_buffer, 452 v8::String::NewFromTwoByte(temp_isolate, source_buffer,
451 v8::NewStringType::kNormal, 453 v8::NewStringType::kNormal, source_length)
452 source_length).ToLocalChecked(); 454 .ToLocalChecked();
453 Local<Value> name_copy; 455 Local<Value> name_copy;
454 if (name_buffer) { 456 if (name_buffer) {
455 name_copy = v8::String::NewFromTwoByte(temp_isolate, name_buffer, 457 name_copy =
456 v8::NewStringType::kNormal, 458 v8::String::NewFromTwoByte(temp_isolate, name_buffer,
457 name_length).ToLocalChecked(); 459 v8::NewStringType::kNormal, name_length)
460 .ToLocalChecked();
458 } else { 461 } else {
459 name_copy = v8::Undefined(temp_isolate); 462 name_copy = v8::Undefined(temp_isolate);
460 } 463 }
461 ScriptCompiler::Source script_source(source_copy, ScriptOrigin(name_copy)); 464 ScriptCompiler::Source script_source(source_copy, ScriptOrigin(name_copy));
462 if (!ScriptCompiler::CompileUnboundScript(temp_isolate, &script_source, 465 if (!ScriptCompiler::CompileUnboundScript(temp_isolate, &script_source,
463 compile_options).IsEmpty() && 466 compile_options)
467 .IsEmpty() &&
464 script_source.GetCachedData()) { 468 script_source.GetCachedData()) {
465 int length = script_source.GetCachedData()->length; 469 int length = script_source.GetCachedData()->length;
466 uint8_t* cache = new uint8_t[length]; 470 uint8_t* cache = new uint8_t[length];
467 memcpy(cache, script_source.GetCachedData()->data, length); 471 memcpy(cache, script_source.GetCachedData()->data, length);
468 result = new ScriptCompiler::CachedData( 472 result = new ScriptCompiler::CachedData(
469 cache, length, ScriptCompiler::CachedData::BufferOwned); 473 cache, length, ScriptCompiler::CachedData::BufferOwned);
470 } 474 }
471 } 475 }
472 temp_isolate->Dispose(); 476 temp_isolate->Dispose();
473 delete[] source_buffer; 477 delete[] source_buffer;
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 std::string absolute_path = 679 std::string absolute_path =
676 NormalizePath(ToSTLString(specifier), dir_name_it->second); 680 NormalizePath(ToSTLString(specifier), dir_name_it->second);
677 auto module_it = d->specifier_to_module_map.find(absolute_path); 681 auto module_it = d->specifier_to_module_map.find(absolute_path);
678 CHECK(module_it != d->specifier_to_module_map.end()); 682 CHECK(module_it != d->specifier_to_module_map.end());
679 return module_it->second.Get(isolate); 683 return module_it->second.Get(isolate);
680 } 684 }
681 685
682 } // anonymous namespace 686 } // anonymous namespace
683 687
684 MaybeLocal<Module> Shell::FetchModuleTree(Local<Context> context, 688 MaybeLocal<Module> Shell::FetchModuleTree(Local<Context> context,
685 const std::string& file_name) { 689 const std::string& file_name,
690 MaybeLocal<Value>* maybe_exception) {
686 DCHECK(IsAbsolutePath(file_name)); 691 DCHECK(IsAbsolutePath(file_name));
687 Isolate* isolate = context->GetIsolate(); 692 Isolate* isolate = context->GetIsolate();
688 TryCatch try_catch(isolate); 693 TryCatch try_catch(isolate);
689 try_catch.SetVerbose(true); 694 try_catch.SetVerbose(true);
690 Local<String> source_text = ReadFile(isolate, file_name.c_str()); 695 Local<String> source_text = ReadFile(isolate, file_name.c_str());
691 if (source_text.IsEmpty()) { 696 if (source_text.IsEmpty()) {
692 printf("Error reading '%s'\n", file_name.c_str()); 697 printf("Error reading '%s'\n", file_name.c_str());
693 Shell::Exit(1); 698 return MaybeLocal<Module>();
694 } 699 }
695 ScriptOrigin origin( 700 ScriptOrigin origin(
696 String::NewFromUtf8(isolate, file_name.c_str(), NewStringType::kNormal) 701 String::NewFromUtf8(isolate, file_name.c_str(), NewStringType::kNormal)
697 .ToLocalChecked(), 702 .ToLocalChecked(),
698 Local<Integer>(), Local<Integer>(), Local<Boolean>(), Local<Integer>(), 703 Local<Integer>(), Local<Integer>(), Local<Boolean>(), Local<Integer>(),
699 Local<Value>(), Local<Boolean>(), Local<Boolean>(), True(isolate)); 704 Local<Value>(), Local<Boolean>(), Local<Boolean>(), True(isolate));
700 ScriptCompiler::Source source(source_text, origin); 705 ScriptCompiler::Source source(source_text, origin);
701 Local<Module> module; 706 Local<Module> module;
702 if (!ScriptCompiler::CompileModule(isolate, &source).ToLocal(&module)) { 707 if (!ScriptCompiler::CompileModule(isolate, &source).ToLocal(&module)) {
703 ReportException(isolate, &try_catch); 708 *maybe_exception = try_catch.Exception();
704 return MaybeLocal<Module>(); 709 return MaybeLocal<Module>();
705 } 710 }
706 711
707 ModuleEmbedderData* d = GetModuleDataFromContext(context); 712 ModuleEmbedderData* d = GetModuleDataFromContext(context);
708 CHECK(d->specifier_to_module_map 713 CHECK(d->specifier_to_module_map
709 .insert(std::make_pair(file_name, Global<Module>(isolate, module))) 714 .insert(std::make_pair(file_name, Global<Module>(isolate, module)))
710 .second); 715 .second);
711 716
712 std::string dir_name = DirName(file_name); 717 std::string dir_name = DirName(file_name);
713 CHECK(d->module_to_directory_map 718 CHECK(d->module_to_directory_map
714 .insert(std::make_pair(Global<Module>(isolate, module), dir_name)) 719 .insert(std::make_pair(Global<Module>(isolate, module), dir_name))
715 .second); 720 .second);
716 721
717 for (int i = 0, length = module->GetModuleRequestsLength(); i < length; ++i) { 722 for (int i = 0, length = module->GetModuleRequestsLength(); i < length; ++i) {
718 Local<String> name = module->GetModuleRequest(i); 723 Local<String> name = module->GetModuleRequest(i);
719 std::string absolute_path = NormalizePath(ToSTLString(name), dir_name); 724 std::string absolute_path = NormalizePath(ToSTLString(name), dir_name);
720 if (!d->specifier_to_module_map.count(absolute_path)) { 725 if (!d->specifier_to_module_map.count(absolute_path)) {
721 if (FetchModuleTree(context, absolute_path).IsEmpty()) { 726 if (FetchModuleTree(context, absolute_path, maybe_exception).IsEmpty()) {
722 return MaybeLocal<Module>(); 727 return MaybeLocal<Module>();
723 } 728 }
724 } 729 }
725 } 730 }
726 731
727 return module; 732 return module;
728 } 733 }
729 734
735 namespace {
736
737 struct DynamicImportData {
738 public:
739 DynamicImportData(Isolate* isolate_, Local<String> referrer_,
740 Local<String> specifier_, Local<Promise> promise_)
741 : isolate(isolate_) {
742 referrer.Reset(isolate, referrer_);
743 specifier.Reset(isolate, specifier_);
744 promise.Reset(isolate, promise_);
745 }
746
747 Isolate* isolate;
748 Global<String> referrer;
749 Global<String> specifier;
750 Global<Promise> promise;
751 };
752
753 } // namespace
754 void Shell::HostImportModuleDynamically(Isolate* isolate,
755 Local<String> referrer,
756 Local<String> specifier,
757 Local<Promise> promise) {
758 DynamicImportData* data =
759 new DynamicImportData(isolate, referrer, specifier, promise);
760 isolate->EnqueueMicrotask(Shell::DoHostImportModuleDynamically, data);
761 }
762
763 void Shell::DoHostImportModuleDynamically(void* import_data) {
764 std::unique_ptr<DynamicImportData> import_data_(
765 static_cast<DynamicImportData*>(import_data));
766 Isolate* isolate(import_data_->isolate);
767 HandleScope handle_scope(isolate);
768
769 Local<String> referrer(import_data_->referrer.Get(isolate));
770 Local<String> specifier(import_data_->specifier.Get(isolate));
771 Local<Promise> promise(import_data_->promise.Get(isolate));
772
773 PerIsolateData* data = PerIsolateData::Get(isolate);
774 Local<Context> realm = data->realms_[data->realm_current_].Get(isolate);
775 Context::Scope context_scope(realm);
776
777 std::string source_url = ToSTLString(referrer);
778 std::string dir_name =
779 IsAbsolutePath(source_url) ? DirName(source_url) : GetWorkingDirectory();
780 std::string file_name = ToSTLString(specifier);
781 std::string absolute_path = NormalizePath(file_name.c_str(), dir_name);
782
783 ModuleEmbedderData* d = GetModuleDataFromContext(realm);
784 Local<Module> root_module;
785 MaybeLocal<Value> maybe_exception;
786 auto module_it = d->specifier_to_module_map.find(absolute_path);
787 if (module_it != d->specifier_to_module_map.end()) {
788 root_module = module_it->second.Get(isolate);
789 } else if (!FetchModuleTree(realm, absolute_path, &maybe_exception)
790 .ToLocal(&root_module)) {
791 if (!maybe_exception.IsEmpty()) {
adamk 2017/03/17 01:00:13 How could FetchModuleTree fail without an exceptio
gsathya 2017/03/17 01:13:11 Changed to be a CHECK
792 CHECK(Module::FinishDynamicImportFailure(
793 realm, promise, maybe_exception.ToLocalChecked()));
794 return;
795 }
796 }
797
798 TryCatch try_catch(isolate);
799 try_catch.SetVerbose(true);
800
801 MaybeLocal<Value> maybe_result;
802 if (root_module->Instantiate(realm, ResolveModuleCallback)) {
803 maybe_result = root_module->Evaluate(realm);
804 EmptyMessageQueues(isolate);
805 }
806
807 Local<Value> result;
808 if (!maybe_result.ToLocal(&result)) {
809 DCHECK(try_catch.HasCaught());
810 CHECK(Module::FinishDynamicImportFailure(realm, promise,
811 try_catch.Exception()));
812 return;
813 }
814
815 DCHECK(!try_catch.HasCaught());
816 CHECK(Module::FinishDynamicImportSuccess(realm, promise, root_module));
817 }
818
730 bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) { 819 bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) {
731 HandleScope handle_scope(isolate); 820 HandleScope handle_scope(isolate);
732 821
733 PerIsolateData* data = PerIsolateData::Get(isolate); 822 PerIsolateData* data = PerIsolateData::Get(isolate);
734 Local<Context> realm = data->realms_[data->realm_current_].Get(isolate); 823 Local<Context> realm = data->realms_[data->realm_current_].Get(isolate);
735 Context::Scope context_scope(realm); 824 Context::Scope context_scope(realm);
736 825
737 std::string absolute_path = NormalizePath(file_name, GetWorkingDirectory()); 826 std::string absolute_path = NormalizePath(file_name, GetWorkingDirectory());
738 827
739 Local<Module> root_module; 828 Local<Module> root_module;
740 if (!FetchModuleTree(realm, absolute_path).ToLocal(&root_module)) { 829 MaybeLocal<Value> maybe_exception;
830 if (!FetchModuleTree(realm, absolute_path, &maybe_exception)
831 .ToLocal(&root_module)) {
832 if (!maybe_exception.IsEmpty()) {
adamk 2017/03/17 01:00:13 Same question here.
gsathya 2017/03/17 01:13:11 Done.
833 v8::String::Utf8Value exception(maybe_exception.ToLocalChecked());
834 CHECK(*exception);
835 const char* exception_string = *exception;
836 printf("%s\n", exception_string);
837 }
741 return false; 838 return false;
742 } 839 }
743 840
744 TryCatch try_catch(isolate); 841 TryCatch try_catch(isolate);
745 try_catch.SetVerbose(true); 842 try_catch.SetVerbose(true);
746 843
747 MaybeLocal<Value> maybe_result; 844 MaybeLocal<Value> maybe_result;
748 if (root_module->Instantiate(realm, ResolveModuleCallback)) { 845 if (root_module->Instantiate(realm, ResolveModuleCallback)) {
749 maybe_result = root_module->Evaluate(realm); 846 maybe_result = root_module->Evaluate(realm);
750 EmptyMessageQueues(isolate); 847 EmptyMessageQueues(isolate);
(...skipping 1408 matching lines...) Expand 10 before | Expand all | Expand 10 after
2159 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less 2256 // On some systems (OSX 10.6) the stack size default is 0.5Mb or less
2160 // which is not enough to parse the big literal expressions used in tests. 2257 // which is not enough to parse the big literal expressions used in tests.
2161 // The stack size should be at least StackGuard::kLimitSize + some 2258 // The stack size should be at least StackGuard::kLimitSize + some
2162 // OS-specific padding for thread startup code. 2Mbytes seems to be enough. 2259 // OS-specific padding for thread startup code. 2Mbytes seems to be enough.
2163 return base::Thread::Options("IsolateThread", 2 * MB); 2260 return base::Thread::Options("IsolateThread", 2 * MB);
2164 } 2261 }
2165 2262
2166 void SourceGroup::ExecuteInThread() { 2263 void SourceGroup::ExecuteInThread() {
2167 Isolate::CreateParams create_params; 2264 Isolate::CreateParams create_params;
2168 create_params.array_buffer_allocator = Shell::array_buffer_allocator; 2265 create_params.array_buffer_allocator = Shell::array_buffer_allocator;
2266 create_params.host_import_module_dynamically_callback_ =
2267 Shell::HostImportModuleDynamically;
2169 Isolate* isolate = Isolate::New(create_params); 2268 Isolate* isolate = Isolate::New(create_params);
2170 for (int i = 0; i < Shell::options.stress_runs; ++i) { 2269 for (int i = 0; i < Shell::options.stress_runs; ++i) {
2171 next_semaphore_.Wait(); 2270 next_semaphore_.Wait();
2172 { 2271 {
2173 Isolate::Scope iscope(isolate); 2272 Isolate::Scope iscope(isolate);
2174 { 2273 {
2175 HandleScope scope(isolate); 2274 HandleScope scope(isolate);
2176 PerIsolateData data(isolate); 2275 PerIsolateData data(isolate);
2177 Local<Context> context = Shell::CreateEvaluationContext(isolate); 2276 Local<Context> context = Shell::CreateEvaluationContext(isolate);
2178 { 2277 {
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
2297 2396
2298 void Worker::WaitForThread() { 2397 void Worker::WaitForThread() {
2299 Terminate(); 2398 Terminate();
2300 thread_->Join(); 2399 thread_->Join();
2301 } 2400 }
2302 2401
2303 2402
2304 void Worker::ExecuteInThread() { 2403 void Worker::ExecuteInThread() {
2305 Isolate::CreateParams create_params; 2404 Isolate::CreateParams create_params;
2306 create_params.array_buffer_allocator = Shell::array_buffer_allocator; 2405 create_params.array_buffer_allocator = Shell::array_buffer_allocator;
2406 create_params.host_import_module_dynamically_callback_ =
2407 Shell::HostImportModuleDynamically;
2307 Isolate* isolate = Isolate::New(create_params); 2408 Isolate* isolate = Isolate::New(create_params);
2308 { 2409 {
2309 Isolate::Scope iscope(isolate); 2410 Isolate::Scope iscope(isolate);
2310 { 2411 {
2311 HandleScope scope(isolate); 2412 HandleScope scope(isolate);
2312 PerIsolateData data(isolate); 2413 PerIsolateData data(isolate);
2313 Local<Context> context = Shell::CreateEvaluationContext(isolate); 2414 Local<Context> context = Shell::CreateEvaluationContext(isolate);
2314 { 2415 {
2315 Context::Scope cscope(context); 2416 Context::Scope cscope(context);
2316 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); 2417 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
(...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after
2947 base::SysInfo::AmountOfPhysicalMemory(), 3048 base::SysInfo::AmountOfPhysicalMemory(),
2948 base::SysInfo::AmountOfVirtualMemory()); 3049 base::SysInfo::AmountOfVirtualMemory());
2949 3050
2950 Shell::counter_map_ = new CounterMap(); 3051 Shell::counter_map_ = new CounterMap();
2951 if (i::FLAG_dump_counters || i::FLAG_dump_counters_nvp || i::FLAG_gc_stats) { 3052 if (i::FLAG_dump_counters || i::FLAG_dump_counters_nvp || i::FLAG_gc_stats) {
2952 create_params.counter_lookup_callback = LookupCounter; 3053 create_params.counter_lookup_callback = LookupCounter;
2953 create_params.create_histogram_callback = CreateHistogram; 3054 create_params.create_histogram_callback = CreateHistogram;
2954 create_params.add_histogram_sample_callback = AddHistogramSample; 3055 create_params.add_histogram_sample_callback = AddHistogramSample;
2955 } 3056 }
2956 3057
3058 create_params.host_import_module_dynamically_callback_ =
3059 Shell::HostImportModuleDynamically;
3060
2957 if (i::trap_handler::UseTrapHandler()) { 3061 if (i::trap_handler::UseTrapHandler()) {
2958 if (!v8::V8::RegisterDefaultSignalHandler()) { 3062 if (!v8::V8::RegisterDefaultSignalHandler()) {
2959 fprintf(stderr, "Could not register signal handler"); 3063 fprintf(stderr, "Could not register signal handler");
2960 exit(1); 3064 exit(1);
2961 } 3065 }
2962 } 3066 }
2963 3067
2964 Isolate* isolate = Isolate::New(create_params); 3068 Isolate* isolate = Isolate::New(create_params);
2965 { 3069 {
2966 Isolate::Scope scope(isolate); 3070 Isolate::Scope scope(isolate);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
3043 } 3147 }
3044 3148
3045 } // namespace v8 3149 } // namespace v8
3046 3150
3047 3151
3048 #ifndef GOOGLE3 3152 #ifndef GOOGLE3
3049 int main(int argc, char* argv[]) { 3153 int main(int argc, char* argv[]) {
3050 return v8::Shell::Main(argc, argv); 3154 return v8::Shell::Main(argc, argv);
3051 } 3155 }
3052 #endif 3156 #endif
OLDNEW
« no previous file with comments | « src/d8.h ('k') | src/factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698