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

Side by Side Diff: src/d8.cc

Issue 2703563002: [ESNext] Implement DynamicImportCall (Closed)
Patch Set: simplify error handling 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
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 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
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) {
686 DCHECK(IsAbsolutePath(file_name)); 690 DCHECK(IsAbsolutePath(file_name));
687 Isolate* isolate = context->GetIsolate(); 691 Isolate* isolate = context->GetIsolate();
688 TryCatch try_catch(isolate);
689 try_catch.SetVerbose(true);
690 Local<String> source_text = ReadFile(isolate, file_name.c_str()); 692 Local<String> source_text = ReadFile(isolate, file_name.c_str());
691 if (source_text.IsEmpty()) { 693 if (source_text.IsEmpty()) {
692 printf("Error reading '%s'\n", file_name.c_str()); 694 printf("Error reading '%s'\n", file_name.c_str());
693 Shell::Exit(1); 695 return MaybeLocal<Module>();
694 } 696 }
695 ScriptOrigin origin( 697 ScriptOrigin origin(
696 String::NewFromUtf8(isolate, file_name.c_str(), NewStringType::kNormal) 698 String::NewFromUtf8(isolate, file_name.c_str(), NewStringType::kNormal)
697 .ToLocalChecked(), 699 .ToLocalChecked(),
698 Local<Integer>(), Local<Integer>(), Local<Boolean>(), Local<Integer>(), 700 Local<Integer>(), Local<Integer>(), Local<Boolean>(), Local<Integer>(),
699 Local<Value>(), Local<Boolean>(), Local<Boolean>(), True(isolate)); 701 Local<Value>(), Local<Boolean>(), Local<Boolean>(), True(isolate));
700 ScriptCompiler::Source source(source_text, origin); 702 ScriptCompiler::Source source(source_text, origin);
701 Local<Module> module; 703 Local<Module> module;
702 if (!ScriptCompiler::CompileModule(isolate, &source).ToLocal(&module)) { 704 if (!ScriptCompiler::CompileModule(isolate, &source).ToLocal(&module)) {
703 ReportException(isolate, &try_catch);
704 return MaybeLocal<Module>(); 705 return MaybeLocal<Module>();
neis 2017/03/17 10:36:31 I'm confused by these changes. Don't we want to th
gsathya 2017/03/17 21:47:59 The exception isn't caught here, but in the caller
neis 2017/03/20 09:36:03 Yes but there is no exception to catch! That's why
gsathya 2017/03/22 03:08:34 The problem here is that throwing an exception her
neis 2017/03/22 10:08:20 I made the following change: --- a/src/d8.cc +++
705 } 706 }
706 707
707 ModuleEmbedderData* d = GetModuleDataFromContext(context); 708 ModuleEmbedderData* d = GetModuleDataFromContext(context);
708 CHECK(d->specifier_to_module_map 709 CHECK(d->specifier_to_module_map
709 .insert(std::make_pair(file_name, Global<Module>(isolate, module))) 710 .insert(std::make_pair(file_name, Global<Module>(isolate, module)))
710 .second); 711 .second);
711 712
712 std::string dir_name = DirName(file_name); 713 std::string dir_name = DirName(file_name);
713 CHECK(d->module_to_directory_map 714 CHECK(d->module_to_directory_map
714 .insert(std::make_pair(Global<Module>(isolate, module), dir_name)) 715 .insert(std::make_pair(Global<Module>(isolate, module), dir_name))
715 .second); 716 .second);
716 717
717 for (int i = 0, length = module->GetModuleRequestsLength(); i < length; ++i) { 718 for (int i = 0, length = module->GetModuleRequestsLength(); i < length; ++i) {
718 Local<String> name = module->GetModuleRequest(i); 719 Local<String> name = module->GetModuleRequest(i);
719 std::string absolute_path = NormalizePath(ToSTLString(name), dir_name); 720 std::string absolute_path = NormalizePath(ToSTLString(name), dir_name);
720 if (!d->specifier_to_module_map.count(absolute_path)) { 721 if (!d->specifier_to_module_map.count(absolute_path)) {
721 if (FetchModuleTree(context, absolute_path).IsEmpty()) { 722 if (FetchModuleTree(context, absolute_path).IsEmpty()) {
722 return MaybeLocal<Module>(); 723 return MaybeLocal<Module>();
723 } 724 }
724 } 725 }
725 } 726 }
726 727
727 return module; 728 return module;
728 } 729 }
729 730
731 namespace {
732
733 struct DynamicImportData {
734 public:
neis 2017/03/17 10:36:31 No need for "public:" here.
gsathya 2017/03/17 21:47:59 Done.
735 DynamicImportData(Isolate* isolate_, Local<String> referrer_,
736 Local<String> specifier_, Local<Promise> promise_)
737 : isolate(isolate_) {
738 referrer.Reset(isolate, referrer_);
739 specifier.Reset(isolate, specifier_);
740 promise.Reset(isolate, promise_);
741 }
742
743 Isolate* isolate;
744 Global<String> referrer;
745 Global<String> specifier;
746 Global<Promise> promise;
747 };
748
749 } // namespace
750 void Shell::HostImportModuleDynamically(Isolate* isolate,
751 Local<String> referrer,
752 Local<String> specifier,
753 Local<Promise> promise) {
754 DynamicImportData* data =
755 new DynamicImportData(isolate, referrer, specifier, promise);
756 isolate->EnqueueMicrotask(Shell::DoHostImportModuleDynamically, data);
757 }
758
759 void Shell::DoHostImportModuleDynamically(void* import_data) {
760 std::unique_ptr<DynamicImportData> import_data_(
761 static_cast<DynamicImportData*>(import_data));
762 Isolate* isolate(import_data_->isolate);
763 HandleScope handle_scope(isolate);
764
765 Local<String> referrer(import_data_->referrer.Get(isolate));
766 Local<String> specifier(import_data_->specifier.Get(isolate));
767 Local<Promise> promise(import_data_->promise.Get(isolate));
768
769 PerIsolateData* data = PerIsolateData::Get(isolate);
770 Local<Context> realm = data->realms_[data->realm_current_].Get(isolate);
771 Context::Scope context_scope(realm);
772
773 std::string source_url = ToSTLString(referrer);
774 std::string dir_name =
775 IsAbsolutePath(source_url) ? DirName(source_url) : GetWorkingDirectory();
776 std::string file_name = ToSTLString(specifier);
777 std::string absolute_path = NormalizePath(file_name.c_str(), dir_name);
778
779 TryCatch try_catch(isolate);
780 try_catch.SetVerbose(true);
781
782 ModuleEmbedderData* d = GetModuleDataFromContext(realm);
783 Local<Module> root_module;
784 auto module_it = d->specifier_to_module_map.find(absolute_path);
785 if (module_it != d->specifier_to_module_map.end()) {
786 root_module = module_it->second.Get(isolate);
787 } else if (!FetchModuleTree(realm, absolute_path).ToLocal(&root_module)) {
788 CHECK(try_catch.HasCaught());
789 CHECK(Module::FinishDynamicImportFailure(realm, promise,
790 try_catch.Exception()));
791 return;
792 }
793
794 MaybeLocal<Value> maybe_result;
795 if (root_module->Instantiate(realm, ResolveModuleCallback)) {
796 maybe_result = root_module->Evaluate(realm);
797 EmptyMessageQueues(isolate);
neis 2017/03/17 12:43:07 Can you explain why this call is needed? (I'm not
gsathya 2017/03/17 21:47:59 This is from ExecuteModule (I tried to mirror the
798 }
799
800 Local<Value> result;
801 if (!maybe_result.ToLocal(&result)) {
802 DCHECK(try_catch.HasCaught());
803 CHECK(Module::FinishDynamicImportFailure(realm, promise,
804 try_catch.Exception()));
805 return;
806 }
807
808 DCHECK(!try_catch.HasCaught());
809 CHECK(Module::FinishDynamicImportSuccess(realm, promise, root_module));
810 }
811
730 bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) { 812 bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) {
731 HandleScope handle_scope(isolate); 813 HandleScope handle_scope(isolate);
732 814
733 PerIsolateData* data = PerIsolateData::Get(isolate); 815 PerIsolateData* data = PerIsolateData::Get(isolate);
734 Local<Context> realm = data->realms_[data->realm_current_].Get(isolate); 816 Local<Context> realm = data->realms_[data->realm_current_].Get(isolate);
735 Context::Scope context_scope(realm); 817 Context::Scope context_scope(realm);
736 818
737 std::string absolute_path = NormalizePath(file_name, GetWorkingDirectory()); 819 std::string absolute_path = NormalizePath(file_name, GetWorkingDirectory());
738 820
739 Local<Module> root_module;
740 if (!FetchModuleTree(realm, absolute_path).ToLocal(&root_module)) {
741 return false;
742 }
743
744 TryCatch try_catch(isolate); 821 TryCatch try_catch(isolate);
745 try_catch.SetVerbose(true); 822 try_catch.SetVerbose(true);
746 823
824 Local<Module> root_module;
825 MaybeLocal<Value> maybe_exception;
826
827 if (!FetchModuleTree(realm, absolute_path).ToLocal(&root_module)) {
828 CHECK(try_catch.HasCaught());
829 ReportException(isolate, &try_catch);
830 return false;
831 }
832
747 MaybeLocal<Value> maybe_result; 833 MaybeLocal<Value> maybe_result;
748 if (root_module->Instantiate(realm, ResolveModuleCallback)) { 834 if (root_module->Instantiate(realm, ResolveModuleCallback)) {
749 maybe_result = root_module->Evaluate(realm); 835 maybe_result = root_module->Evaluate(realm);
750 EmptyMessageQueues(isolate); 836 EmptyMessageQueues(isolate);
751 } 837 }
752 Local<Value> result; 838 Local<Value> result;
753 if (!maybe_result.ToLocal(&result)) { 839 if (!maybe_result.ToLocal(&result)) {
754 DCHECK(try_catch.HasCaught()); 840 DCHECK(try_catch.HasCaught());
755 // Print errors that happened during execution. 841 // Print errors that happened during execution.
756 ReportException(isolate, &try_catch); 842 ReportException(isolate, &try_catch);
(...skipping 1402 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 2245 // 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. 2246 // which is not enough to parse the big literal expressions used in tests.
2161 // The stack size should be at least StackGuard::kLimitSize + some 2247 // The stack size should be at least StackGuard::kLimitSize + some
2162 // OS-specific padding for thread startup code. 2Mbytes seems to be enough. 2248 // OS-specific padding for thread startup code. 2Mbytes seems to be enough.
2163 return base::Thread::Options("IsolateThread", 2 * MB); 2249 return base::Thread::Options("IsolateThread", 2 * MB);
2164 } 2250 }
2165 2251
2166 void SourceGroup::ExecuteInThread() { 2252 void SourceGroup::ExecuteInThread() {
2167 Isolate::CreateParams create_params; 2253 Isolate::CreateParams create_params;
2168 create_params.array_buffer_allocator = Shell::array_buffer_allocator; 2254 create_params.array_buffer_allocator = Shell::array_buffer_allocator;
2255 create_params.host_import_module_dynamically_callback_ =
2256 Shell::HostImportModuleDynamically;
2169 Isolate* isolate = Isolate::New(create_params); 2257 Isolate* isolate = Isolate::New(create_params);
2170 for (int i = 0; i < Shell::options.stress_runs; ++i) { 2258 for (int i = 0; i < Shell::options.stress_runs; ++i) {
2171 next_semaphore_.Wait(); 2259 next_semaphore_.Wait();
2172 { 2260 {
2173 Isolate::Scope iscope(isolate); 2261 Isolate::Scope iscope(isolate);
2174 { 2262 {
2175 HandleScope scope(isolate); 2263 HandleScope scope(isolate);
2176 PerIsolateData data(isolate); 2264 PerIsolateData data(isolate);
2177 Local<Context> context = Shell::CreateEvaluationContext(isolate); 2265 Local<Context> context = Shell::CreateEvaluationContext(isolate);
2178 { 2266 {
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
2297 2385
2298 void Worker::WaitForThread() { 2386 void Worker::WaitForThread() {
2299 Terminate(); 2387 Terminate();
2300 thread_->Join(); 2388 thread_->Join();
2301 } 2389 }
2302 2390
2303 2391
2304 void Worker::ExecuteInThread() { 2392 void Worker::ExecuteInThread() {
2305 Isolate::CreateParams create_params; 2393 Isolate::CreateParams create_params;
2306 create_params.array_buffer_allocator = Shell::array_buffer_allocator; 2394 create_params.array_buffer_allocator = Shell::array_buffer_allocator;
2395 create_params.host_import_module_dynamically_callback_ =
2396 Shell::HostImportModuleDynamically;
2307 Isolate* isolate = Isolate::New(create_params); 2397 Isolate* isolate = Isolate::New(create_params);
2308 { 2398 {
2309 Isolate::Scope iscope(isolate); 2399 Isolate::Scope iscope(isolate);
2310 { 2400 {
2311 HandleScope scope(isolate); 2401 HandleScope scope(isolate);
2312 PerIsolateData data(isolate); 2402 PerIsolateData data(isolate);
2313 Local<Context> context = Shell::CreateEvaluationContext(isolate); 2403 Local<Context> context = Shell::CreateEvaluationContext(isolate);
2314 { 2404 {
2315 Context::Scope cscope(context); 2405 Context::Scope cscope(context);
2316 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); 2406 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
(...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after
2958 base::SysInfo::AmountOfPhysicalMemory(), 3048 base::SysInfo::AmountOfPhysicalMemory(),
2959 base::SysInfo::AmountOfVirtualMemory()); 3049 base::SysInfo::AmountOfVirtualMemory());
2960 3050
2961 Shell::counter_map_ = new CounterMap(); 3051 Shell::counter_map_ = new CounterMap();
2962 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) {
2963 create_params.counter_lookup_callback = LookupCounter; 3053 create_params.counter_lookup_callback = LookupCounter;
2964 create_params.create_histogram_callback = CreateHistogram; 3054 create_params.create_histogram_callback = CreateHistogram;
2965 create_params.add_histogram_sample_callback = AddHistogramSample; 3055 create_params.add_histogram_sample_callback = AddHistogramSample;
2966 } 3056 }
2967 3057
3058 create_params.host_import_module_dynamically_callback_ =
3059 Shell::HostImportModuleDynamically;
3060
2968 if (i::trap_handler::UseTrapHandler()) { 3061 if (i::trap_handler::UseTrapHandler()) {
2969 if (!v8::V8::RegisterDefaultSignalHandler()) { 3062 if (!v8::V8::RegisterDefaultSignalHandler()) {
2970 fprintf(stderr, "Could not register signal handler"); 3063 fprintf(stderr, "Could not register signal handler");
2971 exit(1); 3064 exit(1);
2972 } 3065 }
2973 } 3066 }
2974 3067
2975 Isolate* isolate = Isolate::New(create_params); 3068 Isolate* isolate = Isolate::New(create_params);
2976 { 3069 {
2977 Isolate::Scope scope(isolate); 3070 Isolate::Scope scope(isolate);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
3054 } 3147 }
3055 3148
3056 } // namespace v8 3149 } // namespace v8
3057 3150
3058 3151
3059 #ifndef GOOGLE3 3152 #ifndef GOOGLE3
3060 int main(int argc, char* argv[]) { 3153 int main(int argc, char* argv[]) {
3061 return v8::Shell::Main(argc, argv); 3154 return v8::Shell::Main(argc, argv);
3062 } 3155 }
3063 #endif 3156 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698