Index: third_party/WebKit/Source/core/dom/ScriptModuleResolverImpl.cpp |
diff --git a/third_party/WebKit/Source/core/dom/ScriptModuleResolverImpl.cpp b/third_party/WebKit/Source/core/dom/ScriptModuleResolverImpl.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5d91fb47a8dc720fff968b4cf856fd0a32673aed |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/dom/ScriptModuleResolverImpl.cpp |
@@ -0,0 +1,92 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "core/dom/ScriptModuleResolverImpl.h" |
+ |
+#include "bindings/core/v8/ScriptModule.h" |
+#include "core/dom/Modulator.h" |
+#include "core/dom/ModuleScript.h" |
+ |
+namespace blink { |
+ |
+void ScriptModuleResolverImpl::registerModuleScript( |
+ ModuleScript* moduleScript) { |
+ DVLOG(1) << "ScriptModuleResolverImpl::registerModuleScript(url=\"" |
+ << moduleScript->baseURL().getString() |
+ << "\", hash=" << ScriptModuleHash::hash(moduleScript->record()) |
+ << ")"; |
+ |
+ DCHECK(moduleScript); |
+ DCHECK(!moduleScript->record().isNull()); |
+ auto result = |
+ m_recordToModuleScriptMap.set(moduleScript->record(), moduleScript); |
+ DCHECK(result.isNewEntry); |
+} |
+ |
+ScriptModule ScriptModuleResolverImpl::resolve(const String& specifier, |
+ const ScriptModule& referrer, |
+ ExceptionState& exceptionState) { |
+ // https://html.spec.whatwg.org/commit-snapshots/f8e75a974ed9185e5b462bc5b2dfb32034bd1145#hostresolveimportedmodule(referencingmodule,-specifier) |
+ DVLOG(1) << "ScriptModuleResolverImpl::resolve(specifier=\"" << specifier |
+ << ", referrer.hash=" << ScriptModuleHash::hash(referrer) << ")"; |
+ |
+ // Step 1. Let referencing module script be referencingModule.[[HostDefined]]. |
+ const auto it = m_recordToModuleScriptMap.find(referrer); |
+ CHECK_NE(it, m_recordToModuleScriptMap.end()) |
+ << "Failed to find referrer ModuleScript corresponding to the record"; |
+ ModuleScript* referrerModule = it->value; |
+ |
+ // Step 2. Let moduleMap be referencing module script's settings object's |
+ // module map. Note: Blink finds out "module script's settings object" |
+ // (Modulator) from context where HostResolveImportedModule was called. |
+ |
+ // Step 3. Let url be the result of resolving a module specifier given |
+ // referencing module script and specifier. If the result is failure, then |
+ // throw a TypeError exception and abort these steps. |
+ KURL url = |
+ Modulator::resolveModuleSpecifier(specifier, referrerModule->baseURL()); |
+ if (!url.isValid()) { |
+ exceptionState.throwTypeError("Failed to resolve module specifier '" + |
+ specifier + "'"); |
+ return ScriptModule(); |
+ } |
+ |
+ // Step 4. Let resolved module script be moduleMap[url]. If no such entry |
+ // exists, or if resolved module script is null or "fetching", then throw a |
+ // TypeError exception and abort these steps. |
+ ModuleScript* moduleScript = m_modulator->getFetchedModuleScript(url); |
+ if (!moduleScript) { |
+ exceptionState.throwTypeError( |
+ "Failed to load module script for module specifier '" + specifier + |
+ "'"); |
+ return ScriptModule(); |
+ } |
+ |
+ // Step 5. If resolved module script's instantiation state is "errored", then |
+ // throw resolved module script's instantiation error. |
+ if (moduleScript->instantiationState() == ModuleInstantiationState::Errored) { |
+ // TODO(kouhei): Implement this path once |
+ // https://codereview.chromium.org/2782403002/ landed. |
+ NOTIMPLEMENTED(); |
+ return ScriptModule(); |
+ } |
+ |
+ // Step 6. Assert: resolved module script's instantiation state is |
+ // "instantiated" (and thus its module record is not null). |
+ // TODO(kouhei): Enable below check once once |
+ // https://codereview.chromium.org/2782403002/ landed. |
+ // CHECK_EQ(moduleScript->instantiationState(), |
+ // ModuleInstantiationState::Instantiated); |
+ |
+ // Step 7. Return resolved module script's module record. |
+ return moduleScript->record(); |
+} |
+ |
+DEFINE_TRACE(ScriptModuleResolverImpl) { |
+ ScriptModuleResolver::trace(visitor); |
+ visitor->trace(m_recordToModuleScriptMap); |
+ visitor->trace(m_modulator); |
+} |
+ |
+} // namespace blink |