Bug 1860802 - Make non-parser-created inline module scripts with imports execute...
[gecko.git] / dom / script / ModuleLoader.h
blobdf74aa478df15a4ad58ee11c5c40be19bebfe227
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_ModuleLoader_h
8 #define mozilla_dom_ModuleLoader_h
10 #include "mozilla/dom/ScriptLoadContext.h"
11 #include "js/loader/ModuleLoaderBase.h"
12 #include "js/loader/ScriptLoadRequest.h"
13 #include "ScriptLoader.h"
15 class nsIURI;
17 namespace JS {
19 class CompileOptions;
21 namespace loader {
23 class ModuleLoadRequest;
25 } // namespace loader
26 } // namespace JS
28 namespace mozilla::dom {
30 class ScriptLoader;
31 class SRIMetadata;
33 //////////////////////////////////////////////////////////////
34 // DOM Module loader implementation
35 //////////////////////////////////////////////////////////////
37 class ModuleLoader final : public JS::loader::ModuleLoaderBase {
38 private:
39 virtual ~ModuleLoader();
41 public:
42 enum Kind { Normal, WebExtension };
44 ModuleLoader(ScriptLoader* aLoader, nsIGlobalObject* aGlobalObject,
45 Kind aKind);
47 Kind GetKind() const { return mKind; }
49 ScriptLoader* GetScriptLoader();
51 bool CanStartLoad(ModuleLoadRequest* aRequest, nsresult* aRvOut) override;
53 nsresult StartFetch(ModuleLoadRequest* aRequest) override;
55 void OnModuleLoadComplete(ModuleLoadRequest* aRequest) override;
57 nsresult CompileFetchedModule(
58 JSContext* aCx, JS::Handle<JSObject*> aGlobal,
59 JS::CompileOptions& aOptions, ModuleLoadRequest* aRequest,
60 JS::MutableHandle<JSObject*> aModuleScript) override;
62 // Create a top-level module load request.
63 static already_AddRefed<ModuleLoadRequest> CreateTopLevel(
64 nsIURI* aURI, ReferrerPolicy aReferrerPolicy,
65 ScriptFetchOptions* aFetchOptions, const SRIMetadata& aIntegrity,
66 nsIURI* aReferrer, ScriptLoader* aLoader, ScriptLoadContext* aContext);
68 // Create a module load request for a static module import.
69 already_AddRefed<ModuleLoadRequest> CreateStaticImport(
70 nsIURI* aURI, ModuleLoadRequest* aParent) override;
72 // Create a module load request for a dynamic module import.
73 already_AddRefed<ModuleLoadRequest> CreateDynamicImport(
74 JSContext* aCx, nsIURI* aURI, LoadedScript* aMaybeActiveScript,
75 JS::Handle<JS::Value> aReferencingPrivate,
76 JS::Handle<JSString*> aSpecifier,
77 JS::Handle<JSObject*> aPromise) override;
79 static ModuleLoader* From(ModuleLoaderBase* aLoader) {
80 return static_cast<ModuleLoader*>(aLoader);
83 void AsyncExecuteInlineModule(ModuleLoadRequest* aRequest);
84 void ExecuteInlineModule(ModuleLoadRequest* aRequest);
86 private:
87 const Kind mKind;
90 } // namespace mozilla::dom
92 #endif // mozilla_dom_ModuleLoader_h