summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/loader/nsImportModule.h
blob: 31f6f8c7c193fb38c415f6bb26f9d293f47dadd1 (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
/* -*- 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 nsImportModule_h
#define nsImportModule_h

#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"

#include "nsCOMPtr.h"
#include "mozilla/RefPtr.h"

namespace mozilla {
namespace loader {

nsresult ImportModule(const char* aURI, const char* aExportName,
                      const nsIID& aIID, void** aResult, bool aInfallible);

nsresult ImportESModule(const char* aURI, const char* aExportName,
                        const nsIID& aIID, void** aResult, bool aInfallible);

}  // namespace loader
}  // namespace mozilla

class MOZ_STACK_CLASS nsImportModule final : public nsCOMPtr_helper {
 public:
  nsImportModule(const char* aURI, const char* aExportName, nsresult* aErrorPtr,
                 bool aInfallible)
      : mURI(aURI),
        mExportName(aExportName),
        mErrorPtr(aErrorPtr),
        mInfallible(aInfallible) {
    MOZ_ASSERT_IF(mErrorPtr, !mInfallible);
  }

  virtual nsresult NS_FASTCALL operator()(const nsIID& aIID,
                                          void** aResult) const override {
    nsresult rv = ::mozilla::loader::ImportModule(mURI, mExportName, aIID,
                                                  aResult, mInfallible);
    if (mErrorPtr) {
      *mErrorPtr = rv;
    }
    return rv;
  }

 private:
  const char* mURI;
  const char* mExportName;
  nsresult* mErrorPtr;
  bool mInfallible;
};

/**
 * These helpers make it considerably easier for C++ code to import a JS module
 * and wrap it in an appropriately-defined XPIDL interface for its exports.
 * Typical usage is something like:
 *
 * Foo.jsm:
 *
 *   var EXPORTED_SYMBOLS = ["foo"];
 *
 *   function foo(bar) {
 *     return bar.toString();
 *   }
 *
 * mozIFoo.idl:
 *
 *   interface mozIFoo : nsISupports {
 *     AString foo(double meh);
 *   }
 *
 * Thing.cpp:
 *
 *   nsCOMPtr<mozIFoo> foo = do_ImportModule(
 *     "resource://meh/Foo.jsm");
 *
 *   MOZ_TRY(foo->Foo(42));
 *
 * For JS modules which export all fields within a single named object, a second
 * argument can be passed naming that object.
 *
 * Foo.jsm:
 *
 *   var EXPORTED_SYMBOLS = ["Foo"];
 *
 *   var Foo = {
 *     function foo(bar) {
 *       return bar.toString();
 *     }
 *   };
 *
 * Thing.cpp:
 *
 *   nsCOMPtr<mozIFoo> foo = do_ImportModule(
 *       "resource:://meh/Foo.jsm", "Foo");
 */

template <size_t N>
inline nsImportModule do_ImportModule(const char (&aURI)[N]) {
  return {aURI, nullptr, nullptr, /* infallible */ true};
}

template <size_t N>
inline nsImportModule do_ImportModule(const char (&aURI)[N],
                                      const mozilla::fallible_t&) {
  return {aURI, nullptr, nullptr, /* infallible */ false};
}

template <size_t N>
inline nsImportModule do_ImportModule(const char (&aURI)[N], nsresult* aRv) {
  return {aURI, nullptr, aRv, /* infallible */ false};
}

template <size_t N, size_t N2>
inline nsImportModule do_ImportModule(const char (&aURI)[N],
                                      const char (&aExportName)[N2]) {
  return {aURI, aExportName, nullptr, /* infallible */ true};
}

template <size_t N, size_t N2>
inline nsImportModule do_ImportModule(const char (&aURI)[N],
                                      const char (&aExportName)[N2],
                                      const mozilla::fallible_t&) {
  return {aURI, aExportName, nullptr, /* infallible */ false};
}

template <size_t N, size_t N2>
inline nsImportModule do_ImportModule(const char (&aURI)[N],
                                      const char (&aExportName)[N2],
                                      nsresult* aRv) {
  return {aURI, aExportName, aRv, /* infallible */ false};
}

class MOZ_STACK_CLASS nsImportESModule final : public nsCOMPtr_helper {
 public:
  nsImportESModule(const char* aURI, const char* aExportName,
                   nsresult* aErrorPtr, bool aInfallible)
      : mURI(aURI),
        mExportName(aExportName),
        mErrorPtr(aErrorPtr),
        mInfallible(aInfallible) {
    MOZ_ASSERT_IF(mErrorPtr, !mInfallible);
  }

  virtual nsresult NS_FASTCALL operator()(const nsIID& aIID,
                                          void** aResult) const override {
    nsresult rv = ::mozilla::loader::ImportESModule(mURI, mExportName, aIID,
                                                    aResult, mInfallible);
    if (mErrorPtr) {
      *mErrorPtr = rv;
    }
    return rv;
  }

 private:
  const char* mURI;
  const char* mExportName;
  nsresult* mErrorPtr;
  bool mInfallible;
};

/**
 * Usage with exported name:
 *
 * Foo.sys.mjs:
 *
 *   export function foo(bar) {
 *     return bar.toString();
 *   }
 *
 * mozIFoo.idl:
 *
 *   interface mozIFoo : nsISupports {
 *     AString foo(double meh);
 *   }
 *
 * Thing.cpp:
 *
 *   nsCOMPtr<mozIFoo> foo = do_ImportESModule(
 *     "resource://meh/Foo.sys.mjs");
 *
 *   MOZ_TRY(foo->Foo(42));
 *
 * Usage with a single named object:
 *
 * Foo.sys.mjs:
 *
 *   export var Foo = {
 *     function foo(bar) {
 *       return bar.toString();
 *     }
 *   };
 *
 * Thing.cpp:
 *
 *   nsCOMPtr<mozIFoo> foo = do_ImportESModule(
 *       "resource:://meh/Foo.sys.mjs", "Foo");
 */

template <size_t N>
inline nsImportESModule do_ImportESModule(const char (&aURI)[N]) {
  return {aURI, nullptr, nullptr, /* infallible */ true};
}

template <size_t N>
inline nsImportESModule do_ImportESModule(const char (&aURI)[N],
                                          const mozilla::fallible_t&) {
  return {aURI, nullptr, nullptr, /* infallible */ false};
}

template <size_t N>
inline nsImportESModule do_ImportESModule(const char (&aURI)[N],
                                          nsresult* aRv) {
  return {aURI, nullptr, aRv, /* infallible */ false};
}

template <size_t N, size_t N2>
inline nsImportESModule do_ImportESModule(const char (&aURI)[N],
                                          const char (&aExportName)[N2]) {
  return {aURI, aExportName, nullptr, /* infallible */ true};
}

template <size_t N, size_t N2>
inline nsImportESModule do_ImportESModule(const char (&aURI)[N],
                                          const char (&aExportName)[N2],
                                          const mozilla::fallible_t&) {
  return {aURI, aExportName, nullptr, /* infallible */ false};
}

template <size_t N, size_t N2>
inline nsImportESModule do_ImportESModule(const char (&aURI)[N],
                                          const char (&aExportName)[N2],
                                          nsresult* aRv) {
  return {aURI, aExportName, aRv, /* infallible */ false};
}

#endif  // defined nsImportModule_h