mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-23 18:26:15 +00:00
3e8d266e8a
This helper makes 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)); Differential Revision: https://phabricator.services.mozilla.com/D14209 --HG-- extra : rebase_source : 4d518205b173fc210908235b42ddace590e6b7e5
35 lines
1.1 KiB
C++
35 lines
1.1 KiB
C++
/* -*- 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
|