diff --git a/js/xpconnect/loader/moz.build b/js/xpconnect/loader/moz.build index dac00a26ccea..4c7a0165177a 100644 --- a/js/xpconnect/loader/moz.build +++ b/js/xpconnect/loader/moz.build @@ -9,6 +9,7 @@ UNIFIED_SOURCES += [ 'ChromeScriptLoader.cpp', 'mozJSLoaderUtils.cpp', 'mozJSSubScriptLoader.cpp', + 'nsImportModule.cpp', 'ScriptCacheActors.cpp', 'ScriptPreloader.cpp', 'URLPreloader.cpp', @@ -17,13 +18,17 @@ UNIFIED_SOURCES += [ # mozJSComponentLoader.cpp cannot be built in unified mode because it uses # windows.h SOURCES += [ - 'mozJSComponentLoader.cpp' + 'mozJSComponentLoader.cpp', ] IPDL_SOURCES += [ 'PScriptCache.ipdl', ] +EXPORTS += [ + 'nsImportModule.h' +] + EXPORTS.mozilla += [ 'AutoMemMap.h', 'ScriptPreloader.h', diff --git a/js/xpconnect/loader/nsImportModule.cpp b/js/xpconnect/loader/nsImportModule.cpp new file mode 100644 index 000000000000..a0cc86721740 --- /dev/null +++ b/js/xpconnect/loader/nsImportModule.cpp @@ -0,0 +1,34 @@ +/* -*- 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 "nsImportModule.h" + +#include "mozilla/ResultExtensions.h" +#include "mozilla/dom/ScriptSettings.h" +#include "mozJSComponentLoader.h" +#include "xpcpublic.h" +#include "xpcprivate.h" + +using mozilla::dom::AutoJSAPI; + +namespace mozilla { +namespace loader { + +nsresult ImportModule(const char* aURI, const nsIID& aIID, void** aResult) { + AutoJSAPI jsapi; + MOZ_ALWAYS_TRUE(jsapi.Init(xpc::PrivilegedJunkScope())); + JSContext* cx = jsapi.cx(); + + JS::RootedObject global(cx); + JS::RootedObject exports(cx); + MOZ_TRY(mozJSComponentLoader::Get()->Import(cx, nsDependentCString(aURI), + &global, &exports)); + + return nsXPConnect::XPConnect()->WrapJS(cx, exports, aIID, aResult); +} + +} // namespace loader +} // namespace mozilla diff --git a/js/xpconnect/loader/nsImportModule.h b/js/xpconnect/loader/nsImportModule.h new file mode 100644 index 000000000000..e488f46d9914 --- /dev/null +++ b/js/xpconnect/loader/nsImportModule.h @@ -0,0 +1,77 @@ +/* -*- 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/Attributes.h" + +#include "nsCOMPtr.h" +#include "mozilla/RefPtr.h" + +namespace mozilla { +namespace loader { + +nsresult ImportModule(const char* aURI, const nsIID& aIID, void** aResult); + +} // namespace loader +} // namespace mozilla + +class MOZ_STACK_CLASS nsImportModule final : public nsCOMPtr_helper { + public: + nsImportModule(const char* aURI, nsresult* aErrorPtr) + : mURI(aURI), mErrorPtr(aErrorPtr) {} + + virtual nsresult NS_FASTCALL operator()(const nsIID& aIID, + void** aResult) const override { + nsresult rv = ::mozilla::loader::ImportModule(mURI, aIID, aResult); + if (mErrorPtr) { + *mErrorPtr = rv; + } + return rv; + } + + private: + const char* mURI; + nsresult* mErrorPtr; +}; + +/** + * 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 foo = do_ImportModule( + * "resource://meh/Foo.jsm"); + * + * MOZ_TRY(foo->Foo(42)); + */ + +inline nsImportModule do_ImportModule(const char* aURI) { + return {aURI, nullptr}; +} + +inline nsImportModule do_ImportModule(const char* aURI, nsresult* aRv) { + return {aURI, aRv}; +} + +#endif // defined nsImportModule_h