summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/loader/ComponentModuleLoader.h
blob: 7dd39dd34235d6de5168130b749b271adfda9eeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_loader_ComponentModuleLoader_h
#define mozilla_loader_ComponentModuleLoader_h

#include "js/loader/LoadContextBase.h"
#include "js/loader/ModuleLoaderBase.h"

#include "SkipCheckForBrokenURLOrZeroSized.h"

class mozJSModuleLoader;

namespace mozilla {
namespace loader {

class ComponentScriptLoader : public JS::loader::ScriptLoaderInterface {
 public:
  NS_DECL_ISUPPORTS

 private:
  ~ComponentScriptLoader() = default;

  nsIURI* GetBaseURI() const override;

  void ReportErrorToConsole(ScriptLoadRequest* aRequest,
                            nsresult aResult) const override;

  void ReportWarningToConsole(ScriptLoadRequest* aRequest,
                              const char* aMessageName,
                              const nsTArray<nsString>& aParams) const override;

  nsresult FillCompileOptionsForRequest(
      JSContext* cx, ScriptLoadRequest* aRequest, JS::CompileOptions* aOptions,
      JS::MutableHandle<JSScript*> aIntroductionScript) override;
};

class ComponentModuleLoader : public JS::loader::ModuleLoaderBase {
 public:
  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ComponentModuleLoader,
                                           JS::loader::ModuleLoaderBase)

  ComponentModuleLoader(ComponentScriptLoader* aScriptLoader,
                        nsIGlobalObject* aGlobalObject);

  [[nodiscard]] nsresult ProcessRequests();

  void MaybeReportLoadError(JSContext* aCx);

 private:
  // An event target that dispatches runnables by executing them
  // immediately. This is used to drive mozPromise dispatch for
  // ComponentModuleLoader.
  class SyncEventTarget : public nsISerialEventTarget {
   public:
    NS_DECL_THREADSAFE_ISUPPORTS
    NS_DECL_NSIEVENTTARGET_FULL
   private:
    virtual ~SyncEventTarget() = default;
  };

  ~ComponentModuleLoader();

  already_AddRefed<ModuleLoadRequest> CreateStaticImport(
      nsIURI* aURI, ModuleLoadRequest* aParent) override;

  already_AddRefed<ModuleLoadRequest> CreateDynamicImport(
      JSContext* aCx, nsIURI* aURI, LoadedScript* aMaybeActiveScript,
      JS::Handle<JS::Value> aReferencingPrivate,
      JS::Handle<JSString*> aSpecifier,
      JS::Handle<JSObject*> aPromise) override;

  bool CanStartLoad(ModuleLoadRequest* aRequest, nsresult* aRvOut) override;

  nsresult StartFetch(ModuleLoadRequest* aRequest) override;

  nsresult CompileFetchedModule(
      JSContext* aCx, JS::Handle<JSObject*> aGlobal,
      JS::CompileOptions& aOptions, ModuleLoadRequest* aRequest,
      JS::MutableHandle<JSObject*> aModuleScript) override;

  void OnModuleLoadComplete(ModuleLoadRequest* aRequest) override;

  JS::loader::ScriptLoadRequestList mLoadRequests;

  // If any of module scripts failed to load, exception is set here until it's
  // reported by MaybeReportLoadError.
  JS::PersistentRooted<JS::Value> mLoadException;
};

// Data specific to ComponentModuleLoader that is associated with each load
// request.
class ComponentLoadContext : public JS::loader::LoadContextBase {
 public:
  ComponentLoadContext()
      : LoadContextBase(JS::loader::ContextKind::Component) {}

 public:
  // The result of compiling a module script. These fields are used temporarily
  // before being passed to the module loader.
  nsresult mRv;

  SkipCheckForBrokenURLOrZeroSized mSkipCheck;

  // The exception thrown during compiling a module script. These fields are
  // used temporarily before being passed to the module loader.
  JS::PersistentRooted<JS::Value> mExceptionValue;

  JS::PersistentRooted<JSScript*> mScript;
};

}  // namespace loader
}  // namespace mozilla

#endif  // mozilla_loader_ComponentModuleLoader_h