summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/loader/ComponentModuleLoader.cpp
blob: 9f293fdcc05a26f718ac4d49b8f73ab1284adb75 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* -*- 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/. */

#include "ComponentModuleLoader.h"

#include "nsISupportsImpl.h"

#include "js/loader/ModuleLoadRequest.h"
#include "js/RootingAPI.h"          // JS::Rooted
#include "js/PropertyAndElement.h"  // JS_SetProperty
#include "js/Value.h"               // JS::Value, JS::NumberValue
#include "mozJSModuleLoader.h"

using namespace JS::loader;

namespace mozilla {
namespace loader {

//////////////////////////////////////////////////////////////
// ComponentScriptLoader
//////////////////////////////////////////////////////////////

NS_IMPL_ISUPPORTS0(ComponentScriptLoader)

nsIURI* ComponentScriptLoader::GetBaseURI() const { return nullptr; }

void ComponentScriptLoader::ReportErrorToConsole(ScriptLoadRequest* aRequest,
                                                 nsresult aResult) const {}

void ComponentScriptLoader::ReportWarningToConsole(
    ScriptLoadRequest* aRequest, const char* aMessageName,
    const nsTArray<nsString>& aParams) const {}

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

//////////////////////////////////////////////////////////////
// ComponentModuleLoader
//////////////////////////////////////////////////////////////

NS_IMPL_ADDREF_INHERITED(ComponentModuleLoader, JS::loader::ModuleLoaderBase)
NS_IMPL_RELEASE_INHERITED(ComponentModuleLoader, JS::loader::ModuleLoaderBase)

NS_IMPL_CYCLE_COLLECTION_INHERITED(ComponentModuleLoader,
                                   JS::loader::ModuleLoaderBase, mLoadRequests)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ComponentModuleLoader)
NS_INTERFACE_MAP_END_INHERITING(JS::loader::ModuleLoaderBase)

ComponentModuleLoader::ComponentModuleLoader(
    ComponentScriptLoader* aScriptLoader, nsIGlobalObject* aGlobalObject)
    : ModuleLoaderBase(aScriptLoader, aGlobalObject, new SyncEventTarget()) {}

ComponentModuleLoader::~ComponentModuleLoader() {
  MOZ_ASSERT(mLoadRequests.isEmpty());
}

already_AddRefed<ModuleLoadRequest> ComponentModuleLoader::CreateStaticImport(
    nsIURI* aURI, ModuleLoadRequest* aParent) {
  RefPtr<ComponentLoadContext> context = new ComponentLoadContext();
  RefPtr<ModuleLoadRequest> request = new ModuleLoadRequest(
      aURI, aParent->mFetchOptions, dom::SRIMetadata(), aParent->mURI, context,
      false, /* is top level */
      false, /* is dynamic import */
      this, aParent->mVisitedSet, aParent->GetRootModule());
  return request.forget();
}

already_AddRefed<ModuleLoadRequest> ComponentModuleLoader::CreateDynamicImport(
    JSContext* aCx, nsIURI* aURI, LoadedScript* aMaybeActiveScript,
    JS::Handle<JS::Value> aReferencingPrivate, JS::Handle<JSString*> aSpecifier,
    JS::Handle<JSObject*> aPromise) {
  return nullptr;  // Not yet implemented.
}

bool ComponentModuleLoader::CanStartLoad(ModuleLoadRequest* aRequest,
                                         nsresult* aRvOut) {
  return mozJSModuleLoader::IsTrustedScheme(aRequest->mURI);
}

nsresult ComponentModuleLoader::StartFetch(ModuleLoadRequest* aRequest) {
  MOZ_ASSERT(aRequest->HasLoadContext());

  aRequest->mBaseURL = aRequest->mURI;

  // Loading script source and compilation are intertwined in
  // mozJSModuleLoader. Perform both operations here but only report load
  // failures. Compilation failure is reported in CompileFetchedModule.

  dom::AutoJSAPI jsapi;
  if (!jsapi.Init(GetGlobalObject())) {
    return NS_ERROR_FAILURE;
  }

  JSContext* cx = jsapi.cx();
  JS::RootedScript script(cx);
  nsresult rv =
      mozJSModuleLoader::LoadSingleModuleScript(this, cx, aRequest, &script);
  MOZ_ASSERT_IF(jsapi.HasException(), NS_FAILED(rv));
  MOZ_ASSERT(bool(script) == NS_SUCCEEDED(rv));

  // Check for failure to load script source and abort.
  bool threwException = jsapi.HasException();
  if (NS_FAILED(rv) && !threwException) {
    nsAutoCString uri;
    nsresult rv2 = aRequest->mURI->GetSpec(uri);
    NS_ENSURE_SUCCESS(rv2, rv2);

    JS_ReportErrorUTF8(cx, "Failed to load %s", PromiseFlatCString(uri).get());

    // Remember the error for MaybeReportLoadError.
    if (!mLoadException.initialized()) {
      mLoadException.init(cx);
    }
    if (!jsapi.StealException(&mLoadException)) {
      return NS_ERROR_OUT_OF_MEMORY;
    }

    if (mLoadException.isObject()) {
      // Expose `nsresult`.
      JS::Rooted<JS::Value> resultVal(cx, JS::NumberValue(uint32_t(rv)));
      JS::Rooted<JSObject*> exceptionObj(cx, &mLoadException.toObject());
      if (!JS_SetProperty(cx, exceptionObj, "result", resultVal)) {
        // Ignore the error and keep reporting the exception without the result
        // property.
        JS_ClearPendingException(cx);
      }
    }

    return rv;
  }

  // Otherwise remember the results in this context so we can report them later.
  ComponentLoadContext* context = aRequest->GetComponentLoadContext();
  context->mRv = rv;
  if (threwException) {
    context->mExceptionValue.init(cx);
    if (!jsapi.StealException(&context->mExceptionValue)) {
      return NS_ERROR_OUT_OF_MEMORY;
    }
  }
  if (script) {
    context->mScript.init(cx);
    context->mScript = script;
  }

  mLoadRequests.AppendElement(aRequest);

  return NS_OK;
}

nsresult ComponentModuleLoader::CompileFetchedModule(
    JSContext* aCx, JS::Handle<JSObject*> aGlobal, JS::CompileOptions& aOptions,
    ModuleLoadRequest* aRequest, JS::MutableHandle<JSObject*> aModuleOut) {
  // Compilation already happened in StartFetch. Report the result here.
  ComponentLoadContext* context = aRequest->GetComponentLoadContext();
  nsresult rv = context->mRv;
  if (context->mScript) {
    aModuleOut.set(JS::GetModuleObject(context->mScript));
    context->mScript = nullptr;
  }
  if (NS_FAILED(rv)) {
    JS_SetPendingException(aCx, context->mExceptionValue);
    context->mExceptionValue = JS::UndefinedValue();
  }

  MOZ_ASSERT(JS_IsExceptionPending(aCx) == NS_FAILED(rv));
  MOZ_ASSERT(bool(aModuleOut) == NS_SUCCEEDED(rv));

  return rv;
}

void ComponentModuleLoader::MaybeReportLoadError(JSContext* aCx) {
  if (JS_IsExceptionPending(aCx)) {
    // Do not override.
    return;
  }

  if (mLoadException.isUndefined()) {
    return;
  }

  JS_SetPendingException(aCx, mLoadException);
  mLoadException = JS::UndefinedValue();
}

void ComponentModuleLoader::OnModuleLoadComplete(ModuleLoadRequest* aRequest) {}

nsresult ComponentModuleLoader::ProcessRequests() {
  // Work list to drive module loader since this is all synchronous.
  while (!mLoadRequests.isEmpty()) {
    RefPtr<ScriptLoadRequest> request = mLoadRequests.StealFirst();
    nsresult rv = OnFetchComplete(request->AsModuleRequest(), NS_OK);
    if (NS_FAILED(rv)) {
      mLoadRequests.CancelRequestsAndClear();
      return rv;
    }
  }

  return NS_OK;
}

//////////////////////////////////////////////////////////////
// ComponentModuleLoader::SyncEventTarget
//////////////////////////////////////////////////////////////

NS_IMPL_ADDREF(ComponentModuleLoader::SyncEventTarget)
NS_IMPL_RELEASE(ComponentModuleLoader::SyncEventTarget)

NS_INTERFACE_MAP_BEGIN(ComponentModuleLoader::SyncEventTarget)
  NS_INTERFACE_MAP_ENTRY(nsISerialEventTarget)
  NS_INTERFACE_MAP_ENTRY(nsIEventTarget)
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

NS_IMETHODIMP
ComponentModuleLoader::SyncEventTarget::DispatchFromScript(
    nsIRunnable* aRunnable, uint32_t aFlags) {
  nsCOMPtr<nsIRunnable> event(aRunnable);
  return Dispatch(event.forget(), aFlags);
}

NS_IMETHODIMP
ComponentModuleLoader::SyncEventTarget::Dispatch(
    already_AddRefed<nsIRunnable> aRunnable, uint32_t aFlags) {
  MOZ_ASSERT(IsOnCurrentThreadInfallible());

  nsCOMPtr<nsIRunnable> runnable(aRunnable);
  runnable->Run();

  return NS_OK;
}

NS_IMETHODIMP
ComponentModuleLoader::SyncEventTarget::DelayedDispatch(
    already_AddRefed<nsIRunnable>, uint32_t) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
ComponentModuleLoader::SyncEventTarget::RegisterShutdownTask(
    nsITargetShutdownTask* aTask) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
ComponentModuleLoader::SyncEventTarget::UnregisterShutdownTask(
    nsITargetShutdownTask* aTask) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
ComponentModuleLoader::SyncEventTarget::IsOnCurrentThread(
    bool* aIsOnCurrentThread) {
  MOZ_ASSERT(aIsOnCurrentThread);
  *aIsOnCurrentThread = IsOnCurrentThreadInfallible();
  return NS_OK;
}

NS_IMETHODIMP_(bool)
ComponentModuleLoader::SyncEventTarget::IsOnCurrentThreadInfallible() {
  return NS_IsMainThread();
}

}  // namespace loader
}  // namespace mozilla