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

Unified Diff: third_party/WebKit/Source/core/dom/ModulatorImpl.cpp

Issue 2555653002: [WIP Prototype] ES6 https://html.spec.whatwg.org/#fetch-a-single-module-script implementation (Closed)
Patch Set: ModuleTreeLinkerTest.fetchTreeInstantiationFailure Created 3 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/dom/ModulatorImpl.cpp
diff --git a/third_party/WebKit/Source/core/dom/ModulatorImpl.cpp b/third_party/WebKit/Source/core/dom/ModulatorImpl.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..67deea46b1a8ba4593f5624cf7d526c621193a5e
--- /dev/null
+++ b/third_party/WebKit/Source/core/dom/ModulatorImpl.cpp
@@ -0,0 +1,180 @@
+// 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/ModulatorImpl.h"
+
+#include "core/dom/Document.h"
+#include "core/dom/ExecutionContext.h"
+#include "core/dom/ModuleMap.h"
+#include "core/dom/ModuleScript.h"
+#include "core/dom/ScriptModuleResolverImpl.h"
+#include "core/dom/TaskRunnerHelper.h"
+#include "core/frame/LocalFrame.h"
+#include "core/loader/modulescript//ModuleScriptFetchRequest.h"
+#include "core/loader/modulescript/ModuleScriptLoaderRegistry.h"
+#include "core/loader/modulescript/ModuleTreeLinkerRegistry.h"
+#include "platform/loader/fetch/ResourceFetcher.h"
+
+namespace blink {
+
+ModulatorImpl* ModulatorImpl::Create(RefPtr<ScriptState> script_state,
+ Document& document) {
+ return new ModulatorImpl(
+ std::move(script_state),
+ TaskRunnerHelper::Get(TaskType::kNetworking, &document), &document,
+ document.Fetcher());
+}
+
+ModulatorImpl::ModulatorImpl(RefPtr<ScriptState> script_state,
+ RefPtr<WebTaskRunner> task_runner,
+ ExecutionContext* execution_context,
+ ResourceFetcher* fetcher)
+ : script_state_(std::move(script_state)),
+ task_runner_(std::move(task_runner)),
+ execution_context_(execution_context),
+ fetcher_(fetcher),
+ map_(this, ModuleMap::Create(this)),
+ loader_registry_(ModuleScriptLoaderRegistry::Create()),
+ tree_linker_registry_(ModuleTreeLinkerRegistry::Create()),
+ script_module_resolver_(ScriptModuleResolverImpl::Create(this)) {
+ DCHECK(script_state_);
+ DCHECK(task_runner_);
+ DCHECK(execution_context_);
+ DCHECK(fetcher_);
+}
+
+ModulatorImpl::~ModulatorImpl() {}
+
+ReferrerPolicy ModulatorImpl::GetReferrerPolicy() {
+ return execution_context_->GetReferrerPolicy();
+}
+
+SecurityOrigin* ModulatorImpl::GetSecurityOrigin() {
+ return execution_context_->GetSecurityOrigin();
+}
+
+void ModulatorImpl::FetchTree(const ModuleScriptFetchRequest& request,
+ ModuleTreeClient* client) {
+ // Step 1. Perform the internal module script graph fetching procedure given
+ // url, settings object, destination, cryptographic nonce, parser state,
+ // credentials mode, settings object, a new empty list, "client", and with the
+ // top-level module fetch flag set. If the caller of this algorithm specified
+ // custom perform the fetch steps, pass those along as well.
+
+ // Note: "Fetch a module script graph" algorithm doesn't have "referrer" as
+ // its argument.
+ DCHECK(request.GetReferrer().IsNull());
+
+ AncestorList empty_ancestor_list;
+ FetchTreeInternal(request, empty_ancestor_list,
+ ModuleGraphLevel::kTopLevelModuleFetch, client);
+
+ // Step 2. When the internal module script graph fetching procedure
+ // asynchronously completes with result, asynchronously complete this
+ // algorithm with result.
+ // Note: We delegate to ModuleTreeLinker to notify ModuleTreeClient.
+}
+
+void ModulatorImpl::FetchTreeInternal(const ModuleScriptFetchRequest& request,
+ const AncestorList& ancestor_list,
+ ModuleGraphLevel level,
+ ModuleTreeClient* client) {
+ tree_linker_registry_->Fetch(request, ancestor_list, level, this, client);
+}
+
+void ModulatorImpl::FetchSingle(const ModuleScriptFetchRequest& request,
+ ModuleGraphLevel level,
+ SingleModuleClient* client) {
+ map_->FetchSingleModuleScript(request, level, client);
+}
+
+void ModulatorImpl::FetchNewSingleModule(
+ const ModuleScriptFetchRequest& request,
+ ModuleGraphLevel level,
+ ModuleScriptLoaderClient* client) {
+ loader_registry_->Fetch(request, level, this, fetcher_.Get(), client);
+}
+
+ModuleScript* ModulatorImpl::GetFetchedModuleScript(const KURL& url) {
+ return map_->GetFetchedModuleScript(url);
+}
+
+ScriptModule ModulatorImpl::CompileModule(
+ const String& script,
+ const String& url_str,
+ AccessControlStatus access_control_status) {
+ // Implements Steps 3-6 of
+ // https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-module-script
+
+ // Step 3. Let realm be the provided environment settings object's Realm.
+ // Note: Realm is v8::Context.
+ // Step 4. If scripting is disabled for the given environment settings
+ // object's responsible browsing context, then let script source be the empty
+ // string. Otherwise, let script source be the provided script source.
+ // TODO(kouhei): if(m_executionContext->canExecuteScripts) after the CL
+ // landed.
+ // Step 5. Let result be ParseModule(script source, realm, script).
+ // Step 6. If result is a List of errors, report the exception given by the
+ // first element of result for script, return null, and abort these steps.
+ // Note: reporting is routed via V8Initializer::messageHandlerInMainThread.
+ ScriptState::Scope scope(script_state_.Get());
+ return ScriptModule::Compile(script_state_->GetIsolate(), script, url_str,
+ access_control_status);
+}
+
+ScriptValue ModulatorImpl::InstantiateModule(ScriptModule script_module) {
+ ScriptState::Scope scope(script_state_.Get());
+ return script_module.Instantiate(script_state_.Get());
+}
+
+Vector<String> ModulatorImpl::ModuleRequestsFromScriptModule(
+ ScriptModule script_module) {
+ ScriptState::Scope scope(script_state_.Get());
+ return script_module.ModuleRequests(script_state_.Get());
+}
+
+void ModulatorImpl::ExecuteModule(const ModuleScript* module_script) {
+ // https://html.spec.whatwg.org/#run-a-module-script
+ ScriptState::Scope scope(script_state_.Get());
+
+ // 3. "If s's instantiation state is "errored", then report the exception
+ // given by s's instantiation error for s and abort these steps."
+ ModuleInstantiationState instantiationState = module_script->InstantiationState();
+ if (instantiationState == ModuleInstantiationState::kErrored) {
+ v8::Isolate* isolate = script_state_->GetIsolate();
+ v8::TryCatch try_catch(isolate);
+ try_catch.SetVerbose(true);
+
+ V8ThrowException::ThrowException(
hiroshige 2017/04/18 01:55:41 This should corresponds "report an exception" http
nhiroki 2017/04/18 02:13:20 execution_context_->DispatchErrorEvent()? https:/
kouhei (in TOK) 2017/04/18 02:29:59 offline discussion w/ hiroshige@: - <script> eleme
hiroshige 2017/04/18 02:33:59 ExecuteModule doesn't have to return bool, because
+ isolate, module_script->CreateInstantiationError(isolate));
+ return;
+ }
+
+ // 4. "Assert: s's instantiation state is "instantiated" (and thus its
+ // module record is not null)."
+ CHECK_EQ(instantiationState, ModuleInstantiationState::kInstantiated);
+
+ // 5. "Let record be s's module record."
+ const ScriptModule& record = module_script->Record();
+ CHECK(!record.IsNull());
+
+ // 6.--9.?
+ record.Evaluate(script_state_.Get());
+}
+
+DEFINE_TRACE(ModulatorImpl) {
+ Modulator::Trace(visitor);
+ visitor->Trace(execution_context_);
+ visitor->Trace(fetcher_);
+ visitor->Trace(map_);
+ visitor->Trace(loader_registry_);
+ visitor->Trace(tree_linker_registry_);
+ visitor->Trace(script_module_resolver_);
+}
+
+DEFINE_TRACE_WRAPPERS(ModulatorImpl) {
+ visitor->TraceWrappers(map_);
+}
+
+} // namespace blink
« no previous file with comments | « third_party/WebKit/Source/core/dom/ModulatorImpl.h ('k') | third_party/WebKit/Source/core/dom/ModulePendingScript.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698