Is there a way to call a function in JavaScript from an XPCOM component?
[scriptable, uuid(...)]
interface myFoo : nsISupports {
void bar(in string blah);
}
in myFoo.idl, and
var myFoo = {
bar: function (blah) {
dump(blah);
do whatever else with blah you like
}
};
in a .js file, call it myFoo.js. Then you just need to register the JS object with your XPCOM (presumably, you mean C++) component. To do that, your component would need to supply a factory or service by which you could pass myFoo in. Suppose it does a service, myFooRegistry:
const MY_FOO_REGISTRY_PROGID = "...";
const MY_FOO_INTERFACE = Components.interfaces.myFoo;
var myFooService = Components.classes[MY_FOO_REGISTRY_PROGID].getService();
var myFooRegistry = myFooService.QueryInterface(MY_FOO_INTERFACE);
myFooRegistry.registerFoo(myFoo);
Your C++ component will have to implement the interface method I show being called immediately above:
nsresult myFooRegistry::RegisterFoo(myFoo *aFoo);
(C++ method spelling, NB). It can hold onto the passed-in interface and cal its blah method later.
The old way to do this involves the JS API (http://lxr.mozilla.org/mozilla/source/js/src/jsapi.h, http://www.mozilla.org/js/spidermonkey/), which is not as good as the new way, because it doesn't use XPIDL to separate interface from implementation (and from implementation language).
/be