Bug 1941128 - Turn off network.dns.native_https_query on Mac again
[gecko.git] / js / xpconnect / loader / nsImportModule.cpp
blob3720a444a6dcc9c2856234505c30d596eb084385
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 #include "nsImportModule.h"
9 #include "mozilla/dom/ScriptSettings.h"
10 #include "mozJSModuleLoader.h"
11 #include "nsContentUtils.h"
12 #include "nsExceptionHandler.h"
13 #include "nsPrintfCString.h"
14 #include "xpcpublic.h"
15 #include "xpcprivate.h"
16 #include "js/PropertyAndElement.h" // JS_GetProperty
18 using mozilla::dom::AutoJSAPI;
20 namespace mozilla {
21 namespace loader {
23 static void AnnotateCrashReportWithJSException(JSContext* aCx,
24 const char* aURI) {
25 JS::RootedValue exn(aCx);
26 if (JS_GetPendingException(aCx, &exn)) {
27 JS_ClearPendingException(aCx);
29 JSAutoRealm ar(aCx, xpc::PrivilegedJunkScope());
30 JS_WrapValue(aCx, &exn);
32 nsAutoCString file;
33 uint32_t line;
34 uint32_t column;
35 nsAutoString msg;
36 nsContentUtils::ExtractErrorValues(aCx, exn, file, &line, &column, msg);
38 nsPrintfCString errorString("Failed to load module \"%s\": %s:%u:%u: %s",
39 aURI, file.get(), line, column,
40 NS_ConvertUTF16toUTF8(msg).get());
42 CrashReporter::RecordAnnotationNSCString(
43 CrashReporter::Annotation::JSModuleLoadError, errorString);
47 nsresult ImportESModule(const char* aURI, const char* aExportName,
48 const nsIID& aIID, void** aResult, bool aInfallible) {
49 AutoJSAPI jsapi;
50 MOZ_ALWAYS_TRUE(jsapi.Init(xpc::PrivilegedJunkScope()));
51 JSContext* cx = jsapi.cx();
53 JS::RootedObject moduleNamespace(cx);
54 nsresult rv = mozJSModuleLoader::Get()->ImportESModule(
55 cx, nsDependentCString(aURI), &moduleNamespace);
56 if (NS_WARN_IF(NS_FAILED(rv))) {
57 if (aInfallible) {
58 AnnotateCrashReportWithJSException(cx, aURI);
60 MOZ_CRASH_UNSAFE_PRINTF("Failed to load critical module \"%s\"", aURI);
62 return rv;
65 if (aExportName) {
66 JS::RootedValue namedExport(cx);
67 if (!JS_GetProperty(cx, moduleNamespace, aExportName, &namedExport)) {
68 return NS_ERROR_FAILURE;
70 if (!namedExport.isObject()) {
71 return NS_ERROR_XPC_BAD_CONVERT_JS;
73 moduleNamespace.set(&namedExport.toObject());
76 return nsXPConnect::XPConnect()->WrapJS(cx, moduleNamespace, aIID, aResult);
79 } // namespace loader
80 } // namespace mozilla