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_loader_SyncModuleLoader_h
8 #define mozilla_loader_SyncModuleLoader_h
10 #include "js/loader/LoadContextBase.h"
11 #include "js/loader/ModuleLoaderBase.h"
13 class mozJSModuleLoader
;
18 class SyncScriptLoader
: public JS::loader::ScriptLoaderInterface
{
23 ~SyncScriptLoader() = default;
25 nsIURI
* GetBaseURI() const override
;
27 void ReportErrorToConsole(ScriptLoadRequest
* aRequest
,
28 nsresult aResult
) const override
;
30 void ReportWarningToConsole(ScriptLoadRequest
* aRequest
,
31 const char* aMessageName
,
32 const nsTArray
<nsString
>& aParams
) const override
;
34 nsresult
FillCompileOptionsForRequest(
35 JSContext
* cx
, ScriptLoadRequest
* aRequest
, JS::CompileOptions
* aOptions
,
36 JS::MutableHandle
<JSScript
*> aIntroductionScript
) override
;
39 class SyncModuleLoader
: public JS::loader::ModuleLoaderBase
{
41 NS_DECL_ISUPPORTS_INHERITED
42 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SyncModuleLoader
,
43 JS::loader::ModuleLoaderBase
)
45 SyncModuleLoader(SyncScriptLoader
* aScriptLoader
,
46 nsIGlobalObject
* aGlobalObject
);
48 [[nodiscard
]] nsresult
ProcessRequests();
50 void MaybeReportLoadError(JSContext
* aCx
);
55 already_AddRefed
<ModuleLoadRequest
> CreateStaticImport(
56 nsIURI
* aURI
, JS::ModuleType aModuleType
,
57 ModuleLoadRequest
* aParent
) override
;
59 already_AddRefed
<ModuleLoadRequest
> CreateDynamicImport(
60 JSContext
* aCx
, nsIURI
* aURI
, JS::ModuleType aModuleType
,
61 LoadedScript
* aMaybeActiveScript
, JS::Handle
<JSString
*> aSpecifier
,
62 JS::Handle
<JSObject
*> aPromise
) override
;
64 void OnDynamicImportStarted(ModuleLoadRequest
* aRequest
) override
;
66 bool CanStartLoad(ModuleLoadRequest
* aRequest
, nsresult
* aRvOut
) override
;
68 nsresult
StartFetch(ModuleLoadRequest
* aRequest
) override
;
70 nsresult
CompileFetchedModule(
71 JSContext
* aCx
, JS::Handle
<JSObject
*> aGlobal
,
72 JS::CompileOptions
& aOptions
, ModuleLoadRequest
* aRequest
,
73 JS::MutableHandle
<JSObject
*> aModuleScript
) override
;
75 void OnModuleLoadComplete(ModuleLoadRequest
* aRequest
) override
;
77 JS::loader::ScriptLoadRequestList mLoadRequests
;
79 // If any of module scripts failed to load, exception is set here until it's
80 // reported by MaybeReportLoadError.
81 JS::PersistentRooted
<JS::Value
> mLoadException
;
84 // Data specific to SyncModuleLoader that is associated with each load
86 class SyncLoadContext
: public JS::loader::LoadContextBase
{
88 SyncLoadContext() : LoadContextBase(JS::loader::ContextKind::Sync
) {}
91 // The result of compiling a module script. These fields are used temporarily
92 // before being passed to the module loader.
95 // The exception thrown during compiling a module script. These fields are
96 // used temporarily before being passed to the module loader.
97 JS::PersistentRooted
<JS::Value
> mExceptionValue
;
99 JS::PersistentRooted
<JSScript
*> mScript
;
102 } // namespace loader
103 } // namespace mozilla
105 #endif // mozilla_loader_SyncModuleLoader_h