Bug 1941128 - Turn off network.dns.native_https_query on Mac again
[gecko.git] / js / xpconnect / loader / nsImportModule.h
blob16a5c40a88006c0ad47a838a8d52531e8ba21960
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 nsImportModule_h
8 #define nsImportModule_h
10 #include "mozilla/Assertions.h"
11 #include "mozilla/Attributes.h"
13 #include "nsCOMPtr.h"
14 #include "mozilla/RefPtr.h"
16 namespace mozilla {
17 namespace loader {
19 nsresult ImportESModule(const char* aURI, const char* aExportName,
20 const nsIID& aIID, void** aResult, bool aInfallible);
22 } // namespace loader
23 } // namespace mozilla
25 class MOZ_STACK_CLASS nsImportESModule final : public nsCOMPtr_helper {
26 public:
27 nsImportESModule(const char* aURI, const char* aExportName,
28 nsresult* aErrorPtr, bool aInfallible)
29 : mURI(aURI),
30 mExportName(aExportName),
31 mErrorPtr(aErrorPtr),
32 mInfallible(aInfallible) {
33 MOZ_ASSERT_IF(mErrorPtr, !mInfallible);
36 virtual nsresult NS_FASTCALL operator()(const nsIID& aIID,
37 void** aResult) const override {
38 nsresult rv = ::mozilla::loader::ImportESModule(mURI, mExportName, aIID,
39 aResult, mInfallible);
40 if (mErrorPtr) {
41 *mErrorPtr = rv;
43 return rv;
46 private:
47 const char* mURI;
48 const char* mExportName;
49 nsresult* mErrorPtr;
50 bool mInfallible;
53 /**
54 * Usage with exported name:
56 * Foo.sys.mjs:
58 * export function foo(bar) {
59 * return bar.toString();
60 * }
62 * mozIFoo.idl:
64 * interface mozIFoo : nsISupports {
65 * AString foo(double meh);
66 * }
68 * Thing.cpp:
70 * nsCOMPtr<mozIFoo> foo = do_ImportESModule(
71 * "resource://meh/Foo.sys.mjs");
73 * MOZ_TRY(foo->Foo(42));
75 * Usage with a single named object:
77 * Foo.sys.mjs:
79 * export var Foo = {
80 * function foo(bar) {
81 * return bar.toString();
82 * }
83 * };
85 * Thing.cpp:
87 * nsCOMPtr<mozIFoo> foo = do_ImportESModule(
88 * "resource:://meh/Foo.sys.mjs", "Foo");
91 template <size_t N>
92 inline nsImportESModule do_ImportESModule(const char (&aURI)[N]) {
93 return {aURI, nullptr, nullptr, /* infallible */ true};
96 template <size_t N>
97 inline nsImportESModule do_ImportESModule(const char (&aURI)[N],
98 const mozilla::fallible_t&) {
99 return {aURI, nullptr, nullptr, /* infallible */ false};
102 template <size_t N>
103 inline nsImportESModule do_ImportESModule(const char (&aURI)[N],
104 nsresult* aRv) {
105 return {aURI, nullptr, aRv, /* infallible */ false};
108 template <size_t N, size_t N2>
109 inline nsImportESModule do_ImportESModule(const char (&aURI)[N],
110 const char (&aExportName)[N2]) {
111 return {aURI, aExportName, nullptr, /* infallible */ true};
114 template <size_t N, size_t N2>
115 inline nsImportESModule do_ImportESModule(const char (&aURI)[N],
116 const char (&aExportName)[N2],
117 const mozilla::fallible_t&) {
118 return {aURI, aExportName, nullptr, /* infallible */ false};
121 template <size_t N, size_t N2>
122 inline nsImportESModule do_ImportESModule(const char (&aURI)[N],
123 const char (&aExportName)[N2],
124 nsresult* aRv) {
125 return {aURI, aExportName, aRv, /* infallible */ false};
128 #endif // defined nsImportModule_h