OLD | NEW |
---|---|
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 535 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
546 } | 546 } |
547 | 547 |
548 // Returns the directory part of path, without the trailing '/'. | 548 // Returns the directory part of path, without the trailing '/'. |
549 std::string DirName(const std::string& path) { | 549 std::string DirName(const std::string& path) { |
550 DCHECK(IsAbsolutePath(path)); | 550 DCHECK(IsAbsolutePath(path)); |
551 size_t last_slash = path.find_last_of('/'); | 551 size_t last_slash = path.find_last_of('/'); |
552 DCHECK(last_slash != std::string::npos); | 552 DCHECK(last_slash != std::string::npos); |
553 return path.substr(0, last_slash); | 553 return path.substr(0, last_slash); |
554 } | 554 } |
555 | 555 |
556 std::string EnsureAbsolutePath(const std::string& path, | 556 // Resolves path to an absolute path if necessary, and do some |
neis
2016/10/06 07:18:29
s/do/does/
adamk
2016/10/06 18:50:38
Done.
| |
557 const std::string& dir_name) { | 557 // normalization (eliding references to the current directory |
558 return IsAbsolutePath(path) ? path : dir_name + '/' + path; | 558 // and replacing backslashes with slashes). |
559 std::string NormalizePath(const std::string& path, | |
560 const std::string& dir_name) { | |
561 std::string result; | |
562 if (IsAbsolutePath(path)) { | |
563 result = path; | |
564 } else { | |
565 result = dir_name + '/' + path; | |
566 } | |
567 size_t i; | |
568 while ((i = result.find("/./")) != std::string::npos) { | |
569 result.erase(i, 2); | |
570 } | |
571 std::replace(result.begin(), result.end(), '\\', '/'); | |
neis
2016/10/06 07:18:29
Don't you want to do the replace before searching
adamk
2016/10/06 18:50:38
Done.
| |
572 return result; | |
559 } | 573 } |
560 | 574 |
561 MaybeLocal<Module> ResolveModuleCallback(Local<Context> context, | 575 MaybeLocal<Module> ResolveModuleCallback(Local<Context> context, |
562 Local<String> specifier, | 576 Local<String> specifier, |
563 Local<Module> referrer, | 577 Local<Module> referrer, |
564 Local<Value> data) { | 578 Local<Value> data) { |
565 Isolate* isolate = context->GetIsolate(); | 579 Isolate* isolate = context->GetIsolate(); |
566 auto module_map = static_cast<std::map<std::string, Global<Module>>*>( | 580 auto module_map = static_cast<std::map<std::string, Global<Module>>*>( |
567 External::Cast(*data)->Value()); | 581 External::Cast(*data)->Value()); |
568 Local<String> dir_name = Local<String>::Cast(referrer->GetEmbedderData()); | 582 Local<String> dir_name = Local<String>::Cast(referrer->GetEmbedderData()); |
569 std::string absolute_path = | 583 std::string absolute_path = |
570 EnsureAbsolutePath(ToSTLString(specifier), ToSTLString(dir_name)); | 584 NormalizePath(ToSTLString(specifier), ToSTLString(dir_name)); |
571 auto it = module_map->find(absolute_path); | 585 auto it = module_map->find(absolute_path); |
572 if (it != module_map->end()) { | 586 if (it != module_map->end()) { |
573 return it->second.Get(isolate); | 587 return it->second.Get(isolate); |
574 } | 588 } |
575 return MaybeLocal<Module>(); | 589 return MaybeLocal<Module>(); |
576 } | 590 } |
577 | 591 |
578 } // anonymous namespace | 592 } // anonymous namespace |
579 | 593 |
580 MaybeLocal<Module> Shell::FetchModuleTree( | 594 MaybeLocal<Module> Shell::FetchModuleTree( |
(...skipping 19 matching lines...) Expand all Loading... | |
600 module_map->insert( | 614 module_map->insert( |
601 std::make_pair(file_name, Global<Module>(isolate, module))); | 615 std::make_pair(file_name, Global<Module>(isolate, module))); |
602 | 616 |
603 std::string dir_name = DirName(file_name); | 617 std::string dir_name = DirName(file_name); |
604 module->SetEmbedderData( | 618 module->SetEmbedderData( |
605 String::NewFromUtf8(isolate, dir_name.c_str(), NewStringType::kNormal) | 619 String::NewFromUtf8(isolate, dir_name.c_str(), NewStringType::kNormal) |
606 .ToLocalChecked()); | 620 .ToLocalChecked()); |
607 | 621 |
608 for (int i = 0, length = module->GetModuleRequestsLength(); i < length; ++i) { | 622 for (int i = 0, length = module->GetModuleRequestsLength(); i < length; ++i) { |
609 Local<String> name = module->GetModuleRequest(i); | 623 Local<String> name = module->GetModuleRequest(i); |
610 std::string absolute_path = EnsureAbsolutePath(ToSTLString(name), dir_name); | 624 std::string absolute_path = NormalizePath(ToSTLString(name), dir_name); |
611 if (!module_map->count(absolute_path)) { | 625 if (!module_map->count(absolute_path)) { |
612 if (FetchModuleTree(isolate, absolute_path, module_map).IsEmpty()) { | 626 if (FetchModuleTree(isolate, absolute_path, module_map).IsEmpty()) { |
613 return MaybeLocal<Module>(); | 627 return MaybeLocal<Module>(); |
614 } | 628 } |
615 } | 629 } |
616 } | 630 } |
617 | 631 |
618 return module; | 632 return module; |
619 } | 633 } |
620 | 634 |
621 bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) { | 635 bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) { |
622 HandleScope handle_scope(isolate); | 636 HandleScope handle_scope(isolate); |
623 | 637 |
624 std::string absolute_path = | 638 std::string absolute_path = NormalizePath(file_name, GetWorkingDirectory()); |
625 EnsureAbsolutePath(file_name, GetWorkingDirectory()); | |
626 std::replace(absolute_path.begin(), absolute_path.end(), '\\', '/'); | |
627 | 639 |
628 Local<Module> root_module; | 640 Local<Module> root_module; |
629 std::map<std::string, Global<Module>> module_map; | 641 std::map<std::string, Global<Module>> module_map; |
630 if (!FetchModuleTree(isolate, absolute_path, &module_map) | 642 if (!FetchModuleTree(isolate, absolute_path, &module_map) |
631 .ToLocal(&root_module)) { | 643 .ToLocal(&root_module)) { |
632 return false; | 644 return false; |
633 } | 645 } |
634 | 646 |
635 TryCatch try_catch(isolate); | 647 TryCatch try_catch(isolate); |
636 try_catch.SetVerbose(true); | 648 try_catch.SetVerbose(true); |
(...skipping 2115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2752 } | 2764 } |
2753 | 2765 |
2754 } // namespace v8 | 2766 } // namespace v8 |
2755 | 2767 |
2756 | 2768 |
2757 #ifndef GOOGLE3 | 2769 #ifndef GOOGLE3 |
2758 int main(int argc, char* argv[]) { | 2770 int main(int argc, char* argv[]) { |
2759 return v8::Shell::Main(argc, argv); | 2771 return v8::Shell::Main(argc, argv); |
2760 } | 2772 } |
2761 #endif | 2773 #endif |
OLD | NEW |