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

Side by Side Diff: src/d8.cc

Issue 2361593002: [modules] Do path resolution relative to each module file in d8 (Closed)
Patch Set: Look for backslashes 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/api.cc ('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>
(...skipping 493 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 } 504 }
505 } else { 505 } else {
506 v8::String::Utf8Value str(Stringify(isolate, result)); 506 v8::String::Utf8Value str(Stringify(isolate, result));
507 fwrite(*str, sizeof(**str), str.length(), stdout); 507 fwrite(*str, sizeof(**str), str.length(), stdout);
508 printf("\n"); 508 printf("\n");
509 } 509 }
510 } 510 }
511 return true; 511 return true;
512 } 512 }
513 513
514 namespace {
515
516 bool IsAbsolutePath(const char* path) {
517 #if defined(_WIN32) || defined(_WIN64)
518 // TODO(adamk): Do something more robust.
519 const char* colon = strchr(path, ':');
520 const char* backslash = strchr(path, '\\');
521 const char* slash = strchr(path, '/');
522 return colon != nullptr && (backslash == nullptr || colon < backslash) &&
523 (slash == nullptr || colon < slash);
524 #else
neis 2016/09/22 01:07:39 I don't understand why you make it so complicated
adamk 2016/09/22 19:06:57 Just looking for a colon now.
525 return path[0] == '/';
526 #endif
527 }
528
529 std::string GetWorkingDirectory() {
530 #if defined(_WIN32) || defined(_WIN64)
531 char system_buffer[MAX_PATH];
532 // TODO(adamk): Support Unicode paths.
533 DWORD len = GetCurrentDirectoryA(MAX_PATH, system_buffer);
534 CHECK(len > 0);
535 return system_buffer;
536 #else
537 char curdir[PATH_MAX];
538 CHECK_NOT_NULL(getcwd(curdir, PATH_MAX));
539 return curdir;
540 #endif
541 }
542
543 MaybeLocal<Module> ResolveModuleCallback(Local<Context> context,
544 Local<String> specifier,
545 Local<Module> referrer,
546 Local<Value> data) {
547 Isolate* isolate = context->GetIsolate();
548 auto module_map = static_cast<std::map<std::string, Global<Module>>*>(
549 External::Cast(*data)->Value());
550 String::Utf8Value specifier_utf8(specifier);
551 CHECK(!IsAbsolutePath(*specifier_utf8));
552 Local<String> dir_name = Local<String>::Cast(referrer->GetEmbedderData());
553 std::string absolute_path = *String::Utf8Value(dir_name);
554 absolute_path.append(*specifier_utf8);
555 auto it = module_map->find(absolute_path);
556 if (it != module_map->end()) {
557 return it->second.Get(isolate);
558 }
559 return MaybeLocal<Module>();
560 }
561
562 } // anonymous namespace
563
514 MaybeLocal<Module> Shell::FetchModuleTree( 564 MaybeLocal<Module> Shell::FetchModuleTree(
515 Isolate* isolate, const std::string& file_name, 565 Isolate* isolate, const std::string& file_name,
516 std::map<std::string, Global<Module>>* module_map) { 566 std::map<std::string, Global<Module>>* module_map) {
neis 2016/09/22 01:07:39 Add a DCHECK(IsAbsolute) here?
adamk 2016/09/22 19:06:56 Done.
517 TryCatch try_catch(isolate); 567 TryCatch try_catch(isolate);
518 try_catch.SetVerbose(true); 568 try_catch.SetVerbose(true);
519 Local<String> source_text = ReadFile(isolate, file_name.c_str()); 569 Local<String> source_text = ReadFile(isolate, file_name.c_str());
520 if (source_text.IsEmpty()) { 570 if (source_text.IsEmpty()) {
521 printf("Error reading '%s'\n", file_name.c_str()); 571 printf("Error reading '%s'\n", file_name.c_str());
522 Shell::Exit(1); 572 Shell::Exit(1);
523 } 573 }
524 ScriptOrigin origin( 574 ScriptOrigin origin(
525 String::NewFromUtf8(isolate, file_name.c_str(), NewStringType::kNormal) 575 String::NewFromUtf8(isolate, file_name.c_str(), NewStringType::kNormal)
526 .ToLocalChecked()); 576 .ToLocalChecked());
527 ScriptCompiler::Source source(source_text, origin); 577 ScriptCompiler::Source source(source_text, origin);
528 Local<Module> module; 578 Local<Module> module;
529 if (!ScriptCompiler::CompileModule(isolate, &source).ToLocal(&module)) { 579 if (!ScriptCompiler::CompileModule(isolate, &source).ToLocal(&module)) {
530 ReportException(isolate, &try_catch); 580 ReportException(isolate, &try_catch);
531 return MaybeLocal<Module>(); 581 return MaybeLocal<Module>();
532 } 582 }
533 module_map->insert( 583 module_map->insert(
534 std::make_pair(file_name, Global<Module>(isolate, module))); 584 std::make_pair(file_name, Global<Module>(isolate, module)));
585
586 size_t last_slash = file_name.find_last_of("/\\");
587 CHECK(last_slash != std::string::npos);
588 std::string dir_name = file_name.substr(0, last_slash + 1);
neis 2016/09/22 01:07:39 Please move this into a separate function.
adamk 2016/09/22 19:06:57 Done.
589 module->SetEmbedderData(
590 String::NewFromUtf8(isolate, dir_name.c_str(), NewStringType::kNormal)
591 .ToLocalChecked());
592
535 for (int i = 0, length = module->GetModuleRequestsLength(); i < length; ++i) { 593 for (int i = 0, length = module->GetModuleRequestsLength(); i < length; ++i) {
536 Local<String> name = module->GetModuleRequest(i); 594 Local<String> name = module->GetModuleRequest(i);
537 // TODO(adamk): Resolve the imported module to a full path. 595 String::Utf8Value utf8_value(name);
538 std::string str = *String::Utf8Value(name); 596 CHECK(!IsAbsolutePath(*utf8_value));
neis 2016/09/22 01:07:39 Can you add a comment about this restriction?
adamk 2016/09/22 19:06:56 Removed the restriction instead.
539 if (!module_map->count(str)) { 597 std::string absolute_path = dir_name;
540 if (FetchModuleTree(isolate, str, module_map).IsEmpty()) { 598 absolute_path.append(*utf8_value);
599 if (!module_map->count(absolute_path)) {
600 if (FetchModuleTree(isolate, absolute_path, module_map).IsEmpty()) {
541 return MaybeLocal<Module>(); 601 return MaybeLocal<Module>();
542 } 602 }
543 } 603 }
544 } 604 }
545 605
546 return module; 606 return module;
547 } 607 }
548 608
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) { 609 bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) {
570 HandleScope handle_scope(isolate); 610 HandleScope handle_scope(isolate);
571 611
572 Local<Module> root_module; 612 Local<Module> root_module;
573 std::map<std::string, Global<Module>> module_map; 613 std::map<std::string, Global<Module>> module_map;
574 if (!FetchModuleTree(isolate, file_name, &module_map).ToLocal(&root_module)) { 614
615 std::string absolute_path;
616 if (IsAbsolutePath(file_name)) {
617 absolute_path = file_name;
618 } else {
619 absolute_path = GetWorkingDirectory();
620 absolute_path.push_back('/');
621 absolute_path.append(file_name);
622 }
neis 2016/09/22 01:07:39 Can you move this stuff up a little?
adamk 2016/09/22 19:06:56 Done.
623 if (!FetchModuleTree(isolate, absolute_path, &module_map)
624 .ToLocal(&root_module)) {
575 return false; 625 return false;
576 } 626 }
577 627
578 TryCatch try_catch(isolate); 628 TryCatch try_catch(isolate);
579 try_catch.SetVerbose(true); 629 try_catch.SetVerbose(true);
580 630
581 MaybeLocal<Value> maybe_result; 631 MaybeLocal<Value> maybe_result;
582 { 632 {
583 PerIsolateData* data = PerIsolateData::Get(isolate); 633 PerIsolateData* data = PerIsolateData::Get(isolate);
584 Local<Context> realm = data->realms_[data->realm_current_].Get(isolate); 634 Local<Context> realm = data->realms_[data->realm_current_].Get(isolate);
(...skipping 1310 matching lines...) Expand 10 before | Expand all | Expand 10 after
1895 thread_ = NULL; 1945 thread_ = NULL;
1896 delete[] script_; 1946 delete[] script_;
1897 script_ = NULL; 1947 script_ = NULL;
1898 in_queue_.Clear(); 1948 in_queue_.Clear();
1899 out_queue_.Clear(); 1949 out_queue_.Clear();
1900 } 1950 }
1901 1951
1902 1952
1903 void Worker::StartExecuteInThread(const char* script) { 1953 void Worker::StartExecuteInThread(const char* script) {
1904 running_ = true; 1954 running_ = true;
1955 #undef StrDup
neis 2016/09/22 01:07:39 ??
adamk 2016/09/22 19:06:56 This was from a previous attempt to call the Win32
1905 script_ = i::StrDup(script); 1956 script_ = i::StrDup(script);
1906 thread_ = new WorkerThread(this); 1957 thread_ = new WorkerThread(this);
1907 thread_->Start(); 1958 thread_->Start();
1908 } 1959 }
1909 1960
1910 1961
1911 void Worker::PostMessage(SerializationData* data) { 1962 void Worker::PostMessage(SerializationData* data) {
1912 in_queue_.Enqueue(data); 1963 in_queue_.Enqueue(data);
1913 in_semaphore_.Signal(); 1964 in_semaphore_.Signal();
1914 } 1965 }
(...skipping 780 matching lines...) Expand 10 before | Expand all | Expand 10 after
2695 } 2746 }
2696 2747
2697 } // namespace v8 2748 } // namespace v8
2698 2749
2699 2750
2700 #ifndef GOOGLE3 2751 #ifndef GOOGLE3
2701 int main(int argc, char* argv[]) { 2752 int main(int argc, char* argv[]) {
2702 return v8::Shell::Main(argc, argv); 2753 return v8::Shell::Main(argc, argv);
2703 } 2754 }
2704 #endif 2755 #endif
OLDNEW
« no previous file with comments | « src/api.cc ('k') | src/factory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698