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"
14 #include "mozilla/RefPtr.h"
19 nsresult
ImportESModule(const char* aURI
, const char* aExportName
,
20 const nsIID
& aIID
, void** aResult
, bool aInfallible
);
23 } // namespace mozilla
25 class MOZ_STACK_CLASS nsImportESModule final
: public nsCOMPtr_helper
{
27 nsImportESModule(const char* aURI
, const char* aExportName
,
28 nsresult
* aErrorPtr
, bool aInfallible
)
30 mExportName(aExportName
),
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
);
48 const char* mExportName
;
54 * Usage with exported name:
58 * export function foo(bar) {
59 * return bar.toString();
64 * interface mozIFoo : nsISupports {
65 * AString foo(double meh);
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:
81 * return bar.toString();
87 * nsCOMPtr<mozIFoo> foo = do_ImportESModule(
88 * "resource:://meh/Foo.sys.mjs", "Foo");
92 inline nsImportESModule
do_ImportESModule(const char (&aURI
)[N
]) {
93 return {aURI
, nullptr, nullptr, /* infallible */ true};
97 inline nsImportESModule
do_ImportESModule(const char (&aURI
)[N
],
98 const mozilla::fallible_t
&) {
99 return {aURI
, nullptr, nullptr, /* infallible */ false};
103 inline nsImportESModule
do_ImportESModule(const char (&aURI
)[N
],
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
],
125 return {aURI
, aExportName
, aRv
, /* infallible */ false};
128 #endif // defined nsImportModule_h