OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "core/dom/ScriptModuleResolverImpl.h" |
| 6 |
| 7 #include "bindings/core/v8/ScriptModule.h" |
| 8 #include "core/dom/Modulator.h" |
| 9 #include "core/dom/ModuleScript.h" |
| 10 |
| 11 namespace blink { |
| 12 |
| 13 void ScriptModuleResolverImpl::RegisterModuleScript( |
| 14 ModuleScript* module_script) { |
| 15 DVLOG(1) << "ScriptModuleResolverImpl::registerModuleScript(url=\"" |
| 16 << module_script->BaseURL().GetString() |
| 17 << "\", hash=" << ScriptModuleHash::GetHash(module_script->Record()) |
| 18 << ")"; |
| 19 |
| 20 DCHECK(module_script); |
| 21 DCHECK(!module_script->Record().IsNull()); |
| 22 auto result = |
| 23 record_to_module_script_map_.Set(module_script->Record(), module_script); |
| 24 DCHECK(result.is_new_entry); |
| 25 } |
| 26 |
| 27 ScriptModule ScriptModuleResolverImpl::Resolve( |
| 28 const String& specifier, |
| 29 const ScriptModule& referrer, |
| 30 ExceptionState& exception_state) { |
| 31 // https://html.spec.whatwg.org/commit-snapshots/f8e75a974ed9185e5b462bc5b2dfb
32034bd1145#hostresolveimportedmodule(referencingmodule,-specifier) |
| 32 DVLOG(1) << "ScriptModuleResolverImpl::resolve(specifier=\"" << specifier |
| 33 << ", referrer.hash=" << ScriptModuleHash::GetHash(referrer) << ")"; |
| 34 |
| 35 // Step 1. Let referencing module script be referencingModule.[[HostDefined]]. |
| 36 const auto it = record_to_module_script_map_.Find(referrer); |
| 37 CHECK_NE(it, record_to_module_script_map_.end()) |
| 38 << "Failed to find referrer ModuleScript corresponding to the record"; |
| 39 ModuleScript* referrer_module = it->value; |
| 40 |
| 41 // Step 2. Let moduleMap be referencing module script's settings object's |
| 42 // module map. Note: Blink finds out "module script's settings object" |
| 43 // (Modulator) from context where HostResolveImportedModule was called. |
| 44 |
| 45 // Step 3. Let url be the result of resolving a module specifier given |
| 46 // referencing module script and specifier. If the result is failure, then |
| 47 // throw a TypeError exception and abort these steps. |
| 48 KURL url = |
| 49 Modulator::ResolveModuleSpecifier(specifier, referrer_module->BaseURL()); |
| 50 if (!url.IsValid()) { |
| 51 exception_state.ThrowTypeError("Failed to resolve module specifier '" + |
| 52 specifier + "'"); |
| 53 return ScriptModule(); |
| 54 } |
| 55 |
| 56 // Step 4. Let resolved module script be moduleMap[url]. If no such entry |
| 57 // exists, or if resolved module script is null or "fetching", then throw a |
| 58 // TypeError exception and abort these steps. |
| 59 ModuleScript* module_script = modulator_->GetFetchedModuleScript(url); |
| 60 if (!module_script) { |
| 61 exception_state.ThrowTypeError( |
| 62 "Failed to load module script for module specifier '" + specifier + |
| 63 "'"); |
| 64 return ScriptModule(); |
| 65 } |
| 66 |
| 67 // Step 5. If resolved module script's instantiation state is "errored", then |
| 68 // throw resolved module script's instantiation error. |
| 69 if (module_script->InstantiationState() == |
| 70 ModuleInstantiationState::kErrored) { |
| 71 // TODO(kouhei): Implement this path once |
| 72 // https://codereview.chromium.org/2782403002/ landed. |
| 73 NOTIMPLEMENTED(); |
| 74 return ScriptModule(); |
| 75 } |
| 76 |
| 77 // Step 6. Assert: resolved module script's instantiation state is |
| 78 // "instantiated" (and thus its module record is not null). |
| 79 // TODO(kouhei): Enable below check once once |
| 80 // https://codereview.chromium.org/2782403002/ landed. |
| 81 // CHECK_EQ(moduleScript->instantiationState(), |
| 82 // ModuleInstantiationState::Instantiated); |
| 83 |
| 84 // Step 7. Return resolved module script's module record. |
| 85 return module_script->Record(); |
| 86 } |
| 87 |
| 88 DEFINE_TRACE(ScriptModuleResolverImpl) { |
| 89 ScriptModuleResolver::Trace(visitor); |
| 90 visitor->Trace(record_to_module_script_map_); |
| 91 visitor->Trace(modulator_); |
| 92 } |
| 93 |
| 94 } // namespace blink |
OLD | NEW |