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

Side by Side Diff: src/d8.cc

Issue 2703563002: [ESNext] Implement DynamicImportCall (Closed)
Patch Set: fix build 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 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 if (!d->specifier_to_module_map.count(absolute_path)) { 724 if (!d->specifier_to_module_map.count(absolute_path)) {
721 if (FetchModuleTree(context, absolute_path).IsEmpty()) { 725 if (FetchModuleTree(context, absolute_path).IsEmpty()) {
722 return MaybeLocal<Module>(); 726 return MaybeLocal<Module>();
723 } 727 }
724 } 728 }
725 } 729 }
726 730
727 return module; 731 return module;
728 } 732 }
729 733
734 MaybeLocal<Module> Shell::DynamicFetchModuleTree(Local<Context> context,
735 Local<Promise> promise,
736 const std::string& file_name) {
737 DCHECK(IsAbsolutePath(file_name));
738 Isolate* isolate = context->GetIsolate();
739 TryCatch try_catch(isolate);
740 try_catch.SetVerbose(true);
741
742 Local<String> source_text = ReadFile(isolate, file_name.c_str());
743
744 if (source_text.IsEmpty()) {
745 // TODO(gsathya): Create an exception
746 printf("Error reading '%s'\n", file_name.c_str());
747 return MaybeLocal<Module>();
748 }
749
750 ScriptOrigin origin(
751 String::NewFromUtf8(isolate, file_name.c_str(), NewStringType::kNormal)
752 .ToLocalChecked(),
753 Local<Integer>(), Local<Integer>(), Local<Boolean>(), Local<Integer>(),
754 Local<Value>(), Local<Boolean>(), Local<Boolean>(), True(isolate));
755
756 ScriptCompiler::Source source(source_text, origin);
757 Local<Module> module;
758
759 if (!ScriptCompiler::CompileModule(isolate, &source).ToLocal(&module)) {
760 Module::FinishDynamicImportFailure(context, promise, try_catch.Exception());
761 return MaybeLocal<Module>();
762 }
763
764 ModuleEmbedderData* d = GetModuleDataFromContext(context);
765 // TODO(gsathya): This blows up when we import duplicate modules. Fix this and
766 // uncomment test.
767 CHECK(d->specifier_to_module_map
768 .insert(std::make_pair(file_name, Global<Module>(isolate, module)))
769 .second);
770
771 std::string dir_name = DirName(file_name);
772 CHECK(d->module_to_directory_map
773 .insert(std::make_pair(Global<Module>(isolate, module), dir_name))
774 .second);
775
776 for (int i = 0, length = module->GetModuleRequestsLength(); i < length; ++i) {
777 Local<String> name = module->GetModuleRequest(i);
778 std::string absolute_path = NormalizePath(ToSTLString(name), dir_name);
779 if (!d->specifier_to_module_map.count(absolute_path)) {
780 if (DynamicFetchModuleTree(context, promise, absolute_path).IsEmpty()) {
781 return MaybeLocal<Module>();
782 }
783 }
784 }
785
786 return module;
787 }
788
789 void Shell::HostImportModuleDynamically(Isolate* isolate,
790 Local<String> referrer,
791 Local<String> specifier,
792 Local<Promise> promise) {
793 DynamicImportData* data =
794 new DynamicImportData(isolate, referrer, specifier, promise);
795 isolate->EnqueueMicrotask(Shell::DoHostImportModuleDynamically, data);
796 }
797
798 void Shell::DoHostImportModuleDynamically(void* import_data) {
799 DynamicImportData* import_data_ =
800 static_cast<DynamicImportData*>(import_data);
801 Isolate* isolate(import_data_->isolate_);
802 Local<String> referrer(import_data_->referrer_.Get(isolate));
803 Local<String> specifier(import_data_->specifier_.Get(isolate));
804 Local<Promise> promise(import_data_->promise_.Get(isolate));
805
806 HandleScope handle_scope(isolate);
adamk 2017/03/15 21:36:43 I think you want to put this HandleScope above the
gsathya 2017/03/16 00:59:24 Done.
807
808 PerIsolateData* data = PerIsolateData::Get(isolate);
809 Local<Context> realm = data->realms_[data->realm_current_].Get(isolate);
810 Context::Scope context_scope(realm);
811
812 std::string source_url = ToSTLString(referrer);
813 std::string dir_name =
814 IsAbsolutePath(source_url) ? DirName(source_url) : GetWorkingDirectory();
815 std::string file_name = ToSTLString(specifier);
816 std::string absolute_path = NormalizePath(file_name.c_str(), dir_name);
817
818 Local<Module> root_module;
819 if (!DynamicFetchModuleTree(realm, promise, absolute_path)
820 .ToLocal(&root_module)) {
821 delete import_data_;
822 return;
823 }
824
825 TryCatch try_catch(isolate);
826 try_catch.SetVerbose(true);
827
828 MaybeLocal<Value> maybe_result;
829 if (root_module->Instantiate(realm, ResolveModuleCallback)) {
830 maybe_result = root_module->Evaluate(realm);
831 EmptyMessageQueues(isolate);
832 }
833
834 Local<Value> result;
835 if (!maybe_result.ToLocal(&result)) {
836 DCHECK(try_catch.HasCaught());
837 Module::FinishDynamicImportFailure(realm, promise, try_catch.Exception());
838 delete import_data_;
839 return;
840 }
841
842 DCHECK(!try_catch.HasCaught());
843 Module::FinishDynamicImportSuccess(realm, promise, root_module);
844 delete import_data_;
845 }
846
730 bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) { 847 bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) {
731 HandleScope handle_scope(isolate); 848 HandleScope handle_scope(isolate);
732 849
733 PerIsolateData* data = PerIsolateData::Get(isolate); 850 PerIsolateData* data = PerIsolateData::Get(isolate);
734 Local<Context> realm = data->realms_[data->realm_current_].Get(isolate); 851 Local<Context> realm = data->realms_[data->realm_current_].Get(isolate);
735 Context::Scope context_scope(realm); 852 Context::Scope context_scope(realm);
736 853
737 std::string absolute_path = NormalizePath(file_name, GetWorkingDirectory()); 854 std::string absolute_path = NormalizePath(file_name, GetWorkingDirectory());
738 855
739 Local<Module> root_module; 856 Local<Module> root_module;
(...skipping 1419 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 2276 // 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. 2277 // which is not enough to parse the big literal expressions used in tests.
2161 // The stack size should be at least StackGuard::kLimitSize + some 2278 // The stack size should be at least StackGuard::kLimitSize + some
2162 // OS-specific padding for thread startup code. 2Mbytes seems to be enough. 2279 // OS-specific padding for thread startup code. 2Mbytes seems to be enough.
2163 return base::Thread::Options("IsolateThread", 2 * MB); 2280 return base::Thread::Options("IsolateThread", 2 * MB);
2164 } 2281 }
2165 2282
2166 void SourceGroup::ExecuteInThread() { 2283 void SourceGroup::ExecuteInThread() {
2167 Isolate::CreateParams create_params; 2284 Isolate::CreateParams create_params;
2168 create_params.array_buffer_allocator = Shell::array_buffer_allocator; 2285 create_params.array_buffer_allocator = Shell::array_buffer_allocator;
2286 create_params.host_import_module_dynamically_callback_ =
2287 Shell::HostImportModuleDynamically;
2169 Isolate* isolate = Isolate::New(create_params); 2288 Isolate* isolate = Isolate::New(create_params);
2170 for (int i = 0; i < Shell::options.stress_runs; ++i) { 2289 for (int i = 0; i < Shell::options.stress_runs; ++i) {
2171 next_semaphore_.Wait(); 2290 next_semaphore_.Wait();
2172 { 2291 {
2173 Isolate::Scope iscope(isolate); 2292 Isolate::Scope iscope(isolate);
2174 { 2293 {
2175 HandleScope scope(isolate); 2294 HandleScope scope(isolate);
2176 PerIsolateData data(isolate); 2295 PerIsolateData data(isolate);
2177 Local<Context> context = Shell::CreateEvaluationContext(isolate); 2296 Local<Context> context = Shell::CreateEvaluationContext(isolate);
2178 { 2297 {
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
2297 2416
2298 void Worker::WaitForThread() { 2417 void Worker::WaitForThread() {
2299 Terminate(); 2418 Terminate();
2300 thread_->Join(); 2419 thread_->Join();
2301 } 2420 }
2302 2421
2303 2422
2304 void Worker::ExecuteInThread() { 2423 void Worker::ExecuteInThread() {
2305 Isolate::CreateParams create_params; 2424 Isolate::CreateParams create_params;
2306 create_params.array_buffer_allocator = Shell::array_buffer_allocator; 2425 create_params.array_buffer_allocator = Shell::array_buffer_allocator;
2426 create_params.host_import_module_dynamically_callback_ =
2427 Shell::HostImportModuleDynamically;
2307 Isolate* isolate = Isolate::New(create_params); 2428 Isolate* isolate = Isolate::New(create_params);
2308 { 2429 {
2309 Isolate::Scope iscope(isolate); 2430 Isolate::Scope iscope(isolate);
2310 { 2431 {
2311 HandleScope scope(isolate); 2432 HandleScope scope(isolate);
2312 PerIsolateData data(isolate); 2433 PerIsolateData data(isolate);
2313 Local<Context> context = Shell::CreateEvaluationContext(isolate); 2434 Local<Context> context = Shell::CreateEvaluationContext(isolate);
2314 { 2435 {
2315 Context::Scope cscope(context); 2436 Context::Scope cscope(context);
2316 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate)); 2437 PerIsolateData::RealmScope realm_scope(PerIsolateData::Get(isolate));
(...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after
2947 base::SysInfo::AmountOfPhysicalMemory(), 3068 base::SysInfo::AmountOfPhysicalMemory(),
2948 base::SysInfo::AmountOfVirtualMemory()); 3069 base::SysInfo::AmountOfVirtualMemory());
2949 3070
2950 Shell::counter_map_ = new CounterMap(); 3071 Shell::counter_map_ = new CounterMap();
2951 if (i::FLAG_dump_counters || i::FLAG_dump_counters_nvp || i::FLAG_gc_stats) { 3072 if (i::FLAG_dump_counters || i::FLAG_dump_counters_nvp || i::FLAG_gc_stats) {
2952 create_params.counter_lookup_callback = LookupCounter; 3073 create_params.counter_lookup_callback = LookupCounter;
2953 create_params.create_histogram_callback = CreateHistogram; 3074 create_params.create_histogram_callback = CreateHistogram;
2954 create_params.add_histogram_sample_callback = AddHistogramSample; 3075 create_params.add_histogram_sample_callback = AddHistogramSample;
2955 } 3076 }
2956 3077
3078 create_params.host_import_module_dynamically_callback_ =
3079 Shell::HostImportModuleDynamically;
3080
2957 if (i::trap_handler::UseTrapHandler()) { 3081 if (i::trap_handler::UseTrapHandler()) {
2958 if (!v8::V8::RegisterDefaultSignalHandler()) { 3082 if (!v8::V8::RegisterDefaultSignalHandler()) {
2959 fprintf(stderr, "Could not register signal handler"); 3083 fprintf(stderr, "Could not register signal handler");
2960 exit(1); 3084 exit(1);
2961 } 3085 }
2962 } 3086 }
2963 3087
2964 Isolate* isolate = Isolate::New(create_params); 3088 Isolate* isolate = Isolate::New(create_params);
2965 { 3089 {
2966 Isolate::Scope scope(isolate); 3090 Isolate::Scope scope(isolate);
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
3043 } 3167 }
3044 3168
3045 } // namespace v8 3169 } // namespace v8
3046 3170
3047 3171
3048 #ifndef GOOGLE3 3172 #ifndef GOOGLE3
3049 int main(int argc, char* argv[]) { 3173 int main(int argc, char* argv[]) {
3050 return v8::Shell::Main(argc, argv); 3174 return v8::Shell::Main(argc, argv);
3051 } 3175 }
3052 #endif 3176 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698