SyncModuleLoader.h (3529B)
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/. */ 6 7 #ifndef mozilla_loader_SyncModuleLoader_h 8 #define mozilla_loader_SyncModuleLoader_h 9 10 #include "js/loader/LoadContextBase.h" 11 #include "js/loader/ModuleLoaderBase.h" 12 13 class mozJSModuleLoader; 14 15 namespace mozilla { 16 namespace loader { 17 18 class SyncScriptLoader : public JS::loader::ScriptLoaderInterface { 19 public: 20 NS_DECL_ISUPPORTS 21 22 private: 23 ~SyncScriptLoader() = default; 24 25 nsIURI* GetBaseURI() const override; 26 27 void ReportErrorToConsole(ScriptLoadRequest* aRequest, 28 nsresult aResult) const override; 29 30 void ReportWarningToConsole(ScriptLoadRequest* aRequest, 31 const char* aMessageName, 32 const nsTArray<nsString>& aParams) const override; 33 34 nsresult FillCompileOptionsForRequest( 35 JSContext* cx, ScriptLoadRequest* aRequest, JS::CompileOptions* aOptions, 36 JS::MutableHandle<JSScript*> aIntroductionScript) override; 37 }; 38 39 class SyncModuleLoader : public JS::loader::ModuleLoaderBase { 40 public: 41 NS_DECL_ISUPPORTS_INHERITED 42 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SyncModuleLoader, 43 JS::loader::ModuleLoaderBase) 44 45 SyncModuleLoader(SyncScriptLoader* aScriptLoader, 46 nsIGlobalObject* aGlobalObject); 47 48 [[nodiscard]] nsresult ProcessRequests(); 49 50 void MaybeReportLoadError(JSContext* aCx); 51 52 private: 53 ~SyncModuleLoader(); 54 55 already_AddRefed<ModuleLoadRequest> CreateRequest( 56 JSContext* aCx, nsIURI* aURI, JS::Handle<JSObject*> aModuleRequest, 57 JS::Handle<JS::Value> aHostDefined, JS::Handle<JS::Value> aPayload, 58 bool aIsDynamicImport, JS::loader::ScriptFetchOptions* aOptions, 59 dom::ReferrerPolicy aReferrerPolicy, nsIURI* aBaseURL, 60 const dom::SRIMetadata& aSriMetadata) override; 61 62 void OnDynamicImportStarted(ModuleLoadRequest* aRequest) override; 63 64 bool CanStartLoad(ModuleLoadRequest* aRequest, nsresult* aRvOut) override; 65 66 nsresult StartFetch(ModuleLoadRequest* aRequest) override; 67 68 nsresult CompileFetchedModule( 69 JSContext* aCx, JS::Handle<JSObject*> aGlobal, 70 JS::CompileOptions& aOptions, ModuleLoadRequest* aRequest, 71 JS::MutableHandle<JSObject*> aModuleScript) override; 72 73 void OnModuleLoadComplete(ModuleLoadRequest* aRequest) override; 74 75 JS::loader::ScriptLoadRequestList mLoadRequests; 76 77 // If any of module scripts failed to load, exception is set here until it's 78 // reported by MaybeReportLoadError. 79 JS::PersistentRooted<JS::Value> mLoadException; 80 }; 81 82 // Data specific to SyncModuleLoader that is associated with each load 83 // request. 84 class SyncLoadContext : public JS::loader::LoadContextBase { 85 public: 86 SyncLoadContext() : LoadContextBase(JS::loader::ContextKind::Sync) {} 87 88 public: 89 // The result of compiling a module script. These fields are used temporarily 90 // before being passed to the module loader. 91 nsresult mRv; 92 93 // The exception thrown during compiling a module script. These fields are 94 // used temporarily before being passed to the module loader. 95 JS::PersistentRooted<JS::Value> mExceptionValue; 96 97 JS::PersistentRooted<JSScript*> mScript; 98 }; 99 100 } // namespace loader 101 } // namespace mozilla 102 103 #endif // mozilla_loader_SyncModuleLoader_h