Commit 3aeaf497 authored by adamk's avatar adamk Committed by Commit bot

[modules] Add basic path normalization to d8's module loader

d8 now elides './' when constructing absolute paths.
'../' is still not normalized.

R=neis@chromium.org
BUG=v8:1569

Review-Url: https://codereview.chromium.org/2393243002
Cr-Commit-Position: refs/heads/master@{#40057}
parent 9d836ec6
...@@ -553,9 +553,23 @@ std::string DirName(const std::string& path) { ...@@ -553,9 +553,23 @@ std::string DirName(const std::string& path) {
return path.substr(0, last_slash); return path.substr(0, last_slash);
} }
std::string EnsureAbsolutePath(const std::string& path, // Resolves path to an absolute path if necessary, and does some
const std::string& dir_name) { // normalization (eliding references to the current directory
return IsAbsolutePath(path) ? path : dir_name + '/' + path; // and replacing backslashes with slashes).
std::string NormalizePath(const std::string& path,
const std::string& dir_name) {
std::string result;
if (IsAbsolutePath(path)) {
result = path;
} else {
result = dir_name + '/' + path;
}
std::replace(result.begin(), result.end(), '\\', '/');
size_t i;
while ((i = result.find("/./")) != std::string::npos) {
result.erase(i, 2);
}
return result;
} }
MaybeLocal<Module> ResolveModuleCallback(Local<Context> context, MaybeLocal<Module> ResolveModuleCallback(Local<Context> context,
...@@ -567,7 +581,7 @@ MaybeLocal<Module> ResolveModuleCallback(Local<Context> context, ...@@ -567,7 +581,7 @@ MaybeLocal<Module> ResolveModuleCallback(Local<Context> context,
External::Cast(*data)->Value()); External::Cast(*data)->Value());
Local<String> dir_name = Local<String>::Cast(referrer->GetEmbedderData()); Local<String> dir_name = Local<String>::Cast(referrer->GetEmbedderData());
std::string absolute_path = std::string absolute_path =
EnsureAbsolutePath(ToSTLString(specifier), ToSTLString(dir_name)); NormalizePath(ToSTLString(specifier), ToSTLString(dir_name));
auto it = module_map->find(absolute_path); auto it = module_map->find(absolute_path);
if (it != module_map->end()) { if (it != module_map->end()) {
return it->second.Get(isolate); return it->second.Get(isolate);
...@@ -607,7 +621,7 @@ MaybeLocal<Module> Shell::FetchModuleTree( ...@@ -607,7 +621,7 @@ MaybeLocal<Module> Shell::FetchModuleTree(
for (int i = 0, length = module->GetModuleRequestsLength(); i < length; ++i) { for (int i = 0, length = module->GetModuleRequestsLength(); i < length; ++i) {
Local<String> name = module->GetModuleRequest(i); Local<String> name = module->GetModuleRequest(i);
std::string absolute_path = EnsureAbsolutePath(ToSTLString(name), dir_name); std::string absolute_path = NormalizePath(ToSTLString(name), dir_name);
if (!module_map->count(absolute_path)) { if (!module_map->count(absolute_path)) {
if (FetchModuleTree(isolate, absolute_path, module_map).IsEmpty()) { if (FetchModuleTree(isolate, absolute_path, module_map).IsEmpty()) {
return MaybeLocal<Module>(); return MaybeLocal<Module>();
...@@ -621,9 +635,7 @@ MaybeLocal<Module> Shell::FetchModuleTree( ...@@ -621,9 +635,7 @@ MaybeLocal<Module> Shell::FetchModuleTree(
bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) { bool Shell::ExecuteModule(Isolate* isolate, const char* file_name) {
HandleScope handle_scope(isolate); HandleScope handle_scope(isolate);
std::string absolute_path = std::string absolute_path = NormalizePath(file_name, GetWorkingDirectory());
EnsureAbsolutePath(file_name, GetWorkingDirectory());
std::replace(absolute_path.begin(), absolute_path.end(), '\\', '/');
Local<Module> root_module; Local<Module> root_module;
std::map<std::string, Global<Module>> module_map; std::map<std::string, Global<Module>> module_map;
......
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// MODULE
import {x as y} from "./modules-relative-path.js";
export let x = 0;
assertEquals(0, x);
assertEquals(x, y);
x++;
assertEquals(1, x);
assertEquals(x, y);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment